Skip to content

Commit

Permalink
feat(baseline-year): use startYear instead of baselineYear to be able…
Browse files Browse the repository at this point in the history
… to sync with the map
  • Loading branch information
willian-viana committed Nov 20, 2024
1 parent bd39ad6 commit cf79bf5
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { EuropeFAOCountries } from 'utils/fao-countries';

import getWidgetProps from './selectors';

const MIN_YEAR = 2000;

export default {
widget: 'treeCoverGainOutsidePlantations',
title: {
Expand Down Expand Up @@ -46,12 +48,13 @@ export default {
{
key: 'baselineYear',
label: 'Baseline Year',
type: 'select',
placeholder: '2000',
type: 'baseline-select',
startKey: 'startYear',
placeholder: MIN_YEAR,
clearable: true,
},
],
refetchKeys: ['forestType', 'landCategory', 'baselineYear'],
refetchKeys: ['forestType', 'landCategory', 'startYear'],
chartType: 'pieChart',
colors: 'gainWithinOutsidePlantations',
metaKey: 'widget_tree_cover_gain_outside_plantations',
Expand Down Expand Up @@ -90,7 +93,8 @@ export default {
},
settings: {
threshold: 0,
baselineYear: 2000,
startYear: MIN_YEAR,
endYear: 2020, // reference to display the correct data on the map
},
getData: (params) => {
return getTreeCoverGainByPlantationType(params).then((response) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ export const parseSentence = createSelector(
}
})();

const { baselineYear: dateFromDashboard, startYear: dateFromMapLayer } =
settings;

const params = {
location: locationName,
indicator: indicator && indicator.label,
startYear: settings.startYear,
endYear: settings.endYear,
baselineYear: settings?.baselineYear || 2000,
baselineYear: dateFromMapLayer || dateFromDashboard || 2000,
gainPercent: formatNumber({
num: (100 * data?.areaOutsidePlantations) / data?.totalArea,
unit: '%',
Expand Down
12 changes: 8 additions & 4 deletions components/widgets/forest-change/tree-cover-gain-simple/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {

import getWidgetProps from './selectors';

const MIN_YEAR = 2000;

export default {
widget: 'treeCoverGainSimple',
title: 'Tree cover gain in {location}',
Expand All @@ -24,7 +26,7 @@ export default {
metaKey: 'umd_tree_cover_gain_from_height',
dataType: 'gain',
pendingKeys: ['threshold'],
refetchKeys: ['threshold', 'baselineYear'],
refetchKeys: ['threshold', 'startYear'],
datasets: [
{
dataset: POLITICAL_BOUNDARIES_DATASET,
Expand All @@ -45,7 +47,8 @@ export default {
settings: {
threshold: 0,
extentYear: 2000,
baselineYear: 2000,
startYear: MIN_YEAR,
endYear: 2020, // reference to display the correct data on the map
},
chartType: 'listLegend',
colors: 'gain',
Expand All @@ -55,8 +58,9 @@ export default {
{
key: 'baselineYear',
label: 'Baseline Year',
type: 'select',
placeholder: '2000',
type: 'baseline-select',
startKey: 'startYear',
placeholder: MIN_YEAR,
clearable: true,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ export const parseSentence = createSelector(
(gain, extent, sentence, location, settings) => {
if (!gain && !extent) return null;
const gainPerc = (gain && extent && (gain / extent) * 100) || 0;
const { baselineYear: dateFromDashboard, startYear: dateFromMapLayer } =
settings;

const params = {
gain: formatNumber({ num: gain, unit: 'ha', spaceUnit: true }),
gainPercent: formatNumber({ num: gainPerc, unit: '%' }),
location,
baselineYear: (settings?.baselineYear) || 2000,
baselineYear: dateFromMapLayer || dateFromDashboard || 2000,
};

return {
Expand Down
11 changes: 2 additions & 9 deletions components/widgets/forest-change/tree-cover-gain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,11 @@ export default {
label: 'Baseline Year',
type: 'baseline-select',
startKey: 'startYear',
endKey: 2020,
placeholder: MIN_YEAR,
clearable: true,
},
],
refetchKeys: [
'forestType',
'landCategory',
'threshold',
'baselineYear',
'startYear',
],
refetchKeys: ['forestType', 'landCategory', 'threshold', 'startYear'],
chartType: 'rankedList',
colors: 'gain',
metaKey: 'umd_tree_cover_gain_from_height',
Expand Down Expand Up @@ -97,7 +90,7 @@ export default {
settings: {
threshold: 0,
startYear: MIN_YEAR,
endYear: 2020,
endYear: 2020, // reference to display the correct data on the map
unit: 'ha',
pageSize: 5,
page: 0,
Expand Down
12 changes: 8 additions & 4 deletions components/widgets/forest-change/tree-gain-located/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {

import getWidgetProps from './selectors';

const MIN_YEAR = 2000;

export default {
widget: 'treeGainLocated',
title: 'Location of tree cover gain in {location}',
Expand Down Expand Up @@ -40,12 +42,13 @@ export default {
{
key: 'baselineYear',
label: 'Baseline Year',
type: 'select',
placeholder: '2000',
type: 'baseline-select',
startKey: 'startYear',
placeholder: MIN_YEAR,
clearable: true,
},
],
refetchKeys: ['forestType', 'landCategory', 'baselineYear'],
refetchKeys: ['forestType', 'landCategory', 'startYear'],
chartType: 'rankedList',
colors: 'gain',
datasets: [
Expand Down Expand Up @@ -82,7 +85,8 @@ export default {
page: 0,
extentYear: 2000,
ifl: 2000,
baselineYear: 2000,
startYear: MIN_YEAR,
endYear: 2020, // reference to display the correct data on the map
},
getData: (params) =>
all([getExtentGrouped(params), getGainGrouped(params)]).then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ export const parseSentence = createSelector(
],
(data, sortedData, settings, indicator, locationName, sentences) => {
if (!data || !locationName) return null;

const { initial, withIndicator, initialPercent, withIndicatorPercent } =
sentences;
const totalGain = sumBy(data, 'gain') || 0;
const topRegion = (sortedData && sortedData.length && sortedData[0]) || {};
const avgGainPercentage = sumBy(data, 'percentage') || 0 / data.length;
const avgGain = (sumBy(data, 'gain') || 0) / data.length;
const { baselineYear: dateFromDashboard, startYear: dateFromMapLayer } =
settings;

let percentileGain = 0;
let percentileLength = 0;

Expand All @@ -86,7 +90,7 @@ export const parseSentence = createSelector(
const aveFormat = avgGain < 1 ? '.3r' : '.3s';

const params = {
baselineYear: (settings?.baselineYear) || 2000,
baselineYear: dateFromMapLayer || dateFromDashboard || 2000,
indicator: indicator && indicator.label,
location: locationName,
topGain: formatNumber({
Expand Down
9 changes: 4 additions & 5 deletions services/analysis-cached.js
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ export const getLossFiresGrouped = (params) => {
};

export const getTreeCoverGainByPlantationType = (params) => {
const { forestType, landCategory, ifl, download, baselineYear } = params;
const { forestType, landCategory, ifl, download, startYear } = params;

const requestUrl = getRequestUrl({
...params,
Expand All @@ -904,7 +904,7 @@ export const getTreeCoverGainByPlantationType = (params) => {

const url = encodeURI(
`${requestUrl}${sqlQuery}`
.replace('{baselineYear}', baselineYear || 2000)
.replace('{baselineYear}', startYear || 2000)
.replace('{WHERE}', getWHEREQuery({ ...params }))
);

Expand Down Expand Up @@ -1207,8 +1207,7 @@ export const getExtentGrouped = (params) => {

// summed gain for single location
export const getGain = (params) => {
const { forestType, landCategory, ifl, download, baselineYear } =
params || {};
const { forestType, landCategory, ifl, download, startYear } = params || {};

const requestUrl = getRequestUrl({
...params,
Expand All @@ -1227,7 +1226,7 @@ export const getGain = (params) => {
getLocationSelect({ ...params, cast: false })
)
.replace(/{location}/g, getLocationSelect({ ...params }))
.replace('{baselineYear}', baselineYear || 2000)
.replace('{baselineYear}', startYear || 2000)
.replace('{WHERE}', getWHEREQuery({ ...params, dataset: 'annual' }))
);

Expand Down

0 comments on commit cf79bf5

Please sign in to comment.