var _ = require('lodash'); /** * @param tx {PgTx} * @class */ function DillerDao(tx) { var deviceColumns = 'id, created_timestamp, key, name, description'; var propertyColumns = 'id, created_timestamp, device, key, name, description, last_value, last_timestamp'; // ------------------------------------------------------------------------------------------------------------------- // Device // ------------------------------------------------------------------------------------------------------------------- /** * @returns {Promise} */ function devices() { return tx.manyOrNone("SELECT " + deviceColumns + " FROM device"); } function deviceById(id) { return tx.one("SELECT " + deviceColumns + " FROM device WHERE id=$1", id); } function deviceByKey(key) { return tx.oneOrNone("SELECT " + deviceColumns + " FROM device WHERE key=$1", key); } function insertDevice(key) { 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 = _(attributes).chain() .map(function (value, attribute) { if (attribute == 'name' || attribute == 'description') { value = (value || '').trim(); values.push(value.length > 0 ? value : null); return attribute + ' = $' + i++; } }) .collect() .join(', ') .value(); if (fields.length == 0) { return Promise.resolve({}); } var sql = 'UPDATE device SET ' + fields + ' WHERE id = $1'; return tx.none(sql, values); } // ------------------------------------------------------------------------------------------------------------------- // Device Property // ------------------------------------------------------------------------------------------------------------------- function devicePropertyById(id) { return tx.one('SELECT ' + propertyColumns + ' FROM device_property WHERE id=$1', [id]); } function devicePropertyByDeviceIdAndKey(deviceId, key) { return tx.oneOrNone('SELECT ' + propertyColumns + ' FROM device_property WHERE device=$1 AND key=$2', [deviceId, key]); } function devicePropertiesByDeviceId(deviceId) { return tx.manyOrNone('SELECT ' + propertyColumns + ' FROM device_property WHERE device=$1', [deviceId]); } function insertDeviceProperty(deviceId, key) { return tx.one('INSERT INTO device_property(id, device, key, created_timestamp) VALUES(DEFAULT, $1, $2, CURRENT_TIMESTAMP) RETURNING ' + propertyColumns, [deviceId, key]); } function updateProperty(id, attributes) { var values = [id]; var i = 2; var fields = _(attributes).chain() .map(function (value, attribute) { if (attribute == 'name' || attribute == 'description') { value = (value || '').trim(); values.push(value.length > 0 ? value : null); return attribute + ' = $' + i++; } }) .collect() .join(', ') .value(); if (fields.length == 0) { return Promise.resolve({}); } var sql = 'UPDATE device_property SET ' + fields + ' WHERE id = $1 RETURNING *'; return tx.one(sql, values); } // ------------------------------------------------------------------------------------------------------------------- // Value // ------------------------------------------------------------------------------------------------------------------- function valuesByPropertyId(propertyId, limit) { limit = limit || 10; 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, timestamp, value) { var value_numeric = parseFloat(value) || undefined, value_text = value_numeric ? null : value; 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) { return tx.none('DELETE FROM value_by_hour WHERE property=$1 AND timestamp=DATE_TRUNC(\'hour\', $2::TIMESTAMPTZ)', [propertyId, timestamp]) .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 ' + 'GROUP BY property, DATE_TRUNC(\'hour\', timestamp) ' + 'RETURNING *;', [propertyId, timestamp]); }); } /** * @returns {Promise} */ 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 () { 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 ' + 'WHERE property=$1 AND DATE_TRUNC(\'minute\', timestamp)=DATE_TRUNC(\'minute\', $2::TIMESTAMPTZ) AND value_numeric IS NOT NULL ' + 'GROUP BY property, DATE_TRUNC(\'minute\', timestamp) ' + 'RETURNING *;', [propertyId, timestamp]); }); } /** @lends DillerDao.prototype */ return { devices: devices, deviceById: deviceById, deviceByKey: deviceByKey, insertDevice: insertDevice, updateDevice: updateDevice, devicePropertyById: devicePropertyById, devicePropertyByDeviceIdAndKey: devicePropertyByDeviceIdAndKey, devicePropertiesByDeviceId: devicePropertiesByDeviceId, insertDeviceProperty: insertDeviceProperty, updateProperty: updateProperty, valuesByPropertyId: valuesByPropertyId, insertValue: insertValue, updateHourAggregatesForProperty: updateHourAggregatesForProperty, updateMinuteAggregatesForProperty: updateMinuteAggregatesForProperty }; } /** * @type DillerDao */ var x; x.deviceById(123) module.exports = DillerDao;