Refactor to prepare for getting monthly overview
This commit is contained in:
@@ -116,24 +116,57 @@ class Cost
|
||||
g.data :costs, costs
|
||||
g.write("plots/easy_tariff_%s.png" % date.strftime("%F"))
|
||||
end
|
||||
|
||||
def easy_energy_cost_barplot(date)
|
||||
hour_start = date.in_time_zone(zone).beginning_of_day
|
||||
day_end = hour_start.advance(days: 1)
|
||||
|
||||
# calculate the hourly cost between start_hour and end_hour
|
||||
def easy_energy_hourly_cost_between(start_hour, end_hour)
|
||||
begin_hour = start_hour
|
||||
costs = []
|
||||
while(hour_start < day_end) do
|
||||
while(begin_hour < end_hour) do
|
||||
# get usage_kwh/return_kwh for one hour
|
||||
hour_end = hour_start.end_of_hour
|
||||
hour_readings = Reading.where("created_at > :begin AND created_at < :end", {:begin => hour_start, :end => hour_end})
|
||||
begin_hour_plus1 = begin_hour.end_of_hour
|
||||
hour_readings = Reading.where("created_at > :begin AND created_at < :end", {:begin => begin_hour, :end => begin_hour_plus1})
|
||||
hour_diff = hour_readings.last ? hour_readings.last.diff(hour_readings.first) : UNKNOWN_READING
|
||||
usage_kwh = hour_diff[:total_kwh_consumed_high] + hour_diff[:total_kwh_consumed_low]
|
||||
return_kwh = hour_diff[:total_kwh_produced_high] + hour_diff[:total_kwh_produced_low]
|
||||
|
||||
formatted_hour = hour_start.strftime("%F %H")
|
||||
# helper to remove distiction between low/high tarif consumption
|
||||
usage_kwh, return_kwh = Reading.consumed_and_produced_for_diff(hour_diff)
|
||||
|
||||
formatted_hour = begin_hour.strftime("%F %H")
|
||||
costs << easy_energy_cost(formatted_hour, usage_kwh, return_kwh)
|
||||
# do the next hour
|
||||
hour_start = hour_start.advance(:hours => 1)
|
||||
begin_hour = begin_hour.advance(:hours => 1)
|
||||
end
|
||||
# give the result
|
||||
costs
|
||||
end
|
||||
|
||||
def easy_energy_daily_cost_between(start_day, end_day)
|
||||
curr_day = start_day
|
||||
costs = []
|
||||
while (curr_day <= end_day) do
|
||||
hour_start = curr_day.in_time_zone(zone).beginning_of_day
|
||||
hour_end = hour_start.advance(days: 1)
|
||||
|
||||
costs_24hours = easy_energy_hourly_cost_between(hour_start,hour_end)
|
||||
if costs_24hours.any?{ |e| e.nil? }
|
||||
p "Not all Reading data between %s and %s is available!" % [I18n.localize(start_day, format: "%e %B %Y"), I18n.localize(end_day, format: "%e %B %Y")]
|
||||
end
|
||||
# add the sum of 24 hours
|
||||
costs << costs_24hours.compact.sum
|
||||
# do the next day
|
||||
curr_day = curr_day.advance(:days => 1)
|
||||
end
|
||||
# give the result
|
||||
costs
|
||||
end
|
||||
|
||||
def easy_energy_hourly_cost_for(date)
|
||||
hour_start = date.in_time_zone(zone).beginning_of_day
|
||||
day_end = hour_start.advance(days: 1)
|
||||
easy_energy_hourly_cost_between(hour_start, day_end)
|
||||
end
|
||||
|
||||
def easy_energy_cost_barplot(date)
|
||||
# get array with 24 hourly cost values
|
||||
costs = easy_energy_hourly_cost_for(date)
|
||||
|
||||
# create plot
|
||||
hours = (0..23).to_a
|
||||
|
||||
Reference in New Issue
Block a user