-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from Vizzuality/feature/ascor-benchmarks-chart
feat: Service which calculates data for EP.2 chart
- Loading branch information
Showing
8 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |