From 0e56986a9a8b6975a35b4d0e800635c02ef776ae Mon Sep 17 00:00:00 2001 From: Luis Zenteno Date: Sun, 5 Nov 2023 21:15:33 -0600 Subject: [PATCH 01/12] feat(fao-deforest): Update Widget data source and filter --- .../forest-change/fao-deforest/index.js | 14 ++++---- components/widgets/options.js | 2 ++ data/year-range.json | 14 ++++++++ services/forest-data.js | 33 ++++++++++++------- 4 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 data/year-range.json diff --git a/components/widgets/forest-change/fao-deforest/index.js b/components/widgets/forest-change/fao-deforest/index.js index 79d55717b3..291a0a34cd 100644 --- a/components/widgets/forest-change/fao-deforest/index.js +++ b/components/widgets/forest-change/fao-deforest/index.js @@ -16,9 +16,11 @@ export default { admins: ['global', 'adm0'], settingsConfig: [ { - key: 'period', + key: 'yearRange', label: 'period', type: 'select', + clearable: false, + border: true, }, ], chartType: 'rankedList', @@ -28,20 +30,20 @@ export default { sortOrder: { forestChange: 5, }, - refetchKeys: ['period'], + refetchKeys: ['yearRange'], sentences: { globalInitial: - 'According to the FAO, the {location} rate of deforestation in {year} was {rate} per year.', + 'According to the FAO, the {location} rate of deforestation in between {yearStart} and {yearEnd} was {rate} per year.', globalHuman: 'According to the FAO, the {location} rate of deforestation in {year} was {rate} per year, of which {human} per year was due to human activity.', initial: - 'According to the FAO, the rate of deforestation in {location} was {rate} per year in {year}.', + 'According to the FAO, the rate of deforestation in {location} was {rate} per year between {yearStart} and {yearEnd}.', humanDeforest: 'According to the FAO, the rate of deforestation in {location} was {rate} per year in {year}, of which {human} per year was due to human activity.', noDeforest: 'No deforestation data in {location}.', }, settings: { - period: 2010, + yearRange: '2015-2020', unit: 'ha/year', pageSize: 5, page: 0, @@ -49,7 +51,7 @@ export default { getData: (params) => all([getFAODeforest(params), getFAODeforestRank(params)]).then( spread((getFAODeforestResponse, getFAODeforestRankResponse) => { - const fao = getFAODeforestResponse.data.rows; + const fao = getFAODeforestResponse.data; const rank = getFAODeforestRankResponse.data.rows; return { fao, diff --git a/components/widgets/options.js b/components/widgets/options.js index e835b6701e..6031c8f521 100644 --- a/components/widgets/options.js +++ b/components/widgets/options.js @@ -19,6 +19,7 @@ import bioTypes from 'data/biodiversity-int.json'; import ifl from 'data/ifl.json'; import source from 'data/sources.json'; import faoYear from 'data/fao-cover-years.json'; +import yearRange from 'data/year-range.json'; export default { forestType: forestType.filter((f) => !f.hidden), @@ -27,6 +28,7 @@ export default { decile, firesThreshold, faoYear, + yearRange, unit, gasesIncluded, period, diff --git a/data/year-range.json b/data/year-range.json new file mode 100644 index 0000000000..84bcbbc469 --- /dev/null +++ b/data/year-range.json @@ -0,0 +1,14 @@ +[ + { + "label": "2000-2010", + "value": "2000-2010" + }, + { + "label": "2010-2015", + "value": "2010-2015" + }, + { + "label": "2015-2020", + "value": "2015-2020" + } +] diff --git a/services/forest-data.js b/services/forest-data.js index 984f695668..3b35d45243 100644 --- a/services/forest-data.js +++ b/services/forest-data.js @@ -11,7 +11,8 @@ const NEW_SQL_QUERIES = { faoReforest: 'SELECT country AS iso, name, year, reforest * 1000 AS reforestation__rate, forest*1000 AS fao_treecover_reforest__ha FROM table_1_forest_area_and_characteristics as fao WHERE fao.year = {period} AND reforest > 0 ORDER BY reforestation__rate DESC', faoDeforest: - 'SELECT fao.country as iso, fao.name, fao.deforest * 1000 AS fao_treecover_deforest__ha, fao.humdef, fao.year FROM table_1_forest_area_and_characteristics as fao {location}', + 'SELECT iso, country as name, "deforestation (ha per year)" as fao_treecover_deforest__ha, "reforestation (ha per year)" as fao_reforestation__ha, "forest expansion (ha per year)" as fao_expansion__ha, year FROM data where year = {yearRange}', + // TODO: update this rank query to be supported by new data-api endpoint: https://data-api.globalforestwatch.org/dataset/fao_forest_change/v2020/query/ 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 as fao_treecover_deforest__ha from rank', faoEcoLive: @@ -113,30 +114,40 @@ export const getFAOReforest = ({ period, download }) => { })); }; -export const getFAODeforest = ({ adm0, download }) => { - const url = `/sql?q=${NEW_SQL_QUERIES.faoDeforest}`.replace( - '{location}', - adm0 ? `WHERE fao.country = '${adm0}'` : '' - ); +export const getFAODeforest = async ({ + adm0, + yearRange = '2015-2020', + download, +}) => { + const target = download ? 'download/csv' : 'query/json'; + const url = + `/dataset/fao_forest_change/v2020/${target}?sql=${NEW_SQL_QUERIES.faoDeforest}` + .replace(/{yearRange}/g, `'${yearRange}'`) + .replace(/{location}/g, adm0 ? `AND iso = '${adm0}'` : ''); if (download) { return { name: 'fao_treecover_deforestation__ha', - url: `${CARTO_API}${url}&format=csv`, + url: new URL( + `${window.location.origin}${PROXIES.DATA_API}${url}` + ).toString(), }; } - return cartoRequest.get(url).then((response) => ({ - ...response, + const response = await dataRequest.get(url); + + const widgetData = { data: { - rows: response.data.rows.map((o) => { + rows: response.data.map((o) => { delete Object.assign(o, { country: o.iso }).iso; delete Object.assign(o, { deforest: o.fao_treecover_deforest__ha }) .fao_treecover_deforest__ha; return o; }), }, - })); + }; + + return widgetData; }; export const getFAODeforestRank = ({ period, download }) => { From 48f5b5145e48083fe681b0e87e914e8ec0e70856 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Tue, 28 Nov 2023 14:27:33 -0300 Subject: [PATCH 02/12] feat(fao-deforestation): migrate fao deforestation query from Carto to Data API --- .../forest-change/fao-deforest/index.js | 4 +-- .../forest-change/fao-deforest/selectors.js | 35 ++++++++++--------- services/forest-data.js | 19 +++++----- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/index.js b/components/widgets/forest-change/fao-deforest/index.js index 291a0a34cd..4b8766888d 100644 --- a/components/widgets/forest-change/fao-deforest/index.js +++ b/components/widgets/forest-change/fao-deforest/index.js @@ -33,11 +33,11 @@ export default { refetchKeys: ['yearRange'], sentences: { globalInitial: - 'According to the FAO, the {location} rate of deforestation in between {yearStart} and {yearEnd} was {rate} per year.', + 'According to the FAO, the {location} rate of deforestation in between {yearRange} was {rate} per year.', globalHuman: 'According to the FAO, the {location} rate of deforestation in {year} was {rate} per year, of which {human} per year was due to human activity.', initial: - 'According to the FAO, the rate of deforestation in {location} was {rate} per year between {yearStart} and {yearEnd}.', + 'According to the FAO, the rate of deforestation in {location} was {rate} per year between {yearRange}.', humanDeforest: 'According to the FAO, the rate of deforestation in {location} was {rate} per year in {year}, of which {human} per year was due to human activity.', noDeforest: 'No deforestation data in {location}.', diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index 23568652ea..c67a71296d 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -8,7 +8,6 @@ const getAdm0 = (state) => state.adm0; const getLocationName = (state) => state.locationLabel; const getColors = (state) => state.colors; const getSettings = (state) => state.settings; -const getPeriod = (state) => state.settings.period; const getSentences = (state) => state.sentences; const getTitle = (state) => state.title; @@ -17,44 +16,47 @@ export const parseData = createSelector( (data, adm0, colors) => { if (!data || !data.rank) return null; const { rank } = data; - let dataTrimmed = rank; + + let dataTrimmed = []; if (adm0) { const locationIndex = findIndex(rank, (d) => d.iso === adm0); let trimStart = locationIndex - 2; let trimEnd = locationIndex + 3; + if (locationIndex < 2) { trimStart = 0; trimEnd = 5; } + if (locationIndex > rank.length - 3) { trimStart = rank.length - 5; trimEnd = rank.length; } + dataTrimmed = rank.slice(trimStart, trimEnd); } return dataTrimmed.map((d) => ({ ...d, - label: d.name, + label: d.country, color: colors.main, - value: d.deforest, + value: d.def_per_year, })); } ); export const parseSentence = createSelector( - [getData, getLocationName, getSettings, getPeriod, getSentences], - (data, currentLabel, settings, period, sentences) => { + [getData, getLocationName, getSettings, getSentences], + (data, currentLabel, settings, sentences) => { if (!data || !data.fao) return null; - const { - initial, - noDeforest, - humanDeforest, - globalInitial, - globalHuman, - } = sentences; - const topFao = data.fao.filter((d) => d.year === settings.period); - const { deforest, humdef } = topFao[0] || {}; + const { initial, noDeforest, humanDeforest, globalInitial, globalHuman } = + sentences; + + const topFAOByDeforestation = data.fao.rows + ?.filter((regionData) => regionData.year === settings.yearRange) + .sort((a, b) => Number(b.deforest) - Number(a.deforest)); + + const { deforest, humdef } = topFAOByDeforestation[0] || {}; const totalDeforest = sumBy(data.rank, 'deforest') || 0; const rate = currentLabel === 'global' ? totalDeforest : deforest; @@ -68,7 +70,8 @@ export const parseSentence = createSelector( const params = { location: currentLabel, - year: period, + year: settings.yearRange, + yearRange: settings.yearRange, rate: formatNumber({ num: rate, unit: 'ha', diff --git a/services/forest-data.js b/services/forest-data.js index 3b35d45243..9670d1dddb 100644 --- a/services/forest-data.js +++ b/services/forest-data.js @@ -12,9 +12,8 @@ const NEW_SQL_QUERIES = { 'SELECT country AS iso, name, year, reforest * 1000 AS reforestation__rate, forest*1000 AS fao_treecover_reforest__ha FROM table_1_forest_area_and_characteristics as fao WHERE fao.year = {period} AND reforest > 0 ORDER BY reforestation__rate DESC', faoDeforest: 'SELECT iso, country as name, "deforestation (ha per year)" as fao_treecover_deforest__ha, "reforestation (ha per year)" as fao_reforestation__ha, "forest expansion (ha per year)" as fao_expansion__ha, year FROM data where year = {yearRange}', - // TODO: update this rank query to be supported by new data-api endpoint: https://data-api.globalforestwatch.org/dataset/fao_forest_change/v2020/query/ 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 as fao_treecover_deforest__ha from rank', + 'SELECT iso, country, "deforestation (ha per year)" as def_per_year FROM mytable WHERE "deforestation (ha per year)" IS NOT NULL AND year = {yearRange} ORDER BY def_per_year DESC', faoEcoLive: 'SELECT fao.country as iso, fao.forempl as total_forest_employees, fao.femempl as female_forest_employees, fao.usdrev as revenue__usd, fao.usdexp as expenditure__usd, fao.gdpusd2012 as gdp_2012__usd, fao.totpop1000, fao.year FROM table_7_economics_livelihood as fao WHERE fao.year = 2000 or fao.year = 2005 or fao.year = 2010 or fao.year = 9999', globalLandCover: 'SELECT * FROM global_land_cover_adm2 WHERE {location}', @@ -150,11 +149,13 @@ export const getFAODeforest = async ({ return widgetData; }; -export const getFAODeforestRank = ({ period, download }) => { - const url = `/sql?q=${NEW_SQL_QUERIES.faoDeforestRank}`.replace( - '{year}', - period - ); +export const getFAODeforestRank = ({ yearRange = '2015-2020', download }) => { + const target = download ? 'download/csv' : 'query/json'; + const url = + `/dataset/fao_forest_change/v2020/${target}?sql=${NEW_SQL_QUERIES.faoDeforestRank}`.replace( + /{yearRange}/g, + `'${yearRange}'` + ); if (download) { return { @@ -163,10 +164,10 @@ export const getFAODeforestRank = ({ period, download }) => { }; } - return cartoRequest.get(url).then((response) => ({ + return dataRequest.get(url).then((response) => ({ ...response, data: { - rows: response.data.rows.map((o) => { + rows: response.data.map((o) => { delete Object.assign(o, { deforest: o.fao_treecover_deforest__ha }) .fao_treecover_deforest__ha; return o; From f6d7934466b40ff29c62a8a261e4e294ca6a7610 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Wed, 29 Nov 2023 12:46:34 -0300 Subject: [PATCH 03/12] feat(fao-deforestation): removing human deforestation parameter from the widget logic --- .../forest-change/fao-deforest/index.js | 4 --- .../forest-change/fao-deforest/selectors.js | 25 ++++++++----------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/index.js b/components/widgets/forest-change/fao-deforest/index.js index 4b8766888d..6cc1be6b6f 100644 --- a/components/widgets/forest-change/fao-deforest/index.js +++ b/components/widgets/forest-change/fao-deforest/index.js @@ -34,12 +34,8 @@ export default { sentences: { globalInitial: 'According to the FAO, the {location} rate of deforestation in between {yearRange} was {rate} per year.', - globalHuman: - 'According to the FAO, the {location} rate of deforestation in {year} was {rate} per year, of which {human} per year was due to human activity.', initial: 'According to the FAO, the rate of deforestation in {location} was {rate} per year between {yearRange}.', - humanDeforest: - 'According to the FAO, the rate of deforestation in {location} was {rate} per year in {year}, of which {human} per year was due to human activity.', noDeforest: 'No deforestation data in {location}.', }, settings: { diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index c67a71296d..8e588bd5c6 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -49,24 +49,27 @@ export const parseSentence = createSelector( [getData, getLocationName, getSettings, getSentences], (data, currentLabel, settings, sentences) => { if (!data || !data.fao) return null; - const { initial, noDeforest, humanDeforest, globalInitial, globalHuman } = - sentences; + + const { initial, noDeforest, globalInitial } = sentences; const topFAOByDeforestation = data.fao.rows ?.filter((regionData) => regionData.year === settings.yearRange) .sort((a, b) => Number(b.deforest) - Number(a.deforest)); - const { deforest, humdef } = topFAOByDeforestation[0] || {}; + const { deforest } = topFAOByDeforestation[0] || {}; const totalDeforest = sumBy(data.rank, 'deforest') || 0; const rate = currentLabel === 'global' ? totalDeforest : deforest; + const rateFormat = rate < 1 ? '.3r' : '.3s'; + + let sentence = initial; - let sentence = humdef ? humanDeforest : initial; if (currentLabel === 'global') { - sentence = humdef ? globalHuman : globalInitial; - } else if (!deforest) sentence = noDeforest; + sentence = globalInitial; + } - const rateFormat = rate < 1 ? '.3r' : '.3s'; - const humanFormat = humdef < 1 ? '.3r' : '.3s'; + if (!deforest) { + sentence = noDeforest; + } const params = { location: currentLabel, @@ -78,12 +81,6 @@ export const parseSentence = createSelector( spaceUnit: true, specialSpecifier: rateFormat, }), - human: formatNumber({ - num: humdef, - unit: 'ha', - spaceUnit: true, - specialSpecifier: humanFormat, - }), }; return { From 52e5360908e3e30bd6c7d2f8263d0ec421ff7997 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Wed, 29 Nov 2023 12:56:07 -0300 Subject: [PATCH 04/12] feat(fao-deforestation): format yearRange to fit with the acceptance criteria --- components/widgets/forest-change/fao-deforest/index.js | 4 ++-- .../widgets/forest-change/fao-deforest/selectors.js | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/index.js b/components/widgets/forest-change/fao-deforest/index.js index 6cc1be6b6f..75a1159c37 100644 --- a/components/widgets/forest-change/fao-deforest/index.js +++ b/components/widgets/forest-change/fao-deforest/index.js @@ -33,9 +33,9 @@ export default { refetchKeys: ['yearRange'], sentences: { globalInitial: - 'According to the FAO, the {location} rate of deforestation in between {yearRange} was {rate} per year.', + 'According to the FAO, the {location} rate of deforestation in between {startYearRange} and {endYearRange} was {rate} per year.', initial: - 'According to the FAO, the rate of deforestation in {location} was {rate} per year between {yearRange}.', + 'According to the FAO, the rate of deforestation in {location} was {rate} per year between {startYearRange} and {endYearRange}.', noDeforest: 'No deforestation data in {location}.', }, settings: { diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index 8e588bd5c6..ef1ac90626 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -51,11 +51,14 @@ export const parseSentence = createSelector( if (!data || !data.fao) return null; const { initial, noDeforest, globalInitial } = sentences; - const topFAOByDeforestation = data.fao.rows ?.filter((regionData) => regionData.year === settings.yearRange) .sort((a, b) => Number(b.deforest) - Number(a.deforest)); + const yearRangeSeparated = settings.yearRange.split('-'); + const startYearRange = yearRangeSeparated[0]; + const endYearRange = yearRangeSeparated[1]; + const { deforest } = topFAOByDeforestation[0] || {}; const totalDeforest = sumBy(data.rank, 'deforest') || 0; const rate = currentLabel === 'global' ? totalDeforest : deforest; @@ -74,7 +77,8 @@ export const parseSentence = createSelector( const params = { location: currentLabel, year: settings.yearRange, - yearRange: settings.yearRange, + startYearRange, + endYearRange, rate: formatNumber({ num: rate, unit: 'ha', From f76d0da93322a372e6de4192c55d712167d796a0 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Wed, 29 Nov 2023 17:31:57 -0300 Subject: [PATCH 05/12] fix(fao-deforestation): set rank list when location is global --- components/widgets/forest-change/fao-deforest/selectors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index ef1ac90626..657d3292b5 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -17,7 +17,7 @@ export const parseData = createSelector( if (!data || !data.rank) return null; const { rank } = data; - let dataTrimmed = []; + let dataTrimmed = rank; if (adm0) { const locationIndex = findIndex(rank, (d) => d.iso === adm0); let trimStart = locationIndex - 2; From b91b76cfd2e44443fc335d66d6cd9798d1816e8d Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Wed, 29 Nov 2023 18:31:53 -0300 Subject: [PATCH 06/12] fix(fao-deforestation): set right rank position to adm0 --- .../widgets/forest-change/fao-deforest/selectors.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index 657d3292b5..7afeaa06ed 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -18,8 +18,15 @@ export const parseData = createSelector( const { rank } = data; let dataTrimmed = rank; + + dataTrimmed = dataTrimmed.map((d, i) => ({ + ...d, + rank: i + 1, + })); + if (adm0) { const locationIndex = findIndex(rank, (d) => d.iso === adm0); + let trimStart = locationIndex - 2; let trimEnd = locationIndex + 3; @@ -33,7 +40,7 @@ export const parseData = createSelector( trimEnd = rank.length; } - dataTrimmed = rank.slice(trimStart, trimEnd); + dataTrimmed = dataTrimmed.slice(trimStart, trimEnd); } return dataTrimmed.map((d) => ({ @@ -60,7 +67,7 @@ export const parseSentence = createSelector( const endYearRange = yearRangeSeparated[1]; const { deforest } = topFAOByDeforestation[0] || {}; - const totalDeforest = sumBy(data.rank, 'deforest') || 0; + const totalDeforest = sumBy(data.rank, 'def_per_year') || 0; const rate = currentLabel === 'global' ? totalDeforest : deforest; const rateFormat = rate < 1 ? '.3r' : '.3s'; From a3a1a3037bdefb153267ed7ecb80b3b5dc4dc56f Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Wed, 29 Nov 2023 18:44:04 -0300 Subject: [PATCH 07/12] fix(fao-deforestation): fix request for download button --- services/forest-data.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/services/forest-data.js b/services/forest-data.js index 9670d1dddb..a76c42b32b 100644 --- a/services/forest-data.js +++ b/services/forest-data.js @@ -160,7 +160,9 @@ export const getFAODeforestRank = ({ yearRange = '2015-2020', download }) => { if (download) { return { name: 'fao_treecover_deforestation_rank', - url: `${CARTO_API}${url}&format=csv`, + url: new URL( + `${window.location.origin}${PROXIES.DATA_API}${url}` + ).toString(), }; } From d6d6b7535ed998855e8153bfd25a22818da94287 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Wed, 29 Nov 2023 18:46:49 -0300 Subject: [PATCH 08/12] fix(fao-deforestation): fix rank calculation for adm0 --- .../widgets/forest-change/fao-deforest/selectors.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index 7afeaa06ed..024a47525a 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -53,8 +53,8 @@ export const parseData = createSelector( ); export const parseSentence = createSelector( - [getData, getLocationName, getSettings, getSentences], - (data, currentLabel, settings, sentences) => { + [getData, getLocationName, getSettings, getSentences, getAdm0], + (data, currentLabel, settings, sentences, adm0) => { if (!data || !data.fao) return null; const { initial, noDeforest, globalInitial } = sentences; @@ -67,8 +67,12 @@ export const parseSentence = createSelector( const endYearRange = yearRangeSeparated[1]; const { deforest } = topFAOByDeforestation[0] || {}; - const totalDeforest = sumBy(data.rank, 'def_per_year') || 0; - const rate = currentLabel === 'global' ? totalDeforest : deforest; + const globalDeforestation = sumBy(data.rank, 'def_per_year') || 0; + const countryDeforestation = data.rank.filter( + (country) => country.iso === adm0 + )[0].def_per_year; + const rate = + currentLabel === 'global' ? globalDeforestation : countryDeforestation; const rateFormat = rate < 1 ? '.3r' : '.3s'; let sentence = initial; From a812b1428bc330a6f6c3ff060297bb0480a3e1f9 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Thu, 30 Nov 2023 12:09:22 -0300 Subject: [PATCH 09/12] fix(fao-deforestation): remove countryDeforestation from global location --- components/widgets/forest-change/fao-deforest/selectors.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index 024a47525a..72f2d8491d 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -70,9 +70,11 @@ export const parseSentence = createSelector( const globalDeforestation = sumBy(data.rank, 'def_per_year') || 0; const countryDeforestation = data.rank.filter( (country) => country.iso === adm0 - )[0].def_per_year; + )[0]; const rate = - currentLabel === 'global' ? globalDeforestation : countryDeforestation; + currentLabel === 'global' + ? globalDeforestation + : countryDeforestation?.def_per_year; const rateFormat = rate < 1 ? '.3r' : '.3s'; let sentence = initial; From 9f6daf70a0620f28ebf93459f1b2723a2b02b8f9 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Thu, 30 Nov 2023 14:32:28 -0300 Subject: [PATCH 10/12] chore(fao-deforestation): improve csv filename when try/catch got an error --- .../components/widget-download-button/component.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/widget/components/widget-header/components/widget-download-button/component.jsx b/components/widget/components/widget-header/components/widget-download-button/component.jsx index 1d6bc36ddc..68104d0895 100644 --- a/components/widget/components/widget-header/components/widget-download-button/component.jsx +++ b/components/widget/components/widget-header/components/widget-download-button/component.jsx @@ -207,7 +207,7 @@ class WidgetDownloadButton extends PureComponent { filename = filename.concat('.csv'); } } catch (error) { - filename = `file ${index + 1}.csv`; + filename = `error.csv`; } zip.file(filename, urlToPromise(url), { binary: true }); From 668bc46f855642490f0a52982f11815db25c35e7 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Thu, 30 Nov 2023 14:32:54 -0300 Subject: [PATCH 11/12] fix(fao-deforestation): add async to getDataURL --- components/widgets/forest-change/fao-deforest/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/index.js b/components/widgets/forest-change/fao-deforest/index.js index 75a1159c37..433bbcf9af 100644 --- a/components/widgets/forest-change/fao-deforest/index.js +++ b/components/widgets/forest-change/fao-deforest/index.js @@ -55,9 +55,9 @@ export default { }; }) ), - getDataURL: (params) => [ - getFAODeforest({ ...params, download: true }), - getFAODeforestRank({ ...params, download: true }), + getDataURL: async (params) => [ + await getFAODeforest({ ...params, download: true }), + await getFAODeforestRank({ ...params, download: true }), ], getWidgetProps, }; From a5f06ce6da0daa3ebc391b94cc37e830ed8dca03 Mon Sep 17 00:00:00 2001 From: Willian Viana Date: Thu, 30 Nov 2023 14:58:55 -0300 Subject: [PATCH 12/12] fix(fao-deforestation): validate no FAO deforestation in countries --- components/widgets/forest-change/fao-deforest/index.js | 2 +- components/widgets/forest-change/fao-deforest/selectors.js | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/components/widgets/forest-change/fao-deforest/index.js b/components/widgets/forest-change/fao-deforest/index.js index 433bbcf9af..94e31cb813 100644 --- a/components/widgets/forest-change/fao-deforest/index.js +++ b/components/widgets/forest-change/fao-deforest/index.js @@ -36,7 +36,7 @@ export default { 'According to the FAO, the {location} rate of deforestation in between {startYearRange} and {endYearRange} was {rate} per year.', initial: 'According to the FAO, the rate of deforestation in {location} was {rate} per year between {startYearRange} and {endYearRange}.', - noDeforest: 'No deforestation data in {location}.', + noDeforest: 'No FAO deforestation data in {location}.', }, settings: { yearRange: '2015-2020', diff --git a/components/widgets/forest-change/fao-deforest/selectors.js b/components/widgets/forest-change/fao-deforest/selectors.js index 72f2d8491d..31d99ac819 100644 --- a/components/widgets/forest-change/fao-deforest/selectors.js +++ b/components/widgets/forest-change/fao-deforest/selectors.js @@ -58,15 +58,10 @@ export const parseSentence = createSelector( if (!data || !data.fao) return null; const { initial, noDeforest, globalInitial } = sentences; - const topFAOByDeforestation = data.fao.rows - ?.filter((regionData) => regionData.year === settings.yearRange) - .sort((a, b) => Number(b.deforest) - Number(a.deforest)); - const yearRangeSeparated = settings.yearRange.split('-'); const startYearRange = yearRangeSeparated[0]; const endYearRange = yearRangeSeparated[1]; - const { deforest } = topFAOByDeforestation[0] || {}; const globalDeforestation = sumBy(data.rank, 'def_per_year') || 0; const countryDeforestation = data.rank.filter( (country) => country.iso === adm0 @@ -83,7 +78,7 @@ export const parseSentence = createSelector( sentence = globalInitial; } - if (!deforest) { + if (currentLabel !== 'global' && !countryDeforestation) { sentence = noDeforest; }