Skip to content

Commit

Permalink
Merge branch 'master' into aws-eb-master
Browse files Browse the repository at this point in the history
  • Loading branch information
ldodds committed Jan 20, 2025
2 parents 6ab9679 + 47b9112 commit 44da65f
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 74 deletions.
3 changes: 2 additions & 1 deletion app/controllers/schools/advice/advice_base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def analysis_dates
last_full_week_end_date: last_full_week_end_date(end_date),
recent_data: recent_data?(end_date),
months_of_data: months_between(start_date, end_date),
months_analysed: months_analysed(start_date, end_date)
months_analysed: months_analysed(start_date, end_date),
fixed_academic_year_end: DateService.fixed_academic_year_end(end_date)
)
end

Expand Down
41 changes: 20 additions & 21 deletions app/services/charts/meter_selection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Charts
# Encapsulates presenting a list of meters used to populate a select box for driving a MeterSelectionChartComponent
#
Expand Down Expand Up @@ -40,7 +42,8 @@ def initialize(school,
include_whole_school: true,
date_window: nil,
whole_school_title_key: 'advice_pages.charts.the_whole_school',
whole_school_label_key: 'advice_pages.charts.whole_school')
whole_school_label_key: 'advice_pages.charts.whole_school',
academic_year: false)
@school = school
@meter_collection = meter_collection
@fuel_type = fuel_type
Expand All @@ -49,35 +52,23 @@ def initialize(school,
@filter = filter
@whole_school_title_key = whole_school_title_key
@whole_school_label_key = whole_school_label_key
@academic_year = academic_year
end

def meter_selection_options
@include_whole_school ? displayable_meters.prepend(aggregate_meter_adapter) : displayable_meters
end

def date_ranges_by_meter
ranges_by_meter = {}
if @include_whole_school
ranges_by_meter[aggregate_meter.mpan_mprn] = {
meter: aggregate_meter_adapter,
start_date: start_date(aggregate_meter),
end_date: aggregate_meter.amr_data.end_date
}
end
meters = []
meters << [aggregate_meter, aggregate_meter_adapter] if @include_whole_school
# if single meter, then the underlying meters is the aggregate meter
# just return the range for aggregate adapter in this case so its labelled
# correctly as "the whole school"
return ranges_by_meter if @include_whole_school && underlying_meters.count == 1
displayable_meters.each do |analytics_meter|
end_date = analytics_meter.amr_data.end_date
start_date = start_date(analytics_meter)
ranges_by_meter[analytics_meter.mpan_mprn] = {
meter: analytics_meter,
start_date: start_date,
end_date: end_date
}
meters.concat(displayable_meters) unless @include_whole_school && underlying_meters.count == 1
meters.to_h do |meter, meter_adapter|
[meter.mpan_mprn, { meter: meter_adapter || meter, start_date: start_date(meter), end_date: end_date(meter) }]
end
ranges_by_meter
end

def underlying_meters
Expand All @@ -97,7 +88,7 @@ def displayable_meters
when :gas
@meter_collection.heat_meters
when :storage_heater, :storage_heaters
@meter_collection.storage_heater_meters # TODO likely not used
@meter_collection.storage_heater_meters # TODO: likely not used
else
raise 'Unexpected fuel type'
end
Expand All @@ -108,12 +99,20 @@ def displayable_meters

def start_date(meter)
earliest_date = meter.amr_data.start_date
return earliest_date unless @date_window.present?
return earliest_date if @date_window.blank?

desired_date = meter.amr_data.end_date - @date_window
[desired_date, earliest_date].max
end

def end_date(meter)
if @academic_year
DateService.fixed_academic_year_end(meter.amr_data.end_date)
else
meter.amr_data.end_date
end
end

# Used to override default labelling methods for aggregate meter
def aggregate_meter_adapter
adapter = ActiveSupport::OrderedOptions.new
Expand Down
7 changes: 7 additions & 0 deletions app/services/date_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module DateService
def self.fixed_academic_year_end(date)
Date.new(date.year + (date.month < 9 ? 0 : 1), 8, 31)
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<%= render 'schools/advice/section_title', section_id: 'comparison', section_title: advice_t('electricity_costs.analysis.comparison.title') %>
<%= render 'schools/advice/section_title', section_id: 'comparison',
section_title: advice_t('electricity_costs.analysis.comparison.title') %>

<%= component 'chart', chart_type: :electricity_cost_comparison_last_2_years_accounting, school: school, chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn) do |c| %>
<%= render ChartComponent.new(
chart_type: :electricity_cost_comparison_last_2_years_accounting,
school: school,
chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn)
) do |c| %>
<% c.with_title { advice_t('electricity_costs.charts.cost_comparison_last_2_years_accounting.title') } %>
<% c.with_subtitle { advice_t('electricity_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html', end_date: analysis_dates.end_date.to_fs(:es_short)) } %>
<% c.with_subtitle do
advice_t('electricity_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html',
end_date: analysis_dates.fixed_academic_year_end.to_fs(:es_short))
end %>
<% end %>
30 changes: 24 additions & 6 deletions app/views/schools/advice/electricity_costs/_meter.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<%= component 'chart', chart_type: one_year_breakdown_chart, analysis_controls: true, school: school, chart_config: create_chart_config(school, one_year_breakdown_chart, mpan_mprn) do |c| %>
<%= render ChartComponent.new(chart_type: one_year_breakdown_chart,
school: school,
chart_config: create_chart_config(school, one_year_breakdown_chart, mpan_mprn)) do |c| %>
<% c.with_title { advice_t("electricity_costs.charts.#{one_year_breakdown_chart_key}.meter_title", meter: label) } %>
<% c.with_subtitle { advice_t("electricity_costs.charts.#{one_year_breakdown_chart_key}.meter_subtitle_html", end_date: analysis_dates.end_date.to_fs(:es_short)) } %>
<% c.with_subtitle do
advice_t("electricity_costs.charts.#{one_year_breakdown_chart_key}.meter_subtitle_html",
end_date: analysis_dates.end_date.to_fs(:es_short))
end %>
<% end %>

