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

FLAG-1263: Data mart api client #4966

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
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
38 changes: 35 additions & 3 deletions components/widgets/forest-change/tree-loss-drivers/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { all, spread } from 'axios';

Check failure on line 1 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'all' is defined but never used

Check failure on line 1 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'spread' is defined but never used
import { getYearsRangeFromMinMax } from 'components/widgets/utils/data';

Check failure on line 2 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'getYearsRangeFromMinMax' is defined but never used

import {
POLITICAL_BOUNDARIES_DATASET,
Expand All @@ -14,10 +14,12 @@
import treeLoss from 'components/widgets/forest-change/tree-loss';
import { getExtent, getLoss } from 'services/analysis-cached';

import { fetchDataMart } from 'services/datamart';
import getWidgetProps from './selectors';


const MIN_YEAR = 2001;

Check failure on line 21 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'MIN_YEAR' is assigned a value but never used
const MAX_YEAR = 2023;

Check failure on line 22 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'MAX_YEAR' is assigned a value but never used

export default {
...treeLoss,
Expand Down Expand Up @@ -121,8 +123,36 @@
whitelists: {
checkStatus: true,
},
getData: (params) =>
all([
getData: async (params) => {
const {
adm0,

Check failure on line 128 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'adm0' is assigned a value but never used
adm1,

Check failure on line 129 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'adm1' is assigned a value but never used
adm2,

Check failure on line 130 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'adm2' is assigned a value but never used
analysis,

Check failure on line 131 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'analysis' is assigned a value but never used
dashboard,

Check failure on line 132 in components/widgets/forest-change/tree-loss-drivers/index.js

View workflow job for this annotation

GitHub Actions / lint

'dashboard' is assigned a value but never used
geostore,
threshold,
type, // country, global etc
} = params;
const dataset = 'tree_cover_loss_by_driver';

// TODO: depending on type, send either geostore or adm0, adm1 etc
const response = await fetchDataMart({
dataset,
geostoreId: 'c3833748f6815d31bad47d47f147c0f0',
isGlobal: false,
adm0: '',
adm1: '',
adm2: '',
isAnalyis: true,
threshold: 27,
isDownload: false,
});

return response;

/*
return all([
getLoss({ ...params, landCategory: 'tsc', lossTsc: true }),
getExtent({ ...params }),
]).then(
Expand Down Expand Up @@ -154,7 +184,9 @@
},
};
})
),
);
*/
},
getDataURL: (params) => [
getLoss({
...params,
Expand Down
117 changes: 117 additions & 0 deletions pages/api/datamart/v0/land/[...slug].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// eslint-disable-next-line no-unused-vars
import { NextApiRequest, NextApiResponse } from 'next';
import {
createRequestByGeostoryId,
getDataByGeostoreId,
getDataFromLink,
} from 'services/datamart';
import { GFW_DATA_API, GFW_STAGING_DATA_API } from 'utils/apis';

// types
/**
* @typedef {object} DataLinkObject
* @property {string} link - The URL to POST the content.
*/

/**
* @typedef {object} GetResponseObject
* @property {string} status - status.
* @property {DataLinkObject} data - data link object.
*/

/**
* @typedef {object} NotFoundObject
* @property {string} status - status.
* @property {string} message - message.
*/
// END types

const ENVIRONMENT = process.env.NEXT_PUBLIC_FEATURE_ENV;

export const DATA_API_URL =
ENVIRONMENT === 'staging' ? GFW_STAGING_DATA_API : GFW_DATA_API;

/**
* @param {NextApiRequest} req
* @param {NextApiResponse} res
*/
const fetchDataByDatasetAndGeostore = async (req, res) => {
const { query } = req;
// TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc
const { slug: slugs, geostore_id, canopy_cover } = query;

if (slugs.length === 0) {
res.status(400).send();
return;
}

if (slugs.length === 1) {

Check failure

Code scanning / CodeQL

Type confusion through parameter tampering Critical

Potential type confusion as
this HTTP request parameter
may be either an array or a string.

Copilot Autofix AI 11 days ago

To fix the problem, we need to ensure that the slugs parameter is always treated as an array of strings. We can do this by checking the type of slugs and converting it to an array if it is not already one. This will prevent type confusion attacks and ensure that the code behaves as expected.

We will modify the fetchDataByDatasetAndGeostore and postData functions to include type checks and conversions for the slugs parameter. Specifically, we will:

  1. Check if slugs is an array. If not, convert it to an array containing the single value.
  2. Proceed with the existing logic, which assumes slugs is an array.
Suggested changeset 1
pages/api/datamart/v0/land/[...slug].js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/pages/api/datamart/v0/land/[...slug].js b/pages/api/datamart/v0/land/[...slug].js
--- a/pages/api/datamart/v0/land/[...slug].js
+++ b/pages/api/datamart/v0/land/[...slug].js
@@ -40,3 +40,4 @@
   // TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc
-  const { slug: slugs, geostore_id, canopy_cover } = query;
+  const { slug: slugsParam, geostore_id, canopy_cover } = query;
+  const slugs = Array.isArray(slugsParam) ? slugsParam : [slugsParam];
 
@@ -78,3 +79,4 @@
   // TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc
-  const { slug: slugs, geostore_id, canopy_cover } = query;
+  const { slug: slugsParam, geostore_id, canopy_cover } = query;
+  const slugs = Array.isArray(slugsParam) ? slugsParam : [slugsParam];
 
EOF
@@ -40,3 +40,4 @@
// TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc
const { slug: slugs, geostore_id, canopy_cover } = query;
const { slug: slugsParam, geostore_id, canopy_cover } = query;
const slugs = Array.isArray(slugsParam) ? slugsParam : [slugsParam];

@@ -78,3 +79,4 @@
// TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc
const { slug: slugs, geostore_id, canopy_cover } = query;
const { slug: slugsParam, geostore_id, canopy_cover } = query;
const slugs = Array.isArray(slugsParam) ? slugsParam : [slugsParam];

Copilot is powered by AI and may make mistakes. Always verify output.
const dataByGeostore = await getDataByGeostoreId({
dataset: slugs[0],
geostoreId: geostore_id,
canopy: canopy_cover,
});

res.status(200).send(dataByGeostore);
return;
}

const url = `${DATA_API_URL}/${slugs.join('/')}`;
try {
const dataByUrl = await getDataFromLink({ url });

res.send(dataByUrl);
} catch (error) {
res.status(error.response?.status).send({
status: error.response?.status,
message: error?.message,
});
}
};

/**
* @param {NextApiRequest} req
* @param {NextApiResponse} res
*/
const postData = async (req, res) => {
const { query } = req;
// TODO: add more parameters to the query like, global, adm9, adm1, etc etc etc
const { slug: slugs, geostore_id, canopy_cover } = query;

if (slugs.length === 0) {
res.status(400).send();
return;
}

try {
const submitted = await createRequestByGeostoryId({
dataset: slugs[0],
geostoreId: geostore_id,
canopy: canopy_cover,
});

res.status(201).send(submitted);
} catch (error) {
res.status(error.response?.status).send({
status: error.response?.status,
message: error?.message,
});
}
};

/**
* @param {NextApiRequest} req
* @param {NextApiResponse} res
*/
export default async (req, res) => {
switch (req.method) {
case 'POST':
postData(req, res);
break;
case 'GET':
fetchDataByDatasetAndGeostore(req, res);
break;
default:
res.send(405);
}
};
170 changes: 170 additions & 0 deletions services/datamart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import qs from 'qs';
import { dataRequest } from 'utils/request';
import { GFW_DATA_API, GFW_STAGING_DATA_API } from 'utils/apis';

const ENVIRONMENT = process.env.NEXT_PUBLIC_FEATURE_ENV;
const DATA_API_URL =
ENVIRONMENT === 'staging' ? GFW_STAGING_DATA_API : GFW_DATA_API;

/**
* @typedef {object} DataLinkObject
* @property {string} link - The URL to POST the content.
*/

/**
* @typedef {object} GetResponseObject
* @property {string} status - status.
* @property {DataLinkObject} data - data link object.
*/

/**
* @typedef {object} NotFoundObject
* @property {string} status - status.
* @property {string} message - message.
*/

/**
* 1
* @param {Object} request - request
* @param {string} request.dataset - dataset.
* @param {string} request.geostoreId - a geostore id.
* @param {number} request.canopy - canopy filter.
* @returns {Promise<GetResponseObject | NotFoundObject>} response.
*/
const getDataByGeostoreId = async ({ dataset, geostoreId, canopy }) => {
const url = `/v0/land/${dataset}`;
const params = {
geostore_id: geostoreId,
canopy_cover: canopy,
};

const requestUrl = `${url}/?${qs.stringify(params)}`;

let response;

try {
response = await dataRequest.get(requestUrl);
} catch (error) {
if (error.response?.status === 404) {
return new Promise((resolve) => {
// eslint-disable-next-line prefer-promise-reject-errors
resolve({
status: error.response?.status,
message: error.response?.statusText,
});
});
}
}

return response.data;
};

/**
* 2
* @param {Object} request - request
* @param {string} request.dataset - dataset.
* @param {string} request.geostoreId - a geostore id.
* @param {number} request.canopy - canopy filter.
* @returns {Promise<GetResponseObject>} response.
*/
const createRequestByGeostoryId = async ({
dataset,
geostoreId,
canopy,
}) => {
const url = `/v0/land/${dataset}`;
const params = {
geostore_id: geostoreId,
canopy_cover: canopy,
};

const response = await dataRequest.post(url, params);

return response;
};

/**
* 3
* @param {Object} request - request
* @param {string} request.url - url
* @returns {Promise<GetResponseObject>} response.
*/
const getDataFromLink = async ({ url }) => {
return dataRequest.get(url.replace(DATA_API_URL, ''));
};


const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));

const retryRequest = async (fn, params, retries = 3, interval = 1000, finalErr = 'Retry failed') => {
try {
console.log(`retryRequest retries ${retries} with fn ${fn}`);

Check warning on line 101 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const res = await fn(params);

if (res.data?.status === 'pending') {
console.log(`is pending, waiting ${interval} sec and retrying`);

Check warning on line 105 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
await wait(interval);
return retryRequest(fn, params, retries - 1, interval, finalErr);
}

return res;

} catch (err) {
console.log('caught err: ', err);

Check warning on line 113 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
if (retries <= 0) {
console.log('no more retries, rejecting');

Check warning on line 115 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
return Promise.reject(finalErr);
}
await wait(interval);
return retryRequest(fn, params, retries - 1, interval, finalErr);
}
};

/**
*
* @param {Object} request
* @param {string} request.dataset - dataset
* @param {string} request.geostoreId - geostore id
* @param {boolean} request.isGlobal - whether the query is global or not
* @param {string} request.adm0 - adm0
* @param {string} request.adm1 - adm1
* @param {string} request.adm2 - adm2
* @param {boolean} request.isAnalyis - is analysis
* @param {number} request.threshold - canopy threshold
* @param {boolean} request.isDownload - whether the query is is download
* @param {number} request.retries - this parameter is to manage retries (only for recursion)
* @param
*/
export const fetchDataMart = async ({
dataset,
geostoreId,
isGlobal,
adm0,
adm1,
adm2,
isAnalyis,
threshold,
isDownload,
retries,
}) => {
const response = await getDataByGeostoreId({ dataset, geostoreId, canopy: threshold });

if (response.status !== 404) {
console.log('link exists, need to fetch: ', response.link);

Check warning on line 153 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const existing = await retryRequest(getDataFromLink, { url: response.link });
console.log('existing: ', existing);

Check warning on line 155 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
} else {
// make post to create the data in back end
console.log('make post to create the data in back end');

Check warning on line 158 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
const submitted = await createRequestByGeostoryId({ dataset, geostoreId, canopy: threshold });
console.log('> submitted: ', submitted);

Check warning on line 160 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

// get link and fetch

// retry based on secondTry.headers['retry-after]
const secondTry = await retryRequest(getDataFromLink, { url: submitted.data.link });

console.log('secondTry: ', secondTry);

Check warning on line 167 in services/datamart.js

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement
}

};
2 changes: 1 addition & 1 deletion utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ENVIRONMENT = process.env.NEXT_PUBLIC_FEATURE_ENV;
const GFW_API_URL = GFW_API;
const GFW_METADATA_API_URL =
ENVIRONMENT === 'staging' ? GFW_STAGING_METADATA_API : GFW_METADATA_API;
const DATA_API_URL =
export const DATA_API_URL =
ENVIRONMENT === 'staging' ? GFW_STAGING_DATA_API : GFW_DATA_API;

// We never use the `staging-api.resourcewatch.org`.
Expand Down
Loading