aboutsummaryrefslogtreecommitdiff
path: root/src/web
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2016-03-05 16:47:32 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2016-03-05 16:47:32 +0100
commit7ca173de3de046501d79164da0c8c8871a03089b (patch)
tree16d857cf2ab7fd8b7b3c29efbacd6b01c2eacec7 /src/web
parentdda9ef2ae7971bceaa792e328c8489cb0695b77e (diff)
downloaddiller-server-7ca173de3de046501d79164da0c8c8871a03089b.tar.gz
diller-server-7ca173de3de046501d79164da0c8c8871a03089b.tar.bz2
diller-server-7ca173de3de046501d79164da0c8c8871a03089b.tar.xz
diller-server-7ca173de3de046501d79164da0c8c8871a03089b.zip
web:
o Adding an API method to get per-hour aggregate values. Doesn't use the by_hour table yet. o Adding a simple line graph component that can graph a single property's value.
Diffstat (limited to 'src/web')
-rw-r--r--src/web/DillerWeb.js38
1 files changed, 33 insertions, 5 deletions
diff --git a/src/web/DillerWeb.js b/src/web/DillerWeb.js
index 396c2f8..b42e47e 100644
--- a/src/web/DillerWeb.js
+++ b/src/web/DillerWeb.js
@@ -1,8 +1,11 @@
var express = require('express');
+var moment = require('moment');
var bodyParser = require('body-parser');
var _ = require('lodash');
var di = require('di');
+var isoFormat = 'YYYY-MM-DDTHH:mm:ss';
+
/**
* @param {DillerConfig} config
* @param {DillerMqttClient} mqttClient
@@ -105,14 +108,33 @@ function DillerWeb(config, mqttClient, tx) {
* @param {HttpRes} res
*/
function getValues(req, res) {
+ 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;
+
tx(function (tx, dao) {
- var propertyId = req.params.propertyId;
- return dao.valuesByPropertyId(propertyId, 10);
+ var propertyId = Number(req.params.propertyId);
+
+ 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);
+ } else {
+ log.info('getValues: propertyId', propertyId, 'from', from.toISOString(), 'to', to.toISOString());
+
+ return dao.aggregateValuesByPropertyId(propertyId, 'hour', from.toDate(), to.toDate());
+ }
+ } else {
+ return dao.valuesByPropertyId(propertyId, limit);
+ }
}).then(function (values) {
res.json({values: values});
}, function (err) {
log.warn('fail', err);
- res.status(500).json({message: 'fail'});
+ res.status(500).json({message: typeof err === 'string' ? err : 'fail'});
});
}
@@ -254,7 +276,7 @@ function DillerWeb(config, mqttClient, tx) {
s += _.map(calls, function (call) {
- var s = ' function ' + call.name + '(' + call.keys.join(', ') + ') {\n' +
+ var s = ' function ' + call.name + 'Req(' + call.keys.join(', ') + ') {\n' +
' var req = {};\n' +
' req.method = \'' + call.method + '\';\n' +
' req.url = baseUrl + \'/api' + call.path + '\';\n';
@@ -268,6 +290,12 @@ function DillerWeb(config, mqttClient, tx) {
}
s +=
+ ' return req;\n' +
+ ' }\n' +
+ '\n';
+
+ s += ' function ' + call.name + '(' + call.keys.join(', ') + ') {\n' +
+ ' var req = ' + call.name + 'Req(' + call.keys.join(', ') + ');\n' +
' return $http(req);\n' +
' }\n';
@@ -276,7 +304,7 @@ function DillerWeb(config, mqttClient, tx) {
s += '\n';
s += ' return {\n';
s += _.map(calls, function (call) {
- return ' ' + call.name + ': ' + call.name
+ return ' ' + call.name + 'Req: ' + call.name + 'Req,\n ' + call.name + ': ' + call.name
}).join(',\n');
s += '\n';
s += ' };\n';