<%= component 'meter_costs_table', monthly_costs: monthly_costs, change_in_costs: change_in_costs, school: school, fuel_type: fuel_type %>
<%= render MeterCostsTableComponent.new(monthly_costs: monthly_costs,
change_in_costs: change_in_costs,
school: school,
fuel_type: fuel_type) %>

<%= render 'agreed_capacity_notice' if @agreed_capacity %>

Expand All @@ -15,8 +23,18 @@
<% end %>

<% if analysis_dates.months_of_data > 23 %>
<%= component 'chart', chart_type: :electricity_cost_comparison_last_2_years_accounting, analysis_controls: true, school: school, chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn) do |c| %>
<% c.with_title { advice_t('electricity_costs.charts.cost_comparison_last_2_years_accounting.meter_title', meter: label) } %>
<% c.with_subtitle { advice_t('electricity_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html', end_date: analysis_dates.end_date.to_fs(:es_short)) } %>
<%= render ChartComponent.new(
chart_type: :electricity_cost_comparison_last_2_years_accounting,
analysis_controls: true,
school: school,
chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn)
) do |c| %>
<% c.with_title do
advice_t('electricity_costs.charts.cost_comparison_last_2_years_accounting.meter_title', meter: label)
end %>
<% c.with_subtitle do
advice_t('electricity_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html',
end_date: analysis_dates.fixed_academic_year_end.to_fs(:es_short))
end %>
<% end %>
<% end %>
14 changes: 11 additions & 3 deletions app/views/schools/advice/gas_costs/_cost_comparison.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<%= render 'schools/advice/section_title', section_id: 'comparison', section_title: advice_t('gas_costs.analysis.comparison.title') %>
<%= render 'schools/advice/section_title', section_id: 'comparison',
section_title: advice_t('gas_costs.analysis.comparison.title') %>

<%= component 'chart', chart_type: :electricity_cost_comparison_last_2_years_accounting, school: school, chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn) do |c| %>
<%= render ChartComponent.new(
chart_type: :electricity_cost_comparison_last_2_years_accounting,
school: school,
chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn)
) do |c| %>
<% c.with_title { advice_t('gas_costs.charts.cost_comparison_last_2_years_accounting.title') } %>
<% c.with_subtitle { advice_t('gas_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html', end_date: analysis_dates.end_date.to_fs(:es_short)) } %>
<% c.with_subtitle do
advice_t('gas_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html',
end_date: analysis_dates.fixed_academic_year_end.to_fs(:es_short))
end %>
<% end %>
30 changes: 23 additions & 7 deletions app/views/schools/advice/gas_costs/_meter.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
<%= component 'chart', chart_type: one_year_breakdown_chart, analysis_controls: true, school: school, chart_config: create_chart_config(school, one_year_breakdown_chart, mpan_mprn) do |c| %>
<%= render ChartComponent.new(chart_type: one_year_breakdown_chart,
analysis_controls: true,
school: school,
chart_config: create_chart_config(school, one_year_breakdown_chart, mpan_mprn)) do |c| %>
<% c.with_title { advice_t("gas_costs.charts.#{one_year_breakdown_chart_key}.meter_title", meter: label) } %>
<% c.with_subtitle { advice_t("gas_costs.charts.#{one_year_breakdown_chart_key}.meter_subtitle_html", end_date: analysis_dates.end_date.to_fs(:es_short)) } %>
<% c.with_subtitle do
advice_t("gas_costs.charts.#{one_year_breakdown_chart_key}.meter_subtitle_html",
end_date: analysis_dates.end_date.to_fs(:es_short))
end %>
<% end %>

<%= component 'meter_costs_table', monthly_costs: monthly_costs, change_in_costs: change_in_costs %>
<%= render MeterCostsTableComponent.new(monthly_costs: monthly_costs, change_in_costs: change_in_costs) %>

<% if local_assigns[:tariffs] %>
<%= render 'tariff_summary_table', school: school, tariffs: tariffs %>
<%= render TariffSummaryTable.new(school: school, tariffs: tariffs) %>
<div class="text-right">
<%= t('schools.user_tariffs.view_and_manage_tariffs_html', user_tariffs_path: school_energy_tariffs_path(school)) %>
</div>
<% end %>

