-
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
9ead60f
commit aee7cee
Showing
9 changed files
with
251 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,63 @@ | ||
module Api | ||
module ASCOR | ||
class BubbleChart | ||
MARKET_CAP_QUERY = { | ||
emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Production - excluding LULUCF' | ||
}.freeze | ||
|
||
attr_accessor :assessment_date | ||
|
||
def initialize(assessment_date) | ||
@assessment_date = assessment_date | ||
end | ||
|
||
def call | ||
::ASCOR::AssessmentResult | ||
.by_date(@assessment_date) | ||
.of_type(:area) | ||
.includes(assessment: :country) | ||
.order(:indicator_id) | ||
.map do |result| | ||
{ | ||
pillar: pillars[result.indicator.code.split('.').first]&.first&.text, | ||
area: result.indicator.text, | ||
result: result.answer, | ||
country_id: result.assessment.country_id, | ||
country_name: result.assessment.country.name, | ||
country_path: result.assessment.country.path, | ||
market_cap_group: calculate_market_cap_group(result.assessment.country_id) | ||
} | ||
end | ||
end | ||
|
||
private | ||
|
||
def calculate_market_cap_group(country_id) | ||
recent_emission_level = recent_emission_levels[country_id]&.first&.recent_emission_level | ||
return :small if recent_emission_level.blank? | ||
|
||
market_cap_groups.find { |range, _| range.include?(recent_emission_level) }&.last || :small | ||
end | ||
|
||
def pillars | ||
::ASCOR::AssessmentIndicator.where(indicator_type: :pillar).group_by(&:code) | ||
end | ||
|
||
def recent_emission_levels | ||
@recent_emission_levels ||= ::ASCOR::Pathway.where(MARKET_CAP_QUERY).where(assessment_date: assessment_date) | ||
.select(:country_id, :recent_emission_level) | ||
.group_by(&:country_id) | ||
end | ||
|
||
def market_cap_groups | ||
@market_cap_groups ||= begin | ||
min = ::ASCOR::Pathway.where(MARKET_CAP_QUERY).where(assessment_date: assessment_date).minimum(:recent_emission_level) | ||
max = ::ASCOR::Pathway.where(MARKET_CAP_QUERY).where(assessment_date: assessment_date).maximum(:recent_emission_level) | ||
step = (max.to_f - min.to_f) / 3 | ||
{min..min + step => :small, min + step..min + (2 * step) => :medium, min + (2 * step)..max => :large} | ||
end | ||
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,3 @@ | ||
<!-- TODO: React components need to be replaced!!! --> | ||
<%#= react_component('charts/bank-bubble/Chart', { results: @ascor_assessment_results }) %> | ||
<%#= react_component('charts/bank-bubble/CompaniesAccordion', { results: @ascor_assessment_results }) %> |
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('bubble-chart').innerHTML = "<%= j render('bubble_chart') %>"; | ||
const newUrl = new URL(window.location.href); | ||
newUrl.searchParams.set('assessment_date', <%= @assessment_date %>); | ||
window.history.replaceState({}, '', newUrl.href); | ||
ReactRailsUJS.mountComponents('#assessment-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,105 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Api::ASCOR::BubbleChart do | ||
subject { described_class.new(assessment_date).call } | ||
|
||
before_all do | ||
usa = create(:ascor_country, id: 1, name: 'USA', iso: 'USA') | ||
japan = create(:ascor_country, id: 2, name: 'Japan', iso: 'JPN') | ||
|
||
_indicator_pillar_1 = create(:ascor_assessment_indicator, id: 1, code: 'EP', indicator_type: :pillar, | ||
text: 'Emissions Performance') | ||
_indicator_pillar_2 = create(:ascor_assessment_indicator, id: 2, code: 'CP', indicator_type: :pillar, | ||
text: 'Climate Performance') | ||
indicator_area_1 = create(:ascor_assessment_indicator, id: 3, code: 'EP.1', indicator_type: :area, | ||
text: 'Emissions Performance 1') | ||
indicator_area_2 = create(:ascor_assessment_indicator, id: 4, code: 'EP.2', indicator_type: :area, | ||
text: 'Emissions Performance 2') | ||
indicator_area_3 = create(:ascor_assessment_indicator, id: 5, code: 'CP.1', indicator_type: :area, | ||
text: 'Climate Performance 1') | ||
|
||
create :ascor_assessment, country: usa, assessment_date: Date.new(2019, 1, 1), results: [ | ||
build(:ascor_assessment_result, indicator: indicator_area_1, answer: 'Yes'), | ||
build(:ascor_assessment_result, indicator: indicator_area_2, answer: 'No'), | ||
build(:ascor_assessment_result, indicator: indicator_area_3, answer: 'Yes') | ||
] | ||
create :ascor_assessment, country: usa, assessment_date: Date.new(2019, 2, 1), results: [ | ||
build(:ascor_assessment_result, indicator: indicator_area_1, answer: 'Yes'), | ||
build(:ascor_assessment_result, indicator: indicator_area_2, answer: 'No'), | ||
build(:ascor_assessment_result, indicator: indicator_area_3, answer: 'No') | ||
] | ||
create :ascor_assessment, country: japan, assessment_date: Date.new(2019, 2, 1), results: [ | ||
build(:ascor_assessment_result, indicator: indicator_area_1, answer: 'No'), | ||
build(:ascor_assessment_result, indicator: indicator_area_2, answer: 'Yes'), | ||
build(:ascor_assessment_result, indicator: indicator_area_3, answer: 'No') | ||
] | ||
|
||
create :ascor_pathway, country: usa, assessment_date: Date.new(2019, 1, 1), emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Production - excluding LULUCF', recent_emission_level: 100 | ||
create :ascor_pathway, country: usa, assessment_date: Date.new(2019, 2, 1), emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Production - excluding LULUCF', recent_emission_level: 200 | ||
create :ascor_pathway, country: japan, assessment_date: Date.new(2019, 2, 1), emissions_metric: 'Intensity per capita', | ||
emissions_boundary: 'Production - excluding LULUCF', recent_emission_level: 300 | ||
end | ||
|
||
let(:assessment_date) { Date.new(2019, 2, 1) } | ||
|
||
it 'returns the correct data' do | ||
expect(subject).to eq( | ||
[{ | ||
pillar: 'Emissions Performance', | ||
area: 'Emissions Performance 1', | ||
result: 'Yes', | ||
country_id: 1, | ||
country_name: 'USA', | ||
country_path: '/ascor/usa', | ||
market_cap_group: :small | ||
}, | ||
{ | ||
pillar: 'Emissions Performance', | ||
area: 'Emissions Performance 1', | ||
result: 'No', | ||
country_id: 2, | ||
country_name: 'Japan', | ||
country_path: '/ascor/japan', | ||
market_cap_group: :large | ||
}, | ||
{ | ||
pillar: 'Emissions Performance', | ||
area: 'Emissions Performance 2', | ||
result: 'No', | ||
country_id: 1, | ||
country_name: 'USA', | ||
country_path: '/ascor/usa', | ||
market_cap_group: :small | ||
}, | ||
{ | ||
pillar: 'Emissions Performance', | ||
area: 'Emissions Performance 2', | ||
result: 'Yes', | ||
country_id: 2, | ||
country_name: 'Japan', | ||
country_path: '/ascor/japan', | ||
market_cap_group: :large | ||
}, | ||
{ | ||
pillar: 'Climate Performance', | ||
area: 'Climate Performance 1', | ||
result: 'No', | ||
country_id: 1, | ||
country_name: 'USA', | ||
country_path: '/ascor/usa', | ||
market_cap_group: :small | ||
}, | ||
{ | ||
pillar: 'Climate Performance', | ||
area: 'Climate Performance 1', | ||
result: 'No', | ||
country_id: 2, | ||
country_name: 'Japan', | ||
country_path: '/ascor/japan', | ||
market_cap_group: :large | ||
}] | ||
) | ||
end | ||
end |