diff --git a/.rubocop.yml b/.rubocop.yml index 7d06261..5f27091 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -41,6 +41,8 @@ Minitest/MultipleAssertions: Performance/StringIdentifierArgument: Enabled: false +Rails/CreateTableWithTimestamps: + Enabled: false Rails/HttpStatus: EnforcedStyle: numeric diff --git a/Gemfile b/Gemfile index 4c14d9c..b0ff520 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index 6b4d497..e37ab1d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -322,7 +327,6 @@ DEPENDENCIES better_errors bootsnap capybara - clockwork debug dotenv-rails foreman @@ -337,6 +341,7 @@ DEPENDENCIES rubocop-minitest rubocop-performance rubocop-rails + rufus-scheduler selenium-webdriver solid_queue sqids diff --git a/Procfile b/Procfile index c7ca4a7..78ac768 100644 --- a/Procfile +++ b/Procfile @@ -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 diff --git a/app/lib/solaris/energy_price.rb b/app/lib/solaris/energy_price.rb deleted file mode 100644 index efdbb14..0000000 --- a/app/lib/solaris/energy_price.rb +++ /dev/null @@ -1,50 +0,0 @@ -module Solaris - class EnergyPrice - def self.read(date = Date.current) - new(date).read - end - - def self.store(date = Date.current) - new(date).store - end - - def initialize(date) - @date = date - end - - def read - @read ||= candidate&.new(@date)&.read || nil - end - - def store - store_import_prices - store_export_prices - end - - private - - def candidate - classes.find(&:available?) - end - - def classes - [Solaris::EnergyPrices::ESIOS] - end - - def store_import_prices - read[:import].each do |data| - energy_price = ::EnergyPrice.find_or_initialize_by(datetime: data[:datetime]) - energy_price.assign_attributes(import: data[:value]) - energy_price.save - end - end - - def store_export_prices - read[:export].each do |data| - energy_price = ::EnergyPrice.find_or_initialize_by(datetime: data[:datetime]) - energy_price.assign_attributes(export: data[:value]) - energy_price.save - end - end - end -end diff --git a/app/lib/solaris/energy_prices/base.rb b/app/lib/solaris/energy_prices/base.rb deleted file mode 100644 index 5031ff8..0000000 --- a/app/lib/solaris/energy_prices/base.rb +++ /dev/null @@ -1,25 +0,0 @@ -module Solaris - module EnergyPrices - class Base - def self.available? - raise NotImplementedError - end - - def initialize(date) - @date = date - end - - def read - { import: import || [], export: export || [] } - end - - def import - raise NotImplementedError - end - - def export - raise NotImplementedError - end - end - end -end diff --git a/app/lib/solaris/energy_prices/esios.rb b/app/lib/solaris/energy_prices/esios.rb deleted file mode 100644 index 656a939..0000000 --- a/app/lib/solaris/energy_prices/esios.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Solaris - module EnergyPrices - class ESIOS < Base - def self.available? - ENV.fetch("ESIOS_API_KEY").present? - end - - def import - ::ESIOS::Import.for_date(@date) - end - - def export - ::ESIOS::Export.for_date(@date) - end - end - end -end diff --git a/app/lib/solaris/loop.rb b/app/lib/solaris/loop.rb index bfb129e..06d0203 100644 --- a/app/lib/solaris/loop.rb +++ b/app/lib/solaris/loop.rb @@ -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 diff --git a/app/models/energy_price.rb b/app/models/energy_price.rb deleted file mode 100644 index e9781fc..0000000 --- a/app/models/energy_price.rb +++ /dev/null @@ -1,5 +0,0 @@ -class EnergyPrice < ApplicationRecord - self.table_name = "energy_price" - - scope :by_date, ->(date) { where(datetime: date.all_day).order(datetime: :asc) } -end diff --git a/app/models/pvpc.rb b/app/models/pvpc.rb new file mode 100644 index 0000000..a9c6ee2 --- /dev/null +++ b/app/models/pvpc.rb @@ -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 diff --git a/app/lib/esios/export.rb b/app/services/esios/export.rb similarity index 100% rename from app/lib/esios/export.rb rename to app/services/esios/export.rb diff --git a/app/lib/esios/import.rb b/app/services/esios/import.rb similarity index 100% rename from app/lib/esios/import.rb rename to app/services/esios/import.rb diff --git a/app/lib/esios/indicator.rb b/app/services/esios/indicator.rb similarity index 83% rename from app/lib/esios/indicator.rb rename to app/services/esios/indicator.rb index 6490992..9929615 100644 --- a/app/lib/esios/indicator.rb +++ b/app/services/esios/indicator.rb @@ -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 diff --git a/app/services/store_pvpc.rb b/app/services/store_pvpc.rb new file mode 100644 index 0000000..b700992 --- /dev/null +++ b/app/services/store_pvpc.rb @@ -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 diff --git a/config/application.rb b/config/application.rb index 31894c8..da6f1f5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -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 diff --git a/config/clockwork.rb b/config/clockwork.rb deleted file mode 100644 index d2b5138..0000000 --- a/config/clockwork.rb +++ /dev/null @@ -1,20 +0,0 @@ -require_relative "environment" - -module Clockwork - configure do |config| - config[:tz] = ENV.fetch("SOLARIS_TIMEZONE", "Europe/Madrid").to_s - end - - every(ENV.fetch("SOLARIS_LOOP_INTERVAL", 30).to_i.seconds, "solaris.loop", thread: true) do - Solaris::Loop.run - end - - every(1.day, "solaris.energy_price", at: ENV.fetch("SOLARIS_ENERGY_PRICE_AT", "21:00").to_s, thread: true) do - price_for = ENV.fetch("SOLARIS_ENERGY_PRICE_FOR", "tomorrow").to_s - Solaris::EnergyPrice.store(price_for == "tomorrow" ? Date.tomorrow : Date.current) - end - - every(1.day, "solaris.archive.daily", at: "00:01", thread: true) do - Solaris::DailyArchive.run - end -end diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index b1a54fc..5f3981f 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -12,4 +12,5 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym "ESIOS" + inflect.acronym "PVPC" end diff --git a/config/scheduler.rb b/config/scheduler.rb new file mode 100644 index 0000000..de2245c --- /dev/null +++ b/config/scheduler.rb @@ -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 diff --git a/db/migrate/20230805145711_create_energy_price.rb b/db/migrate/20230805145711_create_energy_price.rb deleted file mode 100644 index 430db9f..0000000 --- a/db/migrate/20230805145711_create_energy_price.rb +++ /dev/null @@ -1,10 +0,0 @@ -class CreateEnergyPrice < ActiveRecord::Migration[7.1] - def change - # rubocop:disable Rails/CreateTableWithTimestamps - create_table :energy_price, id: :datetime, primary_key: :datetime do |t| - t.float :import - t.float :export - end - # rubocop:enable Rails/CreateTableWithTimestamps - end -end diff --git a/db/migrate/20230805145711_create_pvpc.rb b/db/migrate/20230805145711_create_pvpc.rb new file mode 100644 index 0000000..4bf33dd --- /dev/null +++ b/db/migrate/20230805145711_create_pvpc.rb @@ -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 diff --git a/db/migrate/20230805174919_create_archive.rb b/db/migrate/20230805174919_create_archive.rb index 0e1ae60..65fec5b 100644 --- a/db/migrate/20230805174919_create_archive.rb +++ b/db/migrate/20230805174919_create_archive.rb @@ -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 diff --git a/db/migrate/20230806183244_create_archive_daily_solar_power.rb b/db/migrate/20230806183244_create_archive_daily_solar_power.rb index ee2c29b..dce9a1c 100644 --- a/db/migrate/20230806183244_create_archive_daily_solar_power.rb +++ b/db/migrate/20230806183244_create_archive_daily_solar_power.rb @@ -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 diff --git a/db/migrate/20230807072241_create_archive_daily_solar_energy.rb b/db/migrate/20230807072241_create_archive_daily_solar_energy.rb index c9517c8..1a8aea8 100644 --- a/db/migrate/20230807072241_create_archive_daily_solar_energy.rb +++ b/db/migrate/20230807072241_create_archive_daily_solar_energy.rb @@ -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 diff --git a/db/migrate/20230807181438_create_archive_daily_grid_power_export.rb b/db/migrate/20230807181438_create_archive_daily_grid_power_export.rb index 020d73d..f6c64fd 100644 --- a/db/migrate/20230807181438_create_archive_daily_grid_power_export.rb +++ b/db/migrate/20230807181438_create_archive_daily_grid_power_export.rb @@ -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 diff --git a/db/migrate/20230807181440_create_archive_daily_grid_power_import.rb b/db/migrate/20230807181440_create_archive_daily_grid_power_import.rb index 2585a72..12458ee 100644 --- a/db/migrate/20230807181440_create_archive_daily_grid_power_import.rb +++ b/db/migrate/20230807181440_create_archive_daily_grid_power_import.rb @@ -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 diff --git a/db/migrate/20230807181444_create_archive_daily_grid_energy_import.rb b/db/migrate/20230807181444_create_archive_daily_grid_energy_import.rb index c8d1d57..b04d1e0 100644 --- a/db/migrate/20230807181444_create_archive_daily_grid_energy_import.rb +++ b/db/migrate/20230807181444_create_archive_daily_grid_energy_import.rb @@ -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 diff --git a/db/migrate/20230807182537_create_archive_daily_grid_energy_export.rb b/db/migrate/20230807182537_create_archive_daily_grid_energy_export.rb index 507d74c..2366a1e 100644 --- a/db/migrate/20230807182537_create_archive_daily_grid_energy_export.rb +++ b/db/migrate/20230807182537_create_archive_daily_grid_energy_export.rb @@ -1,6 +1,5 @@ class CreateArchiveDailyGridEnergyExport < ActiveRecord::Migration[7.1] def change - # rubocop:disable Rails/CreateTableWithTimestamps create_table :archive_daily_grid_energy_export, id: :date, primary_key: :date do |t| t.float :max, null: false t.datetime :maxtime, null: false diff --git a/db/migrate/20230808184043_create_archive_daily_energy_price_import.rb b/db/migrate/20230808184043_create_archive_daily_energy_price_import.rb index 20d459a..e337310 100644 --- a/db/migrate/20230808184043_create_archive_daily_energy_price_import.rb +++ b/db/migrate/20230808184043_create_archive_daily_energy_price_import.rb @@ -1,6 +1,5 @@ class CreateArchiveDailyEnergyPriceImport < ActiveRecord::Migration[7.1] def change - # rubocop:disable Rails/CreateTableWithTimestamps create_table :archive_daily_energy_price_import, id: :date, primary_key: :date do |t| t.float :max, null: false t.datetime :maxtime, null: false diff --git a/db/migrate/20230808185034_create_archive_daily_energy_price_export.rb b/db/migrate/20230808185034_create_archive_daily_energy_price_export.rb index f1bc270..ee86e77 100644 --- a/db/migrate/20230808185034_create_archive_daily_energy_price_export.rb +++ b/db/migrate/20230808185034_create_archive_daily_energy_price_export.rb @@ -1,6 +1,5 @@ class CreateArchiveDailyEnergyPriceExport < ActiveRecord::Migration[7.1] def change - # rubocop:disable Rails/CreateTableWithTimestamps create_table :archive_daily_energy_price_export, id: :date, primary_key: :date do |t| t.float :max, null: false t.datetime :maxtime, null: false diff --git a/db/schema.rb b/db/schema.rb index 3144cef..c0da596 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -89,11 +89,6 @@ t.index ["code"], name: "index_countries_on_code", unique: true end - create_table "energy_price", primary_key: "datetime", id: :datetime, force: :cascade do |t| - t.float "import" - t.float "export" - end - create_table "holidays", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -142,6 +137,12 @@ t.index ["name"], name: "index_protocols_on_name", unique: true end + create_table "pvpc", primary_key: "datetime", id: :datetime, force: :cascade 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 + create_table "settings", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false diff --git a/test/fixtures/energy_price.yml b/test/fixtures/energy_price.yml deleted file mode 100644 index a75fa1d..0000000 --- a/test/fixtures/energy_price.yml +++ /dev/null @@ -1,119 +0,0 @@ -energy_price0: - datetime: 2023-03-19T00:00:00+01:00 - import: 0.1563 - export: 0.1087 - -energy_price1: - datetime: 2023-03-19T01:00:00+01:00 - import: 0.1542 - export: 0.1047 - -energy_price2: - datetime: 2023-03-19T02:00:00+01:00 - import: 0.1498 - export: 0.0984 - -energy_price3: - datetime: 2023-03-19T03:00:00+01:00 - import: 0.1513 - export: 0.0981 - -energy_price4: - datetime: 2023-03-19T04:00:00+01:00 - import: 0.1507 - export: 0.0975 - -energy_price5: - datetime: 2023-03-19T05:00:00+01:00 - import: 0.1554 - export: 0.1021 - -energy_price6: - datetime: 2023-03-19T06:00:00+01:00 - import: 0.1503 - export: 0.0988 - -energy_price7: - datetime: 2023-03-19T07:00:00+01:00 - import: 0.1508 - export: 0.1012 - -energy_price8: - datetime: 2023-03-19T08:00:00+01:00 - import: 0.1464 - export: 0.0982 - -energy_price9: - datetime: 2023-03-19T09:00:00+01:00 - import: 0.1136 - export: 0.0689 - -energy_price10: - datetime: 2023-03-19T10:00:00+01:00 - import: 0.104 - export: 0.064 - -energy_price11: - datetime: 2023-03-19T11:00:00+01:00 - import: 0.1022 - export: 0.064 - -energy_price12: - datetime: 2023-03-19T12:00:00+01:00 - import: 0.1042 - export: 0.0652 - -energy_price13: - datetime: 2023-03-19T13:00:00+01:00 - import: 0.1076 - export: 0.0686 - -energy_price14: - datetime: 2023-03-19T14:00:00+01:00 - import: 0.1082 - export: 0.0686 - -energy_price15: - datetime: 2023-03-19T15:00:00+01:00 - import: 0.0864 - export: 0.049 - -energy_price16: - datetime: 2023-03-19T16:00:00+01:00 - import: 0.0988 - export: 0.057 - -energy_price17: - datetime: 2023-03-19T17:00:00+01:00 - import: 0.1117 - export: 0.0681 - -energy_price18: - datetime: 2023-03-19T18:00:00+01:00 - import: 0.1491 - export: 0.1028 - -energy_price19: - datetime: 2023-03-19T19:00:00+01:00 - import: 0.2012 - export: 0.1517 - -energy_price20: - datetime: 2023-03-19T20:00:00+01:00 - import: 0.1912 - export: 0.1437 - -energy_price21: - datetime: 2023-03-19T21:00:00+01:00 - import: 0.181 - export: 0.1343 - -energy_price22: - datetime: 2023-03-19T22:00:00+01:00 - import: 0.1792 - export: 0.1319 - -energy_price23: - datetime: 2023-03-19T23:00:00+01:00 - import: 0.1775 - export: 0.129 diff --git a/test/fixtures/pvpc.yml b/test/fixtures/pvpc.yml new file mode 100644 index 0000000..67041f5 --- /dev/null +++ b/test/fixtures/pvpc.yml @@ -0,0 +1,143 @@ +pvpc0: + datetime: 2024-01-01T00:00:00+01:00 + import: 0.1238 + export: 0.0611 + factor: 2.03 + +pvpc1: + datetime: 2024-01-01T01:00:00+01:00 + import: 0.1111 + export: 0.0482 + factor: 2.3 + +pvpc2: + datetime: 2024-01-01T02:00:00+01:00 + import: 0.1108 + export: 0.0453 + factor: 2.45 + +pvpc3: + datetime: 2024-01-01T03:00:00+01:00 + import: 0.1093 + export: 0.0415 + factor: 2.63 + +pvpc4: + datetime: 2024-01-01T04:00:00+01:00 + import: 0.1102 + export: 0.0401 + factor: 2.75 + +pvpc5: + datetime: 2024-01-01T05:00:00+01:00 + import: 0.1115 + export: 0.04 + factor: 2.79 + +pvpc6: + datetime: 2024-01-01T06:00:00+01:00 + import: 0.11 + export: 0.0403 + factor: 2.73 + +pvpc7: + datetime: 2024-01-01T07:00:00+01:00 + import: 0.1069 + export: 0.0404 + factor: 2.65 + +pvpc8: + datetime: 2024-01-01T08:00:00+01:00 + import: 0.1073 + export: 0.0414 + factor: 2.59 + +pvpc9: + datetime: 2024-01-01T09:00:00+01:00 + import: 0.1033 + export: 0.0404 + factor: 2.56 + +pvpc10: + datetime: 2024-01-01T10:00:00+01:00 + import: 0.0764 + export: 0.0234 + factor: 3.26 + +pvpc11: + datetime: 2024-01-01T11:00:00+01:00 + import: 0.0476 + export: 0.002 + factor: 23.8 + +pvpc12: + datetime: 2024-01-01T12:00:00+01:00 + import: 0.0464 + export: 0.0016 + factor: 29 + +pvpc13: + datetime: 2024-01-01T13:00:00+01:00 + import: 0.0447 + export: 0.0005 + factor: 89.4 + +pvpc14: + datetime: 2024-01-01T14:00:00+01:00 + import: 0.0442 + export: 0.0002 + factor: 99 + +pvpc15: + datetime: 2024-01-01T15:00:00+01:00 + import: 0.0513 + export: 0.0041 + factor: 12.51 + +pvpc16: + datetime: 2024-01-01T16:00:00+01:00 + import: 0.0683 + export: 0.0169 + factor: 4.04 + +pvpc17: + datetime: 2024-01-01T17:00:00+01:00 + import: 0.0886 + export: 0.0356 + factor: 2.49 + +pvpc18: + datetime: 2024-01-01T18:00:00+01:00 + import: 0.0962 + export: 0.046 + factor: 2.09 + +pvpc19: + datetime: 2024-01-01T19:00:00+01:00 + import: 0.1033 + export: 0.0533 + factor: 1.94 + +pvpc20: + datetime: 2024-01-01T20:00:00+01:00 + import: 0.1101 + export: 0.0593 + factor: 1.86 + +pvpc21: + datetime: 2024-01-01T21:00:00+01:00 + import: 0.11 + export: 0.0584 + factor: 1.88 + +pvpc22: + datetime: 2024-01-01T22:00:00+01:00 + import: 0.0941 + export: 0.0459 + factor: 2.05 + +pvpc23: + datetime: 2024-01-01T23:00:00+01:00 + import: 0.0885 + export: 0.0405 + factor: 2.19 diff --git a/test/models/pvpc_test.rb b/test/models/pvpc_test.rb new file mode 100644 index 0000000..fb55f19 --- /dev/null +++ b/test/models/pvpc_test.rb @@ -0,0 +1,24 @@ +require "test_helper" + +class PVPCTest < ActiveSupport::TestCase + test "create without import" do + pvpc = PVPC.new + + assert_not pvpc.save + assert pvpc.errors.added?(:import, :blank) + end + + test "create without export" do + pvpc = PVPC.new + + assert_not pvpc.save + assert pvpc.errors.added?(:export, :blank) + end + + test "create with valid attributes" do + pvpc = PVPC.new(datetime: Time.current, import: 0.1, export: 0.05) + + assert pvpc.save + assert_equal 2, pvpc.factor + end +end diff --git a/test/services/store_pvpc_test.rb b/test/services/store_pvpc_test.rb new file mode 100644 index 0000000..76718ed --- /dev/null +++ b/test/services/store_pvpc_test.rb @@ -0,0 +1,52 @@ +require "test_helper" + +class StorePVPCTest < ActiveSupport::TestCase + test "store PVPC" do + assert_difference("PVPC.count", 2) do + stub_esios do + StorePVPC.run(Date.parse("2022-11-22")) + end + end + end + + test "store PVPC with existing data" do + stub_esios do + StorePVPC.run(Date.parse("2022-11-22")) + + assert_no_difference("PVPC.count") do + StorePVPC.run(Date.parse("2022-11-22")) + end + end + end + + test "check stored data" do + stub_esios do + StorePVPC.run(Date.parse("2022-11-22")) + end + + pvpc = PVPC.find_by(datetime: Time.zone.parse("2022-11-22 00:00:00")) + + assert_in_delta(0.1, pvpc.import) + assert_in_delta(0.05, pvpc.export) + end + + def stub_esios(&) + ESIOS::Import.stub(:for_date, import_response) do + ESIOS::Export.stub(:for_date, export_response, &) + end + end + + def import_response + { + Time.zone.parse("2022-11-22 00:00:00") => 0.1, + Time.zone.parse("2022-11-22 01:00:00") => 0.2 + } + end + + def export_response + { + Time.zone.parse("2022-11-22 00:00:00") => 0.05, + Time.zone.parse("2022-11-22 01:00:00") => 0.1 + } + end +end