Skip to content

Commit

Permalink
Merge pull request #453 from Vizzuality/feat/ascor-recent-emissions
Browse files Browse the repository at this point in the history
feat: Calculate ASCOR recent emissions
  • Loading branch information
martintomas authored Oct 6, 2023
2 parents fdc2279 + a168614 commit 53f4a19
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 20 deletions.
2 changes: 1 addition & 1 deletion app/admin/ascor/assessments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
end

panel 'Assessment Results' do
table_for resource.results.includes(:indicator).order(:id) do
table_for resource.results.includes(:indicator).order(:indicator_id) do
column(:indicator)
column(:answer)
column(:source)
Expand Down
23 changes: 20 additions & 3 deletions app/assets/stylesheets/tpi/pages/ascor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -321,17 +321,34 @@ $see-more-width-tablet: 130px;

&__indicator {
margin-bottom: 1px;
outline: solid 1px $grey-lighter-medium;
margin-left: 40px;
padding: 15px 45px 15px 20px;
display: flex;

&__title {
outline: solid 1px $grey-lighter-medium;
font-family: $family-sans-serif;
font-weight: 700;
font-size: 16px;
margin-left: 40px;
padding: 15px 20px;
display: flex;
align-items: center;
gap: 25px;
flex-grow: 2;
}

&__source {
font-family: $family-sans-serif;
font-size: 14px !important;
line-height: 40px;

a {
color: $grey-dark;
text-decoration: underline;
}

a:hover {
color: $dark;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions app/controllers/tpi/ascor_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def index

def show
@assessment = ASCOR::Assessment.find_by country: @country, assessment_date: @assessment_date
@recent_emissions = Api::ASCOR::RecentEmissions.new(@assessment_date, @country).call
fixed_navbar("ASCOR Country #{@country.name}", admin_ascor_country_path(@country.id))
end

Expand Down
35 changes: 35 additions & 0 deletions app/javascript/components/tpi/AscorRecentEmissions.js
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;
40 changes: 40 additions & 0 deletions app/services/api/ascor/recent_emissions.rb
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
39 changes: 23 additions & 16 deletions app/views/tpi/ascor/_assessment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,38 @@

<div class="country-assessment__indicators" for="<%= dom_id(area) %>">
<% ascor_sub_indicators_for(area, indicators).each do |indicator| %>
<div class="country-assessment__indicator">
<div class="country-assessment__indicator__title country-assessment__icon country-assessment__icon--<%= ascor_icon_for(indicator, @assessment) %>">
<%= "#{indicator.code.split('.').last}. #{indicator.text}" %>
<div class="country-assessment__indicator-block">
<div class="country-assessment__indicator">
<div class="country-assessment__indicator__title country-assessment__icon country-assessment__icon--<%= ascor_icon_for(indicator, @assessment) %>">
<%= "#{indicator.code.split('.').last}. #{indicator.text}" %>
</div>
<% if ascor_assessment_result_for(indicator, @assessment).source.present? %>
<div class="country-assessment__indicator__source">
<%= link_to 'Source', ascor_assessment_result_for(indicator, @assessment).source %>
</div>
<% end %>
</div>

<% ascor_sub_indicators_for(indicator, metrics).each do |metric| %>
<div class="country-assessment__metric">
<div class="country-assessment__metric__value">
<% if metric.code == 'EP.1.a.i' %>
<!-- TODO: Custom dynamic indicator -->
<% elsif metric.code == 'EP.1.a.ii' %>
<!-- TODO: Custom dynamic indicator -->
<% elsif ascor_assessment_result_for(metric, @assessment).answer.present? %>
<% if metric.code == 'EP.1.a.i' %>
<%= render 'tpi/ascor/metrics_ep1a', recent_emissions: @recent_emissions %>
<% elsif metric.code == 'EP.1.a.ii' %>
<%# skipped because EP.1.a.i and EP.1.a.ii are rendered via same React component %>
<% else %>
<% if ascor_assessment_result_for(metric, @assessment).answer.present? %>
<div class="country-assessment__metric__value__text">
<%= ascor_assessment_result_for(metric, @assessment).answer %>
</div>
<% end %>
</div>
<div class="country-assessment__metric__title">
<%= "#{metric.code.split('.').last}. #{metric.text}" %>
</div>
<% if ascor_assessment_result_for(metric, @assessment).source.present? %>
<div class="country-assessment__metric__source">
<%= link_to 'Source', ascor_assessment_result_for(metric, @assessment).source %>
<div class="country-assessment__metric__title">
<%= "#{metric.code.split('.').last}. #{metric.text}" %>
</div>
<% if ascor_assessment_result_for(metric, @assessment).source.present? %>
<div class="country-assessment__metric__source">
<%= link_to 'Source', ascor_assessment_result_for(metric, @assessment).source %>
</div>
<% end %>
<% end %>
</div>
<% end %>
Expand Down
9 changes: 9 additions & 0 deletions app/views/tpi/ascor/_metrics_ep1a.html.erb
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
}) %>
82 changes: 82 additions & 0 deletions spec/services/api/ascor/recent_emissions_spec.rb
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

0 comments on commit 53f4a19

Please sign in to comment.