Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make baseload advice longterm table use academic years #4124

Draft
wants to merge 5 commits into
base: 4619-academic-year-comparisons-on-long-term-usage-pages
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
55 changes: 35 additions & 20 deletions app/services/schools/advice/baseload_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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)
Expand All @@ -62,23 +56,33 @@ 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::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
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
meter_breakdown_service = Baseload::BaseloadMeterBreakdownService.new(@meter_collection)
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
Expand All @@ -105,6 +109,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
Expand All @@ -116,6 +121,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
Expand Down Expand Up @@ -186,11 +192,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
Expand All @@ -215,7 +226,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
<tr>
<td>
<%= 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] %>
</td>
<td class="text-right"><%= format_unit(annual_average_baseload[:baseload], :kw) %></td>
<td class="text-right"><%= format_unit(annual_average_baseload[:baseload_usage].£, :£) %></td>
<td class="text-right"><%= format_unit(annual_average_baseload[:baseload_usage].co2, :co2) %></td>
<td class="text-right"><%= format_unit(annual_average_baseload[:baseload_usage_gbp], :£) %></td>
<td class="text-right"><%= format_unit(annual_average_baseload[:baseload_usage_co2], :co2) %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="text-right advice-table-caption">
<%= 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' %>
</div>
2 changes: 1 addition & 1 deletion config/locales/views/advice_pages/baseload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |-
Expand Down
Loading
Loading