<commit_before>import { all, spread } from 'axios';

import { getExtent, getLoss } from 'services/analysis-cached';
import { getYearsRangeFromMinMax } from 'components/widgets/utils/data';

import {
  POLITICAL_BOUNDARIES_DATASET,
  FOREST_LOSS_DATASET
} from 'data/layers-datasets';
import {
  DISPUTED_POLITICAL_BOUNDARIES,
  POLITICAL_BOUNDARIES,
  FOREST_LOSS
} from 'data/layers';

import getWidgetProps from './selectors';

const MIN_YEAR = 2002;
const MAX_YEAR = 2019;

const getGlobalLocation = params => ({
  adm0: params.type === 'global' ? null : params.adm0,
  adm1: params.type === 'global' ? null : params.adm1,
  adm2: params.type === 'global' ? null : params.adm2
});

export default {
  widget: 'treeLossPct',
  title: {
    default: 'Primary Forest loss in {location}',
    global: 'Global Primary Forest loss'
  },
  categories: ['summary', 'forest-change'],
  types: ['global', 'country', 'geostore', 'wdpa', 'use'],
  admins: ['global', 'adm0', 'adm1', 'adm2'],
  large: true,
  visible: ['dashboard', 'analysis'],
  chartType: 'composedChart',
  colors: 'loss',
  settingsConfig: [
    {
      key: 'landCategory',
      label: 'Land Category',
      type: 'select',
      placeholder: 'All categories',
      clearable: true,
      border: true
    },
    {
      key: 'years',
      label: 'years',
      endKey: 'endYear',
      startKey: 'startYear',
      type: 'range-select',
      border: true
    },
    {
      key: 'threshold',
      label: 'canopy density',
      type: 'mini-select',
      metaKey: 'widget_canopy_density'
    }
  ],
  pendingKeys: ['threshold', 'years'],
  refetchKeys: ['landCategory', 'threshold'],
  dataType: 'lossPrimary',
  metaKey: 'widget_primary_forest_loss',
  datasets: [
    {
      dataset: POLITICAL_BOUNDARIES_DATASET,
      layers: [DISPUTED_POLITICAL_BOUNDARIES, POLITICAL_BOUNDARIES],
      boundary: true
    },
    // loss
    {
      dataset: FOREST_LOSS_DATASET,
      layers: [FOREST_LOSS]
    }
  ],
  sortOrder: {
    summary: -1,
    forestChange: -1
  },
  sentence: {
    initial:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b>, making up {percent} of its {total tree cover loss} in the same time period. <b>Total area of humid primary forest in {location} decreased by</b> {extentDelta} in this time period.',
    withIndicator:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b> in {indicator}, making up {percent} of its {total tree cover loss} in the same time period. <b>Total area of humid primary forest in {location} in {indicator} decreased by</b> {extentDelta} in this time period.',
    globalInitial:
      'From {startYear} to {endYear}, there was a total of {loss} <b>humid primary forest lost</b> {location}, making up {percent} of its {total tree cover loss} in the same time period. Total area of humid primary forest decreased {location} by</b> {extentDelta} in this time period.',
    globalWithIndicator:
      'From {startYear} to {endYear}, there was a total of {loss} <b>humid primary forest lost</b> {location} within {indicator}, making up {percent} of its {total tree cover loss} in the same time period. <b>Total area of humid primary forest in {indicator} decreased {location} by</b> {extentDelta} in this time period.',
    noLoss:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b>.',
    noLossWithIndicator:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b> in {indicator}.'
  },
  whitelists: {
    indicators: ['primary_forest'],
    checkStatus: true
  },
  settings: {
    threshold: 30,
    extentYear: 2000
  },
  getData: (params = {}) => {
    const { adm0, adm1, adm2, type } = params || {};
    const globalLocation = {
      adm0: type === 'global' ? null : adm0,
      adm1: type === 'global' ? null : adm1,
      adm2: type === 'global' ? null : adm2
    };

    return all([
      getLoss({
        ...params,
        forestType: null,
        landCategory: null,
        ...globalLocation
      }),
      getLoss({ ...params, ...globalLocation }),
      getLoss({ ...params, ...globalLocation, forestType: 'primary_forest' }),
      getExtent({
        ...params,
        ...globalLocation,
        forestType: 'primary_forest'
      })
    ]).then(
      spread((adminLoss, loss, primaryLoss, extent) => {
        let data = {};
        if (
          adminLoss &&
          adminLoss.data &&
          primaryLoss &&
          primaryLoss.data &&
          loss &&
          loss.data &&
          extent &&
          extent.data
        ) {
          data = {
            adminLoss: adminLoss.data.data,
            loss: loss.data.data,
            primaryLoss: primaryLoss.data.data,
            extent: (loss.data.data && extent.data.data[0].extent) || 0
          };
        }
        const { startYear, endYear, range } = getYearsRangeFromMinMax(
          MIN_YEAR,
          MAX_YEAR
        );
        return {
          ...data,
          settings: {
            startYear,
            endYear,
            yearsRange: range
          },
          options: {
            years: range
          }
        };
      })
    );
  },
  getDataURL: params => {
    const globalLocation = getGlobalLocation(params);
    return [
      getLoss({
        ...params,
        ...globalLocation,
        forestType: 'primary_forest',
        download: true
      }),
      getLoss({
        ...params,
        ...globalLocation,
        forestType: null,
        landCategory: null,
        download: true
      }),
      getExtent({ ...params, forestType: 'primary_forest', download: true })
    ];
  },
  getWidgetProps,
  parseInteraction: (payload = {}) => {
    const { year } = payload;

    return {
      updateLayer: true,
      startYear: parseInt(year, 10),
      endYear: parseInt(year, 10)
    };
  }
};
<commit_msg>Add final loss fetch<commit_after>import { all, spread } from 'axios';

