Skip to content

Commit

Permalink
Merge pull request #430 from Vizzuality/feature/mq-beta-scores
Browse files Browse the repository at this point in the history
feat: MQ Beta Scores
  • Loading branch information
martintomas authored Aug 11, 2023
2 parents c6ed380 + 69247c4 commit a55b6f1
Show file tree
Hide file tree
Showing 52 changed files with 1,108 additions and 146 deletions.
3 changes: 2 additions & 1 deletion app/admin/companies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ def scoped_collection
:geography,
:sector,
:headquarters_geography,
:latest_mq_assessment,
:latest_mq_assessment_only_beta_methodologies,
:latest_mq_assessment_without_beta_methodologies,
*csv_includes
)
end
Expand Down
2 changes: 1 addition & 1 deletion app/assets/images/icons/trending-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/assets/images/icons/trending-up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/stylesheets/tpi.scss
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
@import "tpi/nested-dropdown";
@import "tpi/banking-question-legend";
@import "tpi/latest-information";
@import "tpi/mq_beta_scores";

@import "tpi/pages/*";
@import "tpi/shared/*";
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/tpi/_colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ $grey-blue: #CFD7ED;
$dark: #191919;
$black: #000;
$green: #00C170;
$aquamarine: #60EDE5;

$tpi-level0: $blue-light;
$tpi-level1: $blue;
$tpi-level2: $blue-ribbon;
$tpi-level3: $blue-dark;
$tpi-level4: $blue-darker;
$tpi-level5-background-color: $aquamarine;
$tpi-level5-color: $black;
9 changes: 9 additions & 0 deletions app/assets/stylesheets/tpi/_mixins.scss
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@
&.level4 {
background-color: $tpi-level4;
}

&.level5 {
background-color: $tpi-level5-background-color;
color: $tpi-level5-color;

.mq-level-trend__container {
background-color: $tpi-level5-background-color;
}
}
}
40 changes: 40 additions & 0 deletions app/assets/stylesheets/tpi/_mq_beta_scores.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
.mq-beta-scores {
display: flex;
flex-wrap: wrap;
flex-direction: row;

@include until($desktop) {
flex-direction: column;
}

&__text {
color: white;
line-height: 40px;
margin-right: 10px;
}

&__divider {
margin: 0 15px 0 5px;
border-right: 1px solid rgba(255, 255, 255, 0.5);

@include until($desktop) {
display: none;
}
}

&__beta-button:before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
margin-right: 5px;
display: inline-block;
background-color: $tpi-level5-background-color;
vertical-align: middle;
}

&__download-button {
flex-grow: 4;
text-align: right;
}
}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/tpi/charts/mq-sector-pie-chart.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@import '../variables';

$tpi-pie-chart-colors: #86A9F9 #5587F7 #2465F5 #0A4BDC #083AAB;
$tpi-pie-chart-colors: #86A9F9 #5587F7 #2465F5 #0A4BDC #083AAB #60EDE5;

