From 73062f5a9d8e21a509b2990db6dc5bc2b16718cd Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Mon, 2 Dec 2024 20:00:43 +0000 Subject: [PATCH 01/16] Academic year comparisons on long term usage pages --- .../charts/y_axis_selection_service.rb | 2 +- .../_long_term_trend.html.erb | 29 ----------- .../gas_long_term/_long_term_trend.html.erb | 33 ------------- .../advice/long_term/_analysis.html.erb | 5 +- .../long_term/_long_term_trend.html.erb | 48 +++++++++++++++++++ .../advice/long_term/_recent_trend.html.erb | 20 +++++--- .../advice_pages/electricity_long_term.yml | 2 + .../views/advice_pages/gas_long_term.yml | 2 + ...pper_school_academic_year_comparisons.rake | 12 +++++ 9 files changed, 82 insertions(+), 71 deletions(-) delete mode 100644 app/views/schools/advice/electricity_long_term/_long_term_trend.html.erb delete mode 100644 app/views/schools/advice/gas_long_term/_long_term_trend.html.erb create mode 100644 app/views/schools/advice/long_term/_long_term_trend.html.erb create mode 100644 lib/tasks/deployment/20241202161030_flipper_school_academic_year_comparisons.rake diff --git a/app/services/charts/y_axis_selection_service.rb b/app/services/charts/y_axis_selection_service.rb index ddf5f9b849..9e3ebf86ad 100644 --- a/app/services/charts/y_axis_selection_service.rb +++ b/app/services/charts/y_axis_selection_service.rb @@ -37,7 +37,7 @@ def select_y_axis private def chart_config - @chart_config ||= ChartManager.build_chart_config(ChartManager::STANDARD_CHART_CONFIGURATION[@chart_name]) + @chart_config ||= ChartManager.build_chart_config(ChartManager::STANDARD_CHART_CONFIGURATION.fetch(@chart_name)) end end end diff --git a/app/views/schools/advice/electricity_long_term/_long_term_trend.html.erb b/app/views/schools/advice/electricity_long_term/_long_term_trend.html.erb deleted file mode 100644 index bc348e2576..0000000000 --- a/app/views/schools/advice/electricity_long_term/_long_term_trend.html.erb +++ /dev/null @@ -1,29 +0,0 @@ -<%= render 'schools/advice/section_title', - section_id: 'long-term-trends', - section_title: t('advice_pages.electricity_long_term.analysis.long_term_trends.title') %> - -
<%= t('advice_pages.electricity_long_term.analysis.long_term_trends.summary') %>
- -<% if analysis_dates.months_of_data > 23 %> - <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.subtitle_html') %> - <%= component 'meter_selection_chart', - chart_type: :electricity_longterm_trend, - meter_selection: meter_selection, - chart_subtitle_key: 'advice_pages.electricity_long_term.charts.electricity_longterm_trend.subtitle_html' do |c| %> - <% c.with_title { t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.title') } %> - <% c.with_footer do %> -<%= t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.explanation') %>
- <% end %> - <% end %> -<% end %> - -<%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.group_by_week_electricity_unlimited.subtitle_html') %> -<%= component 'meter_selection_chart', - chart_type: :group_by_week_electricity_unlimited, - meter_selection: meter_selection, - chart_subtitle_key: 'advice_pages.electricity_long_term.charts.group_by_week_electricity_unlimited.subtitle_html' do |c| %> - <% c.with_title { t('advice_pages.electricity_long_term.charts.group_by_week_electricity_unlimited.title') } %> - <% c.with_footer do %> -<%= t('advice_pages.electricity_long_term.charts.group_by_week_electricity_unlimited.explanation') %>
- <% end %> -<% end %> diff --git a/app/views/schools/advice/gas_long_term/_long_term_trend.html.erb b/app/views/schools/advice/gas_long_term/_long_term_trend.html.erb deleted file mode 100644 index cf4fb06df6..0000000000 --- a/app/views/schools/advice/gas_long_term/_long_term_trend.html.erb +++ /dev/null @@ -1,33 +0,0 @@ -<%= render 'schools/advice/section_title', - section_id: 'long-term-trends', - section_title: t('advice_pages.gas_long_term.analysis.long_term_trends.title') %> - -<%= t('advice_pages.gas_long_term.analysis.long_term_trends.summary') %>
- -<% if analysis_dates.months_of_data > 23 %> - <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.subtitle_html') %> - <%= component 'meter_selection_chart', - chart_type: :gas_longterm_trend, - meter_selection: meter_selection, - chart_subtitle_key: 'advice_pages.gas_long_term.charts.gas_longterm_trend.subtitle_html' do |c| %> - <% c.with_title { t('advice_pages.gas_long_term.charts.gas_longterm_trend.title') } %> - <% c.with_footer do %> -<%= t('advice_pages.gas_long_term.charts.gas_longterm_trend.explanation') %>
-<%= t('advice_pages.gas_long_term.charts.gas_longterm_trend.temp_compensation') %>
- <% end %> - <% end %> -<% end %> - -<%# i18n-tasks-use t('advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.subtitle_html') %> -<%= component 'meter_selection_chart', - chart_type: :group_by_week_gas_unlimited, - meter_selection: meter_selection, - chart_subtitle_key: 'advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.subtitle_html' do |c| %> - <% c.with_title { t('advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.title') } %> - <% c.with_header do %> -<%= t('advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.header') %>
- <% end %> - <% c.with_footer do %> -<%= t('advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.explanation') %>
- <% end %> -<% end %> diff --git a/app/views/schools/advice/long_term/_analysis.html.erb b/app/views/schools/advice/long_term/_analysis.html.erb index ebc900ac39..8da42261d7 100644 --- a/app/views/schools/advice/long_term/_analysis.html.erb +++ b/app/views/schools/advice/long_term/_analysis.html.erb @@ -27,7 +27,8 @@ vs_exemplar: vs_exemplar, estimated_savings_vs_exemplar: estimated_savings_vs_exemplar, estimated_savings_vs_benchmark: estimated_savings_vs_benchmark %> - <%= render "schools/advice/#{fuel_type}_long_term/long_term_trend", + <%= render 'schools/advice/long_term/long_term_trend', meter_selection: @meter_selection, - analysis_dates: analysis_dates %> + analysis_dates: analysis_dates, + fuel_type: fuel_type %> <% end %> diff --git a/app/views/schools/advice/long_term/_long_term_trend.html.erb b/app/views/schools/advice/long_term/_long_term_trend.html.erb new file mode 100644 index 0000000000..5f861bc5fb --- /dev/null +++ b/app/views/schools/advice/long_term/_long_term_trend.html.erb @@ -0,0 +1,48 @@ +<%= i18n_prefix = "advice_pages.#{fuel_type}_long_term" + render 'schools/advice/section_title', + section_id: 'long-term-trends', + section_title: t("#{i18n_prefix}.analysis.long_term_trends.title") %> + +<%= t("#{i18n_prefix}.analysis.long_term_trends.summary") %>
+ +<% if analysis_dates.months_of_data > 23 %> + <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.subtitle_html') %> + <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.subtitle_html') %> + <% suffix = :_academic_year if Flipper.enabled?(:school_academic_year_comparisons) %> + <%= :"#{fuel_type}_longterm_trend#{suffix}" %> + <%= component 'meter_selection_chart', + chart_type: :"#{fuel_type}_longterm_trend#{suffix}", + meter_selection: meter_selection, + chart_subtitle_key: "#{i18n_prefix}.charts.#{fuel_type}_longterm_trend.subtitle_html" do |c| %> + <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.title') %> + <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.title_academic_year') %> + <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.title') %> + <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.title_academic_year') %> + <% c.with_title { t("advice_pages.#{fuel_type}_long_term.charts.#{fuel_type}_longterm_trend.title#{suffix}") } %> + <% c.with_footer do %> + <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.explanation') %> + <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.explanation') %> +<%= t("advice_pages.#{fuel_type}_long_term.charts.#{fuel_type}_longterm_trend.explanation") %>
+ <% if fuel_type == :gas %> +<%= t('advice_pages.gas_long_term.charts.gas_longterm_trend.temp_compensation') %>
+ <% end %> + <% end %> + <% end %> +<% end %> + +<%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.group_by_week_electricity_unlimited.subtitle_html') %> +<%# i18n-tasks-use t('advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.subtitle_html') %> +<%= component 'meter_selection_chart', + chart_type: :"group_by_week_#{fuel_type}_unlimited", + meter_selection: meter_selection, + chart_subtitle_key: "#{i18n_prefix}.charts.group_by_week_#{fuel_type}_unlimited.subtitle_html" do |c| %> + <% c.with_title { t("#{i18n_prefix}.charts.group_by_week_#{fuel_type}_unlimited.title") } %> + <% c.with_header do %> +<%= t('advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.header') %>
+ <% end if fuel_type == :gas %> + <% c.with_footer do %> + <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.group_by_week_electricity_unlimited.explanation') %> + <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.group_by_week_gas_unlimited.explanation') %> +<%= t("#{i18n_prefix}.charts.group_by_week_#{fuel_type}_unlimited.explanation") %>
+ <% end %> +<% end %> diff --git a/app/views/schools/advice/long_term/_recent_trend.html.erb b/app/views/schools/advice/long_term/_recent_trend.html.erb index 2d0c81be8e..4667e42639 100644 --- a/app/views/schools/advice/long_term/_recent_trend.html.erb +++ b/app/views/schools/advice/long_term/_recent_trend.html.erb @@ -25,7 +25,8 @@ <%= component 'meter_selection_chart', chart_type: :"group_by_week_#{fuel_type}", meter_selection: meter_selection, - chart_subtitle_key: "advice_pages.#{fuel_type}_long_term.charts.group_by_week_#{fuel_type}.subtitle_html" do |c| %> + chart_subtitle_key: + "advice_pages.#{fuel_type}_long_term.charts.group_by_week_#{fuel_type}.subtitle_html" do |c| %> <% c.with_title { t("advice_pages.#{fuel_type}_long_term.charts.group_by_week_#{fuel_type}.title") } %> <% c.with_footer do %><%= t("advice_pages.#{fuel_type}_long_term.charts.group_by_week_#{fuel_type}.explanation_html") %>
@@ -38,7 +39,8 @@ <%= component 'meter_selection_chart', chart_type: chart_type, meter_selection: meter_selection, - chart_subtitle_key: "advice_pages.#{fuel_type}_long_term.charts.group_by_week_#{fuel_type}.subtitle_html", + chart_subtitle_key: + "advice_pages.#{fuel_type}_long_term.charts.group_by_week_#{fuel_type}.subtitle_html", chart_config: { y_axis_units: select_y_axis(school, chart_type, :£) } do |c| %> <% c.with_title { t("advice_pages.#{fuel_type}_out_of_hours.analysis.holiday_usage.#{chart_type}.title") } %> <% c.with_footer do %> @@ -51,17 +53,23 @@ <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_by_month_year.subtitle_html') %> <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_by_month_year.subtitle_two_years_html') %> <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_by_month_year.subtitle_html') %> -<% key_prefix = "advice_pages.#{fuel_type}_long_term.charts.#{fuel_type}_by_month_year" %> -<% key_suffix = analysis_dates.one_years_data? ? '_two_years' : '' %> +<% key_prefix = "advice_pages.#{fuel_type}_long_term.charts.#{fuel_type}_by_month_year" + key_suffix = '_two_years' if analysis_dates.one_years_data? + chart_type = Flipper.enabled?(:school_academic_year_comparisons) ? :acyear : :year %> <%= component 'meter_selection_chart', - chart_type: :"#{fuel_type}_by_month_year_0_1", + chart_type: :"#{fuel_type}_by_month_#{chart_type}_0_1", meter_selection: meter_selection, chart_subtitle_key: "#{key_prefix}.subtitle#{key_suffix}_html" do |c| %> <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_by_month_year.title_two_years') %> + <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_by_month_year.title_two_years_academic_year') %> <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_by_month_year.title') %> <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_by_month_year.title_two_years') %> + <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_by_month_year.title_two_years_academic_year') %> <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_by_month_year.title') %> - <% c.with_title { t("#{key_prefix}.title#{key_suffix}", fuel_type: fuel_type) } %> + <% if analysis_dates.one_years_data? && Flipper.enabled?(:school_academic_year_comparisons) + key_suffix += '_academic_year' + end + c.with_title { t("#{key_prefix}.title#{key_suffix}", fuel_type: fuel_type) } %> <% c.with_footer do %><%= t("advice_pages.#{fuel_type}_long_term.charts.#{fuel_type}_by_month_year.explanation") %>
<% end %> diff --git a/config/locales/views/advice_pages/electricity_long_term.yml b/config/locales/views/advice_pages/electricity_long_term.yml index ff4dea20b8..127e25df91 100644 --- a/config/locales/views/advice_pages/electricity_long_term.yml +++ b/config/locales/views/advice_pages/electricity_long_term.yml @@ -37,10 +37,12 @@ en: subtitle_two_years_html: This chart shows the difference in electricity consumption for the 12 months ending on %{end_date} and the previous 12 months for %{meter} title: Monthly electricity consumption comparison title_two_years: Monthly electricity consumption comparison over the last two years + title_two_years_academic_year: Monthly electricity consumption comparison over the last few academic years electricity_longterm_trend: explanation: Unless the school has had additional buildings added, if you are managing your electricity consumption well this should show a downward trend; more modern ICT equipment, LED lighting and behaviour change all contribute to reducing electricity usage. subtitle_html: This chart gives the breakdown of electricity consumption between school day open and closed, holidays and weekends for each full year of data we have for %{meter} title: Change in electricity consumption over the last few years + title_academic_year: Change in electricity consumption over the last few academic years group_by_week_electricity: explanation_html: |-diff --git a/config/locales/views/advice_pages/gas_long_term.yml b/config/locales/views/advice_pages/gas_long_term.yml index 8a852dd9d0..a0b3b2e961 100644 --- a/config/locales/views/advice_pages/gas_long_term.yml +++ b/config/locales/views/advice_pages/gas_long_term.yml @@ -38,11 +38,13 @@ en: subtitle_two_years_html: This chart shows the difference in gas consumption for the 12 months ending on %{end_date} and the previous 12 months for %{meter} title: Monthly gas consumption comparison title_two_years: Monthly gas consumption comparison over the last two years + title_two_years_academic_year: Monthly gas consumption comparison over the last few academic years gas_longterm_trend: explanation: Reducing gas consumption can be achieved by turning your school's thermostat down, reducing out of hours usage, insulating your buildings or installing a new more efficient gas boiler. subtitle_html: This chart, and the table below give the breakdown of gas consumption between school day open and closed, holidays and weekends for each full year of data we have for %{meter} temp_compensation: There is no temperature compensation applied to this chart. You should expect the amount of gas used each year to vary and broadly follow degree days - the colder it is outside, the more gas should be used in the school. title: Change in gas consumption over the last few years + title_academic_year: Change in gas consumption over the last few academic years group_by_week_gas: explanation_html: |-
diff --git a/lib/tasks/deployment/20241202161030_flipper_school_academic_year_comparisons.rake b/lib/tasks/deployment/20241202161030_flipper_school_academic_year_comparisons.rake new file mode 100644 index 0000000000..3e9171dc4f --- /dev/null +++ b/lib/tasks/deployment/20241202161030_flipper_school_academic_year_comparisons.rake @@ -0,0 +1,12 @@ +namespace :after_party do + desc 'Deployment task: flipper_school_academic_year_comparisons' + task flipper_school_academic_year_comparisons: :environment do + puts "Running deploy task 'flipper_school_academic_year_comparisons'" + + Flipper.add(:school_academic_year_comparisons) + + # Update task as completed. If you remove the line below, the task will + # run with every deploy (or every time you call after_party:run). + AfterParty::TaskRecord.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp + end +end From c14a2e4e61ca486de46f85785c25c6ca36f1e34b Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Mon, 2 Dec 2024 20:02:02 +0000 Subject: [PATCH 02/16] more --- Gemfile | 2 +- Gemfile.lock | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index ebbb66916c..0cac36162e 100644 --- a/Gemfile +++ b/Gemfile @@ -27,7 +27,7 @@ gem 'pg' gem 'scenic' # Dashboard analytics -gem 'energy-sparks_analytics', github: 'Energy-Sparks/energy-sparks_analytics', tag: '6.1.2' +gem 'energy-sparks_analytics', github: 'Energy-Sparks/energy-sparks_analytics', branch: '4619-academic-year-comparisons-on-long-term-usage-pages' # gem 'energy-sparks_analytics', path: '../energy-sparks_analytics' # Using master due to it having a patch which doesn't override Enumerable#sum if it's already defined diff --git a/Gemfile.lock b/Gemfile.lock index 6fde7551d7..1f07f94178 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT remote: https://github.com/Energy-Sparks/energy-sparks_analytics.git - revision: 94d5b0ef97a6130b13224ce3f5c31405dfcf6562 - tag: 6.1.2 + revision: c9e1610f4fea4667a0f5d5e738f3f0a57f328b39 + branch: 4619-academic-year-comparisons-on-long-term-usage-pages specs: energy-sparks_analytics (1.2.1) activesupport (>= 6.0, < 8.1) From 9126334a0e857085f1c178dcfa448dc1290e1c07 Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Thu, 5 Dec 2024 11:39:15 +0000 Subject: [PATCH 03/16] more --- Gemfile.lock | 2 +- app/services/alerts/adapters/analytics_adapter.rb | 2 -- app/views/schools/advice/long_term/_long_term_trend.html.erb | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 1f07f94178..5b89e8bfa8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/Energy-Sparks/energy-sparks_analytics.git - revision: c9e1610f4fea4667a0f5d5e738f3f0a57f328b39 + revision: 122ee7161a004613d82c40513b497272285688c8 branch: 4619-academic-year-comparisons-on-long-term-usage-pages specs: energy-sparks_analytics (1.2.1) diff --git a/app/services/alerts/adapters/analytics_adapter.rb b/app/services/alerts/adapters/analytics_adapter.rb index 336c050bf8..62cb6785df 100644 --- a/app/services/alerts/adapters/analytics_adapter.rb +++ b/app/services/alerts/adapters/analytics_adapter.rb @@ -6,12 +6,10 @@ module Alerts module Adapters class AnalyticsAdapter < Adapter def report(alert_configuration: nil) - # debugger analysis_object = alert_class.new(@aggregate_school) if analysis_object.respond_to?(:comparison_configuration=) analysis_object.comparison_configuration = alert_configuration end - # debugger if alert_class == AlertImpendingHoliday if analysis_object.valid_alert? benchmark = benchmark_variables?(alert_class) analysis_object.analyse(*[@analysis_date, diff --git a/app/views/schools/advice/long_term/_long_term_trend.html.erb b/app/views/schools/advice/long_term/_long_term_trend.html.erb index 5f861bc5fb..6d65724816 100644 --- a/app/views/schools/advice/long_term/_long_term_trend.html.erb +++ b/app/views/schools/advice/long_term/_long_term_trend.html.erb @@ -9,7 +9,6 @@ <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.subtitle_html') %> <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.subtitle_html') %> <% suffix = :_academic_year if Flipper.enabled?(:school_academic_year_comparisons) %> - <%= :"#{fuel_type}_longterm_trend#{suffix}" %> <%= component 'meter_selection_chart', chart_type: :"#{fuel_type}_longterm_trend#{suffix}", meter_selection: meter_selection, From 8ad55e69ae507effb1dfbe791609bad9106d623a Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:29:36 +0000 Subject: [PATCH 04/16] more --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 5b89e8bfa8..3ce7ab99a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/Energy-Sparks/energy-sparks_analytics.git - revision: 122ee7161a004613d82c40513b497272285688c8 + revision: 8cd9bd58b4c8470278d3f9822e75be464c69e410 branch: 4619-academic-year-comparisons-on-long-term-usage-pages specs: energy-sparks_analytics (1.2.1) From 98655b7ec1ddc19d750731876b5e6bae64a5ed74 Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:55:28 +0000 Subject: [PATCH 05/16] more --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3ce7ab99a5..bacc4f041b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/Energy-Sparks/energy-sparks_analytics.git - revision: 8cd9bd58b4c8470278d3f9822e75be464c69e410 + revision: c670751f9547d2fa99636ae4111f22d06635d6d4 branch: 4619-academic-year-comparisons-on-long-term-usage-pages specs: energy-sparks_analytics (1.2.1) From 2cf40dfeb5bede4b89c000cec640c19c3fe4cc9f Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:58:43 +0000 Subject: [PATCH 06/16] more --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index bacc4f041b..190fae1c6b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/Energy-Sparks/energy-sparks_analytics.git - revision: c670751f9547d2fa99636ae4111f22d06635d6d4 + revision: e801819410ed6971f70fa8c2417eb167ec81b21a branch: 4619-academic-year-comparisons-on-long-term-usage-pages specs: energy-sparks_analytics (1.2.1) From c3675f7cdbfb23af647dc56a0e3a668fab1e6adc Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:47:49 +0000 Subject: [PATCH 07/16] more --- app/views/schools/advice/long_term/_long_term_trend.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/schools/advice/long_term/_long_term_trend.html.erb b/app/views/schools/advice/long_term/_long_term_trend.html.erb index 6d65724816..cd26025624 100644 --- a/app/views/schools/advice/long_term/_long_term_trend.html.erb +++ b/app/views/schools/advice/long_term/_long_term_trend.html.erb @@ -8,7 +8,7 @@ <% if analysis_dates.months_of_data > 23 %> <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.subtitle_html') %> <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.subtitle_html') %> - <% suffix = :_academic_year if Flipper.enabled?(:school_academic_year_comparisons) %> + <% suffix = :_academic_year if Flipper.enabled?(:school_academic_year_comparisons, user) %> <%= component 'meter_selection_chart', chart_type: :"#{fuel_type}_longterm_trend#{suffix}", meter_selection: meter_selection, From 6e0c99aec71e71c43710deeaac0b89017c7effb0 Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Mon, 16 Dec 2024 16:52:38 +0000 Subject: [PATCH 08/16] more --- app/views/schools/advice/long_term/_long_term_trend.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/schools/advice/long_term/_long_term_trend.html.erb b/app/views/schools/advice/long_term/_long_term_trend.html.erb index cd26025624..e235198499 100644 --- a/app/views/schools/advice/long_term/_long_term_trend.html.erb +++ b/app/views/schools/advice/long_term/_long_term_trend.html.erb @@ -8,7 +8,7 @@ <% if analysis_dates.months_of_data > 23 %> <%# i18n-tasks-use t('advice_pages.electricity_long_term.charts.electricity_longterm_trend.subtitle_html') %> <%# i18n-tasks-use t('advice_pages.gas_long_term.charts.gas_longterm_trend.subtitle_html') %> - <% suffix = :_academic_year if Flipper.enabled?(:school_academic_year_comparisons, user) %> + <% suffix = :_academic_year if Flipper.enabled?(:school_academic_year_comparisons, current_user) %> <%= component 'meter_selection_chart', chart_type: :"#{fuel_type}_longterm_trend#{suffix}", meter_selection: meter_selection, From 4efada0f69851a1a546cb0043b4688795d387a7e Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:32:13 +0000 Subject: [PATCH 09/16] more --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index e459b66e56..a655d7a0a2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,6 +1,6 @@ GIT remote: https://github.com/Energy-Sparks/energy-sparks_analytics.git - revision: e801819410ed6971f70fa8c2417eb167ec81b21a + revision: 410575e63c37a6b42a8fc6b2d6653ea460175a1f branch: 4619-academic-year-comparisons-on-long-term-usage-pages specs: energy-sparks_analytics (1.2.1) From 6928712e4bc687fb3ff9d3b32aac7359f1bf67df Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Wed, 18 Dec 2024 15:34:58 +0000 Subject: [PATCH 10/16] make baseload advice longterm table use academic years --- .../schools/advice/baseload_service.rb | 63 +++++++++++++------ .../baseload/_long_term_trends_table.html.erb | 11 ++-- 2 files changed, 50 insertions(+), 24 deletions(-) diff --git a/app/services/schools/advice/baseload_service.rb b/app/services/schools/advice/baseload_service.rb index 291debdd21..88737e53ae 100644 --- a/app/services/schools/advice/baseload_service.rb +++ b/app/services/schools/advice/baseload_service.rb @@ -4,13 +4,9 @@ module Advice class BaseloadService < BaseService include AnalysableMixin - def enough_data? - baseload_service.enough_data? - end + delegate :enough_data?, to: :baseload_service - def data_available_from - baseload_service.data_available_from - end + delegate :data_available_from, to: :baseload_service def has_electricity? @school.has_electricity? @@ -38,9 +34,7 @@ def previous_period_average_baseload_kw(period: :year) end end - def saving_through_1_kw_reduction_in_baseload - baseload_service.saving_through_1_kw_reduction_in_baseload - end + delegate :saving_through_1_kw_reduction_in_baseload, to: :baseload_service def annual_baseload_usage @annual_baseload_usage ||= baseload_service.annual_baseload_usage(include_percentage: true) @@ -62,15 +56,32 @@ def estimated_savings(versus: :benchmark_school) def annual_average_baseloads start_date = aggregate_meter.amr_data.start_date end_date = aggregate_meter.amr_data.end_date - (start_date.year..end_date.year).map do |year| - end_of_year = Date.new(year).end_of_year - baseload_service = Baseload::BaseloadCalculationService.new(aggregate_meter, end_of_year) - { - year: year, - baseload: baseload_service.average_baseload_kw(period: :year), - baseload_usage: baseload_service.annual_baseload_usage - } + baseload_service = Baseload::BaseloadCalculationService.new(aggregate_meter, nil) + baseload_analysis = baseload_service.baseload_analysis + Periods::FixedAcademicYear.enumerator(start_date, end_date).map do |period_start, period_end| + scale_to_year = baseload_analysis.scale_to_year(period_start, period_end) + average_baseload_kw = baseload_analysis.average_baseload_kw(period_start, period_end) + { year: academic_year(period_start, period_end), + baseload: average_baseload_kw, + baseload_usage_gbp: + baseload_analysis.baseload_economic_cost_date_range_£(period_start, period_end, :£) * scale_to_year, + baseload_usage_co2: average_baseload_kw * 365 * 24 * baseload_service.co2_per_kwh } end + # (start_date.year..end_date.year).map do |year| + # end_of_year = Date.new(year).end_of_year + # baseload_service = Baseload::BaseloadCalculationService.new(aggregate_meter, end_of_year) + # { + # year: year, + # baseload: baseload_service.average_baseload_kw(period: :year), + # baseload_usage: baseload_service.annual_baseload_usage + # } + # end + end + + def academic_year(start_date, end_date) + start_year = start_date.month < 9 ? start_date.year - 1 : start_date.year + end_year = end_date.month < 9 ? end_date.year : end_date.year + 1 + "#{start_year}/#{end_year}" end def baseload_meter_breakdown @@ -78,7 +89,8 @@ def baseload_meter_breakdown baseloads = meter_breakdown_service.calculate_breakdown meter_breakdowns = {} baseloads.meters.each do |mpan_mprn| - baseload_service = Baseload::BaseloadCalculationService.new(@meter_collection.meter?(mpan_mprn), end_of_previous_year) + baseload_service = Baseload::BaseloadCalculationService.new(@meter_collection.meter?(mpan_mprn), + end_of_previous_year) previous_year_baseload = baseload_service.enough_data? ? baseload_service.average_baseload_kw(period: :year) : nil meter_breakdowns[mpan_mprn] = build_meter_breakdown(mpan_mprn, baseloads, previous_year_baseload) end @@ -105,6 +117,7 @@ def seasonal_variation def seasonal_variation_by_meter return {} unless electricity_meters.count > 1 + electricity_meters.each_with_object({}) do |meter, variation_by_meter| variation_by_meter[meter.mpan_mprn] = calculate_seasonal_variation(meter, meter.amr_data.end_date, true) end @@ -116,6 +129,7 @@ def intraweek_variation def intraweek_variation_by_meter return {} unless electricity_meters.count > 1 + electricity_meters.each_with_object({}) do |meter, variation_by_meter| variation_by_meter[meter.mpan_mprn] = calculate_intraweek_variation(meter, meter.amr_data.end_date, true) end @@ -186,11 +200,16 @@ def calculate_seasonal_variation(analytics_meter = aggregate_meter, date = asof_ meter = load_meter ? meter_for_mpan(analytics_meter.mpan_mprn) : nil seasonal_baseload_service = Baseload::SeasonalBaseloadService.new(analytics_meter, date) # return if there's not enough data, then return limited object - return OpenStruct.new(meter: meter, enough_data?: false, data_available_from: seasonal_baseload_service.data_available_from) unless enough_data_for_meter?(analytics_meter) + unless enough_data_for_meter?(analytics_meter) + return OpenStruct.new(meter: meter, enough_data?: false, + data_available_from: seasonal_baseload_service.data_available_from) + end + variation = seasonal_baseload_service.seasonal_variation # we may have >1 year of data, but not enough to actually calculate a seasonal analysis # e.g. a meter for a swimming pool only used in the summer return OpenStruct.new(meter: meter, enough_data?: false) if variation.percentage.nan? + saving = seasonal_baseload_service.estimated_costs build_seasonal_variation(meter, variation, saving) end @@ -215,7 +234,11 @@ def seasonal_variation_rating(percentage) def calculate_intraweek_variation(analytics_meter = aggregate_meter, date = asof_date, load_meter = false) intraweek_baseload_service = Baseload::IntraweekBaseloadService.new(analytics_meter, date) meter = load_meter ? meter_for_mpan(analytics_meter.mpan_mprn) : nil - return OpenStruct.new(meter: meter, enough_data?: false, data_available_from: intraweek_baseload_service.data_available_from) unless enough_data_for_meter?(analytics_meter) + unless enough_data_for_meter?(analytics_meter) + return OpenStruct.new(meter: meter, enough_data?: false, + data_available_from: intraweek_baseload_service.data_available_from) + end + variation = intraweek_baseload_service.intraweek_variation saving = intraweek_baseload_service.estimated_costs build_intraweek_variation(meter, variation, saving) diff --git a/app/views/schools/advice/baseload/_long_term_trends_table.html.erb b/app/views/schools/advice/baseload/_long_term_trends_table.html.erb index eaf1821a70..e78ff11c15 100644 --- a/app/views/schools/advice/baseload/_long_term_trends_table.html.erb +++ b/app/views/schools/advice/baseload/_long_term_trends_table.html.erb @@ -15,13 +15,16 @@ <%= partial_year_note(annual_average_baseload[:year], analysis_dates.start_date, analysis_dates.end_date) %>