Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change clockwork by rufus scheduler #76

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Minitest/MultipleAssertions:
Performance/StringIdentifierArgument:
Enabled: false

Rails/CreateTableWithTimestamps:
Enabled: false
Rails/HttpStatus:
EnforcedStyle: numeric

Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ gem "turbo-rails"

## LIBRARIES
gem "bootsnap", require: false
gem "clockwork"
gem "foreman"
gem "http"
gem "rmodbus"
gem "rufus-scheduler"
gem "solid_queue"
gem "sqids"

Expand Down
13 changes: 9 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ GEM
rack-test (>= 0.6.3)
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
clockwork (3.0.2)
activesupport
tzinfo
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
crass (1.0.6)
Expand All @@ -116,11 +113,16 @@ GEM
drb (2.2.0)
ruby2_keywords
erubi (1.12.0)
et-orbi (1.2.7)
tzinfo
ffi (1.16.3)
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
rake
foreman (0.87.2)
fugit (1.9.0)
et-orbi (~> 1, >= 1.2.7)
raabro (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
http (5.1.1)
Expand Down Expand Up @@ -190,6 +192,7 @@ GEM
public_suffix (5.0.3)
puma (6.4.2)
nio4r (~> 2.0)
raabro (1.4.0)
racc (1.7.3)
rack (3.0.8)
rack-session (2.0.0)
Expand Down Expand Up @@ -267,6 +270,8 @@ GEM
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
rufus-scheduler (3.9.1)
fugit (~> 1.1, >= 1.1.6)
selenium-webdriver (4.16.0)
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
Expand Down Expand Up @@ -322,7 +327,6 @@ DEPENDENCIES
better_errors
bootsnap
capybara
clockwork
debug
dotenv-rails
foreman
Expand All @@ -337,6 +341,7 @@ DEPENDENCIES
rubocop-minitest
rubocop-performance
rubocop-rails
rufus-scheduler
selenium-webdriver
solid_queue
sqids
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
web: bin/puma -C config/puma.rb
job: bin/rails solid_queue:start
clock: bin/clockwork config/clockwork.rb
scheduler: bin/rails run config/scheduler.rb
50 changes: 0 additions & 50 deletions app/lib/solaris/energy_price.rb

This file was deleted.

25 changes: 0 additions & 25 deletions app/lib/solaris/energy_prices/base.rb

This file was deleted.

17 changes: 0 additions & 17 deletions app/lib/solaris/energy_prices/esios.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/lib/solaris/loop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def run
private

def archive_interval
ENV.fetch("SOLARIS_ARCHIVE_INTERVAL", 60).to_i
Rails.application.config.x.intervals.archive
end

def last_archive
Expand Down
5 changes: 0 additions & 5 deletions app/models/energy_price.rb

This file was deleted.

18 changes: 18 additions & 0 deletions app/models/pvpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class PVPC < ApplicationRecord
self.table_name = "pvpc"

validates :import, presence: true
validates :export, presence: true

scope :by_date, ->(date) { where(datetime: date.all_day).order(datetime: :asc) }

before_validation :set_factor, on: :create

private

def set_factor
return unless import.present? && export.present?

self.factor = [(import / export).round(2), 99].min
end
end
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions app/lib/esios/indicator.rb → app/services/esios/indicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def perform_request(params = {})
def parse_response(response)
response = response.status.success? ? response.parse : {}

(response.dig("indicator", "values") || []).map do |value|
{
datetime: DateTime.parse(value["datetime"]),
value: (value["value"] / 1000.0).round(4)
}
(response.dig("indicator", "values") || []).to_h do |value|
[
DateTime.parse(value["datetime"]),
(value["value"] / 1000.0).round(4)
]
end
end
end
Expand Down
34 changes: 34 additions & 0 deletions app/services/store_pvpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class StorePVPC
def self.run(date = Date.current)
new(date).run
end

def initialize(date)
@date = date
end

def run
read[:import].each_pair do |datetime, value|
PVPC.find_or_create_by(datetime:) do |pvpc|
pvpc.import = value
pvpc.export = read[:export][datetime]
end
end

read
end

private

def read
@read ||= { import: import || {}, export: export || {} }
end

def import
ESIOS::Import.for_date(@date)
end

def export
ESIOS::Export.for_date(@date)
end
end
14 changes: 6 additions & 8 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.1

# Configuration for the application, engines, and railties goes here.
#
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
config.time_zone = ENV.fetch("SOLARIS_TIMEZONE", "Europe/Madrid").to_s
# Application Timezone
config.time_zone = ENV.fetch("TZ", "Europe/Madrid")

# Intervals
config.x.intervals.loop = ENV.fetch("SOLARIS_LOOP_INTERVAL", 30).to_i
config.x.intervals.archive = ENV.fetch("SOLARIS_ARCHIVE_INTERVAL", 300).to_i
end
end
20 changes: 0 additions & 20 deletions config/clockwork.rb

This file was deleted.

1 change: 1 addition & 0 deletions config/initializers/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@

ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym "ESIOS"
inflect.acronym "PVPC"
end
16 changes: 16 additions & 0 deletions config/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
scheduler = Rufus::Scheduler.new
loop_interval = Rails.application.config.x.intervals.loop

scheduler.every loop_interval, name: "solaris.loop", first_at: Time.current.beginning_of_minute + 1.minute do
Solaris::Loop.run
end

scheduler.cron "0 21 * * * Europe/Madrid", name: "solaris.energy_price" do
StorePVPC.run(Date.tomorrow)
end

scheduler.cron "1 0 * * * #{Rails.application.config.time_zone}", name: "solaris.archive.daily" do
Solaris::DailyArchive.run
end

scheduler.join
10 changes: 0 additions & 10 deletions db/migrate/20230805145711_create_energy_price.rb

This file was deleted.

9 changes: 9 additions & 0 deletions db/migrate/20230805145711_create_pvpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreatePVPC < ActiveRecord::Migration[7.1]
def change
create_table :pvpc, id: :datetime, primary_key: :datetime do |t|
t.decimal :import, precision: 8, scale: 6, null: false
t.decimal :export, precision: 8, scale: 6, null: false
t.decimal :factor, precision: 4, scale: 2, null: false
end
end
end
1 change: 0 additions & 1 deletion db/migrate/20230805174919_create_archive.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class CreateArchive < ActiveRecord::Migration[7.1]
def change
# rubocop:disable Rails/CreateTableWithTimestamps
create_table :archive, id: :datetime, primary_key: :created_at do |t|
t.float :solar_power, null: false
t.float :solar_energy, null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class CreateArchiveDailySolarPower < ActiveRecord::Migration[7.1]
def change
# rubocop:disable Rails/CreateTableWithTimestamps
create_table :archive_daily_solar_power, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.datetime :maxtime, null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class CreateArchiveDailySolarEnergy < ActiveRecord::Migration[7.1]
def change
# rubocop:disable Rails/CreateTableWithTimestamps
create_table :archive_daily_solar_energy, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.datetime :maxtime, null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class CreateArchiveDailyGridPowerExport < ActiveRecord::Migration[7.1]
def change
# rubocop:disable Rails/CreateTableWithTimestamps
create_table :archive_daily_grid_power_export, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.datetime :maxtime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class CreateArchiveDailyGridPowerImport < ActiveRecord::Migration[7.1]
def change
# rubocop:disable Rails/CreateTableWithTimestamps
create_table :archive_daily_grid_power_import, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.datetime :maxtime
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class CreateArchiveDailyGridEnergyImport < ActiveRecord::Migration[7.1]
def change
# rubocop:disable Rails/CreateTableWithTimestamps
create_table :archive_daily_grid_energy_import, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.datetime :maxtime, null: false
Expand Down
Loading