Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fao global #3400

Merged
merged 4 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class WidgetContainer extends PureComponent {
!isEqual(settings.threshold, this.props.settings.threshold) ||
!isEqual(settings.indicator, this.props.settings.indicator) ||
!isEqual(settings.extentYear, this.props.settings.extentYear) ||
!isEqual(settings.period, this.props.settings.period) ||
!isEqual(settings.type, this.props.settings.type))
) {
getWidgetData({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const initialState = {
config: {
size: 'small',
categories: ['forest-change'],
admins: ['country'],
admins: ['global', 'country'],
selectors: ['periods'],
type: 'fao',
metaKey: 'widget_deforestation_fao',
Expand All @@ -12,6 +12,10 @@ export const initialState = {
},
colors: 'loss',
sentences: {
globalInitial:
'According to the FAO, the {location} rate of deforestation in {year} was {rate}.',
globalHuman:
'According to the FAO, the {location} rate of deforestation in {year} was {rate}, of which {human} was due to human activity.',
initial:
'According to the FAO the rate of deforestation in {location} was {rate}, in {year}.',
humanDeforest:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createSelector } from 'reselect';
import findIndex from 'lodash/findIndex';
import sumBy from 'lodash/sumBy';
import { format } from 'd3-format';

const getData = state => state.data || null;
Expand Down Expand Up @@ -49,18 +50,29 @@ export const getSentence = createSelector(
],
(data, location, currentLabel, settings, period, sentences) => {
if (!data || !data.fao) return '';
const { initial, noDeforest, humanDeforest } = sentences;
const {
initial,
noDeforest,
humanDeforest,
globalInitial,
globalHuman
} = sentences;
const topFao = data.fao.filter(d => d.year === settings.period);
const { deforest, humdef } = topFao[0];
const totalDeforest = sumBy(data.rank, 'deforest');

let sentence = humdef ? humanDeforest : initial;
if (currentLabel === 'global') {
sentence = humdef ? globalHuman : globalInitial;
} else if (!deforest) sentence = noDeforest;

let sentence = noDeforest;
if (deforest) {
sentence = humdef ? humanDeforest : initial;
}
const params = {
location: currentLabel,
year: period && period.label,
rate: `${format('.3s')(deforest)}ha/yr`,
rate:
currentLabel === 'global'
? `${format('.3s')(totalDeforest)}ha/yr`
: `${format('.3s')(deforest)}ha/yr`,
human: `${format('.3s')(humdef)}ha/yr`
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { getFAOExtent } from 'services/forest-data';

export const getData = ({ params, dispatch, setWidgetData, widget, state }) => {
export const getData = ({ params, dispatch, setWidgetData, widget }) => {
getFAOExtent({ ...params })
.then(response => {
const data = response.data.rows;
const hasCountryData =
(data.length &&
data.find(d => d.iso === state().location.payload.country)) ||
null;
dispatch(setWidgetData({ data: hasCountryData ? data : {}, widget }));
const hasCountryData = (data.length && data.find(d => d.iso)) || null;
dispatch(
setWidgetData({
data: hasCountryData ? data : {},
widget
})
);
})
.catch(error => {
dispatch(setWidgetData({ widget, error: true }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const initialState = {
config: {
size: 'small',
categories: ['forest-change'],
admins: ['country'],
admins: ['global', 'country'],
selectors: ['periods'],
type: 'fao',
metaKey: 'widget_rate_of_restoration_fao',
Expand All @@ -12,9 +12,11 @@ export const initialState = {
},
colors: 'gain',
sentences: {
globalInitial:
'According to the FAO, the {location} rate of reforestation in {year} was {rate}.',
initial:
'According to the FAO the rate of deforestation in {location} was {rate}, in {year}',
noReforest: 'No deforestation data in {location}.'
'According to the FAO the rate of reforestation in {location} was {rate}, in {year}',
noReforest: 'No reforestation data in {location}.'
}
},
settings: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { format } from 'd3-format';
const getData = state => state.data || null;
const getLocation = state => state.payload || null;
const getColors = state => state.colors || null;
const getCurrentLocation = state => state.currentLabel || null;
const getSettings = state => state.settings || null;
const getPeriod = state => state.period || null;
const getSentences = state => state.config && state.config.sentences;
Expand Down Expand Up @@ -46,18 +47,37 @@ export const parseData = createSelector(
);

export const getSentence = createSelector(
[parseData, getLocation, getSettings, getPeriod, getSentences],
(data, location, settings, period, sentences) => {
[
getSortedData,
parseData,
getLocation,
getSettings,
getPeriod,
getSentences,
getCurrentLocation
],
(sortedData, data, location, settings, period, sentences, currentLabel) => {
if (!data || !data.length) return null;
const { initial, noReforest } = sentences;
const { initial, noReforest, globalInitial } = sentences;
const countryData = data.find(d => location.country === d.iso) || null;

const sentence = countryData.value > 0 ? initial : noReforest;
let globalRate = 0;
Object.keys(sortedData).forEach(k => {
globalRate += sortedData[k].rate;
});

let sentence = globalInitial;
if (currentLabel !== 'global') {
sentence = countryData.value > 0 ? initial : noReforest;
}

const params = {
location: countryData.label,
location: currentLabel,
year: period && period.label,
rate: `${format('.3s')(countryData.value)}ha/yr`
rate:
currentLabel === 'global'
? `${format('.3s')(globalRate)}ha/yr`
: `${format('.3s')(countryData.value)}ha/yr`
};

return {
Expand Down
6 changes: 3 additions & 3 deletions app/javascript/services/forest-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const SQL_QUERIES = {
faoExtent:
'SELECT country AS iso, name, year, reforest * 1000 AS rate, forest*1000 AS extent FROM table_1_forest_area_and_characteristics as fao WHERE fao.year = {period} AND reforest > 0 ORDER BY rate DESC',
faoDeforest:
"SELECT fao.country, fao.name, fao.deforest * 1000 AS deforest, fao.humdef, fao.year FROM table_1_forest_area_and_characteristics as fao WHERE fao.country = '{country}'",
'SELECT fao.country, fao.name, fao.deforest * 1000 AS deforest, fao.humdef, fao.year FROM table_1_forest_area_and_characteristics as fao {location}',
faoDeforestRank:
'WITH mytable AS (SELECT fao.country as iso, fao.name, fao.deforest * 1000 AS deforest, fao.humdef FROM table_1_forest_area_and_characteristics as fao WHERE fao.year = {year} AND deforest is not null), rank AS (SELECT deforest, iso, name from mytable ORDER BY mytable.deforest DESC) SELECT row_number() over () as rank, iso, name, deforest from rank',
faoEcoLive:
Expand Down Expand Up @@ -201,8 +201,8 @@ export const getFAOExtent = ({ period }) => {

export const getFAODeforest = ({ country }) => {
const url = `${CARTO_REQUEST_URL}${SQL_QUERIES.faoDeforest}`.replace(
'{country}',
country
'{location}',
country ? `WHERE fao.country = '${country}'` : ''
);
return axios.get(url);
};
Expand Down