@golemio/energetics 1.16.2-dev.2870938995 → 1.17.0-dev.2892367644
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/example/00_clear_test_data.sql +1 -0
- package/db/example/09_enapo_consumption_history.sql +42 -0
- package/db/migrations/postgresql/20260923090000-enapo-consumption-history-eb.js +53 -0
- package/db/migrations/postgresql/sqls/20260923090000-enapo-consumption-history-eb-down.sql +452 -0
- package/db/migrations/postgresql/sqls/20260923090000-enapo-consumption-history-eb-up.sql +511 -0
- package/dist/integration-engine/enapo/repositories/AbstractMaterializedViewIndexRepository.d.ts +1 -0
- package/dist/integration-engine/enapo/repositories/AbstractMaterializedViewIndexRepository.js +10 -1
- package/dist/integration-engine/enapo/repositories/AbstractMaterializedViewIndexRepository.js.map +1 -1
- package/dist/output-gateway/models/interfaces/IEnapoOmConsumptionView.d.ts +1 -1
- package/dist/output-gateway/routers/interfaces/IEnoBuildingDetailResponse.d.ts +18 -0
- package/dist/output-gateway/transformations/EnoBuildingDetailTransformation.d.ts +2 -0
- package/dist/output-gateway/transformations/EnoBuildingDetailTransformation.js +18 -5
- package/dist/output-gateway/transformations/EnoBuildingDetailTransformation.js.map +1 -1
- package/dist/output-gateway/transformations/helpers/ConsumptionHistoryBuilder.d.ts +16 -1
- package/dist/output-gateway/transformations/helpers/ConsumptionHistoryBuilder.js +114 -4
- package/dist/output-gateway/transformations/helpers/ConsumptionHistoryBuilder.js.map +1 -1
- package/docs/implementation_documentation.md +3 -1
- package/docs/openapi-output.yaml +56 -1
- package/package.json +1 -1
|
@@ -26,6 +26,7 @@ truncate enapo_ppas_commercial_invoice_installations;
|
|
|
26
26
|
truncate enapo_ppas_commercial_invoice_device;
|
|
27
27
|
truncate enapo_ppas_commercial_invoice_price;
|
|
28
28
|
truncate enapo_budova_spotrebic;
|
|
29
|
+
truncate enapo_eb_consumption;
|
|
29
30
|
truncate enapo_porsenna_buildings;
|
|
30
31
|
truncate enapo_porsenna_devices;
|
|
31
32
|
truncate enapo_porsenna_consumption;
|
|
@@ -187,5 +187,47 @@ INSERT INTO enapo_porsenna_consumption (source, device_id, "var", "period", valu
|
|
|
187
187
|
('prague-7', '9a000000-0000-0000-0000-000000000002', 'monthly', '2025-13', 5.0, 'm3', 'water', 30);
|
|
188
188
|
|
|
189
189
|
|
|
190
|
+
-- =================================================================================================
|
|
191
|
+
-- Energy Broker history (enapo_eb_consumption), on its own GID so GIDCH1 keeps one electricity series
|
|
192
|
+
-- =================================================================================================
|
|
193
|
+
|
|
194
|
+
INSERT INTO eno_budova (gid, zdroj, nazev, oznaceni, platnost_od) VALUES
|
|
195
|
+
('GIDCH4', 'mhmp', 'Objekt s historií EB', 'OHE', '2019-01-01');
|
|
196
|
+
|
|
197
|
+
INSERT INTO enapo_gid_mapping (place_id, id_type, commodity, gid, is_first_gid, link_source, valid_from, valid_to) VALUES
|
|
198
|
+
('859182400000000401', 'ean', 'electricity', 'GIDCH4', true, 'mock', '2022-01-01', NULL),
|
|
199
|
+
('27ZG100Z0000CH41', 'eic', 'gas', 'GIDCH4', true, 'mock', '2022-01-01', NULL);
|
|
200
|
+
|
|
201
|
+
-- Values are MWh; the view serves kWh.
|
|
202
|
+
INSERT INTO enapo_eb_consumption (idodbmisto_rspotrebyfa, idodbmistoom, ean_eic, komodita, datum_od, datum_do,
|
|
203
|
+
spotreba_vt, spotreba_nt, spotreba_mwh, rok) VALUES
|
|
204
|
+
-- 2026-01: 1500 kWh. The second row is the same month under a re-registered OM (production has
|
|
205
|
+
-- these with identical values) - it must not double the month to 3000.
|
|
206
|
+
(9000001, 90401, '859182400000000401', 1, '2026-01-01', '2026-01-31', 1.0, 0.5, 1.5, 2026),
|
|
207
|
+
(9000002, 90402, '859182400000000401', 1, '2026-01-01', '2026-01-31', 1.0, 0.5, 1.5, 2026),
|
|
208
|
+
-- 2026-02: 800 kWh, kept although PRE reports 10 days of February - a partial live month loses.
|
|
209
|
+
(9000003, 90401, '859182400000000401', 1, '2026-02-01', '2026-02-28', 0.5, 0.3, 0.8, 2026),
|
|
210
|
+
-- 2026-03: PRE read the whole month (310 kWh), so this 9000 kWh must not show up anywhere.
|
|
211
|
+
(9000004, 90401, '859182400000000401', 1, '2026-03-01', '2026-03-31', 6.0, 3.0, 9.0, 2026),
|
|
212
|
+
-- Two-month and mid-month billing periods are out of scope and must be skipped, not allocated.
|
|
213
|
+
(9000005, 90401, '859182400000000401', 1, '2025-11-01', '2025-12-31', 4.0, 2.0, 6.0, 2025),
|
|
214
|
+
(9000006, 90401, '859182400000000401', 1, '2025-10-15', '2025-10-31', 1.0, 0.0, 1.0, 2025),
|
|
215
|
+
-- Gas: kWh as well, EB has no combustion heat to derive m3.
|
|
216
|
+
(9000007, 90403, '27ZG100Z0000CH41', 2, '2026-01-01', '2026-01-31', 12.345, NULL, 12.345, 2026);
|
|
217
|
+
|
|
218
|
+
-- The PPAS meter reads all of January too, but in m3 only: EB must still serve the month, being its only kWh.
|
|
219
|
+
INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number) VALUES
|
|
220
|
+
('27ZG100Z0000CH41', 'eic', 1200.0, '2026-02-01 01:00:00+01', 'corediff', 'ppas_ave_api', '9900041', 'GAS-CH-4'),
|
|
221
|
+
('27ZG100Z0000CH41', 'eic', 600000.0, '2026-01-01 01:00:00+01', 'core', 'ppas_ave_api', '9900041', 'GAS-CH-4'),
|
|
222
|
+
('27ZG100Z0000CH41', 'eic', 601200.0, '2026-02-01 01:00:00+01', 'core', 'ppas_ave_api', '9900041', 'GAS-CH-4');
|
|
223
|
+
|
|
224
|
+
INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number)
|
|
225
|
+
SELECT '859182400000000401', 'ean', 10.0, d, 'EActi_VT_NT_DaySum', 'predi_input', '8110000401', 'EL-CH-4'
|
|
226
|
+
FROM generate_series(timestamptz '2026-03-01 00:00:00+00', timestamptz '2026-03-31 00:00:00+00', interval '1 day') d;
|
|
227
|
+
INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number)
|
|
228
|
+
SELECT '859182400000000401', 'ean', 10.0, d, 'EActi_VT_NT_DaySum', 'predi_input', '8110000401', 'EL-CH-4'
|
|
229
|
+
FROM generate_series(timestamptz '2026-02-19 00:00:00+00', timestamptz '2026-02-28 00:00:00+00', interval '1 day') d;
|
|
230
|
+
|
|
231
|
+
|
|
190
232
|
REFRESH MATERIALIZED VIEW mv_ded_building_search;
|
|
191
233
|
REFRESH MATERIALIZED VIEW mv_enapo_om_consumption;
|
|
@@ -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', '20260923090000-enapo-consumption-history-eb-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', '20260923090000-enapo-consumption-history-eb-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,452 @@
|
|
|
1
|
+
DROP MATERIALIZED VIEW IF EXISTS mv_enapo_om_consumption;
|
|
2
|
+
|
|
3
|
+
CREATE MATERIALIZED VIEW mv_enapo_om_consumption AS
|
|
4
|
+
WITH bounds AS (
|
|
5
|
+
SELECT
|
|
6
|
+
((date_trunc('month', now() AT TIME ZONE 'Europe/Prague') - interval '48 months')
|
|
7
|
+
AT TIME ZONE 'Europe/Prague') AS month_from_tstz,
|
|
8
|
+
(date_trunc('month', now() AT TIME ZONE 'Europe/Prague') - interval '48 months') AS month_from,
|
|
9
|
+
(date_trunc('month', now() AT TIME ZONE 'Europe/Prague')
|
|
10
|
+
AT TIME ZONE 'Europe/Prague') AS month_to_tstz,
|
|
11
|
+
date_trunc('month', now() AT TIME ZONE 'Europe/Prague') AS month_to
|
|
12
|
+
),
|
|
13
|
+
|
|
14
|
+
point AS (
|
|
15
|
+
SELECT DISTINCT gid, place_id, id_type, commodity
|
|
16
|
+
FROM v_enapo_building_consumption_points
|
|
17
|
+
),
|
|
18
|
+
|
|
19
|
+
electricity_metered AS (
|
|
20
|
+
SELECT
|
|
21
|
+
em.id AS place_id,
|
|
22
|
+
date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date AS period_start,
|
|
23
|
+
sum(em.value) FILTER (WHERE em.var = 'EActi_VT_NT_DaySum') AS total_kwh,
|
|
24
|
+
sum(em.value) FILTER (WHERE em.var = 'EActi_VT_DaySum') AS vt_kwh,
|
|
25
|
+
sum(em.value) FILTER (WHERE em.var = 'EActi_NT_DaySum') AS nt_kwh,
|
|
26
|
+
-- Coverage is the max day-count across the three vars, not just the total's.
|
|
27
|
+
GREATEST(
|
|
28
|
+
count(*) FILTER (WHERE em.var = 'EActi_VT_NT_DaySum'),
|
|
29
|
+
count(*) FILTER (WHERE em.var = 'EActi_VT_DaySum'),
|
|
30
|
+
count(*) FILTER (WHERE em.var = 'EActi_NT_DaySum')
|
|
31
|
+
) AS days_covered
|
|
32
|
+
FROM enapo_measurements em
|
|
33
|
+
CROSS JOIN bounds b
|
|
34
|
+
WHERE em.source = 'predi_input'
|
|
35
|
+
AND em.var IN ('EActi_VT_NT_DaySum', 'EActi_VT_DaySum', 'EActi_NT_DaySum')
|
|
36
|
+
AND em."timestamp" >= b.month_from_tstz
|
|
37
|
+
AND em."timestamp" < b.month_to_tstz
|
|
38
|
+
GROUP BY em.id, date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date
|
|
39
|
+
),
|
|
40
|
+
|
|
41
|
+
gas_registration AS (
|
|
42
|
+
SELECT DISTINCT ON (em.id, em.device_serial_number)
|
|
43
|
+
em.id,
|
|
44
|
+
em.device_serial_number,
|
|
45
|
+
em.place_id
|
|
46
|
+
FROM enapo_measurements em
|
|
47
|
+
CROSS JOIN bounds b
|
|
48
|
+
WHERE em.source = 'ppas_ave_api'
|
|
49
|
+
AND em.var IN ('core2', 'corediff')
|
|
50
|
+
AND em."timestamp" >= b.month_from_tstz
|
|
51
|
+
GROUP BY em.id, em.device_serial_number, em.place_id
|
|
52
|
+
ORDER BY em.id, em.device_serial_number, max(em."timestamp") DESC, em.place_id DESC
|
|
53
|
+
),
|
|
54
|
+
gas_core_month AS (
|
|
55
|
+
-- Cumulative 'core' reading (m3) at the first measured hour of each first-of-month day, kept
|
|
56
|
+
-- per (id, device_serial_number, place_id) - an id can carry several registrations (see
|
|
57
|
+
-- gas_registration) whose diffs must be summed separately, not collapsed into one reading.
|
|
58
|
+
SELECT DISTINCT ON (em.id, em.device_serial_number, em.place_id,
|
|
59
|
+
date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague'))
|
|
60
|
+
em.id,
|
|
61
|
+
em.device_serial_number,
|
|
62
|
+
em.place_id,
|
|
63
|
+
date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date AS month_start,
|
|
64
|
+
em.value
|
|
65
|
+
FROM enapo_measurements em
|
|
66
|
+
CROSS JOIN bounds b
|
|
67
|
+
INNER JOIN gas_registration gr
|
|
68
|
+
ON gr.id = em.id
|
|
69
|
+
AND gr.device_serial_number = em.device_serial_number
|
|
70
|
+
AND gr.place_id = em.place_id
|
|
71
|
+
WHERE em.source = 'ppas_ave_api'
|
|
72
|
+
AND em.var = 'core'
|
|
73
|
+
AND (em."timestamp" AT TIME ZONE 'Europe/Prague')::date
|
|
74
|
+
= date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date
|
|
75
|
+
AND em."timestamp" >= b.month_from_tstz
|
|
76
|
+
AND em."timestamp" < (b.month_to_tstz + interval '1 month')
|
|
77
|
+
ORDER BY em.id, em.device_serial_number, em.place_id,
|
|
78
|
+
date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague'), em."timestamp"
|
|
79
|
+
),
|
|
80
|
+
gas_metered AS (
|
|
81
|
+
SELECT
|
|
82
|
+
cur.id AS place_id,
|
|
83
|
+
cur.month_start AS period_start,
|
|
84
|
+
-- A negative diff means the meter was exchanged/reset between the two boundary readings -
|
|
85
|
+
-- null rather than a nonsense negative volume.
|
|
86
|
+
(CASE WHEN sum(nxt.value - cur.value) < 0 THEN NULL ELSE sum(nxt.value - cur.value) END) AS m3_sum,
|
|
87
|
+
((cur.month_start + interval '1 month')::date - cur.month_start) AS days_covered
|
|
88
|
+
FROM gas_core_month cur
|
|
89
|
+
INNER JOIN gas_core_month nxt
|
|
90
|
+
ON nxt.id = cur.id
|
|
91
|
+
AND nxt.device_serial_number = cur.device_serial_number
|
|
92
|
+
AND nxt.place_id = cur.place_id
|
|
93
|
+
AND nxt.month_start = (cur.month_start + interval '1 month')::date
|
|
94
|
+
GROUP BY cur.id, cur.month_start
|
|
95
|
+
),
|
|
96
|
+
|
|
97
|
+
distribution_device AS (
|
|
98
|
+
SELECT DISTINCT ON (ins.eic, d.place_id, d.device_serial_number, d.date_from, d.date_to, d.kind, d.unit)
|
|
99
|
+
ins.eic,
|
|
100
|
+
d.place_id,
|
|
101
|
+
d.invoice_id,
|
|
102
|
+
d.date_from,
|
|
103
|
+
d.date_to,
|
|
104
|
+
d.kind,
|
|
105
|
+
d.unit,
|
|
106
|
+
d.consumption,
|
|
107
|
+
d.normometers_nm3,
|
|
108
|
+
d.gas_consumption_kwh,
|
|
109
|
+
d.combustion_heat
|
|
110
|
+
FROM enapo_ppas_distribution_invoice_device d
|
|
111
|
+
INNER JOIN enapo_ppas_distribution_invoice i
|
|
112
|
+
ON i.id = d.invoice_id
|
|
113
|
+
INNER JOIN enapo_ppas_distribution_installation ins
|
|
114
|
+
ON ins.invoice_id = d.invoice_id
|
|
115
|
+
AND ins.customer_id = i.customer_id
|
|
116
|
+
AND ins.place_id = d.place_id
|
|
117
|
+
WHERE COALESCE(i.is_canceled, false) = false
|
|
118
|
+
AND d.date_to >= d.date_from
|
|
119
|
+
AND d.consumption IS NOT NULL
|
|
120
|
+
AND d.kind IN ('ZIZWC', 'ZIABN3')
|
|
121
|
+
AND d.reading_type = 'STANDARD'
|
|
122
|
+
AND NOT EXISTS (
|
|
123
|
+
SELECT 1 FROM enapo_ppas_distribution_invoice s
|
|
124
|
+
WHERE s.preceding_id = i.id AND COALESCE(s.is_canceled, false) = false
|
|
125
|
+
)
|
|
126
|
+
ORDER BY ins.eic, d.place_id, d.device_serial_number, d.date_from, d.date_to, d.kind, d.unit,
|
|
127
|
+
i.facts_doc_date DESC, i.id DESC
|
|
128
|
+
),
|
|
129
|
+
commercial_device AS (
|
|
130
|
+
SELECT DISTINCT ON (ins.eic, d.place_id, d.device_serial_number, d.date_from, d.date_to, d.kind, d.unit)
|
|
131
|
+
ins.eic,
|
|
132
|
+
d.place_id,
|
|
133
|
+
d.invoice_id,
|
|
134
|
+
d.date_from,
|
|
135
|
+
d.date_to,
|
|
136
|
+
d.kind,
|
|
137
|
+
d.unit,
|
|
138
|
+
d.consumption,
|
|
139
|
+
d.normometers_nm3,
|
|
140
|
+
d.gas_consumption_kwh,
|
|
141
|
+
d.combustion_heat
|
|
142
|
+
FROM enapo_ppas_commercial_invoice_device d
|
|
143
|
+
INNER JOIN enapo_ppas_commercial_invoice i
|
|
144
|
+
ON i.id = d.invoice_id
|
|
145
|
+
INNER JOIN enapo_ppas_commercial_invoice_installations ins
|
|
146
|
+
ON ins.invoice_id = d.invoice_id
|
|
147
|
+
AND ins.customer_id = i.customer_id
|
|
148
|
+
AND ins.place_id = d.place_id
|
|
149
|
+
WHERE COALESCE(i.is_canceled, false) = false
|
|
150
|
+
AND d.date_to >= d.date_from
|
|
151
|
+
AND d.consumption IS NOT NULL
|
|
152
|
+
AND d.reading_type = 'STANDARD'
|
|
153
|
+
AND NOT EXISTS (
|
|
154
|
+
SELECT 1 FROM enapo_ppas_commercial_invoice s
|
|
155
|
+
WHERE s.preceding_id = i.id AND COALESCE(s.is_canceled, false) = false
|
|
156
|
+
)
|
|
157
|
+
ORDER BY ins.eic, d.place_id, d.device_serial_number, d.date_from, d.date_to, d.kind, d.unit,
|
|
158
|
+
i.facts_doc_date DESC, i.id DESC
|
|
159
|
+
),
|
|
160
|
+
|
|
161
|
+
invoice_device_month AS (
|
|
162
|
+
SELECT
|
|
163
|
+
'ppas_distribution_invoice' AS source,
|
|
164
|
+
d.eic AS place_id,
|
|
165
|
+
d.invoice_id,
|
|
166
|
+
gs.month::date AS period_start,
|
|
167
|
+
CASE WHEN d.unit = 'KWH' THEN NULL ELSE d.consumption END * o.share AS volume,
|
|
168
|
+
CASE WHEN d.unit = 'NM3' THEN 'Nm3' ELSE 'm3' END AS volume_unit,
|
|
169
|
+
d.normometers_nm3 * o.share AS nm3,
|
|
170
|
+
NULLIF(d.gas_consumption_kwh, 0) * o.share AS kwh,
|
|
171
|
+
(o.share < 1) AS is_estimated,
|
|
172
|
+
o.overlap_days AS days_covered
|
|
173
|
+
FROM distribution_device d
|
|
174
|
+
CROSS JOIN bounds b
|
|
175
|
+
CROSS JOIN LATERAL generate_series(
|
|
176
|
+
date_trunc('month', d.date_from::timestamp),
|
|
177
|
+
date_trunc('month', d.date_to::timestamp),
|
|
178
|
+
interval '1 month'
|
|
179
|
+
) gs(month)
|
|
180
|
+
CROSS JOIN LATERAL (
|
|
181
|
+
SELECT
|
|
182
|
+
ov.overlap_days,
|
|
183
|
+
ov.overlap_days::numeric / NULLIF(d.date_to - d.date_from + 1, 0) AS share
|
|
184
|
+
FROM (
|
|
185
|
+
SELECT LEAST(d.date_to, (gs.month + interval '1 month - 1 day')::date)
|
|
186
|
+
- GREATEST(d.date_from, gs.month::date) + 1 AS overlap_days
|
|
187
|
+
) ov
|
|
188
|
+
) o
|
|
189
|
+
WHERE gs.month >= b.month_from AND gs.month < b.month_to
|
|
190
|
+
|
|
191
|
+
UNION ALL
|
|
192
|
+
|
|
193
|
+
SELECT
|
|
194
|
+
'ppas_commercial_invoice',
|
|
195
|
+
d.eic,
|
|
196
|
+
d.invoice_id,
|
|
197
|
+
gs.month::date,
|
|
198
|
+
CASE WHEN d.unit = 'KWH' THEN NULL ELSE d.consumption END * o.share,
|
|
199
|
+
CASE WHEN d.unit = 'NM3' THEN 'Nm3' ELSE 'm3' END,
|
|
200
|
+
d.normometers_nm3 * o.share,
|
|
201
|
+
NULLIF(d.gas_consumption_kwh, 0) * o.share,
|
|
202
|
+
(o.share < 1),
|
|
203
|
+
o.overlap_days
|
|
204
|
+
FROM commercial_device d
|
|
205
|
+
CROSS JOIN bounds b
|
|
206
|
+
CROSS JOIN LATERAL generate_series(
|
|
207
|
+
date_trunc('month', d.date_from::timestamp),
|
|
208
|
+
date_trunc('month', d.date_to::timestamp),
|
|
209
|
+
interval '1 month'
|
|
210
|
+
) gs(month)
|
|
211
|
+
CROSS JOIN LATERAL (
|
|
212
|
+
SELECT
|
|
213
|
+
ov.overlap_days,
|
|
214
|
+
ov.overlap_days::numeric / NULLIF(d.date_to - d.date_from + 1, 0) AS share
|
|
215
|
+
FROM (
|
|
216
|
+
SELECT LEAST(d.date_to, (gs.month + interval '1 month - 1 day')::date)
|
|
217
|
+
- GREATEST(d.date_from, gs.month::date) + 1 AS overlap_days
|
|
218
|
+
) ov
|
|
219
|
+
) o
|
|
220
|
+
WHERE gs.month >= b.month_from AND gs.month < b.month_to
|
|
221
|
+
),
|
|
222
|
+
invoice_month AS (
|
|
223
|
+
SELECT
|
|
224
|
+
source,
|
|
225
|
+
place_id,
|
|
226
|
+
period_start,
|
|
227
|
+
sum(volume) AS volume,
|
|
228
|
+
max(volume_unit) AS volume_unit,
|
|
229
|
+
sum(nm3) AS nm3,
|
|
230
|
+
sum(kwh) AS kwh,
|
|
231
|
+
bool_or(is_estimated) AS is_estimated,
|
|
232
|
+
-- Clamp to the month's length: sum() would exceed it when meters are billed in parallel.
|
|
233
|
+
LEAST(
|
|
234
|
+
sum(days_covered),
|
|
235
|
+
(period_start + interval '1 month')::date - period_start
|
|
236
|
+
) AS days_covered,
|
|
237
|
+
array_agg(DISTINCT invoice_id::text ORDER BY invoice_id::text) AS invoice_ids
|
|
238
|
+
FROM invoice_device_month
|
|
239
|
+
GROUP BY source, place_id, period_start
|
|
240
|
+
),
|
|
241
|
+
invoice_year AS (
|
|
242
|
+
SELECT
|
|
243
|
+
source,
|
|
244
|
+
place_id,
|
|
245
|
+
date_trunc('year', period_start)::date AS period_start,
|
|
246
|
+
array_agg(DISTINCT invoice_id::text ORDER BY invoice_id::text) AS invoice_ids
|
|
247
|
+
FROM invoice_device_month
|
|
248
|
+
GROUP BY source, place_id, date_trunc('year', period_start)::date
|
|
249
|
+
),
|
|
250
|
+
|
|
251
|
+
porsenna_period AS (
|
|
252
|
+
SELECT *
|
|
253
|
+
FROM (
|
|
254
|
+
SELECT
|
|
255
|
+
d.identifier AS place_id,
|
|
256
|
+
c.commodity,
|
|
257
|
+
c."var" AS period_type,
|
|
258
|
+
f_enapo_safe_period(c."var", c."period") AS period_start,
|
|
259
|
+
c.value,
|
|
260
|
+
c.unit,
|
|
261
|
+
c."count" AS days_covered
|
|
262
|
+
FROM enapo_porsenna_consumption c
|
|
263
|
+
INNER JOIN enapo_porsenna_devices d
|
|
264
|
+
ON d.source = c.source AND d.id = c.device_id
|
|
265
|
+
WHERE d.identifier IS NOT NULL
|
|
266
|
+
AND (d."level" IS NULL OR d."level" = 'main')
|
|
267
|
+
AND c.commodity IS NOT NULL
|
|
268
|
+
AND c."var" IN ('yearly', 'monthly')
|
|
269
|
+
) parsed
|
|
270
|
+
WHERE parsed.period_start IS NOT NULL
|
|
271
|
+
),
|
|
272
|
+
|
|
273
|
+
monthly AS (
|
|
274
|
+
SELECT
|
|
275
|
+
p.gid,
|
|
276
|
+
p.place_id,
|
|
277
|
+
p.id_type,
|
|
278
|
+
p.commodity,
|
|
279
|
+
'predi_input'::varchar(50) AS source,
|
|
280
|
+
'monthly'::varchar(10) AS period_type,
|
|
281
|
+
e.period_start,
|
|
282
|
+
COALESCE(e.total_kwh, e.vt_kwh + e.nt_kwh, e.vt_kwh, e.nt_kwh) AS value,
|
|
283
|
+
'kWh'::varchar(10) AS unit,
|
|
284
|
+
COALESCE(e.total_kwh, e.vt_kwh + e.nt_kwh, e.vt_kwh, e.nt_kwh) AS value_kwh,
|
|
285
|
+
'EActi_VT_NT_DaySum'::varchar(50) AS internal_source_var,
|
|
286
|
+
e.days_covered,
|
|
287
|
+
false AS is_estimated,
|
|
288
|
+
'{}'::text[] AS invoice_ids
|
|
289
|
+
FROM point p
|
|
290
|
+
INNER JOIN electricity_metered e ON e.place_id = p.place_id
|
|
291
|
+
WHERE p.commodity = 'electricity'
|
|
292
|
+
|
|
293
|
+
UNION ALL
|
|
294
|
+
|
|
295
|
+
SELECT
|
|
296
|
+
p.gid,
|
|
297
|
+
p.place_id,
|
|
298
|
+
p.id_type,
|
|
299
|
+
p.commodity,
|
|
300
|
+
'ppas_ave_api',
|
|
301
|
+
'monthly',
|
|
302
|
+
g.period_start,
|
|
303
|
+
g.m3_sum,
|
|
304
|
+
'm3',
|
|
305
|
+
NULL::numeric,
|
|
306
|
+
'core',
|
|
307
|
+
g.days_covered,
|
|
308
|
+
false,
|
|
309
|
+
'{}'::text[]
|
|
310
|
+
FROM point p
|
|
311
|
+
INNER JOIN gas_metered g ON g.place_id = p.place_id
|
|
312
|
+
WHERE p.commodity = 'gas'
|
|
313
|
+
|
|
314
|
+
UNION ALL
|
|
315
|
+
|
|
316
|
+
SELECT
|
|
317
|
+
p.gid,
|
|
318
|
+
p.place_id,
|
|
319
|
+
p.id_type,
|
|
320
|
+
p.commodity,
|
|
321
|
+
i.source,
|
|
322
|
+
'monthly',
|
|
323
|
+
i.period_start,
|
|
324
|
+
COALESCE(i.volume, i.nm3::numeric),
|
|
325
|
+
CASE WHEN i.volume IS NOT NULL THEN i.volume_unit ELSE 'Nm3' END,
|
|
326
|
+
i.kwh,
|
|
327
|
+
NULL,
|
|
328
|
+
i.days_covered,
|
|
329
|
+
i.is_estimated,
|
|
330
|
+
i.invoice_ids
|
|
331
|
+
FROM point p
|
|
332
|
+
INNER JOIN invoice_month i ON i.place_id = p.place_id
|
|
333
|
+
WHERE p.commodity = 'gas'
|
|
334
|
+
|
|
335
|
+
),
|
|
336
|
+
|
|
337
|
+
yearly_rollup AS (
|
|
338
|
+
SELECT
|
|
339
|
+
gid,
|
|
340
|
+
place_id,
|
|
341
|
+
id_type,
|
|
342
|
+
commodity,
|
|
343
|
+
source,
|
|
344
|
+
'yearly'::varchar(10) AS period_type,
|
|
345
|
+
date_trunc('year', period_start)::date AS period_start,
|
|
346
|
+
sum(value) AS value,
|
|
347
|
+
unit,
|
|
348
|
+
-- count(*) = count(value_kwh) is an all-non-null check: only sum value_kwh when every month has one.
|
|
349
|
+
CASE WHEN count(*) = count(value_kwh) THEN sum(value_kwh) END AS value_kwh,
|
|
350
|
+
max(internal_source_var) AS internal_source_var,
|
|
351
|
+
sum(days_covered) AS days_covered,
|
|
352
|
+
bool_or(is_estimated) AS is_estimated
|
|
353
|
+
FROM monthly m
|
|
354
|
+
GROUP BY gid, place_id, id_type, commodity, source, date_trunc('year', period_start)::date, unit
|
|
355
|
+
),
|
|
356
|
+
yearly AS (
|
|
357
|
+
SELECT
|
|
358
|
+
r.gid,
|
|
359
|
+
r.place_id,
|
|
360
|
+
r.id_type,
|
|
361
|
+
r.commodity,
|
|
362
|
+
r.source,
|
|
363
|
+
r.period_type,
|
|
364
|
+
r.period_start,
|
|
365
|
+
r.value,
|
|
366
|
+
r.unit,
|
|
367
|
+
r.value_kwh,
|
|
368
|
+
r.internal_source_var,
|
|
369
|
+
r.days_covered,
|
|
370
|
+
r.is_estimated,
|
|
371
|
+
COALESCE(iy.invoice_ids, '{}'::text[]) AS invoice_ids
|
|
372
|
+
FROM yearly_rollup r
|
|
373
|
+
LEFT JOIN invoice_year iy
|
|
374
|
+
ON iy.source = r.source AND iy.place_id = r.place_id AND iy.period_start = r.period_start
|
|
375
|
+
|
|
376
|
+
UNION ALL
|
|
377
|
+
|
|
378
|
+
SELECT
|
|
379
|
+
p.gid,
|
|
380
|
+
p.place_id,
|
|
381
|
+
p.id_type,
|
|
382
|
+
p.commodity,
|
|
383
|
+
'porsenna',
|
|
384
|
+
'yearly',
|
|
385
|
+
pp.period_start,
|
|
386
|
+
pp.value,
|
|
387
|
+
pp.unit,
|
|
388
|
+
CASE WHEN lower(pp.unit) = 'kwh' THEN pp.value ELSE NULL END,
|
|
389
|
+
NULL,
|
|
390
|
+
pp.days_covered,
|
|
391
|
+
false,
|
|
392
|
+
'{}'::text[]
|
|
393
|
+
FROM point p
|
|
394
|
+
INNER JOIN porsenna_period pp
|
|
395
|
+
ON pp.place_id = p.place_id AND pp.commodity = p.commodity
|
|
396
|
+
WHERE pp.period_type = 'yearly'
|
|
397
|
+
)
|
|
398
|
+
|
|
399
|
+
SELECT
|
|
400
|
+
gid::varchar AS gid,
|
|
401
|
+
place_id::varchar(50) AS place_id,
|
|
402
|
+
max(id_type)::varchar(10) AS id_type,
|
|
403
|
+
commodity::varchar(20) AS commodity,
|
|
404
|
+
source::varchar(50) AS source,
|
|
405
|
+
period_type::varchar(10) AS period_type,
|
|
406
|
+
period_start,
|
|
407
|
+
sum(value)::numeric(30, 6) AS value,
|
|
408
|
+
unit::varchar(10) AS unit,
|
|
409
|
+
(CASE WHEN count(*) = count(value_kwh) THEN sum(value_kwh) END)::numeric(30, 6) AS value_kwh,
|
|
410
|
+
max(internal_source_var)::varchar(50) AS internal_source_var,
|
|
411
|
+
COALESCE(max(invoice_ids), '{}'::text[]) AS invoice_ids,
|
|
412
|
+
max(days_covered)::int AS coverage_count,
|
|
413
|
+
(
|
|
414
|
+
CASE period_type
|
|
415
|
+
WHEN 'yearly' THEN (make_date(extract(year FROM period_start)::int + 1, 1, 1) - period_start)
|
|
416
|
+
ELSE (period_start + interval '1 month')::date - period_start
|
|
417
|
+
END
|
|
418
|
+
)::int AS expected_count,
|
|
419
|
+
bool_or(is_estimated) AS is_estimated
|
|
420
|
+
FROM (
|
|
421
|
+
SELECT * FROM monthly
|
|
422
|
+
UNION ALL
|
|
423
|
+
SELECT * FROM yearly
|
|
424
|
+
) all_periods
|
|
425
|
+
WHERE value IS NOT NULL
|
|
426
|
+
GROUP BY gid, place_id, commodity, source, unit, period_type, period_start;
|
|
427
|
+
|
|
428
|
+
|
|
429
|
+
COMMENT ON MATERIALIZED VIEW mv_enapo_om_consumption IS
|
|
430
|
+
'Monthly and yearly consumption per consumption point per source, for the last 48 months, behind the DeD building detail endpoint. One row per (gid, place_id, commodity, source, unit, period_type, period_start). Refreshed by EnapoConsumptionHistoryRefreshTask (integration engine).';
|
|
431
|
+
|
|
432
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.place_id IS
|
|
433
|
+
'EAN / EIC / OM code, as in enapo_gid_mapping.place_id - NOT enapo_measurements.place_id, which holds a PPAS-internal place id. Measurements are joined through enapo_measurements.id.';
|
|
434
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.value IS
|
|
435
|
+
'Consumption for the period in `unit`. Electricity is kWh (the upstream T1_Wh_total naming is wrong); metered gas is m3, the difference between the cumulative `core` reading on the first day of the next month and the first day of the current month; invoiced gas follows the invoice unit; Porsenna carries its own unit.';
|
|
436
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.value_kwh IS
|
|
437
|
+
'Derived comparable energy: for gas, normometers_nm3 * combustion_heat (verified exact). NULL when the conversion inputs are missing - `value` + `unit` stay authoritative. Always NULL for metered gas (no normometers_nm3 input).';
|
|
438
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.internal_source_var IS
|
|
439
|
+
'Which enapo_measurements.var produced the row; NULL for invoice and Porsenna rows. Provenance for debugging, not part of the public response.';
|
|
440
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.invoice_ids IS
|
|
441
|
+
'Billing documents the row was allocated from, sorted and deduplicated; empty for metered and Porsenna rows. A period can be allocated from several invoices (a yearly roll-up usually is), which is why it is an array.';
|
|
442
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.coverage_count IS
|
|
443
|
+
'Days of the period that carry data. Days for every commodity and both period types, so it is comparable with expected_count.';
|
|
444
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.expected_count IS 'Days in the period (28-31, or 365/366).';
|
|
445
|
+
COMMENT ON COLUMN mv_enapo_om_consumption.is_estimated IS
|
|
446
|
+
'True when the value was allocated pro rata from a billing period spanning more than one month, rather than read for the period itself.';
|
|
447
|
+
|
|
448
|
+
CREATE UNIQUE INDEX mv_enapo_om_consumption_uidx
|
|
449
|
+
ON mv_enapo_om_consumption (gid, place_id, commodity, source, unit, period_type, period_start);
|
|
450
|
+
|
|
451
|
+
CREATE INDEX mv_enapo_om_consumption_gid_period_idx
|
|
452
|
+
ON mv_enapo_om_consumption (gid, period_type, period_start);
|