Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 6, 2024
1 parent 28e646e commit bd2b894
Show file tree
Hide file tree
Showing 93 changed files with 1,175 additions and 582 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
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ gem "turbo-rails"

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

Expand Down
16 changes: 12 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,13 +113,20 @@ 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)
groupdate (6.4.0)
activesupport (>= 6.1)
http (5.1.1)
addressable (~> 2.8)
http-cookie (~> 1.0)
Expand Down Expand Up @@ -190,6 +194,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 +272,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,10 +329,10 @@ DEPENDENCIES
better_errors
bootsnap
capybara
clockwork
debug
dotenv-rails
foreman
groupdate
http
importmap-rails
propshaft
Expand All @@ -337,6 +344,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
28 changes: 28 additions & 0 deletions app/aggregations/application_aggregation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class ApplicationAggregation
def initialize(date, archives)
@date = date
@archives = archives
end

def run
instance = model.find_or_initialize_by(date: @date)
instance.assign_attributes(aggregations)
instance.save!
end

private

def aggregations_columns
%i[max maxtime min mintime sum avg]
end

def aggregations
aggregations_columns.each_with_object({}) do |column, hash|
hash[column] = send(column) if respond_to?(column)
end
end

def model
"ArchiveDaily#{self.class.name.demodulize}".constantize
end
end
31 changes: 31 additions & 0 deletions app/aggregations/daily_grid_energy_export_aggregation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class DailyGridEnergyExportAggregation < Base
def max
sum_by_hour.values.max.round(2)
end

def maxtime
date.to_time.change(hour: sum_by_hour.max_by { |a| a[1] }.first)
end

def sum
(archives_array.last.grid_energy_export - archives_array.first.grid_energy_export).round(2)
end

private

def archives_array
@archives_array ||= archives.to_a
end

def group_by_hour
@group_by_hour ||= archives_array.group_by do |archive|
archive.created_at.hour
end
end

def sum_by_hour
@sum_by_hour ||= group_by_hour.transform_values do |archives|
archives.last.grid_energy_export - archives.first.grid_energy_export
end
end
end
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
File renamed without changes.
File renamed without changes.
34 changes: 0 additions & 34 deletions app/lib/solaris/daily_archives/base.rb

This file was deleted.

25 changes: 0 additions & 25 deletions app/lib/solaris/daily_archives/energy_price_export.rb

This file was deleted.

35 changes: 0 additions & 35 deletions app/lib/solaris/daily_archives/grid_energy_export.rb

This file was deleted.

27 changes: 27 additions & 0 deletions app/lib/solaris/pvpcs/daily/export_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Solaris
module PVPCs
module Daily
class ExportPrice < Base
def max
::EnergyPrice.by_date(date).maximum(:export)
end

def maxtime
::EnergyPrice.by_date(date).where(export: max).first.datetime
end

def min
::EnergyPrice.by_date(date).minimum(:export)
end

def mintime
::EnergyPrice.by_date(date).where(export: min).first.datetime
end

def avg
::EnergyPrice.by_date(date).average(:export).round(4)
end
end
end
end
end
File renamed without changes.
3 changes: 0 additions & 3 deletions app/models/archive_daily_energy_price_export.rb

This file was deleted.

3 changes: 0 additions & 3 deletions app/models/archive_daily_energy_price_import.rb

This file was deleted.

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
Loading

0 comments on commit bd2b894

Please sign in to comment.