Skip to content

Commit

Permalink
Merge pull request #80 from Vizzuality/feature/no-of-assessments-chart
Browse files Browse the repository at this point in the history
Feature/ 'assessments levels over the years' chart
  • Loading branch information
tsubik authored Oct 28, 2019
2 parents 8669a22 + 5ab995c commit 9645f24
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 26 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/tpi.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ container-columns {
flex-flow: row wrap;
justify-content: center;
}

.summary-box {
border-width: 1px;
border-style: dotted;
padding: 10px;
width: 400px;
}
27 changes: 23 additions & 4 deletions app/controllers/tpi/companies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
module TPI
class CompaniesController < ApplicationController
before_action :fetch_company

def show
@company = Company.find(params[:id])
@company_summary = company_presenter.summary
@company_mq_assessments = company_presenter.mq_assessments
end

# Data: Company MQ Assessments Levels over the years
# Section: MQ
# Type: line chart
# On pages: :show
def assessments_levels_chart_data
data = ::Api::Charts::Company.new(@company).assessments_levels_data

@company_details = ::Api::Presenters::Company.new(@company).company_details
render json: data.chart_json
end

# Data: Company emissions
# Section: CP
# Type: line chart
# On pages: :show
def emissions_chart_data
@company = Company.find(params[:id])

data = ::Api::Charts::Company.new(@company).emissions_data

render json: data.chart_json
end

private

def company_presenter
@company_presenter ||= ::Api::Presenters::Company.new(@company)
end

def fetch_company
@company = Company.friendly.find(params[:id])
end
end
end
8 changes: 4 additions & 4 deletions app/controllers/tpi/sectors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module TPI
class SectorsController < ApplicationController
def index
@sectors_names = TPISector.order(:name).pluck(:id, :name)
@companies_names = Company.limit(50).pluck(:id, :name)
@sectors = TPISector.select(:id, :name, :slug).order(:name)
@companies = Company.select(:id, :name, :slug).limit(50)
end

def show
@sector = TPISector.find(params[:id])
@sector = TPISector.friendly.find(params[:id])

@companies_by_levels = ::Api::Charts::Sector.new(companies_scope(params)).companies_summaries_by_level
end
Expand Down Expand Up @@ -47,7 +47,7 @@ def benchmarks_chart_data

def companies_scope(params)
if params[:id]
TPISector.find(params[:id]).companies
TPISector.friendly.find(params[:id]).companies
else
Company
end
Expand Down
31 changes: 30 additions & 1 deletion app/helpers/chart_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ def default_chart_options

