Skip to content

Commit

Permalink
Merge pull request #16 from urbanriskmap/dev
Browse files Browse the repository at this point in the history
timeperiod parameter for reports
  • Loading branch information
tomasholderness authored Feb 24, 2017
2 parents c9963d3 + c63e888 commit a309c15
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/api/routes/reports/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 9 additions & 6 deletions src/api/routes/reports/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit a309c15

Please sign in to comment.