-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from urbanriskmap/dev
Merge dev to form v3.0.3
- Loading branch information
Showing
24 changed files
with
1,266 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* CogniCity Server /floods/archive archive endpoint | ||
* @module src/api/floods/archive/index | ||
**/ | ||
import {Router} from 'express'; | ||
|
||
// Import our data model | ||
import archive from './model'; | ||
|
||
// Import any required utility functions | ||
import {cacheResponse} from '../../../../lib/util'; | ||
|
||
// Import validation dependencies | ||
import BaseJoi from 'joi'; | ||
import Extension from 'joi-date-extensions'; | ||
const Joi = BaseJoi.extend(Extension); | ||
import validate from 'celebrate'; | ||
|
||
/** | ||
* Endpoint specification for floods archive | ||
* @alias module:src/api/floods/archive/index | ||
* @param {Object} config Server configuration | ||
* @param {Object} db PG Promise database instance | ||
* @param {Object} logger Configured Winston logger instance | ||
* @return {Object} api Express router object for route | ||
*/ | ||
export default ({config, db, logger}) => { | ||
let api = Router(); // eslint-disable-line new-cap | ||
// TODO add support for multiple cities | ||
// Just get the states without the geographic boundaries | ||
api.get('/', cacheResponse('1 minute'), | ||
validate({ | ||
query: { | ||
start: Joi.date().format('YYYY-MM-DDTHH:mm:ssZ').required(), | ||
end: Joi.date().format('YYYY-MM-DDTHH:mm:ssZ') | ||
.min(Joi.ref('start')).required(), | ||
}, | ||
}), | ||
(req, res, next) => { | ||
// validate the time window, if fails send 400 error | ||
let maxWindow = new Date(req.query.start).getTime() + | ||
(config.API_REPORTS_TIME_WINDOW_MAX * 1000); | ||
let end = new Date(req.query.end); | ||
if (end > maxWindow) { | ||
res.status(400).json({'statusCode': 400, 'error': 'Bad Request', | ||
'message': 'child \'end\' fails because [end is more than ' | ||
+ config.API_REPORTS_TIME_WINDOW_MAX | ||
+ ' seconds greater than \'start\']', | ||
'validation': { | ||
'source': 'query', | ||
'keys': [ | ||
'end', | ||
]}}); | ||
return; | ||
} | ||
archive(config, db, logger).maxstate(req.query.start, req.query.end) | ||
.then((data) => res.status(200).json({statusCode: 200, result: data})) | ||
.catch((err) => { | ||
/* istanbul ignore next */ | ||
logger.error(err); | ||
/* istanbul ignore next */ | ||
next(err); | ||
}); | ||
} | ||
); | ||
return api; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* CogniCity Server /floods/archive data model | ||
* @module src/api/floods/archive model | ||
**/ | ||
import Promise from 'bluebird'; | ||
|
||
/** | ||
* Methods to interact with flood layers in database | ||
* @alias module:src/api/floods/model | ||
* @param {Object} config Server configuration | ||
* @param {Object} db PG Promise database instance | ||
* @param {Object} logger Configured Winston logger instance | ||
* @return {Object} Query methods | ||
*/ | ||
export default (config, db, logger) => ({ | ||
|
||
// Get max state of all flood reports over time | ||
maxstate: (start, end) => new Promise((resolve, reject) => { | ||
// Setup query | ||
let query = `SELECT local_area as area_id, changed as last_updated, | ||
max_state FROM cognicity.rem_get_max_flood($1, $2)`; | ||
|
||
// Setup values | ||
let values = [start, end]; | ||
|
||
// Execute | ||
logger.debug(query, values); | ||
db.any(query, values).timeout(config.PGTIMEOUT) | ||
.then((data) => { | ||
resolve(data); | ||
}) | ||
.catch((err) => { | ||
/* istanbul ignore next */ | ||
reject(err); | ||
}); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* CogniCity Server /floods timeseries endpoint | ||
* @module src/api/floods/timeseries/index | ||
**/ | ||
import {Router} from 'express'; | ||
|
||
// Import our data model | ||
import timeseries from './model'; | ||
|
||
// Import any required utility functions | ||
import {cacheResponse} from '../../../../lib/util'; | ||
|
||
// Import validation dependencies | ||
import BaseJoi from 'joi'; | ||
import Extension from 'joi-date-extensions'; | ||
const Joi = BaseJoi.extend(Extension); | ||
import validate from 'celebrate'; | ||
|
||
/** | ||
* Endpoint specification for floods timeseries data | ||
* @alias module:src/api/floods/timeseries/index | ||
* @param {Object} config Server configuration | ||
* @param {Object} db PG Promise database instance | ||
* @param {Object} logger Configured Winston logger instance | ||
* @return {Object} api Express router object for route | ||
*/ | ||
export default ({config, db, logger}) => { | ||
let api = Router(); // eslint-disable-line new-cap | ||
// TODO add support for multiple cities | ||
// Just get the states without the geographic boundaries | ||
api.get('/', cacheResponse('1 minute'), | ||
validate({ | ||
query: { | ||
start: Joi.date().format('YYYY-MM-DDTHH:mm:ssZ').required(), | ||
end: Joi.date().format('YYYY-MM-DDTHH:mm:ssZ') | ||
.min(Joi.ref('start')).required(), | ||
}, | ||
}), | ||
(req, res, next) => { | ||
// validate the time window, if fails send 400 error | ||
let maxWindow = new Date(req.query.start).getTime() + | ||
(config.API_REPORTS_TIME_WINDOW_MAX * 1000); | ||
let end = new Date(req.query.end); | ||
if (end > maxWindow) { | ||
res.status(400).json({'statusCode': 400, 'error': 'Bad Request', | ||
'message': 'child \'end\' fails because [end is more than ' | ||
+ config.API_REPORTS_TIME_WINDOW_MAX | ||
+ ' seconds greater than \'start\']', | ||
'validation': { | ||
'source': 'query', | ||
'keys': [ | ||
'end', | ||
]}}); | ||
return; | ||
} | ||
|
||
timeseries(config, db, logger).count(req.query.start, req.query.end) | ||
.then((data) => res.status(200).json({statusCode: 200, result: data})) | ||
.catch((err) => { | ||
/* istanbul ignore next */ | ||
logger.error(err); | ||
/* istanbul ignore next */ | ||
next(err); | ||
}); | ||
} | ||
); | ||
return api; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* CogniCity Server /floods/timeseries data model | ||
* @module src/api/floods/timeseries/model | ||
**/ | ||
import Promise from 'bluebird'; | ||
|
||
/** | ||
* Methods to interact with flood layers in database | ||
* @alias module:src/api/floods/timeseries/model | ||
* @param {Object} config Server configuration | ||
* @param {Object} db PG Promise database instance | ||
* @param {Object} logger Configured Winston logger instance | ||
* @return {Object} Query methods | ||
*/ | ||
export default (config, db, logger) => ({ | ||
|
||
// Get all flood reports for a given city | ||
count: (start, end) => new Promise((resolve, reject) => { | ||
// Setup query | ||
let query = `SELECT ts, count(local_area) FROM | ||
(SELECT (cognicity.rem_get_flood(ts)).local_area, ts | ||
FROM generate_series($1::timestamp with time zone, | ||
$2::timestamp with time zone,'1 hour') | ||
as series(ts)) output | ||
GROUP BY ts ORDER BY ts`; | ||
|
||
// Setup values | ||
let values = [start, end]; | ||
|
||
// Execute | ||
logger.debug(query, values); | ||
db.any(query, values).timeout(config.PGTIMEOUT) | ||
.then((data) => { | ||
resolve(data); | ||
}) | ||
.catch((err) => { | ||
/* istanbul ignore next */ | ||
reject(err); | ||
}); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.