From 02d6e77bd180cbbf6f7f6e1a69c670e922d8204d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 31 Oct 2015 12:04:28 +0100 Subject: web: o Starting on edit button for device name. --- src/DillerDao.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'src/DillerDao.js') diff --git a/src/DillerDao.js b/src/DillerDao.js index cae80d7..2133dfc 100644 --- a/src/DillerDao.js +++ b/src/DillerDao.js @@ -1,3 +1,5 @@ +var _ = require('lodash'); + function DillerDao(tx) { var deviceColumns = 'id, key, created_timestamp'; @@ -24,6 +26,30 @@ function DillerDao(tx) { return tx.one("INSERT INTO device(id, key, created_timestamp) VALUES(DEFAULT, $1, CURRENT_TIMESTAMP) RETURNING " + deviceColumns, key); } + function updateDevice(id, attributes) { + + var values = [id]; + var i = 2; + var fields = _.map(attributes, function (value, name) { + console.log('name', name, 'value', value, 'i', i); + if (name == 'name') { + values.push(value); + return 'name = $' + i++; + } + }); + + if (fields.length == 0) { + return; // TODO: return an empty promise; + } + + fields = _.collect(fields); + + var x = 'UPDATE device SET ' + fields.join(', ') + ' WHERE id = $1'; + console.log('x', x); + console.log('values', values); + return tx.none(x, values); + } + // ------------------------------------------------------------------------------------------------------------------- // Device Property // ------------------------------------------------------------------------------------------------------------------- @@ -70,10 +96,10 @@ function DillerDao(tx) { function updateHourAggregatesForProperty(propertyId, timestamp) { return tx.none('DELETE FROM value_by_hour WHERE property=$1 AND timestamp=DATE_TRUNC(\'hour\', $2::TIMESTAMPTZ)', [propertyId, timestamp]) - .then(function() { + .then(function () { return tx.oneOrNone('INSERT INTO value_by_hour(property, timestamp, count, max, min, avg) ' + 'SELECT property, DATE_TRUNC(\'hour\', timestamp) AS timestamp, COUNT(value_numeric) AS count, MAX(value_numeric) AS max, MIN(value_numeric) AS min, AVG(value_numeric) AS avg ' + - 'FROM value WHERE property=$1 AND DATE_TRUNC(\'hour\', timestamp)=DATE_TRUNC(\'hour\', $2::TIMESTAMPTZ) AND value_numeric IS NOT NULL ' + + 'FROM value WHERE property=$1 AND DATE_TRUNC(\'hour\', timestamp)=DATE_TRUNC(\'hour\', $2::TIMESTAMPTZ) AND value_numeric IS NOT NULL ' + 'GROUP BY property, DATE_TRUNC(\'hour\', timestamp) ' + 'RETURNING *;', [propertyId, timestamp]); }); @@ -81,7 +107,7 @@ function DillerDao(tx) { function updateMinuteAggregatesForProperty(propertyId, timestamp) { return tx.none('DELETE FROM value_by_minute WHERE property=$1 AND timestamp=DATE_TRUNC(\'minute\', $2::TIMESTAMPTZ)', [propertyId, timestamp]) - .then(function() { + .then(function () { return tx.oneOrNone('INSERT INTO value_by_minute(property, timestamp, count, max, min, avg) ' + 'SELECT property, DATE_TRUNC(\'minute\', timestamp) AS timestamp, COUNT(value_numeric) AS count, MAX(value_numeric) AS max, MIN(value_numeric) AS min, AVG(value_numeric) AS avg ' + 'FROM value ' + @@ -96,6 +122,7 @@ function DillerDao(tx) { deviceById: deviceById, deviceByKey: deviceByKey, insertDevice: insertDevice, + updateDevice: updateDevice, devicePropertyById: devicePropertyById, devicePropertyByDeviceIdAndKey: devicePropertyByDeviceIdAndKey, -- cgit v1.2.3