aboutsummaryrefslogtreecommitdiff
path: root/src/DillerDao.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/DillerDao.js')
-rw-r--r--src/DillerDao.js33
1 files changed, 30 insertions, 3 deletions
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,