Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop' into staging
Browse files Browse the repository at this point in the history
* develop:
  Hide carbon cycle insight in production
  Text changes and improvements
  Adds default thresh to tabs and enables it on the javascript
  Small adjustments
  Update table name
  Fixes some queries due to changes on carto tables
  More fixes to display and indicators list
  Fixed bug with the total carbon stored widget: name of the country wasn't properly shown
  Fixed bug with the total carbon stored widget: name of the country wasn't properly shown
  Fixed download widget (query was buggy)
  Fixed the open data portal link in the widgets download modal
  Fix table name
  Update code and indicators ids following changes from David
  • Loading branch information
j8seangel committed Sep 24, 2018
2 parents 4efe5b1 + d21bc28 commit 4ea6a4c
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ define(
unit: t.switch ? t['switch'][0]['unit'] : null,
start_date: t.range ? t['range'][0] : null,
end_date: t.range ? t['range'][t['range'].length - 1] : null,
thresh: t.thresh ? this.status.get('globalThresh') : 0,
thresh: t.thresh ? this.status.get('globalThresh') : (t.default_thresh ? t.default_thresh : 0),
section: t.sectionswitch ? t['sectionswitch'][0]['unit'] : null,
template: t.template ? t['template'] : null
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ define(
var scientific = d3.format('.3s')(data.value);
var displayName = data.id_1
? data.sub_nat_name
: data.boundary_id != 1 ? data.boundary_name : data.country_name;
: data.boundary_name != 'administrative boundary'
? data.boundary_name
: data.country_name;
return {
location_name: displayName,
valueString: data.value > 1000 ? scientific : shortened,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ define(
var aux = values[i.id][0];
var displayName = aux.id_1
? aux.sub_nat_name
: aux.boundary_id != 1 ? aux.boundary_name : aux.country_name;
: aux.boundary_name != 'administrative boundary' ? aux.boundary_name : aux.country_name;
i.location_name = displayName;
i.data = values[i.id];
return i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ define(
if (aux) {
displayName = aux.id_1
? aux.sub_nat_name // eslint-disable-line
: aux.boundary_id !== 1
: aux.boundary_name !== 'administrative boundary'
? aux.boundary_name
: aux.country_name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ define(

data['indicator_ids[]'] = model.indicators.join(',') || '';
return (
window.gfw.config.CLIMATE_API_HOST + '/api/downloads?' + $.param(data)
window.gfw.config.CLIMATE_API_HOST + '/downloads?' + $.param(data)
);
},

Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/widgets/presenters/TabPresenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ define(['mps', 'backbone', 'countries/presenters/PresenterClass'], function(
{
'Threshold/change': function(thresh) {
var tabs = _.clone(this.status.get('tabs'));
tabs.thresh = tabs.thresh ? thresh : 0;
tabs.thresh = tabs.thresh ? thresh : (tabs.default_thresh ? tabs.default_thresh : 0);
this.status.set('tabs', tabs);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ define(
unit: t.switch ? t['switch'][0]['unit'] : null,
start_date: t.range ? t['range'][0] : null,
end_date: t.range ? t['range'][t['range'].length - 1] : null,
thresh: t.thresh ? this.thresh : 0,
thresh: t.thresh ? this.thresh : (t.default_thresh ? t.default_thresh : 0),
section: t.sectionswitch ? t['sectionswitch'][0]['unit'] : null,
template: t.template ? t['template'] : null,
lock:
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/widgets/templates/tab.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
</div>
{{/if}}
{{#if this.note}}
{{#if this.default_thresh}}
<input type="hidden" value="{{this.default_thresh}}" class="threshold" />
{{/if}}
<div class="canopy-threshold">
<span>{{this.note}}</span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<div class="m-select -multi">
<span class="label">Data</span>
<div class="select-wrapper">
<select id="download-indicators" data-attribute="data" data-placeholder="Choose a indicator" class="chosen-select js-select" multiple style="width:100%;">
<select id="download-indicators" data-attribute="data" data-placeholder="Choose an indicator" class="chosen-select js-select" multiple style="width:100%;">
{{#each data}}
<option value="{{this.value}}" {{#if selected}}selected{{/if}}>{{this.label}}</option>
{{/each}}
Expand All @@ -38,7 +38,7 @@
<div class="m-select -multi">
<span class="label">Canopy density</span>
<div class="select-wrapper">
<select id="download-thresh" data-attribute="thresh" data-placeholder="Choose a thresholder" class="chosen-select js-select" multiple style="width:100%;">
<select id="download-thresh" data-attribute="thresh" data-placeholder="Choose a threshold" class="chosen-select js-select" multiple style="width:100%;">
{{#each treshold}}
<option value="{{this.value}}" {{#if selected}}selected{{/if}}>> {{this.label}}%</option>
{{/each}}
Expand All @@ -57,7 +57,7 @@
<span class="separator">to</span>
<div class="select-wrapper -inline">
<select id="download-end-date" data-attribute="end_date" class="chosen-select js-select">
{{#each start_date}}
{{#each end_date}}
<option value="{{this.value}}" {{#if selected}}selected{{/if}}>{{this.label}}</option>
{{/each}}
</select>
Expand All @@ -67,7 +67,7 @@
</div>
<div class="modal-footer">
<div class="m-btncontainer is-center">
<a href="http://opendata.arcgis.com/" target="_blank" class="">OPEN DATA PORTAL</a>
<a href="http://data.globalforestwatch.org/" target="_blank" class="">OPEN DATA PORTAL</a>
<button class="btn medium blue js-submit">Download</button>
</div>
</div>
Expand Down
10 changes: 7 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
class ApplicationController < ActionController::Base

helper_method :terms_cookie

protect_from_forgery with: :exception


def index
@is_production = Rails.env.production?
end

private
def terms_cookie
cookies.permanent[ENV['TERMS_COOKIE'].to_sym]
end

end
end
11 changes: 6 additions & 5 deletions app/models/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ def jurisdictions_for iso

def areas_of_interest_for iso
sql = <<-SQL
SELECT DISTINCT boundary_id AS id, boundary_name AS name, boundary_code AS code
SELECT DISTINCT b.cartodb_id AS id, boundary_name AS name,
b.boundary_code AS code
FROM #{CDB_INDICATORS_VALUES_TABLE} i
INNER JOIN #{CDB_BOUNDARIES_TABLE} b ON
i.boundary_id = b.cartodb_id
WHERE UPPER(iso) = UPPER('#{iso}') AND
boundary <> 'admin'
ORDER BY boundary_id
i.boundary_code = b.boundary_code
WHERE UPPER(iso_and_sub_nat) = UPPER('#{iso}') AND
b.boundary_code <> 'admin'
ORDER BY b.boundary_code
SQL

get(base_countries_path+sql)['rows']
Expand Down
27 changes: 13 additions & 14 deletions app/models/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def as_zip

def results_to_csv results
file_names = []
grouped = results.group_by{|t| t["indicator_name"]}
grouped = results.group_by{|t| t["indicator_id"]}
grouped.each do |id, data|
csv_file = Tempfile.new([id, ".csv"])
csv_file = Tempfile.new([id.to_s, ".csv"])
file_names << ["#{id}.csv", csv_file.path]
CSV.open(csv_file, "wb") do |csv|
csv << data.first.keys # adds the attributes name on the first line
Expand All @@ -70,18 +70,17 @@ def query_url

def select_query
<<-SQL
SELECT values.indicator_id, values.cartodb_id AS cartodb_id,
values.iso, values.sub_nat_id, values.boundary, values.boundary_id,
values.thresh, values.admin0_name AS country, values.year,
values.value, subnat.name_1 AS province,
boundaries.boundary_name, indicators.indicator_short AS indicator_name,
indicators.value_units AS units
FROM #{CDB_INDICATORS_VALUES_TABLE} AS values
INNER JOIN indicators ON values.indicator_id = indicators.indicator_id
LEFT JOIN #{CDB_SUBNAT_TABLE} AS subnat
SELECT values.indicator_id, values.cartodb_id AS cartodb_id, values.iso, values.sub_nat_id,
values.boundary_code AS boundary, values.boundary_code AS boundary_id,
values.thresh, values.country,
values.year, CAST(values.value AS double precision) AS value, subnat.name_1 AS province,
boundaries.boundary_name, indicators.units AS units
FROM gfw_climate_country_pages_indicator_values_2017_data_20180828 AS values
INNER JOIN gfw_climate_country_pages_indicator_info_2017_data AS indicators ON values.indicator_id = indicators.indicator_id
LEFT JOIN gadm28_adm1 AS subnat
ON values.sub_nat_id = subnat.id_1 AND values.iso = subnat.iso
LEFT JOIN #{CDB_BOUNDARIES_TABLE} AS boundaries
ON values.boundary_id = boundaries.cartodb_id
LEFT JOIN gfw_climate_country_pages_boundary_info_2017_data AS boundaries
ON values.boundary_code = boundaries.boundary_code
SQL
end

Expand All @@ -95,7 +94,7 @@ def where_clause
where += "AND thresh IN (#{@thresholds.join(",")})"
end

where += "AND values.boundary_id = #{@area.blank? ? ADMIN_BOUNDARY_ID : area}"
where += "AND values.boundary_code = #{@area.blank? ? "'#{ADMIN_BOUNDARY_ID}' " : area}"
where += "AND values.sub_nat_id #{@id_1.blank? ? 'IS NULL' : "= #{@id_1}"}"

if @start_year && @end_year
Expand Down
22 changes: 15 additions & 7 deletions app/models/indicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ def find_indicator(filter_params)

ids = "#{iso}_#{id_1}_#{area}"

puts url

timeouts do
item_caching(indicator_id, ids, nil, thresh_value) do
get(url)['rows'].blank? ? {} : get(url)['rows']
Expand All @@ -52,23 +54,29 @@ def index_query
def show_query(indicator_id, iso, id_1, area, thresh_value)
filter = <<-SQL
indicator_id = #{indicator_id}
AND (value IS NOT NULL OR text_value IS NOT NULL)
AND value IS NOT NULL
AND thresh = #{thresh_value}
SQL
filter += filter_location(iso, id_1, area) if iso.present?

sql = <<-SQL
SELECT indicator_id, values.cartodb_id AS cartodb_id,
values.iso, values.sub_nat_id, values.boundary, values.boundary_id,
values.thresh, values.the_geom, values.the_geom_webmercator,
values.admin0_name, values.year, values.value, values.text_value,
SELECT
indicator_id, values.cartodb_id AS cartodb_id,
values.iso, values.sub_nat_id,
values.boundary_code,
values.thresh, values.the_geom,
values.the_geom_webmercator,
values.country AS country_name,
values.country AS admin0_name,
values.year, CAST(values.value AS double precision) AS value,
values.value AS text_value,
subnat.name_1 AS sub_nat_name,
boundaries.boundary_name
FROM #{CDB_INDICATORS_VALUES_TABLE} AS values
LEFT JOIN #{CDB_SUBNAT_TABLE} AS subnat
ON values.sub_nat_id = subnat.id_1 AND values.iso = subnat.iso
LEFT JOIN #{CDB_BOUNDARIES_TABLE} AS boundaries
ON values.boundary_id = boundaries.cartodb_id
ON values.boundary_code = boundaries.boundary_code
WHERE #{filter}
SQL

Expand All @@ -79,7 +87,7 @@ def filter_location(iso, id_1, area)
<<-SQL
AND values.iso = UPPER('#{iso}')
AND values.sub_nat_id #{id_1.blank? ? 'IS NULL' : "= #{id_1}" }
AND values.boundary_id = #{area.blank? ? ADMIN_BOUNDARY_ID : area}
#{area.blank? ? "AND values.boundary_code = '#{ADMIN_BOUNDARY_ID}'" : "AND boundaries.cartodb_id = #{area}"}
SQL
end

Expand Down
1 change: 1 addition & 0 deletions app/views/api/v1/widgets/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def tabs
rangetype: tab['rangetype'],
switch: tab['switch'],
thresh: tab['thresh'],
default_thresh: tab['default_thresh'],
threshunit: tab['threshunit'],
cumulative: tab['cumulative'],
sectionswitch: tab['sectionswitch'],
Expand Down
25 changes: 13 additions & 12 deletions app/views/insights/index.slim
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@
.title Build your own insights with our custom dataset downloader
.link
a class="btn medium blue" href="./data-download" Explore
.insights.background
.inner
.carbon-cycle.-large
.item-2
.content
.title Carbon dioxide is a key greenhouse gas. Its build up in the Earth’s atmosphere depends on the Carbon Cycle
.link
a class="btn medium blue" href="./insights/carbon-cycle" How does it work?
.item-2
.thumb
a href="./insights/carbon-cycle"
img class="image -large" src="#{image_url('insights/carbon-cycle/carbon-cycle@2x.png')}"
- unless @is_production
.insights.background
.inner
.carbon-cycle.-large
.item-2
.content
.title Carbon dioxide is a key greenhouse gas. Its build up in the Earth’s atmosphere depends on the Carbon Cycle
.link
a class="btn medium blue" href="./insights/carbon-cycle" How does it work?
.item-2
.thumb
a href="./insights/carbon-cycle"
img class="image -large" src="#{image_url('insights/carbon-cycle/carbon-cycle@2x.png')}"
8 changes: 4 additions & 4 deletions config/initializers/cartodb_constants.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ADMIN_BOUNDARY_ID = 1
CDB_INDICATORS_VALUES_TABLE = "indicators_values"
CDB_INDICATORS_TABLE = "indicators"
CDB_BOUNDARIES_TABLE="boundaries_table"
ADMIN_BOUNDARY_ID = "admin"
CDB_INDICATORS_VALUES_TABLE = "gfw_climate_country_pages_indicator_values_2017_data_20180913"
CDB_INDICATORS_TABLE = "gfw_climate_country_pages_indicator_info_2017_data"
CDB_BOUNDARIES_TABLE="gfw_climate_country_pages_boundary_info_2017_data"
CDB_COUNTRIES_TABLE = "gadm28_countries"
CDB_SUBNAT_TABLE = "gadm28_adm1"

0 comments on commit 4ea6a4c

Please sign in to comment.