Skip to content

Commit

Permalink
Merge pull request #475 from Vizzuality/feature/ascor-benchmarks-chart
Browse files Browse the repository at this point in the history
feat: Service which calculates data for EP.2 chart
  • Loading branch information
barbara-chaves authored Nov 17, 2023
2 parents b906857 + fec7123 commit c8548ef
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/ascor/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions app/models/ascor/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions app/models/ascor/pathway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
45 changes: 45 additions & 0 deletions app/services/api/ascor/benchmarks_chart.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions app/views/tpi/ascor/_assessment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<div class="country-assessment__indicators" for="<%= dom_id(area) %>">
<% 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 %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/tpi/ascor/_benchmarks_chart.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%#= react_component('charts/ascor-benchmarks', { # TODO: Finalise react component
data: Api::ASCOR::BenchmarksChart.new(@assessment_date, @country.id).call
}) %>
2 changes: 1 addition & 1 deletion app/views/tpi/ascor/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<% end %>

<div class="container notes">
<% if @assessment.notes.present? %>
<% if @assessment&.notes.present? %>
<h6>Research notes:</h6>
<p>
<%= @assessment.notes %>
Expand Down
37 changes: 37 additions & 0 deletions spec/services/api/ascor/benchmarks_chart_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c8548ef

Please sign in to comment.