@golemio/energetics 1.2.0 → 1.2.1
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/20230118164116-optimize_vpalac_measurements_view.js +53 -0
- package/db/migrations/postgresql/sqls/20230118164116-optimize_vpalac_measurements_view-down.sql +45 -0
- package/db/migrations/postgresql/sqls/20230118164116-optimize_vpalac_measurements_view-up.sql +52 -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', '20230118164116-optimize_vpalac_measurements_view-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', '20230118164116-optimize_vpalac_measurements_view-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
|
+
};
|
package/db/migrations/postgresql/sqls/20230118164116-optimize_vpalac_measurements_view-down.sql
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
CREATE OR REPLACE VIEW v_vpalac_apartments_daily_consumption
|
|
2
|
+
AS WITH last_measurement AS (
|
|
3
|
+
SELECT date_part('year'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone) AS measure_year,
|
|
4
|
+
date_trunc('day'::text, max(to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone)) AS last_measurement,
|
|
5
|
+
m.var_id
|
|
6
|
+
FROM vpalac_measurement m
|
|
7
|
+
GROUP BY (date_part('year'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone)), m.var_id
|
|
8
|
+
), daily_status AS (
|
|
9
|
+
SELECT
|
|
10
|
+
CASE
|
|
11
|
+
WHEN me.me_serial::text = '5640201'::text THEN 'Prostor 109,111'::text
|
|
12
|
+
WHEN mm.location_name IS NOT NULL THEN mm.location_name::text
|
|
13
|
+
WHEN me.umisteni::text ~~ '%Byt%'::text THEN "right"(me.umisteni::text, length(me.umisteni::text) - "position"(me.umisteni::text, 'Byt'::text) + 1)
|
|
14
|
+
ELSE "right"(me.umisteni::text, length(me.umisteni::text) - "position"(me.umisteni::text, 'Prostor'::text) + 1)
|
|
15
|
+
END AS apartment,
|
|
16
|
+
CASE
|
|
17
|
+
WHEN mt.met_nazev::text = 'Sensor'::text THEN 'Teplo'::character varying
|
|
18
|
+
ELSE mt.met_nazev
|
|
19
|
+
END AS met_nazev,
|
|
20
|
+
me.mis_nazev,
|
|
21
|
+
me.var_id,
|
|
22
|
+
u.jed_nazev,
|
|
23
|
+
date_trunc('hour'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone) AS measure_date,
|
|
24
|
+
min(m.value) AS start_value
|
|
25
|
+
FROM vpalac_measuring_equipment me
|
|
26
|
+
JOIN vpalac_measurement m ON me.var_id = m.var_id
|
|
27
|
+
LEFT JOIN vpalac_meter_type mt ON me.met_id = mt.met_id
|
|
28
|
+
LEFT JOIN vpalac_units u ON me.pot_id = u.pot_id
|
|
29
|
+
LEFT JOIN vpalac_meter_mapping mm ON mm.var_id = me.var_id
|
|
30
|
+
WHERE to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone >= (now() - '3 years'::interval)
|
|
31
|
+
GROUP BY mm.location_name, me.umisteni, me.me_serial, (date_part('month'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone)), me.var_id, ("right"(me.umisteni::text, 6)), mt.met_nazev, me.mis_nazev, (date_trunc('hour'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone)), u.jed_nazev
|
|
32
|
+
)
|
|
33
|
+
SELECT ds.apartment,
|
|
34
|
+
ds.met_nazev AS measure_type,
|
|
35
|
+
ds.mis_nazev AS measure_detail,
|
|
36
|
+
ds.jed_nazev,
|
|
37
|
+
ds.measure_date,
|
|
38
|
+
lm.last_measurement,
|
|
39
|
+
CASE
|
|
40
|
+
WHEN ds.met_nazev::text = 'Teplo'::text AND ds.measure_date = lm.last_measurement THEN lead(ds.start_value) OVER (PARTITION BY ds.var_id ORDER BY ds.measure_date)
|
|
41
|
+
ELSE ds.start_value - lag(ds.start_value) OVER (PARTITION BY ds.var_id ORDER BY ds.measure_date)
|
|
42
|
+
END AS consumption,
|
|
43
|
+
ds.var_id
|
|
44
|
+
FROM daily_status ds
|
|
45
|
+
JOIN last_measurement lm ON ds.var_id = lm.var_id AND date_part('year'::text, ds.measure_date) = lm.measure_year;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
create or replace view v_vpalac_apartments_daily_consumption as WITH last_measurement AS (
|
|
2
|
+
SELECT date_part('year'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone) AS measure_year,
|
|
3
|
+
date_trunc('day'::text, max(to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone)) AS last_measurement,
|
|
4
|
+
m.var_id
|
|
5
|
+
FROM vpalac_measurement m
|
|
6
|
+
GROUP BY (date_part('year'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone)), m.var_id
|
|
7
|
+
), daily_status AS (
|
|
8
|
+
SELECT
|
|
9
|
+
CASE
|
|
10
|
+
WHEN me.me_serial::text = '5640201'::text THEN 'Prostor 109,111'::text
|
|
11
|
+
WHEN mm.location_name IS NOT NULL THEN mm.location_name::text
|
|
12
|
+
WHEN me.umisteni::text ~~ '%Byt%'::text THEN "right"(me.umisteni::text, length(me.umisteni::text) - "position"(me.umisteni::text, 'Byt'::text) + 1)
|
|
13
|
+
ELSE "right"(me.umisteni::text, length(me.umisteni::text) - "position"(me.umisteni::text, 'Prostor'::text) + 1)
|
|
14
|
+
END AS apartment,
|
|
15
|
+
CASE
|
|
16
|
+
WHEN mt.met_nazev::text = 'Sensor'::text THEN 'Teplo'::character varying
|
|
17
|
+
ELSE mt.met_nazev
|
|
18
|
+
END AS met_nazev,
|
|
19
|
+
me.mis_nazev,
|
|
20
|
+
me.var_id,
|
|
21
|
+
u.jed_nazev,
|
|
22
|
+
m.measure_date,
|
|
23
|
+
m.start_value
|
|
24
|
+
FROM vpalac_measuring_equipment me
|
|
25
|
+
JOIN
|
|
26
|
+
(select
|
|
27
|
+
m.var_id,
|
|
28
|
+
date_trunc('hour'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone) AS measure_date,
|
|
29
|
+
min(m.value) AS start_value
|
|
30
|
+
from
|
|
31
|
+
vpalac_measurement m
|
|
32
|
+
where to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone >= (now() - '3 years'::interval)
|
|
33
|
+
group by
|
|
34
|
+
m.var_id,
|
|
35
|
+
date_trunc('hour'::text, to_timestamp((m.time_measurement / 1000)::double precision)::timestamp without time zone)) m ON me.var_id = m.var_id
|
|
36
|
+
LEFT JOIN vpalac_meter_type mt ON me.met_id = mt.met_id
|
|
37
|
+
LEFT JOIN vpalac_units u ON me.pot_id = u.pot_id
|
|
38
|
+
LEFT JOIN vpalac_meter_mapping mm ON mm.var_id = me.var_id
|
|
39
|
+
)
|
|
40
|
+
SELECT ds.apartment,
|
|
41
|
+
ds.met_nazev AS measure_type,
|
|
42
|
+
ds.mis_nazev AS measure_detail,
|
|
43
|
+
ds.jed_nazev,
|
|
44
|
+
ds.measure_date,
|
|
45
|
+
lm.last_measurement,
|
|
46
|
+
CASE
|
|
47
|
+
WHEN ds.met_nazev::text = 'Teplo'::text AND ds.measure_date = lm.last_measurement THEN lead(ds.start_value) OVER (PARTITION BY ds.var_id ORDER BY ds.measure_date)
|
|
48
|
+
ELSE ds.start_value - lag(ds.start_value) OVER (PARTITION BY ds.var_id ORDER BY ds.measure_date)
|
|
49
|
+
END AS consumption,
|
|
50
|
+
ds.var_id
|
|
51
|
+
FROM daily_status ds
|
|
52
|
+
JOIN last_measurement lm ON ds.var_id = lm.var_id AND date_part('year'::text, ds.measure_date) = lm.measure_year;
|