def mq_sector_pie_chart_options
{
tooltip: {enabled: false},
tooltip: {
enabled: false
},
plotOptions: {
pie: {
dataLabels: {
Expand All @@ -31,4 +33,31 @@ def mq_sector_pie_chart_options
}
}
end

def mq_company_no_of_assessments_chart_options
{
tooltip: {
dateTimeLabelFormats: {
day: '%B %Y',
hour: '%B %Y',
minute: '%B %Y',
month: '%B %Y',
second: '%B %Y',
year: '%Y'
}
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: {
year: '%Y'
}
},
yAxis: {
tickInterval: 1,
title: {
text: 'Level'
}
}
}
end
end
4 changes: 4 additions & 0 deletions app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ def latest_mq_assessment
mq_assessments.order(:assessment_date).last
end

def oldest_mq_assessment
mq_assessments.order(:assessment_date).first
end

def latest_cp_assessment
cp_assessments.order(:assessment_date).last
end
Expand Down
1 change: 1 addition & 0 deletions app/models/mq/assessment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Assessment < ApplicationRecord

scope :latest_first, -> { order(assessment_date: :desc) }
scope :all_publication_dates, -> { distinct.order(publication_date: :desc).pluck(:publication_date) }
scope :by_assessment_year, ->(year) { where('extract(year from assessment_date) <= ?', year) }

validates :level, inclusion: {in: LEVELS}
validates_presence_of :assessment_date, :publication_date, :level
Expand Down
40 changes: 37 additions & 3 deletions app/services/api/charts/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,33 @@ def emissions_data
].flatten
end

# Returns MQ assessments Levels for each year for given Company.
#
# @return [Array<Array>]
# @example
# [ ['2016-03-01', 3], ['2017-01-01', 3], ['2018-08-20', 4]]
# #
def assessments_levels_data
return [] unless company_assessments.any?

# we are adding first and last point with nil value to have those ticks on the chart
# to fool Highcharts
first_point = [company_assessments.first.assessment_date.beginning_of_year.to_s, nil]
last_point = [Time.now.to_s, nil]

results = [first_point]
company_assessments.each do |a|
results << [a.assessment_date.to_s, a.level]
end
results << last_point
end

private

def company_assessments
@company.mq_assessments.order(:assessment_date)
end

def emissions_data_from_company
{
name: @company.name,
Expand Down Expand Up @@ -79,9 +104,7 @@ def sector_average_emission_for_year(year)

# @return [Array] of years for which emissions was reported
def years_with_reported_emissions
last_reported_year = Time.new.year

(sector_all_emission_years.min..last_reported_year).map.to_a
(sector_all_emission_years.min..sector_last_reported_year).map.to_a
end

# @return [Array] unique array of years as numbers
Expand All @@ -99,6 +122,17 @@ def sector_all_emissions
.includes(:cp_assessments)
.flat_map { |c| c.latest_cp_assessment.emissions }
end

# @return [Integer]
def sector_last_reported_year
# TODO: change to last reported year (to be done in https://www.pivotaltracker.com/story/show/168850542)
current_year
end

# @return [Integer]
def current_year
Time.new.year
end
end
end
end
11 changes: 8 additions & 3 deletions app/services/api/presenters/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ def initialize(company)
@company = company
end

def company_details
def summary
{
name: @company.name,
country: @company.geography.name,
sector: @company.sector.name,
market_cap: 'Large',
market_cap: @company.size.titlecase,
isin: @company.isin,
sedol: 60,
ca100: @company.ca100 ? 'Yes' : 'No',
ca100: @company.ca100 ? 'Yes' : 'No'
}
end

def mq_assessments
{
latest_assessment_questions: @company.latest_mq_assessment.questions_by_level,
levels_descriptions: SECTORS_LEVELS_DESC
}
Expand Down
25 changes: 16 additions & 9 deletions app/views/tpi/companies/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<hr>

<div class="company-performance-summary container">
<div class="mq">
<div class="mq summary-box">
<h3>Management Quality</h3>
<h5>Number of assessments: <%= @company.mq_assessments.size %></h5>
<div class="mq-status-summary">
Expand All @@ -13,7 +13,7 @@
</div>
</div>

<div class="cp">
<div class="cp summary-box">
<h3>Carbon Performance</h3>
<h5>Number of assessments: <%= @company.cp_assessments.size %></h5>
<div class="cp-status-summary">
Expand All @@ -25,7 +25,7 @@
<br><br>

<div class="company-info container">
<%= @company_details.slice(:name, :country, :sector, :market_cap, :isin, :sedol) %>
<%= @company_summary.slice(:name, :country, :sector, :market_cap, :isin, :sedol) %>
</div>

<hr>
Expand All @@ -34,22 +34,29 @@
<p>Assessment of <strong><%= @company.name %></strong> according to the management of their greenhouse gas emissions and of risks and opportunities related to the low-carbon transition.</p>

<div class="company-performance-details container">
<div class="mq-status">
<div class="mq-status summary-box">
<div>Current level</div>
<%= @company.mq_level %>
<%= @company.mq_status %>
<div>TODO: chart ..</div>
<div>[TODO] summary of answers</div>
</div>
<div class="cp-status">
<div>TODO: chart with assessments ..</div>
<div class="cp-status summary-box">
<div>Nr of assessments: <%= @company.mq_assessments.size %></div>
<div>
<%= line_chart(
assessments_levels_chart_data_tpi_company_path(@company.id),
library: mq_company_no_of_assessments_chart_options
) %>
</div>
</div>
</div>

<br><br>

<div class="company-assessments container-columns">
<%- @company_details[:latest_assessment_questions].each do |level, questions| %>
<%- @company_mq_assessments[:latest_assessment_questions].each do |level, questions| %>
<div class="assessment-level">
<h3>Level <%= level %>: <%= @company_details[:levels_descriptions][level.to_s] || 'Answers' %></h3>
<h3>Level <%= level %>: <%= @company_mq_assessments[:levels_descriptions][level.to_s] || 'Answers' %></h3>
<div class="assessment-details">
<%- questions.each do |question| %>
<div class="container">
Expand Down
4 changes: 2 additions & 2 deletions app/views/tpi/sectors/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<h1>Sectors dashboard</h1>

Sectors:
<%= @sectors_names.map {|s| link_to s.last, tpi_sector_path(s.first)}.join(', ').html_safe %>
<%= @sectors.map {|s| link_to s.name, tpi_sector_path(s.slug)}.join(', ').html_safe %>
<br>
<br>
Companies:
<%= @companies_names.map {|c| link_to c.last, tpi_company_path(c.first)}.join(', ').html_safe %>
<%= @companies.map {|c| link_to c.name, tpi_company_path(c.slug)}.join(', ').html_safe %>

<br><br><hr>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
resources :companies, only: [:show] do
member do
get :emissions_chart_data
get :assessments_levels_chart_data
end
end
end
Expand Down

0 comments on commit 9645f24

Please sign in to comment.