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 28e9583
Show file tree
Hide file tree
Showing 54 changed files with 553 additions and 420 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.

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
11 changes: 11 additions & 0 deletions app/models/dailies/grid_energy_export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 }
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
8 changes: 8 additions & 0 deletions app/models/dailies/grid_power_export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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
end
end
10 changes: 10 additions & 0 deletions app/models/dailies/grid_power_import.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Dailies
class GridPowerImport < Daily
self.table_name = "daily_grid_power_import"

validates :max, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :maxtime, presence: true
validates :min, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates :mintime, presence: true
end
end
35 changes: 35 additions & 0 deletions app/models/dailies/solar_energy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Dailies
class SolarEnergy < Daily
self.table_name = "daily_solar_energy"

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 }

def compute_max
sum_by_hour.values.max.round(2)
end

def compute_maxhour
sum_by_hour.max_by(&:last).first
end

def compute_sum
Archive.by_date(date).maximum(:solar_energy)
end

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.last.solar_energy - archives.first.solar_energy
end
end
end
end
16 changes: 16 additions & 0 deletions app/models/dailies/solar_power.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Dailies
class SolarPower < Daily
self.table_name = "daily_solar_power"

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).maximum(:solar_power)
end

def compute_maxtime
Archive.by_date(date).max_by(&:solar_power).try(:datetime)
end
end
end
Loading

0 comments on commit 28e9583

Please sign in to comment.