aboutsummaryrefslogtreecommitdiff
path: root/src/Diller.js
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2015-10-19 21:53:49 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2015-10-19 21:53:49 +0200
commit73d272ffe8954b3169901eda74428bad3d2740fe (patch)
tree1f1cca7f57809e05d0c1fea7363555a5521f8bcb /src/Diller.js
parent52eb8072664a61ea61dbdbef7485d6c81dbbcfe9 (diff)
downloaddiller-server-73d272ffe8954b3169901eda74428bad3d2740fe.tar.gz
diller-server-73d272ffe8954b3169901eda74428bad3d2740fe.tar.bz2
diller-server-73d272ffe8954b3169901eda74428bad3d2740fe.tar.xz
diller-server-73d272ffe8954b3169901eda74428bad3d2740fe.zip
o Adding aggregation tables.
o Adding migration scripts for the schema.
Diffstat (limited to 'src/Diller.js')
-rw-r--r--src/Diller.js45
1 files changed, 43 insertions, 2 deletions
diff --git a/src/Diller.js b/src/Diller.js
index 9a1de23..eeaf726 100644
--- a/src/Diller.js
+++ b/src/Diller.js
@@ -1,5 +1,20 @@
var DillerDao = require('./DillerDao');
-var pgp = require('pg-promise')();
+var pgpOptions = {
+ //query: function (e) {
+ // console.log("Query:", e.query);
+ // if (e.ctx) {
+ // // this query is executing inside a task or transaction,
+ // if (e.ctx.isTX) {
+ // // this query is inside a transaction;
+ // } else {
+ // // this query is inside a task;
+ // }
+ //
+ // }
+ //}
+};
+
+var pgp = require('pg-promise')(pgpOptions);
function Diller(config, log) {
@@ -10,7 +25,12 @@ function Diller(config, log) {
value: value
});
- return dao.insertValue(property.id, value);
+ return dao.insertValue(property.id, value)
+ .then(function (res) {
+ log.info('typeof', typeof res.timestamp, res.timestamp, res.timestamp.getTime());
+ updateAggregates(property.id, res.timestamp);
+ return res;
+ });
}
function newName(dao, device, property, name) {
@@ -25,6 +45,27 @@ function Diller(config, log) {
return dao.updatePropertyDescription(property.id, description);
}
+ function updateAggregates(propertyId, timestamp) {
+ log.info('Updating aggregates', {propertyId: propertyId, timestamp: timestamp});
+ return pgp(config.postgresqlConfig)
+ .tx(function (pg) {
+ var dao = new DillerDao(pg);
+
+ return dao.updateMinuteAggregatesForProperty(propertyId, timestamp).then(function (minute) {
+ return dao.updateHourAggregatesForProperty(propertyId, timestamp).then(function (hour) {
+ return {
+ minute: minute,
+ hour: hour
+ };
+ });
+ });
+ }).then(function (res) {
+ log.info('updateAggregates: ok', {propertyId: propertyId, aggregate: res});
+ }, function (res) {
+ log.warn('updateAggregates: failed', {res: res, propertyId: propertyId});
+ });
+ }
+
function onMessage(topic, message, payload) {
var parts = topic.split(/\//);