From accb87763524a6c05d60d86e150a0a0156f62517 Mon Sep 17 00:00:00 2001 From: Alicia Date: Wed, 30 Aug 2023 16:15:15 +0200 Subject: [PATCH] update start date --- cloud-functions/alerts-tiler/index.js | 2 +- cloud-functions/fetch-alerts-heatmap/index.js | 36 ++++----- cloud-functions/fetch-alerts/index.js | 79 ++++++++----------- 3 files changed, 53 insertions(+), 64 deletions(-) diff --git a/cloud-functions/alerts-tiler/index.js b/cloud-functions/alerts-tiler/index.js index 92d5a038f..919f54fa3 100644 --- a/cloud-functions/alerts-tiler/index.js +++ b/cloud-functions/alerts-tiler/index.js @@ -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 diff --git a/cloud-functions/fetch-alerts-heatmap/index.js b/cloud-functions/fetch-alerts-heatmap/index.js index ee9bb553c..01fc9bd9c 100644 --- a/cloud-functions/fetch-alerts-heatmap/index.js +++ b/cloud-functions/fetch-alerts-heatmap/index.js @@ -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 }); @@ -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], }, })), @@ -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}`; @@ -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 @@ -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(); } diff --git a/cloud-functions/fetch-alerts/index.js b/cloud-functions/fetch-alerts/index.js index 928cfdfc9..2cfe97be5 100644 --- a/cloud-functions/fetch-alerts/index.js +++ b/cloud-functions/fetch-alerts/index.js @@ -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 }); @@ -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, }; @@ -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 @@ -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); @@ -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(); }