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) %> <%= format_unit(annual_average_baseload[:baseload], :kw) %> - <%= format_unit(annual_average_baseload[:baseload_usage].£, :£) %> - <%= format_unit(annual_average_baseload[:baseload_usage].co2, :co2) %> + <%= format_unit(annual_average_baseload[:baseload_usage_gbp], :£) %> + <%= format_unit(annual_average_baseload[:baseload_usage_co2], :co2) %> <% end %>

- <%= t('advice_pages.baseload.current_baseload.calculation_dates', start_date: I18n.l(analysis_dates.start_date, format: '%-d %B %Y'), end_date: I18n.l(analysis_dates.end_date, format: '%-d %B %Y')) %> - <%= render 'schools/advice/how_have_we_analysed_your_data_table_caption', data_target: "how-have-we-analysed-your-data-footnotes" %> + <%= t('advice_pages.baseload.current_baseload.calculation_dates', + start_date: I18n.l(analysis_dates.start_date, format: '%-d %B %Y'), + end_date: I18n.l(analysis_dates.end_date, format: '%-d %B %Y')) %> + <%= render 'schools/advice/how_have_we_analysed_your_data_table_caption', + data_target: 'how-have-we-analysed-your-data-footnotes' %>
From 0479aeee2e87fc013cbf492b56bb129394186d2a Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Thu, 19 Dec 2024 09:32:35 +0000 Subject: [PATCH 11/16] more --- config/locales/views/advice_pages/baseload.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/views/advice_pages/baseload.yml b/config/locales/views/advice_pages/baseload.yml index be42eea0e4..cf5df73fa6 100644 --- a/config/locales/views/advice_pages/baseload.yml +++ b/config/locales/views/advice_pages/baseload.yml @@ -93,7 +93,7 @@ en: percentage_of_total: "% of total baseload" potential_saving_co2: CO2 reduction (kg) potential_saving_gbp: Potential saving (£) - year: Year + year: Academic Year labels: all_meters: All meters what_is_baseload_html: |- From 2ae86eee0bed68206f461680eb1b6edd5550532e Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Thu, 19 Dec 2024 12:01:20 +0000 Subject: [PATCH 12/16] more --- Gemfile | 2 +- Gemfile.lock | 4 ++-- app/services/schools/advice/baseload_service.rb | 12 ++---------- .../advice/baseload/_long_term_trends_table.html.erb | 2 +- 4 files changed, 6 insertions(+), 14 deletions(-) diff --git a/Gemfile b/Gemfile index f04605418c..cce410e48a 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', branch: '4619-academic-year-comparisons-on-long-term-usage-pages' +gem 'energy-sparks_analytics', github: 'Energy-Sparks/energy-sparks_analytics', branch: '4691-academic-year-comparisons-for-baseload' # 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 a655d7a0a2..8caaaf7729 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ GIT remote: https://github.com/Energy-Sparks/energy-sparks_analytics.git - revision: 410575e63c37a6b42a8fc6b2d6653ea460175a1f - branch: 4619-academic-year-comparisons-on-long-term-usage-pages + revision: 94a6734b221d49934aefbe37c9891245ddaff2dc + branch: 4691-academic-year-comparisons-for-baseload specs: energy-sparks_analytics (1.2.1) activesupport (>= 6.0, < 8.1) diff --git a/app/services/schools/advice/baseload_service.rb b/app/services/schools/advice/baseload_service.rb index 88737e53ae..4e4a97a748 100644 --- a/app/services/schools/advice/baseload_service.rb +++ b/app/services/schools/advice/baseload_service.rb @@ -59,23 +59,15 @@ def annual_average_baseloads 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) + scale_to_year = Baseload::BaseloadAnalysis.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), + partial: [period_start.month, period_start.day] != [9, 1] || [period_end.month, period_end.day] != [8, 31], 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) 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 e78ff11c15..2148cec0fa 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 @@ -12,7 +12,7 @@ <%= annual_average_baseload[:year] %> - <%= partial_year_note(annual_average_baseload[:year], analysis_dates.start_date, analysis_dates.end_date) %> + <%= I18n.t('advice_pages.tables.labels.partial') if annual_average_baseload[:partial] %> <%= format_unit(annual_average_baseload[:baseload], :kw) %> <%= format_unit(annual_average_baseload[:baseload_usage_gbp], :£) %> From 1713153103fa71294747d101f76e2714b2dcb8df Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Thu, 19 Dec 2024 13:11:49 +0000 Subject: [PATCH 13/16] more --- .../schools/advice/baseload_service_spec.rb | 131 ++++++++++++------ 1 file changed, 87 insertions(+), 44 deletions(-) diff --git a/spec/services/schools/advice/baseload_service_spec.rb b/spec/services/schools/advice/baseload_service_spec.rb index b403ed4cdc..6563ce028a 100644 --- a/spec/services/schools/advice/baseload_service_spec.rb +++ b/spec/services/schools/advice/baseload_service_spec.rb @@ -1,32 +1,38 @@ +# frozen_string_literal: true + require 'rails_helper' RSpec.describe Schools::Advice::BaseloadService, type: :service do + subject(:service) { described_class.new(school, meter_collection) } + let(:school) { create(:school) } - let!(:fuel_configuration) { Schools::FuelConfiguration.new(has_electricity: true, has_gas: true, has_storage_heaters: true)} + let!(:fuel_configuration) do + Schools::FuelConfiguration.new(has_electricity: true, has_gas: true, has_storage_heaters: true) + end let(:electricity_meters) { ['electricity-meter'] } - let(:electricity_aggregate_meter) { double('electricity-aggregated-meter', aggregate_meter?: true)} - - let(:meter_collection) { double(:meter_collection, electricity_meters: electricity_meters, aggregated_electricity_meters: electricity_aggregate_meter) } + let(:electricity_aggregate_meter) { double('electricity-aggregated-meter', aggregate_meter?: true) } + + let(:meter_collection) do + meter_collection_double = instance_double(MeterCollection, + electricity_meters:, + aggregated_electricity_meters: electricity_aggregate_meter) + allow(amr_data).to receive_messages(start_date:, end_date:) + allow(electricity_aggregate_meter).to receive_messages(fuel_type: :electricity, amr_data:) + meter_collection_double + end let(:amr_data) { double('amr-data') } - let(:start_date) { Date.parse('20190101')} - let(:end_date) { Date.parse('20210101')} + let(:start_date) { Date.parse('20190101') } + let(:end_date) { Date.parse('20210101') } let(:usage) { CombinedUsageMetric.new(£: 0, kwh: 0, co2: 0) } let(:average_baseload_kw) { 2.1 } let(:exemplar_average_baseload_kw) { 1.9 } let(:savings) { double(£: 1, co2: 2) } - let(:service) { Schools::Advice::BaseloadService.new(school, meter_collection) } - before do school.configuration.update!(fuel_configuration: fuel_configuration) - allow(amr_data).to receive(:start_date).and_return(start_date) - allow(amr_data).to receive(:end_date).and_return(end_date) - allow(electricity_aggregate_meter).to receive(:fuel_type).and_return(:electricity) - allow(electricity_aggregate_meter).to receive(:amr_data).and_return(amr_data) - allow(meter_collection).to receive(:aggregated_electricity_meters).and_return(electricity_aggregate_meter) end describe '#has_electricity?' do @@ -118,41 +124,55 @@ end describe '#annual_average_baseloads' do - let(:start_date) { Date.parse('20190101')} - let(:end_date) { Date.parse('20210101')} - - before do - allow_any_instance_of(Baseload::BaseloadCalculationService).to receive(:annual_baseload_usage).and_return(usage) - allow_any_instance_of(Baseload::BaseloadCalculationService).to receive(:average_baseload_kw).and_return(average_baseload_kw) + let(:start_date) { Date.parse('20190101') } + let(:end_date) { Date.parse('20210101') } + let(:meter_collection) do + grid_carbon_intensity = build(:grid_carbon_intensity, :with_days, start_date:, end_date:, + kwh_data_x48: Array.new(48, 0.2)) + build(:meter_collection, :with_fuel_and_aggregate_meters, start_date:, end_date:, grid_carbon_intensity:) + end + + def round_floats(array) + array.each do |hash| + hash.each do |key, value| + hash[key] = value.round if value.is_a?(Float) + end + end end it 'returns usage by years' do result = service.annual_average_baseloads - expect(result.count).to eq(3) - expect(result[0][:year]).to eq(2019) - expect(result[0][:baseload_usage]).to eq(usage) - expect(result[2][:year]).to eq(2021) - expect(result[2][:baseload_usage]).to eq(usage) + expect(round_floats(result)).to eq( + round_floats( + [ + { year: '2020/2021', partial: true, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 }, + { year: '2019/2020', partial: false, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 }, + { year: '2018/2019', partial: true, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 } + ] + ) + ) end end describe '#baseload_meter_breakdown' do - let(:meter_1) { 1591058886735 } + let(:meter_1) { 1_591_058_886_735 } let(:data) { { meter_1 => { kw: 0, percent: 0, £: 0 } } } let(:breakdown) { Baseload::MeterBaseloadBreakdown.new(meter_breakdown: data) } - let!(:db_meter) { create(:electricity_meter, school: school, mpan_mprn: 1591058886735) } + let!(:db_meter) { create(:electricity_meter, school: school, mpan_mprn: 1_591_058_886_735) } before do allow_any_instance_of(Baseload::BaseloadMeterBreakdownService).to receive(:calculate_breakdown).and_return(breakdown) allow_any_instance_of(Baseload::BaseloadCalculationService).to receive(:annual_baseload_usage).and_return(usage) allow_any_instance_of(Baseload::BaseloadCalculationService).to receive(:average_baseload_kw).and_return(average_baseload_kw) - allow(meter_collection).to receive(:meter?).and_return(double('meter', fuel_type: :electricity, amr_data: amr_data)) + allow(meter_collection).to receive(:meter?).and_return(double('meter', fuel_type: :electricity, + amr_data: amr_data)) end it 'returns usage by years' do result = service.baseload_meter_breakdown - expect(result.keys).to match_array([meter_1]) - expect(result[meter_1].to_h.keys).to match_array([:baseload_kw, :baseload_change_kw, :baseload_cost_£, :percentage_baseload, :baseload_previous_year_kw, :meter]) + expect(result.keys).to contain_exactly(meter_1) + expect(result[meter_1].to_h.keys).to match_array(%i[baseload_kw baseload_change_kw baseload_cost_£ + percentage_baseload baseload_previous_year_kw meter]) expect(result[meter_1].meter).to eq(db_meter) expect(result[meter_1].baseload_previous_year_kw).to eq(average_baseload_kw) end @@ -184,14 +204,21 @@ it 'returns variation' do result = service.seasonal_variation - expect(result.to_h.keys).to match_array([:estimated_saving_co2, :estimated_saving_£, :percentage, :summer_kw, :variation_rating, :winter_kw, :meter, :enough_data?]) + expect(result.to_h.keys).to match_array(%i[estimated_saving_co2 estimated_saving_£ percentage summer_kw + variation_rating winter_kw meter enough_data?]) end end describe '#seasonal_variation_by_meter' do let(:seasonal_variation) { double(winter_kw: 1, summer_kw: 2, percentage: 3.0) } - let(:electricity_meter_1) { double(mpan_mprn: 'meter1', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, aggregate_meter?: false) } - let(:electricity_meter_2) { double(mpan_mprn: 'meter2', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, aggregate_meter?: false) } + let(:electricity_meter_1) do + double(mpan_mprn: 'meter1', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, + aggregate_meter?: false) + end + let(:electricity_meter_2) do + double(mpan_mprn: 'meter2', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, + aggregate_meter?: false) + end let(:electricity_meters) { [electricity_meter_1, electricity_meter_2] } let(:enough_data) { true } let(:data_available_from) { nil } @@ -206,7 +233,8 @@ it 'returns variation' do result = service.seasonal_variation_by_meter expect(result.keys).to match_array(%w[meter1 meter2]) - expect(result['meter1'].to_h.keys).to match_array([:estimated_saving_co2, :estimated_saving_£, :percentage, :summer_kw, :variation_rating, :winter_kw, :meter, :enough_data?]) + expect(result['meter1'].to_h.keys).to match_array(%i[estimated_saving_co2 estimated_saving_£ percentage + summer_kw variation_rating winter_kw meter enough_data?]) end context 'and theres not enough data' do @@ -216,12 +244,15 @@ it 'returns a limited variation' do result = service.seasonal_variation_by_meter expect(result.keys).to match_array(%w[meter1 meter2]) - expect(result['meter1'].to_h.keys).to match_array([:meter, :enough_data?, :data_available_from]) + expect(result['meter1'].to_h.keys).to match_array(%i[meter enough_data? data_available_from]) end end context 'when there is a solar meter that hasnt been configured' do - let(:solar_meter) { double(mpan_mprn: 'solar_meter', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :solar_pv, aggregate_meter?: false) } + let(:solar_meter) do + double(mpan_mprn: 'solar_meter', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :solar_pv, + aggregate_meter?: false) + end let(:electricity_meters) { [electricity_meter_1, electricity_meter_2, solar_meter] } it 'returns variation for the actual electricity meters only' do @@ -232,7 +263,9 @@ end describe '#intraweek_variation' do - let(:intraweek_variation) { double(max_day_kw: 1, min_day_kw: 2, percent_intraday_variation: 3, max_day: 0, min_day: 1) } + let(:intraweek_variation) do + double(max_day_kw: 1, min_day_kw: 2, percent_intraday_variation: 3, max_day: 0, min_day: 1) + end before do allow_any_instance_of(Baseload::IntraweekBaseloadService).to receive(:intraweek_variation).and_return(intraweek_variation) @@ -241,14 +274,23 @@ it 'returns variation' do result = service.intraweek_variation - expect(result.to_h.keys).to match_array([:estimated_saving_co2, :estimated_saving_£, :max_day_kw, :min_day_kw, :percent_intraday_variation, :variation_rating, :meter, :enough_data?, :min_day, :max_day]) + expect(result.to_h.keys).to match_array(%i[estimated_saving_co2 estimated_saving_£ max_day_kw min_day_kw + percent_intraday_variation variation_rating meter enough_data? min_day max_day]) end end describe '#intraweek_variation_by_meter' do - let(:intraweek_variation) { double(max_day_kw: 1, min_day_kw: 2, percent_intraday_variation: 3, max_day: 0, min_day: 1) } - let(:electricity_meter_1) { double(mpan_mprn: 'meter1', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, aggregate_meter?: false) } - let(:electricity_meter_2) { double(mpan_mprn: 'meter2', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, aggregate_meter?: false) } + let(:intraweek_variation) do + double(max_day_kw: 1, min_day_kw: 2, percent_intraday_variation: 3, max_day: 0, min_day: 1) + end + let(:electricity_meter_1) do + double(mpan_mprn: 'meter1', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, + aggregate_meter?: false) + end + let(:electricity_meter_2) do + double(mpan_mprn: 'meter2', amr_data: double(end_date: Date.parse('20200101')), fuel_type: :electricity, + aggregate_meter?: false) + end let(:electricity_meters) { [electricity_meter_1, electricity_meter_2] } let(:enough_data) { true } let(:data_available_from) { nil } @@ -263,7 +305,8 @@ it 'returns variation' do result = service.intraweek_variation_by_meter expect(result.keys).to match_array(%w[meter1 meter2]) - expect(result['meter1'].to_h.keys).to match_array([:estimated_saving_co2, :estimated_saving_£, :max_day_kw, :min_day_kw, :percent_intraday_variation, :variation_rating, :meter, :enough_data?, :min_day, :max_day]) + expect(result['meter1'].to_h.keys).to match_array(%i[estimated_saving_co2 estimated_saving_£ max_day_kw + min_day_kw percent_intraday_variation variation_rating meter enough_data? min_day max_day]) end context 'and theres not enough data' do @@ -273,14 +316,14 @@ it 'returns a limited variation' do result = service.intraweek_variation_by_meter expect(result.keys).to match_array(%w[meter1 meter2]) - expect(result['meter1'].to_h.keys).to match_array([:meter, :enough_data?, :data_available_from]) + expect(result['meter1'].to_h.keys).to match_array(%i[meter enough_data? data_available_from]) end end end describe '#calculate_rating_from_range' do - let(:good) {0.0} - let(:bad) {0.5} + let(:good) { 0.0 } + let(:bad) { 0.5 } it 'shows 0% as 10.0' do expect(service.calculate_rating_from_range(good, bad, 0)).to eq(10.0) From 64e9e58d203731aa897db11751255076445da240 Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:23:11 +0000 Subject: [PATCH 14/16] more --- .../services/schools/advice/baseload_service_spec.rb | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/spec/services/schools/advice/baseload_service_spec.rb b/spec/services/schools/advice/baseload_service_spec.rb index 6563ce028a..b8093b2a13 100644 --- a/spec/services/schools/advice/baseload_service_spec.rb +++ b/spec/services/schools/advice/baseload_service_spec.rb @@ -143,13 +143,11 @@ def round_floats(array) it 'returns usage by years' do result = service.annual_average_baseloads expect(round_floats(result)).to eq( - round_floats( - [ - { year: '2020/2021', partial: true, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 }, - { year: '2019/2020', partial: false, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 }, - { year: '2018/2019', partial: true, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 } - ] - ) + [ + { year: '2020/2021', partial: true, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 }, + { year: '2019/2020', partial: false, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 }, + { year: '2018/2019', partial: true, baseload: 2, baseload_usage_gbp: 1752, baseload_usage_co2: 3504 } + ] ) end end From 2f7f57b396e32778c56bb497ece809ac9ef182f8 Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Tue, 7 Jan 2025 09:28:46 +0000 Subject: [PATCH 15/16] more --- app/helpers/advice_page_helper.rb | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/helpers/advice_page_helper.rb b/app/helpers/advice_page_helper.rb index 549c244aab..0e47ea134a 100644 --- a/app/helpers/advice_page_helper.rb +++ b/app/helpers/advice_page_helper.rb @@ -41,16 +41,6 @@ def month_year(date) I18n.t('date.month_names')[date.month] + ' ' + date.year.to_s end - def partial_year_note(year, amr_start_date, amr_end_date) - if year == amr_start_date.year && (amr_start_date > Date.new(year, 1, 1)) - I18n.t('advice_pages.tables.labels.partial') - elsif year == amr_end_date.year && amr_end_date < Date.new(year, 12, 31) - I18n.t('advice_pages.tables.labels.partial') - else - '' - end - end - def advice_baseload_high?(estimated_savings_vs_benchmark) estimated_savings_vs_benchmark > 0.0 end From 9e676cbef4d4153be0ef2e73a3adf1bd8ffdb896 Mon Sep 17 00:00:00 2001 From: Tristan Hill <878258+tbhi@users.noreply.github.com> Date: Tue, 7 Jan 2025 09:33:59 +0000 Subject: [PATCH 16/16] more --- app/views/schools/advice/long_term/_recent_trend.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 4667e42639..10d2ab377a 100644 --- a/app/views/schools/advice/long_term/_recent_trend.html.erb +++ b/app/views/schools/advice/long_term/_recent_trend.html.erb @@ -55,7 +55,7 @@ <%# 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 = '_two_years' if analysis_dates.one_years_data? - chart_type = Flipper.enabled?(:school_academic_year_comparisons) ? :acyear : :year %> + chart_type = Flipper.enabled?(:school_academic_year_comparisons, current_user) ? :acyear : :year %> <%= component 'meter_selection_chart', chart_type: :"#{fuel_type}_by_month_#{chart_type}_0_1", meter_selection: meter_selection, @@ -66,7 +66,7 @@ <%# 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') %> - <% if analysis_dates.one_years_data? && Flipper.enabled?(:school_academic_year_comparisons) + <% if analysis_dates.one_years_data? && Flipper.enabled?(:school_academic_year_comparisons, current_user) key_suffix += '_academic_year' end c.with_title { t("#{key_prefix}.title#{key_suffix}", fuel_type: fuel_type) } %>