diff options
-rw-r--r-- | migrations/20151031131941-last-timestamp.js | 30 | ||||
-rw-r--r-- | migrations/sqls/20151031131941-last-timestamp-down.sql | 1 | ||||
-rw-r--r-- | migrations/sqls/20151031131941-last-timestamp-up.sql | 2 | ||||
-rw-r--r-- | src/Diller.js | 12 | ||||
-rw-r--r-- | src/DillerDao.js | 9 | ||||
-rw-r--r-- | web/static/app/templates/device.html | 14 |
6 files changed, 57 insertions, 11 deletions
diff --git a/migrations/20151031131941-last-timestamp.js b/migrations/20151031131941-last-timestamp.js new file mode 100644 index 0000000..913203f --- /dev/null +++ b/migrations/20151031131941-last-timestamp.js @@ -0,0 +1,30 @@ +var dbm = global.dbm || require('db-migrate'); +var type = dbm.dataType; +var fs = require('fs'); +var path = require('path'); + +exports.up = function(db, callback) { + var filePath = path.join(__dirname + '/sqls/20151031131941-last-timestamp-up.sql'); + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return callback(err); + console.log('received data: ' + data); + + db.runSql(data, function(err) { + if (err) return callback(err); + callback(); + }); + }); +}; + +exports.down = function(db, callback) { + var filePath = path.join(__dirname + '/sqls/20151031131941-last-timestamp-down.sql'); + fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){ + if (err) return callback(err); + console.log('received data: ' + data); + + db.runSql(data, function(err) { + if (err) return callback(err); + callback(); + }); + }); +}; diff --git a/migrations/sqls/20151031131941-last-timestamp-down.sql b/migrations/sqls/20151031131941-last-timestamp-down.sql new file mode 100644 index 0000000..44f074e --- /dev/null +++ b/migrations/sqls/20151031131941-last-timestamp-down.sql @@ -0,0 +1 @@ +/* Replace with your SQL commands */
\ No newline at end of file diff --git a/migrations/sqls/20151031131941-last-timestamp-up.sql b/migrations/sqls/20151031131941-last-timestamp-up.sql new file mode 100644 index 0000000..0268983 --- /dev/null +++ b/migrations/sqls/20151031131941-last-timestamp-up.sql @@ -0,0 +1,2 @@ +ALTER TABLE device_property +ADD COLUMN last_timestamp TIMESTAMPTZ; diff --git a/src/Diller.js b/src/Diller.js index edb501d..46dc8e2 100644 --- a/src/Diller.js +++ b/src/Diller.js @@ -3,14 +3,14 @@ var _ = require('lodash'); function Diller(config, pg, dao) { var log = config.log(); - function newValue(dao, device, property, value) { + function newValue(dao, device, property, timestamp, value) { log.info('new value for device ' + device.key + '/' + property.key + ' = ' + value, { deviceId: device.id, propertyId: property.id, value: value }); - return dao.insertValue(property.id, value) + return dao.insertValue(property.id, timestamp, value) .then(function (res) { // ignore the result updateAggregates(property.id, res.timestamp); @@ -18,13 +18,13 @@ function Diller(config, pg, dao) { }); } - function newName(dao, device, property, name) { + function newName(dao, device, property, timestamp, name) { log.info('New name for property ', device.key + '/' + property.key + '.name = ' + name); return dao.updatePropertyName(property.id, name); } - function newDescription(dao, device, property, description) { + function newDescription(dao, device, property, timestamp, description) { log.info('New description for property ', device.key + '/' + property.key + '.description = ' + description); return dao.updatePropertyDescription(property.id, description); @@ -56,6 +56,8 @@ function Diller(config, pg, dao) { //noinspection JSUnusedLocalSymbols function onMessage(topic, message, payload) { + var timestamp = new Date(); + var parts = topic.split(/\//); log.info('Processing message to ' + topic); @@ -108,7 +110,7 @@ function Diller(config, pg, dao) { }) .then(function (data) { // log.info('data.device', data.device, 'data.property', data.property); - return f(dao, data.device, data.property, message.toString()); + return f(dao, data.device, data.property, timestamp, message.toString()); }) .then(function (res) { log.warn('success', res); diff --git a/src/DillerDao.js b/src/DillerDao.js index a47f181..fa27aa1 100644 --- a/src/DillerDao.js +++ b/src/DillerDao.js @@ -3,7 +3,7 @@ var _ = require('lodash'); function DillerDao(tx) { var deviceColumns = 'id, created_timestamp, key, name, description'; - var propertyColumns = 'id, device, key, created_timestamp'; + var propertyColumns = 'id, created_timestamp, device, key, last_value, last_timestamp'; var valueColumns = 'property, timestamp, value_text, value_numeric'; // ------------------------------------------------------------------------------------------------------------------- @@ -85,11 +85,14 @@ function DillerDao(tx) { return tx.manyOrNone('SELECT timestamp, coalesce(value_numeric::text, value_text) AS value FROM value WHERE property=$1 ORDER BY timestamp DESC LIMIT $2', [propertyId, limit]); } - function insertValue(propertyId, value) { + function insertValue(propertyId, timestamp, value) { var value_numeric = parseFloat(value) || undefined, value_text = value_numeric ? null : value; - return tx.one('INSERT INTO value(property, timestamp, value_text, value_numeric) VALUES($1, CURRENT_TIMESTAMP, $2, $3::NUMERIC) RETURNING timestamp', [propertyId, value_text, value_numeric]); + return tx.none('UPDATE device_property SET last_value = $2, last_timestamp = $3 WHERE id = $1', [propertyId, value, timestamp]) + .then(function () { + return tx.one('INSERT INTO value(property, timestamp, value_text, value_numeric) VALUES($1, $2, $3, $4::NUMERIC) RETURNING timestamp', [propertyId, timestamp, value_text, value_numeric]); + }); } function updateHourAggregatesForProperty(propertyId, timestamp) { diff --git a/web/static/app/templates/device.html b/web/static/app/templates/device.html index 270c461..d1fb6f9 100644 --- a/web/static/app/templates/device.html +++ b/web/static/app/templates/device.html @@ -90,17 +90,25 @@ <table class="table" ng-if="ctrl.device.properties.length > 0"> <thead> <tr> - <td>Registered</td> <td>Key</td> + <td>Last value</td> + <td>Last timestamp</td> + <td>Registered</td> </tr> </thead> <tbody> <tr ng-repeat="p in ctrl.device.properties | orderBy:'key'"> <td> - {{p.created_timestamp | date:'medium'}} + <a href="#/device/{{ctrl.device.id}}/property/{{p.id}}">{{p.key}}</a> </td> <td> - <a href="#/device/{{ctrl.device.id}}/property/{{p.id}}">{{p.key}}</a> + {{p.last_value}} + </td> + <td> + {{p.last_timestamp | date:'medium'}} + </td> + <td> + {{p.created_timestamp | date:'medium'}} </td> </tr> </tbody> |