.chart--mq-sector-pie-chart {
margin: 0 auto;
Expand Down
18 changes: 14 additions & 4 deletions app/assets/stylesheets/tpi/pages/company.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@

a {
color: white;
}

a + a {
margin-left: 20px;
line-height: 40px;
margin-right: 10px;
}

@include until($desktop) {
Expand Down Expand Up @@ -243,6 +241,10 @@

border-left: 2px solid $hawkes-blue;

&.level5 {
background-color: rgba($tpi-level5-background-color, 0.15) ;
}

&__header {
font-size: 16px;
font-weight: bold;
Expand Down Expand Up @@ -284,6 +286,10 @@
&--yes {
@include mq-level-backgrounds;

&.level5 {
background-color: $tpi-level4;
}

background-image: image-url('icons/check.svg');
}

Expand Down Expand Up @@ -480,6 +486,10 @@

&--yes {
@include mq-level-backgrounds;

&.level5 {
background-color: $tpi-level4;
}
}

&--not-applicable {
Expand Down
6 changes: 2 additions & 4 deletions app/assets/stylesheets/tpi/pages/sector.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ $tape-color: rgba(25,25,25,0.1);

a {
color: white;
}

a + a {
margin-left: 20px;
line-height: 40px;
margin-right: 10px;
}

@include until($desktop) {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/tpi/pages/sectors-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
a {
color: white;
line-height: 40px;
margin-right: 20px;
margin-right: 10px;
}

@include until($desktop) {
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/concerns/tpi/user_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ def send_tpi_user_file(mq_assessments:, cp_assessments:, filename:)
.includes(sector: [:cp_units])

mq_assessments_files = mq_assessments_by_methodology.map do |methodology, assessments|
is_beta_methodology = MQ::Assessment::BETA_METHODOLOGIES.include? methodology
name = is_beta_methodology ? "#{methodology}_BETA_#{timestamp}" : "#{methodology}_#{timestamp}"
{
"MQ_Assessments_Methodology_#{methodology}_#{timestamp}.csv" => CSVExport::User::MQAssessments
.new(assessments).call
"MQ_Assessments_Methodology_#{name}.csv" => CSVExport::User::MQAssessments.new(assessments).call
}
end.reduce(&:merge)

timestamp = Time.now.strftime('%d%m%Y')

latest_cp_assessments_csv = CSVExport::User::CompanyLatestAssessments.new(mq_assessments, cp_assessments).call
latest_cp_assessments_beta_csv = CSVExport::User::CompanyLatestAssessments.new(
mq_assessments, cp_assessments, enable_beta_mq_assessments: true
).call
cp_assessments_csv = CSVExport::User::CompanyCPAssessments.new(cp_assessments).call
cp_assessments_regional_csv = CSVExport::User::CompanyCPAssessmentsRegional.new(cp_assessments).call
sector_benchmarks_csv = CSVExport::User::CPBenchmarks.new(cp_benchmarks).call
user_guide = File.binread(Rails.root.join('public', 'tpi', 'export_support', 'User guide TPI files.xlsx'))

render zip: (mq_assessments_files || {}).merge(
'Company_Latest_Assessments.csv' => latest_cp_assessments_csv,
'Company_Latest_Assessments_BETA_5.0.csv' => latest_cp_assessments_beta_csv,
"CP_Assessments_#{timestamp}.csv" => cp_assessments_csv,
"CP_Assessments_Regional_#{timestamp}.csv" => cp_assessments_regional_csv,
"Sector_Benchmarks_#{timestamp}.csv" => sector_benchmarks_csv,
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/tpi/companies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def cp_assessment; end
# Type: line chart
# On pages: :show
def assessments_levels_chart_data
data = ::Api::Charts::MQAssessment.new(@mq_assessment).assessments_levels_data
data = ::Api::Charts::MQAssessment.new(
@mq_assessment,
enable_beta_mq_assessments: @company.show_beta_mq_assessments
).assessments_levels_data

render json: data.chart_json
end
Expand All @@ -58,6 +61,7 @@ def user_download

def fetch_company
@company = TPI::CompanyDecorator.decorate(Company.published.friendly.find(params[:id]))
@company.show_beta_mq_assessments = session[:enable_beta_mq_assessments]
end

def redirect_if_numeric_or_historic_slug
Expand Down
12 changes: 12 additions & 0 deletions app/controllers/tpi/mq_assessments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
module TPI
class MQAssessmentsController < TPIController
def show; end

def enable_beta_data
session[:enable_beta_mq_assessments] = true

redirect_back fallback_location: tpi_root_url
end

def disable_beta_data
session[:enable_beta_mq_assessments] = false

redirect_back fallback_location: tpi_root_url
end
end
end
14 changes: 11 additions & 3 deletions app/controllers/tpi/sectors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ class SectorsController < TPIController
helper_method :any_cp_assessment?

def index
@companies_by_sectors = Rails.cache.fetch(TPICache::KEY, expires_in: TPICache::EXPIRES_IN) do
::Api::Charts::Sector.new(companies_scope(params)).companies_market_cap_by_sector
@companies_by_sectors = Rails.cache.fetch(
"#{TPICache::KEY}-mq-beta-scores-#{session[:enable_beta_mq_assessments]}",
expires_in: TPICache::EXPIRES_IN
) do
::Api::Charts::Sector.new(
companies_scope(params), enable_beta_mq_assessments: session[:enable_beta_mq_assessments]
).companies_market_cap_by_sector
end
@publications_and_articles = publications_and_articles
sectors_page = TPIPage.find_by(slug: 'publicly-listed-equities-content')
Expand Down Expand Up @@ -45,7 +50,10 @@ def show
# Type: pie chart
# On pages: :index, :show
def levels_chart_data
data = ::Api::Charts::Sector.new(companies_scope(params)).companies_count_by_level
data = ::Api::Charts::Sector.new(
companies_scope(params),
enable_beta_mq_assessments: session[:enable_beta_mq_assessments]
).companies_count_by_level

render json: data.chart_json
end
Expand Down
5 changes: 3 additions & 2 deletions app/decorators/company_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ def isin_as_tags
end

def mq_level_tag
return if model.latest_mq_assessment.nil?
mq_assessment = model.latest_mq_assessment_only_beta_methodologies || model.latest_mq_assessment_without_beta_methodologies
return if mq_assessment.nil?

model.latest_mq_assessment.decorate.level_tag
mq_assessment.decorate.level_tag
end

def preview_url
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/components/tpi/charts/bubble/Chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const LEVELS_COLORS = [
'#2465F5',
'#0A4BDC',
'#083AAB',
'#042b82'
'#60EDE5'
];

const LEVELS_SUBTITLES = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const LEVELS_SUBTITLES = {
1: 'Awareness',
2: 'Building capacity',
3: 'Integrating into operational decision making',
4: 'Strategic assessment'
4: 'Strategic assessment',
5: 'Transition planning and implementation'
};

const blueDarkColor = '#2E3152';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function getOptions({ chartData }) {
styledMode: true,
type: 'pie'
},
colors: ['#86A9F9', '#5587F7', '#2465F5', '#0A4BDC', '#083AAB'],
colors: ['#86A9F9', '#5587F7', '#2465F5', '#0A4BDC', '#083AAB', '#60EDE5'],
tooltip: {
enabled: false
},
Expand Down
14 changes: 14 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Company < ApplicationRecord
include TPICache
extend FriendlyId

attr_accessor :show_beta_mq_assessments

friendly_id :name, use: [:slugged, :history], routes: :default

MARKET_CAP_GROUPS = %w[small medium large unlisted].freeze
Expand All @@ -39,6 +41,12 @@ class Company < ApplicationRecord
belongs_to :headquarters_geography, class_name: 'Geography'

has_many :mq_assessments, class_name: 'MQ::Assessment', inverse_of: :company
has_one :latest_mq_assessment_without_beta_methodologies, -> {
currently_published.without_beta_methodologies.order(assessment_date: :desc)
}, class_name: 'MQ::Assessment'
has_one :latest_mq_assessment_only_beta_methodologies, -> {
currently_published.only_beta_methodologies.order(assessment_date: :desc)
}, class_name: 'MQ::Assessment'
has_one :latest_mq_assessment, -> { currently_published.order(assessment_date: :desc) }, class_name: 'MQ::Assessment'
has_many :cp_assessments, class_name: 'CP::Assessment', as: :cp_assessmentable
has_one :latest_cp_assessment, -> {
Expand All @@ -61,6 +69,12 @@ class Company < ApplicationRecord

scope :active, -> { where(active: true) }

def latest_mq_assessment
return latest_mq_assessment_without_beta_methodologies if show_beta_mq_assessments.blank?

latest_mq_assessment_only_beta_methodologies || latest_mq_assessment_without_beta_methodologies
end

def should_generate_new_friendly_id?
name_changed? || super
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/concerns/tpi_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ module TPICache

def clear_tpi_cache
Rails.cache.delete(KEY)
Rails.cache.delete("#{KEY}-mq-beta-scores-true")
Rails.cache.delete("#{KEY}-mq-beta-scores-false")
end
end
17 changes: 16 additions & 1 deletion app/models/mq/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,21 @@ class Assessment < ApplicationRecord
include DiscardableModel
include TPICache

LEVELS = %w[0 1 2 3 4 4STAR].freeze
LEVELS = %w[0 1 2 3 4 4STAR 5].freeze
BETA_METHODOLOGIES = [5].freeze # taken into account only when beta is enabled
BETA_LEVELS_PER_METHODOLOGY = { # which levels should be automatically added and marked as BETAs based on methodology
5 => %w[5]
}.freeze
BETA_LEVELS = BETA_LEVELS_PER_METHODOLOGY.values.flatten.freeze

belongs_to :company, inverse_of: :mq_assessments

scope :latest_first, -> { order(assessment_date: :desc) }
scope :all_publication_dates, -> { distinct.order(publication_date: :desc).pluck(:publication_date) }
scope :all_methodology_versions, -> { distinct.order(methodology_version: :asc).pluck(:methodology_version) }
scope :currently_published, -> { where('publication_date <= ?', DateTime.now) }
scope :without_beta_methodologies, -> { where.not(methodology_version: BETA_METHODOLOGIES) }
scope :only_beta_methodologies, -> { where(methodology_version: BETA_METHODOLOGIES) }

validates :level, inclusion: {in: LEVELS}
validates_presence_of :assessment_date, :publication_date, :level, :methodology_version
Expand Down Expand Up @@ -91,5 +98,13 @@ def find_answer_by_key(key)
def questions_by_level
questions.group_by(&:level)
end

def beta_methodology?
BETA_METHODOLOGIES.include? methodology_version
end

def beta_levels
Array.wrap BETA_LEVELS_PER_METHODOLOGY[methodology_version]
end
end
end
Loading

0 comments on commit a55b6f1

Please sign in to comment.