<% if analysis_dates.months_of_data > 23 %>
<%= component 'chart', chart_type: :electricity_cost_comparison_last_2_years_accounting, analysis_controls: true, school: school, chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn) do |c| %>
<% c.with_title { advice_t('gas_costs.charts.cost_comparison_last_2_years_accounting.meter_title', meter: label) } %>
<% c.with_subtitle { advice_t('gas_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html', end_date: analysis_dates.end_date.to_fs(:es_short)) } %>
<%= render ChartComponent.new(
chart_type: :electricity_cost_comparison_last_2_years_accounting,
analysis_controls: true,
school: school,
chart_config: create_chart_config(school, :electricity_cost_comparison_last_2_years_accounting, mpan_mprn)
) do |c| %>
<% c.with_title do
advice_t('gas_costs.charts.cost_comparison_last_2_years_accounting.meter_title', meter: label)
end %>
<% c.with_subtitle do
advice_t('gas_costs.charts.cost_comparison_last_2_years_accounting.subtitle_html',
end_date: analysis_dates.fixed_academic_year_end.to_fs(:es_short))
end %>
<% end %>
<% end %>
7 changes: 2 additions & 5 deletions app/views/schools/advice/long_term/_long_term_trend.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
<% 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, current_user) %>
<%= component 'meter_selection_chart',
chart_type: :"#{fuel_type}_longterm_trend#{suffix}",
chart_type: :"#{fuel_type}_longterm_trend_academic_year",
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_title { t("advice_pages.#{fuel_type}_long_term.charts.#{fuel_type}_longterm_trend.title_academic_year") } %>
<% 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') %>
Expand Down
14 changes: 4 additions & 10 deletions app/views/schools/advice/long_term/_recent_trend.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,16 @@
<%# 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 = '_two_years' if analysis_dates.one_years_data?
chart_type = Flipper.enabled?(:school_academic_year_comparisons, current_user) ? :acyear : :year %>
key_suffix = '_two_years' if analysis_dates.one_years_data? %>
<%= component 'meter_selection_chart',
chart_type: :"#{fuel_type}_by_month_#{chart_type}_0_1",
meter_selection: meter_selection,
chart_type: :"#{fuel_type}_by_month_acyear_0_1",
meter_selection: Charts::MeterSelection.new(@school, @aggregate_school, fuel_type, academic_year: true),
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') %>
<% 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) } %>
<% c.with_title { t("#{key_prefix}.title#{key_suffix}", fuel_type: fuel_type) } %>
<% c.with_footer do %>
<p><%= t("advice_pages.#{fuel_type}_long_term.charts.#{fuel_type}_by_month_year.explanation") %></p>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion config/locales/views/advice_pages/electricity_costs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ en:
title: Electricity cost components for the last few weeks
cost_comparison_last_2_years_accounting:
meter_title: Electricity cost comparison for last 2 academic years for %{meter}
subtitle_html: This chart compares your monthly consumption for the academic year ending on <span class='end-date'>%{end_date}</span> with the previous academic year
subtitle_html: This chart compares your monthly consumption for the academic year ending on <span>%{end_date}</span> with the previous academic year
title: Electricity cost comparison for last 2 academic years
insights:
current_costs:
Expand Down
5 changes: 2 additions & 3 deletions config/locales/views/advice_pages/electricity_long_term.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@ en:
electricity_by_month_year:
explanation: This chart can be a quick way to spot whether energy saving behaviour and lighting and equipment updates are having an impact on your electricity consumption. You need to be careful when comparing months with holidays, particularly Easter, which some years is in March and other times in April.
subtitle_html: This chart shows the monthly electricity consumption from <span class='start-date'>%{start_date}</span> to <span class='end-date'>%{end_date}</span> for <span class='meter'>%{meter}</span>
subtitle_two_years_html: This chart shows the difference in electricity consumption for the 12 months ending on <span class='end-date'>%{end_date}</span> and the previous 12 months for <span class='meter'>%{meter}</span>
subtitle_two_years_html: This chart shows the difference in electricity consumption for the academic year ending on <span>%{end_date}</span> and the previous academic year for <span class='meter'>%{meter}</span>
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
title_two_years: 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 <span class='meter'>%{meter}</span>
Expand Down
2 changes: 1 addition & 1 deletion config/locales/views/advice_pages/gas_costs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ en:
title: Gas cost components for the last few weeks
cost_comparison_last_2_years_accounting:
meter_title: Gas cost comparison for last 2 academic years for %{meter}
subtitle_html: This chart compares your monthly consumption for the academic year ending on <span class='end-date'>%{end_date}</span> with the previous academic year
subtitle_html: This chart compares your monthly consumption for the academic year up to <span>%{end_date}</span> with the previous academic year
title: Gas cost comparison for last 2 academic years
insights:
current_costs:
Expand Down
Loading

0 comments on commit 44da65f

Please sign in to comment.