From c381086fb720296dcafdd3b9886f402d1c24a0fc Mon Sep 17 00:00:00 2001 From: martintomas Date: Thu, 9 Nov 2023 10:04:55 +0100 Subject: [PATCH] refactoring: Adding new bubble sizes to ASCOR tool --- app/services/api/ascor/bubble_chart.rb | 23 ++++++++++++++------ spec/services/api/ascor/bubble_chart_spec.rb | 12 +++++----- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/app/services/api/ascor/bubble_chart.rb b/app/services/api/ascor/bubble_chart.rb index dfae7d345..1ac13ff9b 100644 --- a/app/services/api/ascor/bubble_chart.rb +++ b/app/services/api/ascor/bubble_chart.rb @@ -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' @@ -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 @@ -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 diff --git a/spec/services/api/ascor/bubble_chart_spec.rb b/spec/services/api/ascor/bubble_chart_spec.rb index 039a6adf1..ef437f3ec 100644 --- a/spec/services/api/ascor/bubble_chart_spec.rb +++ b/spec/services/api/ascor/bubble_chart_spec.rb @@ -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', @@ -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', @@ -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', @@ -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', @@ -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', @@ -98,7 +98,7 @@ country_id: 2, country_name: 'Japan', country_path: '/ascor/japan', - market_cap_group: :medium + market_cap_group: 3 }] ) end