From fec71236c1a56654cc28a4f241d02da41159ac1a Mon Sep 17 00:00:00 2001 From: martintomas Date: Thu, 9 Nov 2023 11:24:31 +0100 Subject: [PATCH] feat: Service which calculates data for EP.2 chart --- app/models/ascor/assessment.rb | 2 + app/models/ascor/benchmark.rb | 2 + app/models/ascor/pathway.rb | 2 + app/services/api/ascor/benchmarks_chart.rb | 45 +++++++++++++++++++ app/views/tpi/ascor/_assessment.html.erb | 3 ++ .../tpi/ascor/_benchmarks_chart.html.erb | 3 ++ app/views/tpi/ascor/show.html.erb | 2 +- .../api/ascor/benchmarks_chart_spec.rb | 37 +++++++++++++++ 8 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 app/services/api/ascor/benchmarks_chart.rb create mode 100644 app/views/tpi/ascor/_benchmarks_chart.html.erb create mode 100644 spec/services/api/ascor/benchmarks_chart_spec.rb diff --git a/app/models/ascor/assessment.rb b/app/models/ascor/assessment.rb index 743dd5dea..0bebf1cfe 100644 --- a/app/models/ascor/assessment.rb +++ b/app/models/ascor/assessment.rb @@ -19,4 +19,6 @@ class ASCOR::Assessment < ApplicationRecord validates_presence_of :assessment_date accepts_nested_attributes_for :results, allow_destroy: true + + scope :currently_published, -> { where('ascor_assessments.publication_date <= ?', DateTime.now) } end diff --git a/app/models/ascor/benchmark.rb b/app/models/ascor/benchmark.rb index 922d72003..b7be62b98 100644 --- a/app/models/ascor/benchmark.rb +++ b/app/models/ascor/benchmark.rb @@ -22,4 +22,6 @@ class ASCOR::Benchmark < ApplicationRecord validates :emissions_metric, inclusion: {in: ASCOR::EmissionsMetric::VALUES}, allow_nil: true validates :emissions_boundary, inclusion: {in: ASCOR::EmissionsBoundary::VALUES}, allow_nil: true validates :benchmark_type, inclusion: {in: ASCOR::BenchmarkType::VALUES}, allow_nil: true + + scope :currently_published, -> { where('ascor_benchmarks.publication_date <= ?', DateTime.now) } end diff --git a/app/models/ascor/pathway.rb b/app/models/ascor/pathway.rb index e85b56d15..32aab9ac4 100644 --- a/app/models/ascor/pathway.rb +++ b/app/models/ascor/pathway.rb @@ -30,4 +30,6 @@ class ASCOR::Pathway < ApplicationRecord validates_presence_of :emissions_metric, :emissions_boundary, :units, :assessment_date validates :emissions_metric, inclusion: {in: ASCOR::EmissionsMetric::VALUES}, allow_nil: true validates :emissions_boundary, inclusion: {in: ASCOR::EmissionsBoundary::VALUES}, allow_nil: true + + scope :currently_published, -> { where('ascor_pathways.publication_date <= ?', DateTime.now) } end diff --git a/app/services/api/ascor/benchmarks_chart.rb b/app/services/api/ascor/benchmarks_chart.rb new file mode 100644 index 000000000..972eaa456 --- /dev/null +++ b/app/services/api/ascor/benchmarks_chart.rb @@ -0,0 +1,45 @@ +module Api + module ASCOR + class BenchmarksChart + attr_accessor :assessment_date, :country_id + + def initialize(assessment_date, country_id) + @assessment_date = assessment_date + @country_id = country_id + end + + def call + {data: collect_data, metadata: collect_metadata} + end + + private + + def collect_data + { + emissions: pathway&.emissions || {}, + last_historical_year: pathway&.last_historical_year, + benchmarks: benchmarks.map do |benchmark| + {benchmark_type: benchmark.benchmark_type, emissions: benchmark.emissions} + end + } + end + + def collect_metadata + {unit: benchmarks.first&.units} + end + + def pathway + @pathway ||= ::ASCOR::Pathway.where( + country_id: country_id, + emissions_metric: benchmarks.first&.emissions_metric, + emissions_boundary: benchmarks.first&.emissions_boundary, + assessment_date: assessment_date + ).first + end + + def benchmarks + @benchmarks ||= ::ASCOR::Benchmark.where(country_id: country_id) + end + end + end +end diff --git a/app/views/tpi/ascor/_assessment.html.erb b/app/views/tpi/ascor/_assessment.html.erb index d0149efaa..afbcd21e3 100644 --- a/app/views/tpi/ascor/_assessment.html.erb +++ b/app/views/tpi/ascor/_assessment.html.erb @@ -23,6 +23,9 @@
<% if ascor_sub_indicators_for(area, indicators).present? %> <%= render 'tpi/ascor/assessment_indicators', area: area, indicators: ascor_sub_indicators_for(area, indicators), metrics: metrics %> + <% if area.code == 'EP.2' %> + <%= render 'tpi/ascor/benchmarks_chart' %> + <% end %> <% else %> <%= render 'tpi/ascor/assessment_metrics', metrics: ascor_sub_indicators_for(area, metrics) %> <% end %> diff --git a/app/views/tpi/ascor/_benchmarks_chart.html.erb b/app/views/tpi/ascor/_benchmarks_chart.html.erb new file mode 100644 index 000000000..ba29d7f39 --- /dev/null +++ b/app/views/tpi/ascor/_benchmarks_chart.html.erb @@ -0,0 +1,3 @@ +<%#= react_component('charts/ascor-benchmarks', { # TODO: Finalise react component + data: Api::ASCOR::BenchmarksChart.new(@assessment_date, @country.id).call +}) %> \ No newline at end of file diff --git a/app/views/tpi/ascor/show.html.erb b/app/views/tpi/ascor/show.html.erb index 0ffb49734..f01e3839a 100644 --- a/app/views/tpi/ascor/show.html.erb +++ b/app/views/tpi/ascor/show.html.erb @@ -40,7 +40,7 @@ <% end %>
- <% if @assessment.notes.present? %> + <% if @assessment&.notes.present? %>
Research notes:

