From 5d86e65d8daa46445727f1c1726e0e0683bd025c Mon Sep 17 00:00:00 2001 From: Aart van Halteren Date: Thu, 22 Sep 2022 19:20:07 +0200 Subject: [PATCH] Adds actual cost barplot --- app/models/cost.rb | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/app/models/cost.rb b/app/models/cost.rb index 41d616d..25f5eef 100644 --- a/app/models/cost.rb +++ b/app/models/cost.rb @@ -82,15 +82,44 @@ class Cost result end - def easy_energy_barplot(date) + def easy_energy_tariff_barplot(date) hours = (0..23).to_a costs = easy_energy_hours(date) GR.barplot(hours,costs) # make sure you have set GKS_WSTYPE=100 in the environment (for headless setup) - title = "Kosten per kwH (incl. belastingen en BTW) - %s" % date.strftime("%A, %e %B %Y") + title = "Tarief per kwH (incl. belastingen en BTW) - %s" % date.strftime("%A, %e %B %Y") xlabel = "uur" ylabel = "EUR" - GR.savefig("easy_%s.png" % date.strftime("%F"), title: title, xlabel: xlabel, ylabel: ylabel) + GR.savefig("easy_tariff_%s.png" % date.strftime("%F"), title: title, xlabel: xlabel, ylabel: ylabel) + end + + def easy_energy_cost_barplot(date) + hour_start = date.in_time_zone(zone).beginning_of_day + day_end = hour_start.advance(days: 1) + costs = [] + while(hour_start < day_end) 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}) + 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") + costs << easy_energy_cost(formatted_hour, usage_kwh, return_kwh) + # do the next hour + hour_start = hour_start.advance(:hours => 1) + end + + # create plot + hours = (0..23).to_a + GR.barplot(hours,costs) + # make sure you have set GKS_WSTYPE=100 in the environment (for headless setup) + title = "Verbruikskosten (incl. belastingen en BTW) - %s" % date.strftime("%A, %e %B %Y") + xlabel = "uur" + ylabel = "EUR" + GR.savefig("easy_cost_%s.png" % date.strftime("%F"), title: title, xlabel: xlabel, ylabel: ylabel) + end