Skip to content

Commit

Permalink
refactoring: Adding new bubble sizes to ASCOR tool
Browse files Browse the repository at this point in the history
  • Loading branch information
martintomas committed Nov 9, 2023
1 parent 6829793 commit c381086
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
23 changes: 16 additions & 7 deletions app/services/api/ascor/bubble_chart.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Api
module ASCOR
class BubbleChart
NUMBER_OF_MARKET_CAP_GROUPS = 4
MARKET_CAP_QUERY = {
emissions_metric: 'Intensity per capita',
emissions_boundary: 'Production - excluding LULUCF'
Expand Down Expand Up @@ -34,10 +35,10 @@ def call
private

def calculate_market_cap_group(country_id)
recent_emission_level = recent_emission_levels[country_id]&.first&.recent_emission_level
return :medium if recent_emission_level.blank?
recent_emission_level = recent_emission_levels[country_id]&.first&.recent_emission_level.to_f
return 1 if recent_emission_level.blank?

market_cap_groups.find { |range, _| range.include?(recent_emission_level) }&.last || :medium
market_cap_groups.find { |range, _| range.include?(recent_emission_level) }&.last || 1
end

def pillars
Expand All @@ -53,12 +54,20 @@ def recent_emission_levels
def market_cap_groups
@market_cap_groups ||= begin
values = ::ASCOR::Pathway.where(MARKET_CAP_QUERY).where(assessment_date: assessment_date)
.where.not(recent_emission_level: nil).pluck(:recent_emission_level).sort
{values.first..values[(values.size * 1.0 / 3).ceil - 1] => :small,
values[(values.size * 1.0 / 3).ceil - 1]..values[(values.size * 2.0 / 3).ceil - 1] => :medium,
values[(values.size * 2.0 / 3).ceil - 1]..values.last => :large}
.pluck(:recent_emission_level).map(&:to_f).compact.sort
NUMBER_OF_MARKET_CAP_GROUPS.times.each_with_object({}) do |i, result|
next if values.empty?

result[values[cap_index_for(i, values.size)]..values[cap_index_for(i + 1, values.size)]] = i + 1
end
end
end

def cap_index_for(index, size)
return 0 if index.zero?

(size * index / NUMBER_OF_MARKET_CAP_GROUPS.to_f).ceil - 1
end
end
end
end
12 changes: 6 additions & 6 deletions spec/services/api/ascor/bubble_chart_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
country_id: 1,
country_name: 'USA',
country_path: '/ascor/usa',
market_cap_group: :small
market_cap_group: 2
},
{
pillar: 'Emissions Performance',
Expand All @@ -62,7 +62,7 @@
country_id: 2,
country_name: 'Japan',
country_path: '/ascor/japan',
market_cap_group: :medium
market_cap_group: 3
},
{
pillar: 'Emissions Performance',
Expand All @@ -71,7 +71,7 @@
country_id: 1,
country_name: 'USA',
country_path: '/ascor/usa',
market_cap_group: :small
market_cap_group: 2
},
{
pillar: 'Emissions Performance',
Expand All @@ -80,7 +80,7 @@
country_id: 2,
country_name: 'Japan',
country_path: '/ascor/japan',
market_cap_group: :medium
market_cap_group: 3
},
{
pillar: 'Climate Performance',
Expand All @@ -89,7 +89,7 @@
country_id: 1,
country_name: 'USA',
country_path: '/ascor/usa',
market_cap_group: :small
market_cap_group: 2
},
{
pillar: 'Climate Performance',
Expand All @@ -98,7 +98,7 @@
country_id: 2,
country_name: 'Japan',
country_path: '/ascor/japan',
market_cap_group: :medium
market_cap_group: 3
}]
)
end
Expand Down

0 comments on commit c381086

Please sign in to comment.