-
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.
- Loading branch information
1 parent
e947b33
commit 07b8224
Showing
9 changed files
with
245 additions
and
0 deletions.
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,51 @@ | ||
module Api | ||
module ASCOR | ||
class EmissionsChart | ||
attr_accessor :assessment_date, :emissions_assessment_date, :emissions_metric, :emissions_boundary, :country_ids | ||
|
||
def initialize(assessment_date, emissions_metric, emissions_boundary, country_ids) | ||
@assessment_date = assessment_date | ||
@emissions_metric = emissions_metric || 'Absolute' | ||
@emissions_boundary = emissions_boundary || 'Production - excluding LULUCF' | ||
@country_ids = country_ids | ||
end | ||
|
||
def call | ||
{data: collect_data, metadata: collect_metadata} | ||
end | ||
|
||
private | ||
|
||
def collect_data | ||
countries.each_with_object({}) do |country, result| | ||
pathway = pathways[country.id]&.first | ||
result[country.id] = { | ||
emissions: pathway&.emissions || {}, | ||
last_historical_year: pathway&.last_historical_year | ||
} | ||
end | ||
end | ||
|
||
def collect_metadata | ||
{unit: pathways.values.flatten.first.units} | ||
end | ||
|
||
def countries | ||
@countries ||= if country_ids.blank? | ||
::ASCOR::Country.where(iso: ::ASCOR::Country::DEFAULT_COUNTRIES) | ||
else | ||
::ASCOR::Country.where(id: country_ids) | ||
end | ||
end | ||
|
||
def pathways | ||
@pathways ||= ::ASCOR::Pathway.where( | ||
country: countries, | ||
emissions_metric: emissions_metric, | ||
emissions_boundary: emissions_boundary, | ||
assessment_date: assessment_date | ||
).group_by(&: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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<!-- TODO: React components need to be replaced!!! --> | ||
<%#= react_component('TODO', { | ||
emissions_metric_filter: ASCOR::EmissionsMetric::VALUES, | ||
default_emissions_metric_filter: 'Absolute', | ||
emissions_boundary_filter: ASCOR::EmissionsBoundary::VALUES, | ||
default_emissions_boundary_filter: 'Production - excluding LULUCF', | ||
countries: ASCOR::Country.all.map { |c| { id: c.id, iso: c.iso, name: c.name } }, | ||
default_countries: ASCOR::Country.where(iso: ASCOR::Country::DEFAULT_COUNTRIES).map(&:id), | ||
emissions_data_url: index_emissions_assessment_tpi_ascor_index_path | ||
}) %> |
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,7 @@ | ||
(function () { | ||
document.getElementById('emissions-chart').innerHTML = "<%= j render('emissions_chart') %>"; | ||
const newUrl = new URL(window.location.href); | ||
newUrl.searchParams.set('emissions_assessment_date', <%= @assessment_date %>); | ||
window.history.replaceState({}, '', newUrl.href); | ||
ReactRailsUJS.mountComponents('#emissions-charts'); | ||
})(); |
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,99 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Api::ASCOR::EmissionsChart do | ||
subject { described_class.new(assessment_date, emissions_metric, emissions_boundary, country_ids).call } | ||
|
||
before_all do | ||
@usa = create(:ascor_country, id: 1, name: 'USA', iso: 'USA') | ||
@czechia = create(:ascor_country, id: 2, name: 'Czechia', iso: 'CZE') | ||
|
||
create :ascor_pathway, | ||
country: @usa, | ||
assessment_date: Date.new(2019, 1, 1), | ||
emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Consumption - excluding LULUCF', | ||
emissions: {2013 => 100, 2014 => 100, 2015 => 100} | ||
create :ascor_pathway, | ||
country: @usa, | ||
assessment_date: Date.new(2019, 2, 1), | ||
emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Consumption - excluding LULUCF', | ||
emissions: {2013 => 110, 2014 => 110, 2015 => 110} | ||
create :ascor_pathway, | ||
country: @czechia, | ||
assessment_date: Date.new(2019, 2, 1), | ||
emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Consumption - excluding LULUCF', | ||
emissions: {2013 => 120, 2014 => 120, 2015 => 120} | ||
create :ascor_pathway, | ||
country: @usa, | ||
assessment_date: Date.new(2019, 2, 1), | ||
emissions_metric: 'Absolute', | ||
emissions_boundary: 'Production - excluding LULUCF', | ||
emissions: {2013 => 130, 2014 => 130, 2015 => 130} | ||
create :ascor_pathway, | ||
country: @czechia, | ||
assessment_date: Date.new(2019, 2, 1), | ||
emissions_metric: 'Absolute', | ||
emissions_boundary: 'Production - excluding LULUCF', | ||
emissions: {2013 => 140, 2014 => 140, 2015 => 140} | ||
end | ||
|
||
let(:assessment_date) { Date.new(2019, 2, 1) } | ||
let(:emissions_metric) { 'Intensity per capita' } | ||
let(:emissions_boundary) { 'Consumption - excluding LULUCF' } | ||
let(:country_ids) { [@usa.id, @czechia.id] } | ||
|
||
it 'returns expected result' do | ||
expect(subject).to eq( | ||
data: { | ||
@usa.id => { | ||
emissions: {'2013' => 110, '2014' => 110, '2015' => 110}, | ||
last_historical_year: 2010 | ||
}, | ||
@czechia.id => { | ||
emissions: {'2013' => 120, '2014' => 120, '2015' => 120}, | ||
last_historical_year: 2010 | ||
} | ||
}, | ||
metadata: {unit: 'MtCO2e'} | ||
) | ||
end | ||
|
||
context 'when country_ids are nil' do | ||
let(:country_ids) { nil } | ||
|
||
it 'returns expected result' do | ||
expect(subject).to eq( | ||
data: { | ||
@usa.id => { | ||
emissions: {'2013' => 110, '2014' => 110, '2015' => 110}, | ||
last_historical_year: 2010 | ||
} | ||
}, | ||
metadata: {unit: 'MtCO2e'} | ||
) | ||
end | ||
end | ||
|
||
context 'when emissions_metric and emissions_boundary is nil' do | ||
let(:emissions_metric) { nil } | ||
let(:emissions_boundary) { nil } | ||
|
||
it 'returns expected result' do | ||
expect(subject).to eq( | ||
data: { | ||
@usa.id => { | ||
emissions: {'2013' => 130, '2014' => 130, '2015' => 130}, | ||
last_historical_year: 2010 | ||
}, | ||
@czechia.id => { | ||
emissions: {'2013' => 140, '2014' => 140, '2015' => 140}, | ||
last_historical_year: 2010 | ||
} | ||
}, | ||
metadata: {unit: 'MtCO2e'} | ||
) | ||
end | ||
end | ||
end |