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

PROD Deploy 2024-06-28 03:52pm EST #4819

Merged
merged 2 commits into from
Jun 28, 2024
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
6 changes: 4 additions & 2 deletions components/widgets/forest-change/net-change/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,11 @@ export default {
};
});
},
getDataURL: (params) => {
getDataURL: async (params) => {
const globalLocation = getGlobalLocation(params);
return [getNetChange({ ...params, ...globalLocation, download: true })];
return [
await getNetChange({ ...params, ...globalLocation, download: true }),
];
},
getWidgetProps,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"proj4": "^2.5.0",
"promise.prototype.finally": "^3.1.2",
"prop-types": "^15.7.2",
"qs": "^6.12.1",
"query-string": "^6.11.1",
"rc-slider": "8.6.3",
"rc-tooltip": "3.7.0",
Expand Down
92 changes: 92 additions & 0 deletions pages/api/datamart/net-change/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { dataRequest } from 'utils/request';

// this PoC is only meant for net change widget (we chose net change for its simplicity)
export default async (req, res) => {
// example request:
// localhost:3000/api/datamart/net-change/?type=global&adm0=MEX&adm1=9&adm2=3&download=true
const { adm0 = '', adm1 = '', adm2 = '', download } = req.query;

const isDownload = download === 'true';

// checks
if (adm1 === undefined && adm2 !== undefined) {
return res.status(400).send({
message: 'if adm2 is present, adm1 can not be empty',
});
}

if (!adm0 && (adm1 || adm2)) {
return res.status(400).send({
message: 'if adm1 or adm2 are present, adm0 can not be empty',
});
}

let url = '/dataset';
const fieldsList = [
'iso',
'stable',
'loss',
'gain',
'disturb',
'net',
'change',
'gfw_area__ha',
];
let where = ``;
/*
possible datasets based on the adm lvl:
umd_adm0_net_tree_cover_change_from_height --> for iso and adm0
umd_adm1_net_tree_cover_change_from_height --> for adm1
umd_adm2_net_tree_cover_change_from_height --> for adm2

1. if iso and adm1 and adm2 are not present, then its global, therefore use umd_adm0_net_tree_cover_change_from_height
2. if adm2 is present then use umd_adm2_net_tree_cover_change_from_height
3. if only iso and adm1 are present then use umd_adm1_net_tree_cover_change_from_height
"params": {
"iso": "MEX",
"adm1": "9",
"adm2": "3",
"download": "false"
}
*/
let dataset = 'umd_adm0_net_tree_cover_change_from_height';

if (adm0 !== '') {
where = `WHERE iso='${adm0}'`;
}

if (adm1 !== '' && adm2 === '') {
dataset = 'umd_adm1_net_tree_cover_change_from_height';
where = `${where} AND adm1='${adm1}'`;
fieldsList.push('adm1');
fieldsList.push('adm1_name');
}

if (adm1 !== '' && adm2 !== '') {
dataset = 'umd_adm2_net_tree_cover_change_from_height';
where = `${where} AND adm1='${adm1}' AND adm2='${adm2}'`;
fieldsList.push('adm1');
fieldsList.push('adm1_name');
fieldsList.push('adm2');
fieldsList.push('adm2_name');
}

url = `${url}/${dataset}/v202209/${
isDownload ? 'download/csv' : 'query'
}?sql=SELECT ${fieldsList} FROM data ${where}`;

if (isDownload) {
return res.status(200).json({
data: {
url,
},
});
}

// fetch
const response = await dataRequest.get(url);

return res.status(200).json({
data: response?.data,
});
};
56 changes: 28 additions & 28 deletions services/analysis-cached.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cartoRequest, dataRequest } from 'utils/request';
import qs from 'qs';
import { cartoRequest, dataMartRequest, dataRequest } from 'utils/request';
import { PROXIES } from 'utils/proxies';

import forestTypes from 'data/forest-types';
Expand Down Expand Up @@ -941,49 +942,48 @@ export const getTreeCoverByLandCoverClass = (params) => {
};

// Net Change
export const getNetChange = (params) => {
const { forestType, landCategory, ifl, download, adm1 } = params || {};
export const getNetChange = async (params) => {
const { forestType, landCategory, ifl, type, adm0, adm1, adm2, download } =
params || {};

const requestUrl = getRequestUrl({
...params,
dataset: 'net_change',
datasetType: 'umd',
version: 'v202209',
});
const requestParams = qs.stringify(
{
type,
adm0,
adm1,
adm2,
download,
},
{ arrayFormat: 'comma' }
);

if (!requestUrl) {
return new Promise(() => {});
}
const url = `/net-change/?${requestParams}`;

const sqlQuery = adm1 ? SQL_QUERIES.netChange : SQL_QUERIES.netChangeIso;
/**
* localhost:3000/api/datamart/net-change/?
* &iso=MEX
* &adm1=9
* &adm2=3
* &download=true
*/

const url = encodeURI(
`${requestUrl}${sqlQuery}`
.replace(
/{select_location}/g,
getLocationSelect({ ...params, dataset: 'net_change', cast: false })
)
.replace('{WHERE}', getWHEREQuery({ ...params, dataset: 'net_change' }))
);
const response = await dataMartRequest.get(url);

if (download) {
const indicator = getIndicator(forestType, landCategory, ifl);
return {
name: `net_tree_cover_change_from_height${
indicator ? `_in_${snakeCase(indicator.label)}` : ''
}__ha`,
url: getDownloadUrl(url),
url: `${window.location.origin}${PROXIES.DATA_API}${response.data?.url}`,
};
}

return dataRequest.get(url).then((response) => ({
...response,
return {
data: {
data: response?.data?.map((d) => ({
...d,
})),
data: response.data,
},
}));
};
};

// summed extent for single location
Expand Down
1 change: 1 addition & 0 deletions utils/proxies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const PROXIES = {
GFW_API: '/api/gfw',
DATA_API: '/api/data',
DATAMART_API: '/api/datamart',
METADATA_API: '/api/metadata',
RESOURCE_WATCH_API: '/api/gfw/v1',
PLANET_API: '/api/planet-mosaics',
Expand Down
14 changes: 14 additions & 0 deletions utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ export const dataRequest = axios.create({
transformResponse: [(data) => JSON.parse(data)?.data],
});

export const dataMartRequest = axios.create({
...defaultRequestConfig,
...(isServer && {
baseURL: DATA_API_URL,
headers: {
'x-api-key': DATA_API_KEY,
},
}),
...(!isServer && {
baseURL: PROXIES.DATAMART_API,
}),
transformResponse: [(data) => JSON.parse(data)?.data],
});

export const metadataRequest = axios.create({
...defaultRequestConfig,
...(isServer && {
Expand Down
Loading
Loading