Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Dec 29, 2023
1 parent 28e646e commit 25b5f37
Show file tree
Hide file tree
Showing 49 changed files with 557 additions and 377 deletions.
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.8.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.0)
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.8.2)
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: ruby config/scheduler.rb
4 changes: 0 additions & 4 deletions app/lib/esios/export.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ class Export < Indicator
def url
"https://api.esios.ree.es/indicators/1739"
end

def geo_id
ENV.fetch("ESIOS_COUNTRY_GEO_ID", 3).to_i
end
end
end
4 changes: 0 additions & 4 deletions app/lib/esios/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@ class Import < Indicator
def url
"https://api.esios.ree.es/indicators/1001"
end

def geo_id
ENV.fetch("ESIOS_ZONE_GEO_ID", 8741).to_i
end
end
end
25 changes: 9 additions & 16 deletions app/lib/esios/indicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ module ESIOS
class Indicator
include MissingSingleton

def for_today
perform_request
end

def for_date(date)
def for_date(date, geo_id = nil)
if date.is_a?(Range)
perform_request(
"start_date" => date.first.beginning_of_day.iso8601,
"end_date" => date.last.end_of_day.iso8601
new(
"start_date" => date.first.beginning_of_day.iso8601, "end_date" => date.last.end_of_day.iso8601,
"geo_ids[]" => geo_id
)
else
perform_request("datetime" => date.iso8601)
perform_request("datetime" => date.iso8601, "geo_ids[]" => geo_id)
end
end

Expand All @@ -23,15 +19,11 @@ def base_request
HTTP
.headers("Accept" => "application/json; application/vnd.esios-api-v2+json")
.headers("Content-Type" => "application/json")
.headers("x-api-key" => ENV.fetch("ESIOS_API_KEY"))
end

def base_params
{ "geo_ids[]" => geo_id }
.headers("x-api-key" => ENV.fetch("SOLARIS_ENERGY_PRICE_ESIOS_API_KEY"))
end

def perform_request(params = {})
parse_response(base_request.get(url, params: base_params.merge(params)))
parse_response(base_request.get(url, params: params.compact))
end

def parse_response(response)
Expand All @@ -40,7 +32,8 @@ def parse_response(response)
(response.dig("indicator", "values") || []).map do |value|
{
datetime: DateTime.parse(value["datetime"]),
value: (value["value"] / 1000.0).round(4)
value: (value["value"] / 1000.0).round(4),
geo_id: value["geo_id"]
}
end
end
Expand Down
16 changes: 16 additions & 0 deletions app/models/cost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Cost < ApplicationRecord
validates :start_at, presence: true
validates :transport_toll_p1, presence: true
validates :transport_toll_p2, presence: true
validates :transport_toll_p3, presence: true
validates :distribution_toll_p1, presence: true
validates :distribution_toll_p2, presence: true
validates :distribution_toll_p3, presence: true
validates :charge_p1, presence: true
validates :charge_p2, presence: true
validates :charge_p3, presence: true

def self.for_time(time)
where('start_at <= ?', time).order(start_at: :desc).first
end
end
13 changes: 13 additions & 0 deletions app/models/energy_period.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class EnergyPeriod < ApplicationRecord
belongs_to :zone

enum :day_type, %i[working_day weekend_holiday].to_enum_hash
enum :kind, %i[p1 p2 p3].to_enum_hash # P1 (Punta), P2 (Llano), P3 (Valle)

validates :start_hour, presence: true
validates :end_hour, presence: true

def self.kind_for(time, zone)
where(zone: zone, day_type: time.day_type).where('start_hour <= ? AND end_hour >= ?', time.hour, time.hour).sole.kind
end
end
5 changes: 0 additions & 5 deletions app/models/energy_price.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/models/holiday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Holiday < ApplicationRecord
validates :name, presence: true
validates :date, presence: true, uniqueness: true
end
5 changes: 4 additions & 1 deletion app/models/inverter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class Inverter < ApplicationRecord
include Sqideable

enum :installation_type, %i[domestic other].to_enum_hash

belongs_to :protocol
belongs_to :zone

validates :name, presence: true
validates :timezone, presence: true
validates :installation_type, presence: true
end
19 changes: 19 additions & 0 deletions app/models/pvpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class PVPC < ApplicationRecord
self.table_name = "pvpc"

belongs_to :zone

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

def self.read(date, zone)
create!({ import: import(date, zone) || [], export: export(date, zone) || [] })
end

def self.import(date, zone)
::ESIOS::Import.for_date(date, zone)
end

def self.export(date, zone)
::ESIOS::Export.for_date(date, zone)
end
end
26 changes: 0 additions & 26 deletions app/models/setting.rb

This file was deleted.

15 changes: 15 additions & 0 deletions app/models/tax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Tax < ApplicationRecord
enum :kind, %i[value_add other].to_enum_hash
enum :installation_type, %i[domestic other].to_enum_hash

belongs_to :zone

validates :installation_type, presence: true
validates :start_at, presence: true
validates :name, presence: true
validates :percentage, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 1 }

def self.for_time_and_zone(time, zone)
where(zone: zone).where('start_at <= ?', time).order(start_at: :desc).first # TODO, puede haber varios impuestos
end
end
9 changes: 9 additions & 0 deletions app/models/zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Zone < ApplicationRecord
has_many :energy_periods, dependent: :destroy
has_many :pvpc, dependent: :destroy

validates :name, presence: true, uniqueness: true
validates :timezone, presence: true
validates :esios_import_geo_id, presence: true
validates :esios_export_geo_id, presence: true
end
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ class Application < Rails::Application
#
# 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
config.time_zone = "UTC"
end
end
20 changes: 0 additions & 20 deletions config/clockwork.rb

This file was deleted.

1 change: 1 addition & 0 deletions config/initializers/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require "core_extensions/array"
require "core_extensions/time"
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
18 changes: 18 additions & 0 deletions config/scheduler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require_relative "environment"

scheduler = Rufus::Scheduler.new
loop_interval = ENV.fetch("SOLARIS_LOOP_INTERVAL", 30).to_i

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_prices.esios_pvpc" do
Solaris::EnergyPrice.store(Date.tomorrow)
end

scheduler.cron "1 0 * * * #{tz}", 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.

18 changes: 0 additions & 18 deletions db/migrate/20231031072840_create_settings.rb

This file was deleted.

12 changes: 12 additions & 0 deletions db/migrate/20231224202745_create_zones.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateZones < ActiveRecord::Migration[7.1]
def change
create_table :zones do |t|
t.timestamps null: false
t.string :name, null: false
t.string :timezone, null: false
t.integer :esios_import_geo_id, null: false
t.integer :esios_export_geo_id, null: false
t.index :name, unique: true
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20231224203015_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: :time, primary_key: :time do |t|
t.references :zone, null: false, foreign_key: true
t.decimal :import, precision: 8, scale: 6, null: false
t.decimal :export, precision: 8, scale: 6, null: false
end
end
end
Loading

0 comments on commit 25b5f37

Please sign in to comment.