diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2016-03-06 21:48:19 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2016-03-06 21:48:19 +0100 |
commit | 681cd42eda3b1099f13eefa585673a06f12de405 (patch) | |
tree | e26f7674d6a9e7a027462d960b6e2917a2f2b4fe /src | |
parent | 45267e41368fbb9fb318fb455981110e83c97953 (diff) | |
download | diller-server-681cd42eda3b1099f13eefa585673a06f12de405.tar.gz diller-server-681cd42eda3b1099f13eefa585673a06f12de405.tar.bz2 diller-server-681cd42eda3b1099f13eefa585673a06f12de405.tar.xz diller-server-681cd42eda3b1099f13eefa585673a06f12de405.zip |
core:
o Supporting aggregate levels of 'minute' and 'day' in addition to the existing 'hour'.
Diffstat (limited to 'src')
-rw-r--r-- | src/DillerDao.js | 4 | ||||
-rw-r--r-- | src/web/DillerWeb.js | 11 |
2 files changed, 9 insertions, 6 deletions
diff --git a/src/DillerDao.js b/src/DillerDao.js index 7620541..a59adb8 100644 --- a/src/DillerDao.js +++ b/src/DillerDao.js @@ -192,13 +192,13 @@ function DillerDao(tx, as) { function aggregateValuesByPropertyId(propertyId, level, from, to) { var sql, args = [level, as.date(from), as.date(to), propertyId]; - if (level == 'hour') { + if (level == 'hour' || level == 'minute' || level == 'day') { // TODO: use correct table instead of querying raw table } else { throw 'Unsupported level: ' + level; } - sql = 'with g as (select * from generate_series($2::timestamp, $3::timestamp, (\'1 \' || $1)::interval) as times(ts)),\n' + + sql = 'with g as (select date_trunc($1, ts) as ts from generate_series($2::timestamp, $3::timestamp, (\'1 \' || $1)::interval) as times(ts)),\n' + 'v as (select\n' + ' date_trunc($1, timestamp) as ts,\n' + ' count(timestamp)::real as count,\n' + diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js index b42e47e..cf65f1b 100644 --- a/src/web/DillerWeb.js +++ b/src/web/DillerWeb.js @@ -111,21 +111,24 @@ function DillerWeb(config, mqttClient, tx) { var from = req.query.from && moment(req.query.from, isoFormat); var to = req.query.to && moment(req.query.to, isoFormat); var limit = req.query.limit || 10; + var aggregateLevel = req.query.aggregateLevel; tx(function (tx, dao) { var propertyId = Number(req.params.propertyId); + var validAggregateLevel = aggregateLevel == 'minute' || aggregateLevel == 'hour' || aggregateLevel == 'day'; + if (from || to) { from = (from || moment()).startOf('hour'); to = (to || moment()).startOf('hour'); - if (typeof propertyId !== 'number' || !from.isValid() || !to.isValid()) { - log.info('getValues: Invalid parameters: propertyId', propertyId, 'from', from.toISOString(), 'to', to.toISOString()); - return Promise.reject('Invalid parameters: ' + typeof propertyId); + if (typeof propertyId !== 'number' || !from.isValid() || !to.isValid() || !validAggregateLevel) { + log.info('getValues: Invalid parameters: propertyId', propertyId, 'from', from.toISOString(), 'to', to.toISOString(), 'aggregateLevel', aggregateLevel); + return Promise.reject('Invalid parameters'); } else { log.info('getValues: propertyId', propertyId, 'from', from.toISOString(), 'to', to.toISOString()); - return dao.aggregateValuesByPropertyId(propertyId, 'hour', from.toDate(), to.toDate()); + return dao.aggregateValuesByPropertyId(propertyId, aggregateLevel, from.toDate(), to.toDate()); } } else { return dao.valuesByPropertyId(propertyId, limit); |