Skip to content

Commit

Permalink
Improvement of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 29, 2024
1 parent ee0ca2d commit edc0e5e
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 85 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gem "tailwindcss-rails"
gem "turbo-rails"

## LIBRARIES
gem "active_median"
gem "bindata"
gem "bootsnap", require: false
gem "concurrent-ruby"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ GEM
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
active_median (0.4.0)
activesupport (>= 6.1)
activejob (7.1.1)
activesupport (= 7.1.1)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -327,6 +329,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
active_median
amazing_print
better_errors
bindata
Expand Down
15 changes: 15 additions & 0 deletions app/lib/loops/archive.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Loops
class Archive
def self.run(store)
new(store).run
end

def initialize(store)
@store = store
end

def run
::Archive.create!(@store.get.merge(datetime: Time.current))
end
end
end
15 changes: 15 additions & 0 deletions app/lib/loops/main.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Loops
class Main
def self.run(store)
new(store).run
end

def initialize(store)
@store = store
end

def run
@store.set(Inverter.read)
end
end
end
46 changes: 46 additions & 0 deletions app/lib/metric_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class MetricStore
def initialize
@mutex = Mutex.new
reset
end

def set(metrics)
@mutex.synchronize do
@metrics.each_key do |key|
@metrics[key] << metrics[key] if metrics.key?(key)
end
end
end

def get
@mutex.synchronize do
@metrics.to_h { |key, values| [key, send("reduce_#{key}", values)] }.tap { reset }
end
end

private

def reset
@metrics = { solar_power: [], solar_energy: [], grid_power: [], grid_energy_import: [], grid_energy_export: [] }
end

def reduce_solar_power(values)
values.median || 0
end

def reduce_solar_energy(values)
values.max || 0.0
end

def reduce_grid_power(values)
values.median || 0
end

def reduce_grid_energy_import(values)
values.max || 0.0
end

def reduce_grid_energy_export(values)
values.max || 0.0
end
end
31 changes: 0 additions & 31 deletions app/services/loop.rb

This file was deleted.

10 changes: 8 additions & 2 deletions config/scheduler.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
scheduler = Rufus::Scheduler.new
loop_interval = Rails.configuration.x.intervals.loop
archive_interval = Rails.configuration.x.intervals.archive
store = MetricStore.new

scheduler.every loop_interval, name: "solaris.loop" do
Loop.run
scheduler.every loop_interval, name: "solaris.loop.main" do
Loops::Main.run(store)
end

scheduler.every archive_interval, name: "solaris.loop.archive" do
Loops::Archive.run(store)
end

if Rails.configuration.x.esios.api_key.present?
Expand Down
25 changes: 25 additions & 0 deletions test/lib/loops/archive_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "test_helper"

module Loops
class ArchiveTest < ActiveSupport::TestCase
test "create archive with stored data" do
store = MetricStore.new

store.set(solar_power: 1, solar_energy: 4, grid_power: 7, grid_energy_import: 10, grid_energy_export: 13)
store.set(solar_power: 2, solar_energy: 5, grid_power: 8, grid_energy_import: 11, grid_energy_export: 14)
store.set(solar_power: 3, solar_energy: 6, grid_power: 9, grid_energy_import: 12, grid_energy_export: 15)

assert_difference("::Archive.count", 1) do
Loops::Archive.run(store)
end

archive = ::Archive.last

assert_equal 2, archive.solar_power
assert_in_delta(6.0, archive.solar_energy)
assert_equal 8, archive.grid_power
assert_in_delta(12.0, archive.grid_energy_import)
assert_in_delta(15.0, archive.grid_energy_export)
end
end
end
15 changes: 15 additions & 0 deletions test/lib/loops/main_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "test_helper"

module Loops
class MainTest < ActiveSupport::TestCase
test "store inverter data" do
store = Minitest::Mock.new
store.expect(:set, nil, [{ solar_power: 123 }])

Inverter.stub(:read, { solar_power: 123 }) do
Loops::Main.run(store)
store.verify
end
end
end
end
40 changes: 40 additions & 0 deletions test/lib/metric_store_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require "test_helper"

class MetricStoreTest < ActiveSupport::TestCase
test "get empty" do
store = MetricStore.new

assert_equal(
{ solar_power: 0, solar_energy: 0.0, grid_power: 0, grid_energy_import: 0.0, grid_energy_export: 0.0 },
store.get
)
end

test "get with values" do
store = MetricStore.new

store.set(solar_power: 1, solar_energy: 4, grid_power: 7, grid_energy_import: 10, grid_energy_export: 13)
store.set(solar_power: 2, solar_energy: 5, grid_power: 8, grid_energy_import: 11, grid_energy_export: 14)
store.set(solar_power: 3, solar_energy: 6, grid_power: 9, grid_energy_import: 12, grid_energy_export: 15)

assert_equal(
{ solar_power: 2, solar_energy: 6.0, grid_power: 8, grid_energy_import: 12.0, grid_energy_export: 15.0 },
store.get
)
end

test "reset after get" do
store = MetricStore.new

store.set(solar_power: 1, solar_energy: 4, grid_power: 7, grid_energy_import: 10, grid_energy_export: 13)
store.set(solar_power: 2, solar_energy: 5, grid_power: 8, grid_energy_import: 11, grid_energy_export: 14)
store.set(solar_power: 3, solar_energy: 6, grid_power: 9, grid_energy_import: 12, grid_energy_export: 15)

store.get

assert_equal(
{ solar_power: 0, solar_energy: 0.0, grid_power: 0, grid_energy_import: 0.0, grid_energy_export: 0.0 },
store.get
)
end
end
52 changes: 0 additions & 52 deletions test/services/loop_test.rb

This file was deleted.

0 comments on commit edc0e5e

Please sign in to comment.