Skip to content

Commit

Permalink
update start date
Browse files Browse the repository at this point in the history
  • Loading branch information
aagm committed Aug 30, 2023
1 parent d5d2b06 commit accb877
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cloud-functions/alerts-tiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ const alertsJob = async (
x,
y,
z,
startDate = '2020-01-01',
startDate = '2019-01-01',
endDate = new Date().toISOString().split('T')[0]
) => {
// First try to get data from cache in order to reduce costs
Expand Down
36 changes: 18 additions & 18 deletions cloud-functions/fetch-alerts-heatmap/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { BigQuery } = require("@google-cloud/bigquery");
const axios = require("axios").default;
const reverse = require("turf-reverse");
const http = require("http");
const https = require("https");
const { BigQuery } = require('@google-cloud/bigquery');
const axios = require('axios').default;
const reverse = require('turf-reverse');
const http = require('http');
const https = require('https');

const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });
Expand Down Expand Up @@ -30,16 +30,16 @@ const makeQuery = (startDate, endDate) => {
};

const serializeToGeoJSON = (data) => ({
type: "FeatureCollection",
name: "deforestation-alerts",
type: 'FeatureCollection',
name: 'deforestation-alerts',
features: data.map((d) => ({
type: "Feature",
type: 'Feature',
properties: {
count: d.count,
intensity: d.count / d.max,
},
geometry: {
type: "Point",
type: 'Point',
coordinates: [d.longitude, d.latitude],
},
})),
Expand All @@ -56,8 +56,8 @@ const serializeToGeoJSON = (data) => ({
* @param {Date} endDate
*/
const alertsJob = async (
startDate = "2020-01-01",
endDate = new Date().toISOString().split("T")[0]
startDate = '2019-01-01',
endDate = new Date().toISOString().split('T')[0]
) => {
// First try to get data from cache in order to reduce costs
const cacheKey = `_${startDate}_${endDate}`;
Expand All @@ -68,7 +68,7 @@ const alertsJob = async (
const options = {
query: makeQuery(startDate, endDate),
// Location must match that of the dataset(s) referenced in the query.
location: "US",
location: 'US',
};

// Run the query as a job
Expand Down Expand Up @@ -96,14 +96,14 @@ exports.fetchAlertsHeatmap = (req, res) => {
// Set CORS headers for preflight requests
// Allows GETs from any origin with the Content-Type header
// and caches preflight response for 3600s
res.set("Access-Control-Allow-Origin", "*");
res.set('Access-Control-Allow-Origin', '*');

if (req.method === "OPTIONS") {
if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set("Access-Control-Allow-Methods", "GET");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Access-Control-Max-Age", "3600");
res.status(204).send("");
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.status(204).send('');
} else {
fetch();
}
Expand Down
79 changes: 34 additions & 45 deletions cloud-functions/fetch-alerts/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { BigQuery } = require("@google-cloud/bigquery");
const axios = require("axios").default;
const reverse = require("turf-reverse");
const mapshaper = require("mapshaper");
const { BigQuery } = require('@google-cloud/bigquery');
const axios = require('axios').default;
const reverse = require('turf-reverse');
const mapshaper = require('mapshaper');

const crypto = require("crypto");
const http = require("http");
const https = require("https");
const crypto = require('crypto');
const http = require('http');
const https = require('https');

const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });
Expand All @@ -14,41 +14,38 @@ const bigquery = new BigQuery();

const cache = {};

const md5 = (x) =>
crypto.createHash("md5").update(JSON.stringify(x), "utf8").digest("hex");
const md5 = (x) => crypto.createHash('md5').update(JSON.stringify(x), 'utf8').digest('hex');

const getLocation = async (locationId, env) => {
if (!locationId) return null;
console.log("Getting geometry from locations API");
console.log('Getting geometry from locations API');
const apiUrl = {
production: "https://mangrove-atlas-api.herokuapp.com",
staging: "https://mangrove-atlas-api-staging.herokuapp.com",
production: 'https://mangrove-atlas-api.herokuapp.com',
staging: 'https://mangrove-atlas-api-staging.herokuapp.com',
};
const response = await axios.get(
`${apiUrl[env]}/api/v2/locations/${locationId}`,
{ httpAgent, httpsAgent }
);
const response = await axios.get(`${apiUrl[env]}/api/v2/locations/${locationId}`, {
httpAgent,
httpsAgent,
});
if (response && response.data) return response.data.data;
return null;
};

const simplifyGeoJSON = async (geoJSON) => {
const input = { "input.geojson": JSON.stringify(geoJSON) };
const input = { 'input.geojson': JSON.stringify(geoJSON) };
const cmd =
"-i input.geojson -simplify dp 20% keep-shapes -clean -o output.geojson format=geojson geojson-type=Feature";
'-i input.geojson -simplify dp 20% keep-shapes -clean -o output.geojson format=geojson geojson-type=Feature';

const geoJSONsimp = (await mapshaper.applyCommands(cmd, input))[
"output.geojson"
].toString();
const geoJSONsimp = (await mapshaper.applyCommands(cmd, input))['output.geojson'].toString();
return geoJSONsimp;
};

const makeQuery = async (location, startDate, endDate) => {
let whereQuery = "";
let whereQuery = '';

if (location) {
const geoJSON = {
type: "Feature",
type: 'Feature',
properties: {},
geometry: location.geometry,
};
Expand Down Expand Up @@ -76,18 +73,17 @@ const makeQuery = async (location, startDate, endDate) => {
const alertsJob = async (locationId, startDate, endDate, env, geojson) => {
// First try to get data from cache in order to reduce costs
const geojson_md5 = geojson ? md5(geojson) : null;
const cacheKey = `${locationId || geojson_md5 || ""}_${startDate}_${endDate}`;
const cacheKey = `${locationId || geojson_md5 || ''}_${startDate}_${endDate}`;
if (cache[cacheKey]) {
console.log(`Response from cache ${cacheKey}`);
return cache[cacheKey];
}

const location =
(locationId && (await getLocation(locationId, env))) || geojson.features[0];
const location = (locationId && (await getLocation(locationId, env))) || geojson.features[0];
const options = {
query: await makeQuery(location, startDate, endDate),
// Location must match that of the dataset(s) referenced in the query.
location: "US",
location: 'US',
};

// Run the query as a job
Expand All @@ -108,20 +104,13 @@ exports.fetchAlerts = (req, res) => {
// Get data and return a JSON
async function fetch() {
try {
const startDate = req.query.startDate || "2020-01-01";
const endDate =
req.query.end_date || new Date().toISOString().split("T")[0];
const env = req.query.env || "production";
const startDate = req.query.startDate || '2019-01-01';
const endDate = req.query.end_date || new Date().toISOString().split('T')[0];
const env = req.query.env || 'production';
const locationId = req.query.location_id || null;
const geojson = (req.body && req.body.geometry) || null;

const result = await alertsJob(
locationId,
startDate,
endDate,
env,
geojson
);
const result = await alertsJob(locationId, startDate, endDate, env, geojson);
res.status(200).json(result);
} catch (error) {
console.log(error);
Expand All @@ -132,15 +121,15 @@ exports.fetchAlerts = (req, res) => {
// Set CORS headers for preflight requests
// Allows GETs from any origin with the Content-Type header
// and caches preflight response for 3600s
res.set("Access-Control-Allow-Origin", "*");
res.set('Access-Control-Allow-Origin', '*');

if (req.method === "OPTIONS") {
if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set("Access-Control-Allow-Methods", "GET");
res.set("Access-Control-Allow-Methods", "POST");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set("Access-Control-Max-Age", "3600");
res.status(204).send("");
res.set('Access-Control-Allow-Methods', 'GET');
res.set('Access-Control-Allow-Methods', 'POST');
res.set('Access-Control-Allow-Headers', 'Content-Type');
res.set('Access-Control-Max-Age', '3600');
res.status(204).send('');
} else {
fetch();
}
Expand Down

0 comments on commit accb877

Please sign in to comment.