<%= @assessment.notes %> diff --git a/spec/services/api/ascor/benchmarks_chart_spec.rb b/spec/services/api/ascor/benchmarks_chart_spec.rb new file mode 100644 index 000000000..5b59f6b51 --- /dev/null +++ b/spec/services/api/ascor/benchmarks_chart_spec.rb @@ -0,0 +1,37 @@ +require 'rails_helper' + +RSpec.describe Api::ASCOR::BenchmarksChart do + subject { described_class.new(assessment_date, country_id).call } + + before_all do + @country = create :ascor_country + + create :ascor_pathway, + country: @country, + assessment_date: Date.new(2019, 2, 1), + emissions_metric: 'Absolute', + emissions_boundary: 'Production - excluding LULUCF', + emissions: {2013 => 130, 2014 => 130, 2015 => 130} + create :ascor_benchmark, + country: @country, + emissions_metric: 'Absolute', + emissions_boundary: 'Production - excluding LULUCF', + emissions: {2015 => 10, 2016 => 11, 2017 => 12} + end + + let(:assessment_date) { Date.new(2019, 2, 1) } + let(:country_id) { @country.id } + + it 'returns expected result' do + expect(subject).to eq( + data: { + emissions: {'2013' => 130, '2014' => 130, '2015' => 130}, + last_historical_year: 2010, + benchmarks: [ + {benchmark_type: 'National 1.5C benchmark', emissions: {'2015' => 10, '2016' => 11, '2017' => 12}} + ] + }, + metadata: {unit: 'MtCO2e'} + ) + end +end