aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/DillerDao.js4
-rw-r--r--src/web/DillerWeb.js11
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);