Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 3, 2024
1 parent 28e646e commit 851bbdf
Show file tree
Hide file tree
Showing 65 changed files with 950 additions and 394 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
18 changes: 18 additions & 0 deletions app/models/cost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Cost < ApplicationRecord
belongs_to :country

validates :start_at, presence: true
validates :transport_toll_p1, presence: true
validates :distribution_toll_p1, presence: true
validates :charges_p1, presence: true
validates :transport_toll_p2, presence: true
validates :distribution_toll_p2, presence: true
validates :charges_p2, presence: true
validates :transport_toll_p3, presence: true
validates :distribution_toll_p3, presence: true
validates :charges_p3, presence: true

def self.for_time(time)
where('start_at <= ?', time).order(start_at: :desc).first
end
end
11 changes: 11 additions & 0 deletions app/models/country.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Country < ApplicationRecord
has_many :costs, dependent: :destroy
has_many :zones, dependent: :destroy
has_many :holidays, class_name: 'CountryHoliday', dependent: :destroy

validates :code, presence: true, uniqueness: true
validates :name, presence: true
validates :p1_name, presence: true
validates :p2_name, presence: true
validates :p3_name, presence: true
end
6 changes: 6 additions & 0 deletions app/models/country_holiday.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class CountryHoliday < ApplicationRecord
belongs_to :country

validates :name, presence: true
validates :date, presence: true, uniqueness: { scope: :country_id }
end
14 changes: 14 additions & 0 deletions app/models/energy_period.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class EnergyPeriod < ApplicationRecord
has_and_belongs_to_many :zones, join_table: :zones_energy_periods

enum :name, %i[p1 p2 p3].to_enum_hash

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

# TODO, this is not working with new format
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.name
end
end
5 changes: 0 additions & 5 deletions app/models/energy_price.rb

This file was deleted.

7 changes: 6 additions & 1 deletion app/models/inverter.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
class Inverter < ApplicationRecord
include Sqideable

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

belongs_to :protocol
belongs_to :zone

has_many :rates, dependent: :destroy

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
5 changes: 5 additions & 0 deletions app/models/rate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Rate < ApplicationRecord
belongs_to :inverter

validates :start_at, presence: true, uniqueness: { scope: :inverter_id }
end
4 changes: 4 additions & 0 deletions app/models/rates/by_period.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Rates
class ByPeriod < Rate
end
end
4 changes: 4 additions & 0 deletions app/models/rates/fixed.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Rates
class Fixed < Rate
end
end
4 changes: 4 additions & 0 deletions app/models/rates/pvpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Rates
class PVPC < Rate
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, prefix: true
enum :installation_type, %i[domestic other].to_enum_hash, prefix: true

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
12 changes: 12 additions & 0 deletions app/models/zone.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Zone < ApplicationRecord
belongs_to :country

has_many :energy_periods, dependent: :destroy
has_many :pvpc, dependent: :destroy

has_and_belongs_to_many :energy_periods, join_table: :zones_energy_periods

validates :name, presence: true, uniqueness: true
validates :timezone, presence: true
validates :configuration, 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.

Loading

0 comments on commit 851bbdf

Please sign in to comment.