diff --git a/src/api/routes/reports/index.js b/src/api/routes/reports/index.js index 6206283..1ac810a 100644 --- a/src/api/routes/reports/index.js +++ b/src/api/routes/reports/index.js @@ -18,11 +18,12 @@ export default ({ config, db, logger }) => { validate({ query: { city: Joi.any().valid(config.REGION_CODES), + timeperiod: Joi.number().integer().positive().max(config.API_REPORTS_TIME_WINDOW_MAX).default(config.API_REPORTS_TIME_WINDOW), format: Joi.any().valid(config.FORMATS).default(config.FORMAT_DEFAULT), geoformat: Joi.any().valid(config.GEO_FORMATS).default(config.GEO_FORMAT_DEFAULT) } }), - (req, res, next) => reports(config, db, logger).all(req.query.city) + (req, res, next) => reports(config, db, logger).all(req.query.timeperiod, req.query.city) .then((data) => handleGeoResponse(data, req, res, next)) .catch((err) => { logger.error(err); diff --git a/src/api/routes/reports/model.js b/src/api/routes/reports/model.js index 60552fa..34308ad 100644 --- a/src/api/routes/reports/model.js +++ b/src/api/routes/reports/model.js @@ -2,9 +2,12 @@ import Promise from 'bluebird'; export default (config, db, logger) => ({ - // Return all reports within the defined max period - // Optional: city (Petabencana.id Instance Region 3 letter code) - all: (city) => new Promise((resolve, reject) => { + /** + * Return all reports within a defined time period, and optionally city + * @param {integer} timeperiod Length of time period in seconds + * @param {string} city Optional, instance region code (e.g. 'jbd') + */ + all: (timeperiod, city) => new Promise((resolve, reject) => { // Setup query let query = `SELECT pkey, created_at, source, @@ -14,9 +17,9 @@ export default (config, db, logger) => ({ AND ($2 IS NULL OR tags->>'instance_region_code'=$2) ORDER BY created_at DESC LIMIT $3`; - // Setup values - let timeWindow = (Date.now() / 1000) - config.API_REPORTS_TIME_WINDOW; - let values = [ timeWindow, city, config.API_REPORTS_LIMIT ] + var timeWindow = (Date.now() / 1000) - timeperiod; + + let values = [ timeWindow, city, config.API_REPORTS_LIMIT ]; // Execute logger.debug(query, values); diff --git a/src/config.js b/src/config.js index ebb4681..f4d7eda 100644 --- a/src/config.js +++ b/src/config.js @@ -6,6 +6,7 @@ export default { API_FEEDS_QLUE_DISASTER_TYPES: (process.env.API_FEEDS_QLUE_DISASTER_TYPES || 'flood').split(','), API_FEEDS_DETIK_DISASTER_TYPES: (process.env.API_FEEDS_DEIK_DISASTER_TYPES || 'flood').split(','), API_REPORTS_TIME_WINDOW: process.env.API_REPORTS_TIME_WINDOW || 3600, + API_REPORTS_TIME_WINDOW_MAX: process.env.API_REPORTS_TIME_WINDOW_MAX || 604800, // 1w API_REPORTS_LIMIT: process.env.API_REPORTS_LIMIT, API_FLOODGAUGE_REPORTS_TIME_WINDOW: process.env.API_FLOODGAUGE_REPORTS_TIME_WINDOW || 43200, API_FLOODGAUGE_REPORTS_LIMIT: process.env.API_FLOODGAUGE_REPORTS_LIMIT,