import { getExtent, getLoss } from 'services/analysis-cached';
import { getYearsRangeFromMinMax } from 'components/widgets/utils/data';

import {
  POLITICAL_BOUNDARIES_DATASET,
  FOREST_LOSS_DATASET
} from 'data/layers-datasets';
import {
  DISPUTED_POLITICAL_BOUNDARIES,
  POLITICAL_BOUNDARIES,
  FOREST_LOSS
} from 'data/layers';

import getWidgetProps from './selectors';

const MIN_YEAR = 2002;
const MAX_YEAR = 2019;

const getGlobalLocation = params => ({
  adm0: params.type === 'global' ? null : params.adm0,
  adm1: params.type === 'global' ? null : params.adm1,
  adm2: params.type === 'global' ? null : params.adm2
});

export default {
  widget: 'treeLossPct',
  title: {
    default: 'Primary Forest loss in {location}',
    global: 'Global Primary Forest loss'
  },
  categories: ['summary', 'forest-change'],
  types: ['global', 'country', 'geostore', 'wdpa', 'use'],
  admins: ['global', 'adm0', 'adm1', 'adm2'],
  large: true,
  visible: ['dashboard', 'analysis'],
  chartType: 'composedChart',
  colors: 'loss',
  settingsConfig: [
    {
      key: 'landCategory',
      label: 'Land Category',
      type: 'select',
      placeholder: 'All categories',
      clearable: true,
      border: true
    },
    {
      key: 'years',
      label: 'years',
      endKey: 'endYear',
      startKey: 'startYear',
      type: 'range-select',
      border: true
    },
    {
      key: 'threshold',
      label: 'canopy density',
      type: 'mini-select',
      metaKey: 'widget_canopy_density'
    }
  ],
  pendingKeys: ['threshold', 'years'],
  refetchKeys: ['landCategory', 'threshold'],
  dataType: 'lossPrimary',
  metaKey: 'widget_primary_forest_loss',
  datasets: [
    {
      dataset: POLITICAL_BOUNDARIES_DATASET,
      layers: [DISPUTED_POLITICAL_BOUNDARIES, POLITICAL_BOUNDARIES],
      boundary: true
    },
    // loss
    {
      dataset: FOREST_LOSS_DATASET,
      layers: [FOREST_LOSS]
    }
  ],
  sortOrder: {
    summary: -1,
    forestChange: -1
  },
  sentence: {
    initial:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b>, making up {percent} of its {total tree cover loss} in the same time period. <b>Total area of humid primary forest in {location} decreased by</b> {extentDelta} in this time period.',
    withIndicator:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b> in {indicator}, making up {percent} of its {total tree cover loss} in the same time period. <b>Total area of humid primary forest in {location} in {indicator} decreased by</b> {extentDelta} in this time period.',
    globalInitial:
      'From {startYear} to {endYear}, there was a total of {loss} <b>humid primary forest lost</b> {location}, making up {percent} of its {total tree cover loss} in the same time period. Total area of humid primary forest decreased {location} by</b> {extentDelta} in this time period.',
    globalWithIndicator:
      'From {startYear} to {endYear}, there was a total of {loss} <b>humid primary forest lost</b> {location} within {indicator}, making up {percent} of its {total tree cover loss} in the same time period. <b>Total area of humid primary forest in {indicator} decreased {location} by</b> {extentDelta} in this time period.',
    noLoss:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b>.',
    noLossWithIndicator:
      'From {startYear} to {endYear}, <b>{location} lost {loss} of humid primary forest</b> in {indicator}.'
  },
  whitelists: {
    indicators: ['primary_forest'],
    checkStatus: true
  },
  settings: {
    threshold: 30,
    extentYear: 2000
  },
  getData: (params = {}) => {
    const { adm0, adm1, adm2, type } = params || {};
    const globalLocation = {
      adm0: type === 'global' ? null : adm0,
      adm1: type === 'global' ? null : adm1,
      adm2: type === 'global' ? null : adm2
    };

    return all([
      getLoss({
        ...params,
        forestType: null,
        landCategory: null,
        ...globalLocation
      }),
      getLoss({ ...params, ...globalLocation }),
      getLoss({ ...params, ...globalLocation, forestType: 'primary_forest' }),
      getExtent({
        ...params,
        ...globalLocation,
        forestType: 'primary_forest'
      })
    ]).then(
      spread((adminLoss, loss, primaryLoss, extent) => {
        let data = {};
        if (
          adminLoss &&
          adminLoss.data &&
          primaryLoss &&
          primaryLoss.data &&
          loss &&
          loss.data &&
          extent &&
          extent.data
        ) {
          data = {
            adminLoss: adminLoss.data.data,
            loss: loss.data.data,
            primaryLoss: primaryLoss.data.data,
            extent: (loss.data.data && extent.data.data[0].extent) || 0
          };
        }
        const { startYear, endYear, range } = getYearsRangeFromMinMax(
          MIN_YEAR,
          MAX_YEAR
        );
        return {
          ...data,
          settings: {
            startYear,
            endYear,
            yearsRange: range
          },
          options: {
            years: range
          }
        };
      })
    );
  },
  getDataURL: params => {
    const globalLocation = getGlobalLocation(params);
    return [
      getLoss({
        ...params,
        ...globalLocation,
        forestType: null,
        landCategory: null,
        download: true
      }),
      getLoss({
        ...params,
        ...globalLocation,
        download: true
      }),
      getLoss({
        ...params,
        ...globalLocation,
        forestType: 'primary_forest',
        download: true
      }),
      getExtent({ ...params, forestType: 'primary_forest', download: true })
    ];
  },
  getWidgetProps,
  parseInteraction: (payload = {}) => {
    const { year } = payload;

    return {
      updateLayer: true,
      startYear: parseInt(year, 10),
      endYear: parseInt(year, 10)
    };
  }
};

{'fraction_non_alphanumeric': 0.13621151271753681, 'fraction_numerical': 0.006693440428380187, 'mean_word_length': 2.4486439699942295, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '376498', 'n_tokens_mistral': 3783, 'n_tokens_neox': 3667, 'n_words': 1018}