-
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 #453 from Vizzuality/feat/ascor-recent-emissions
feat: Calculate ASCOR recent emissions
- Loading branch information
Showing
8 changed files
with
211 additions
and
20 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,35 @@ | ||
import PropTypes from 'prop-types'; | ||
|
||
// eslint-disable-next-line no-empty-pattern | ||
const AscorRecentEmissions = ({ }) => { | ||
|
||
}; | ||
|
||
AscorRecentEmissions.propTypes = { | ||
emissions_metric_filter: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
default_emissions_metric_filter: PropTypes.string.isRequired, | ||
emissions_boundary_filter: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
default_emissions_boundary_filter: PropTypes.string.isRequired, | ||
trend_filters: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
default_trend_filter: PropTypes.string.isRequired, | ||
data: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
value: PropTypes.number, | ||
source: PropTypes.string, | ||
year: PropTypes.number, | ||
unit: PropTypes.string.isRequired, | ||
trends: PropTypes.shape({ | ||
source: PropTypes.string, | ||
year: PropTypes.number, | ||
values: PropTypes.arrayOf( | ||
PropTypes.shape({ | ||
filter: PropTypes.string.isRequired, | ||
value: PropTypes.string.isRequired | ||
}) | ||
).isRequired | ||
}).isRequired | ||
}) | ||
).isRequired | ||
}; | ||
|
||
export default AscorRecentEmissions; |
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,40 @@ | ||
module Api | ||
module ASCOR | ||
class RecentEmissions | ||
attr_accessor :assessment_date, :country | ||
|
||
def initialize(assessment_date, country) | ||
@assessment_date = assessment_date | ||
@country = country | ||
end | ||
|
||
def call | ||
pathways.map do |pathway| | ||
{ | ||
value: pathway.recent_emission_level, | ||
source: pathway.recent_emission_source, | ||
year: pathway.recent_emission_year, | ||
emissions_metric: pathway.emissions_metric, | ||
emissions_boundary: pathway.emissions_boundary, | ||
unit: pathway.units, | ||
trend: { | ||
source: pathway.trend_source, | ||
year: pathway.trend_year, | ||
values: [ | ||
{filter: '1 year trend', value: pathway.trend_1_year}, | ||
{filter: '3 years trend', value: pathway.trend_3_year}, | ||
{filter: '5 years trend', value: pathway.trend_5_year} | ||
] | ||
} | ||
} | ||
end | ||
end | ||
|
||
private | ||
|
||
def pathways | ||
@pathways ||= ::ASCOR::Pathway.where(assessment_date: assessment_date, country: country) | ||
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,9 @@ | ||
<%= react_component('AscorRecentEmissions', { | ||
emissions_metric_filter: ASCOR::EmissionsMetric::VALUES, | ||
default_emissions_metric_filter: 'Absolute', | ||
emissions_boundary_filter: ASCOR::EmissionsBoundary::VALUES, | ||
default_emissions_boundary_filter: 'Production - excluding LULUCF', | ||
trend_filters: ['1 year trend', '3 years trend', '5 years trend'], | ||
default_trend_filter: '1 year trend', | ||
data: recent_emissions | ||
}) %> |
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,82 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Api::ASCOR::RecentEmissions do | ||
subject { described_class.new(@assessment_date, @country).call } | ||
|
||
before_all do | ||
@country = create(:ascor_country, id: 1, name: 'USA', iso: 'USA') | ||
@assessment_date = Date.new(2019, 1, 1) | ||
|
||
create :ascor_pathway, | ||
country: @country, | ||
assessment_date: Date.new(2019, 1, 1), | ||
emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Consumption - excluding LULUCF', | ||
units: 'MtCO2e', | ||
trend_1_year: '+ 6%', | ||
trend_3_year: '+ 8%', | ||
trend_5_year: '+ 9%', | ||
trend_source: 'source', | ||
trend_year: 2019, | ||
recent_emission_level: 100, | ||
recent_emission_source: 'source', | ||
recent_emission_year: 2019 | ||
create :ascor_pathway, | ||
country: @country, | ||
assessment_date: Date.new(2019, 1, 1), | ||
emissions_metric: 'Absolute', | ||
emissions_boundary: 'Production - only LULUCF', | ||
units: 'MtCO2e', | ||
trend_1_year: '- 6%', | ||
trend_3_year: '+ 6%', | ||
trend_5_year: '+ 8%', | ||
trend_source: 'source 2', | ||
trend_year: 2020, | ||
recent_emission_level: 200, | ||
recent_emission_source: 'source 2', | ||
recent_emission_year: 2020 | ||
create :ascor_pathway, country: create(:ascor_country, id: 2, name: 'Czechia', iso: 'CZE') | ||
create :ascor_pathway, country: @country, assessment_date: Date.new(2019, 2, 1) | ||
end | ||
|
||
it 'returns expected result' do | ||
expect(subject).to match_array( | ||
[ | ||
{ | ||
value: 100, | ||
source: 'source', | ||
year: 2019, | ||
unit: 'MtCO2e', | ||
emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Consumption - excluding LULUCF', | ||
trend: { | ||
source: 'source', | ||
year: 2019, | ||
values: [ | ||
{filter: '1 year trend', value: '+ 6%'}, | ||
{filter: '3 years trend', value: '+ 8%'}, | ||
{filter: '5 years trend', value: '+ 9%'} | ||
] | ||
} | ||
}, | ||
{ | ||
value: 200, | ||
source: 'source 2', | ||
year: 2020, | ||
unit: 'MtCO2e', | ||
emissions_metric: 'Absolute', | ||
emissions_boundary: 'Production - only LULUCF', | ||
trend: { | ||
source: 'source 2', | ||
year: 2020, | ||
values: [ | ||
{filter: '1 year trend', value: '- 6%'}, | ||
{filter: '3 years trend', value: '+ 6%'}, | ||
{filter: '5 years trend', value: '+ 8%'} | ||
] | ||
} | ||
} | ||
] | ||
) | ||
end | ||
end |