@golemio/energetics 1.1.1-dev.746690471 → 1.1.1-dev.749143389
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/db/migrations/postgresql/20230116095151-correct_v_energetis_upadate.js +53 -0
- package/db/migrations/postgresql/sqls/20230116095151-correct_v_energetis_upadate-down.sql +27 -0
- package/db/migrations/postgresql/sqls/20230116095151-correct_v_energetis_upadate-up.sql +28 -0
- package/package.json +1 -1
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var dbm;
|
|
4
|
+
var type;
|
|
5
|
+
var seed;
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var path = require('path');
|
|
8
|
+
var Promise;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* We receive the dbmigrate dependency from dbmigrate initially.
|
|
12
|
+
* This enables us to not have to rely on NODE_PATH.
|
|
13
|
+
*/
|
|
14
|
+
exports.setup = function(options, seedLink) {
|
|
15
|
+
dbm = options.dbmigrate;
|
|
16
|
+
type = dbm.dataType;
|
|
17
|
+
seed = seedLink;
|
|
18
|
+
Promise = options.Promise;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
exports.up = function(db) {
|
|
22
|
+
var filePath = path.join(__dirname, 'sqls', '20230116095151-correct_v_energetis_upadate-up.sql');
|
|
23
|
+
return new Promise( function( resolve, reject ) {
|
|
24
|
+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
|
|
25
|
+
if (err) return reject(err);
|
|
26
|
+
console.log('received data: ' + data);
|
|
27
|
+
|
|
28
|
+
resolve(data);
|
|
29
|
+
});
|
|
30
|
+
})
|
|
31
|
+
.then(function(data) {
|
|
32
|
+
return db.runSql(data);
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.down = function(db) {
|
|
37
|
+
var filePath = path.join(__dirname, 'sqls', '20230116095151-correct_v_energetis_upadate-down.sql');
|
|
38
|
+
return new Promise( function( resolve, reject ) {
|
|
39
|
+
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
|
|
40
|
+
if (err) return reject(err);
|
|
41
|
+
console.log('received data: ' + data);
|
|
42
|
+
|
|
43
|
+
resolve(data);
|
|
44
|
+
});
|
|
45
|
+
})
|
|
46
|
+
.then(function(data) {
|
|
47
|
+
return db.runSql(data);
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
exports._meta = {
|
|
52
|
+
"version": 1
|
|
53
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
DROP VIEW v_consumption_energy_last_update;
|
|
2
|
+
|
|
3
|
+
CREATE OR REPLACE VIEW v_consumption_energy_last_update
|
|
4
|
+
AS WITH last_update AS (
|
|
5
|
+
SELECT b.building_name,
|
|
6
|
+
c.addr,
|
|
7
|
+
max(c.time_utc) AS last_update
|
|
8
|
+
FROM energetics.consumption_energy_consumption c
|
|
9
|
+
JOIN energetics.consumption_energy_devices d ON c.addr::text = d.addr::text
|
|
10
|
+
JOIN energetics.consumption_energy_buildings b ON d.building_id = b.id
|
|
11
|
+
GROUP BY b.building_name, c.addr
|
|
12
|
+
), last_not_zero AS (
|
|
13
|
+
SELECT consumption_energy_consumption.addr,
|
|
14
|
+
max(consumption_energy_consumption.time_utc) AS last_not_zero_value
|
|
15
|
+
FROM energetics.consumption_energy_consumption
|
|
16
|
+
WHERE consumption_energy_consumption.value > 0::numeric
|
|
17
|
+
GROUP BY consumption_energy_consumption.addr
|
|
18
|
+
)
|
|
19
|
+
SELECT lu.building_name,
|
|
20
|
+
lu.last_update,
|
|
21
|
+
lu.addr,
|
|
22
|
+
CASE
|
|
23
|
+
WHEN lnz.last_not_zero_value = lu.last_update THEN NULL::timestamp without time zone
|
|
24
|
+
ELSE lnz.last_not_zero_value
|
|
25
|
+
END AS zero_values_after
|
|
26
|
+
FROM last_update lu
|
|
27
|
+
LEFT JOIN last_not_zero lnz ON lu.addr::text = lnz.addr::text;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
DROP VIEW v_consumption_energy_last_update;
|
|
2
|
+
|
|
3
|
+
CREATE OR REPLACE VIEW v_consumption_energy_last_update
|
|
4
|
+
AS WITH last_update AS (
|
|
5
|
+
SELECT b.building_name,
|
|
6
|
+
replace(c.addr, concat('/', c.var), '') as addr,
|
|
7
|
+
max(c.time_utc) AS last_update
|
|
8
|
+
FROM energetics.consumption_energy_consumption c
|
|
9
|
+
JOIN energetics.consumption_energy_devices d ON replace(c.addr, concat('/', c.var), '') = d.addr::text
|
|
10
|
+
JOIN energetics.consumption_energy_buildings b ON d.building_id = b.id
|
|
11
|
+
GROUP BY b.building_name, replace(c.addr, concat('/', c.var), '')
|
|
12
|
+
), last_not_zero AS (
|
|
13
|
+
SELECT replace(addr, concat('/', var), '') as addr,
|
|
14
|
+
max(time_utc) AS last_not_zero_value
|
|
15
|
+
FROM consumption_energy_consumption
|
|
16
|
+
WHERE value > 0::numeric
|
|
17
|
+
GROUP BY replace(addr, concat('/', var), '')
|
|
18
|
+
)
|
|
19
|
+
SELECT lu.building_name,
|
|
20
|
+
lu.last_update,
|
|
21
|
+
lu.addr,
|
|
22
|
+
CASE
|
|
23
|
+
WHEN lnz.last_not_zero_value = lu.last_update THEN NULL::timestamp without time zone
|
|
24
|
+
ELSE lnz.last_not_zero_value
|
|
25
|
+
END AS zero_values_after
|
|
26
|
+
FROM last_update lu
|
|
27
|
+
LEFT JOIN last_not_zero lnz ON lu.addr::text = lnz.addr::text
|
|
28
|
+
|