Skip to content

Commit

Permalink
[skip ci] WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 19, 2024
1 parent 3417d5b commit 921b9e7
Show file tree
Hide file tree
Showing 59 changed files with 869 additions and 173 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ gem "bindata"
gem "bootsnap", require: false
gem "concurrent-ruby"
gem "foreman"
gem "groupdate"
gem "http"
gem "mqtt"
gem "rails-settings-cached"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ GEM
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 @@ -336,6 +338,7 @@ DEPENDENCIES
debug
dotenv-rails
foreman
groupdate
http
importmap-rails
mqtt
Expand Down
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
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
2 changes: 2 additions & 0 deletions app/models/daily_energy_export_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class DailyEnergyExportPrice < ApplicationRecord
end
2 changes: 2 additions & 0 deletions app/models/daily_energy_import_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class DailyEnergyImportPrice < ApplicationRecord
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
15 changes: 15 additions & 0 deletions app/models/energy_price.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class EnergyPrice < ApplicationRecord
self.store_full_sti_class = false

belongs_to :zone

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

validates :datetime, uniqueness: { scope: %i[type zone_id] }
validates :import, presence: true, numericality: true
validates :export, presence: true, numericality: true

def self.load(date, zone)
create!({ import: import(date, zone) || [], export: export(date, zone) || [] })
end
end
11 changes: 11 additions & 0 deletions app/models/energy_prices/pvpc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module EnergyPrices
class PVPC < EnergyPrice
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
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
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
2 changes: 1 addition & 1 deletion app/services/esios/indicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def url
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 Down
2 changes: 2 additions & 0 deletions config/initializers/core_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "core_extensions/array"
require "core_extensions/string"
require "core_extensions/time"

Array.include CoreExtensions::Array
String.include CoreExtensions::String
Time.include CoreExtensions::Time
10 changes: 5 additions & 5 deletions db/migrate/20230805174919_create_archive.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class CreateArchive < ActiveRecord::Migration[7.1]
def change
create_table :archive, id: { type: :datetime, precision: 0 }, primary_key: :datetime do |t|
t.float :solar_power, null: false
t.float :solar_energy, null: false
t.float :grid_power, null: false
t.float :grid_energy_export, null: false
t.float :grid_energy_import, null: false
t.integer :solar_power, null: false
t.decimal :solar_energy, precision: 4, scale: 2, null: false
t.integer :grid_power, null: false
t.decimal :grid_energy_export, precision: 8, scale: 2, null: false
t.decimal :grid_energy_import, precision: 8, scale: 2, null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class CreateArchiveDailySolarPower < ActiveRecord::Migration[7.1]
def change
create_table :archive_daily_solar_power, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.integer :max, null: false
t.datetime :maxtime, null: false
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class CreateArchiveDailySolarEnergy < ActiveRecord::Migration[7.1]
def change
create_table :archive_daily_solar_energy, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.decimal :max, precision: 4, scale: 2, null: false
t.datetime :maxtime, null: false
t.float :sum, null: false
t.decimal :sum, precision: 8, scale: 2, null: false
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class CreateArchiveDailyGridPowerExport < ActiveRecord::Migration[7.1]
def change
create_table :archive_daily_grid_power_export, id: :date, primary_key: :date do |t|
t.float :max, null: false
t.datetime :maxtime
t.integer :max, null: false
t.datetime :maxtime, null: false
end
end
end
Loading

0 comments on commit 921b9e7

Please sign in to comment.