From 73d272ffe8954b3169901eda74428bad3d2740fe Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 19 Oct 2015 21:53:49 +0200 Subject: o Adding aggregation tables. o Adding migration scripts for the schema. --- src/Diller.js | 45 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'src/Diller.js') 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(/\//); -- cgit v1.2.3