Skip to content

Commit

Permalink
Refactor dailies and energy prices
Browse files Browse the repository at this point in the history
  • Loading branch information
javierav committed Jan 21, 2024
1 parent 3417d5b commit 18f6680
Show file tree
Hide file tree
Showing 60 changed files with 819 additions and 460 deletions.
32 changes: 0 additions & 32 deletions app/lib/solaris/daily_archive.rb

This file was deleted.

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.

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

This file was deleted.

17 changes: 0 additions & 17 deletions app/lib/solaris/daily_archives/grid_power_export.rb

This file was deleted.

17 changes: 0 additions & 17 deletions app/lib/solaris/daily_archives/grid_power_import.rb

This file was deleted.

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

This file was deleted.

17 changes: 0 additions & 17 deletions app/lib/solaris/daily_archives/solar_power.rb

This file was deleted.

4 changes: 3 additions & 1 deletion app/models/archive.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Archive < ApplicationRecord
self.table_name = "archive"

scope :by_date, ->(date) { where(created_at: date.all_day).order(created_at: :asc) }
delegate :hour, to: :datetime

scope :by_date, ->(date) { where(datetime: date.all_day).order(datetime: :asc) }
end
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.

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

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

This file was deleted.

35 changes: 35 additions & 0 deletions app/models/dailies/energy_price_export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Dailies
class EnergyPriceExport < Daily
self.table_name = "daily_energy_price_export"

validates :max, presence: true, numericality: true
validates :maxhour, presence: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 23
}
validates :min, presence: true, numericality: true
validates :minhour, presence: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 23
}
validates :avg, presence: true, numericality: true

def compute_max
EnergyPrice.by_date(date).maximum(:export)
end

def compute_maxhour
EnergyPrice.by_date(date).max_by(&:export).try(:datetime).try(:hour)
end

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

def compute_minhour
EnergyPrice.by_date(date).min_by(&:export).try(:datetime).try(:hour)
end

def compute_avg
EnergyPrice.by_date(date).average(:export).try(:round, 4)
end
end
end
35 changes: 35 additions & 0 deletions app/models/dailies/energy_price_import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Dailies
class EnergyPriceImport < Daily
self.table_name = "daily_energy_price_import"

validates :max, presence: true, numericality: true
validates :maxhour, presence: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 23
}
validates :min, presence: true, numericality: true
validates :minhour, presence: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 23
}
validates :avg, presence: true, numericality: true

def compute_max
EnergyPrice.by_date(date).maximum(:import)
end

def compute_maxhour
EnergyPrice.by_date(date).max_by(&:import).try(:datetime).try(:hour)
end

def compute_min
EnergyPrice.by_date(date).minimum(:import)
end

def compute_minhour
EnergyPrice.by_date(date).min_by(&:import).try(:datetime).try(:hour)
end

def compute_avg
EnergyPrice.by_date(date).average(:import).try(:round, 4)
end
end
end
24 changes: 24 additions & 0 deletions app/models/dailies/grid_energy_export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Dailies
class GridEnergyExport < Daily
self.table_name = "daily_grid_energy_export"

validates :max, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :maxhour, presence: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 23
}
validates :sum, presence: true, numericality: { greater_than_or_equal_to: 0 }

private

def group_by_hour
@group_by_hour ||= Archive.by_date(date).group_by(&:hour)
end

def sum_by_hour
@sum_by_hour ||= group_by_hour.transform_values do |archives|
archives.max_by(&:grid_energy_export).grid_energy_export -
archives.min_by(&:grid_energy_export).grid_energy_export
end
end
end
end
15 changes: 15 additions & 0 deletions app/models/dailies/grid_energy_import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Dailies
class GridEnergyImport < Daily
self.table_name = "daily_grid_energy_import"

validates :max, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :maxhour, presence: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 23
}
validates :min, presence: true, numericality: { greater_than_or_equal_to: 0 }
validates :minhour, presence: true, numericality: {
only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 23
}
validates :sum, presence: true, numericality: { greater_than_or_equal_to: 0 }
end
end
16 changes: 16 additions & 0 deletions app/models/dailies/grid_power_export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Dailies
class GridPowerExport < Daily
self.table_name = "daily_grid_power_export"

validates :max, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :maxtime, presence: true

def compute_max
Archive.by_date(date).where("grid_power >= 0").maximum(:grid_power).try(:round, 2) || 0
end

def compute_maxtime
Archive.by_date(date).where("grid_power >= 0").max_by(&:grid_power).try(:datetime)
end
end
end
Loading

0 comments on commit 18f6680

Please sign in to comment.