@golemio/energetics 1.13.1-dev.2809036872 → 1.14.0-rc.2809913491

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.
@@ -67,48 +67,43 @@ FROM generate_series(timestamptz '2026-03-01 00:00:00+00', timestamptz '2026-03-
67
67
  -- =================================================================================================
68
68
  -- Gas, metered (PPAS InternetAVE)
69
69
  -- =================================================================================================
70
+ -- Value is core(first day of next month) - core(first day of current month), core being the
71
+ -- cumulative meter state in m3. The corediff rows below carry no weight in that computation any
72
+ -- more; they exist only to seed gas_registration, which picks the (device_serial_number, place_id)
73
+ -- an EIC's 'core' readings must be joined through.
70
74
 
71
- -- 27ZG100Z0000CH01, 2026-07: corediff is the primary value -> 3 * 50 = 150 m3, over 3 distinct days.
72
- -- core2 is present and non-zero but must NOT win.
75
+ -- 27ZG100Z0000CH01: single registration, one meter.
73
76
  INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number) VALUES
74
- ('27ZG100Z0000CH01', 'eic', 50.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
75
- ('27ZG100Z0000CH01', 'eic', 50.0, '2026-07-02 01:00:00+02', 'corediff', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
76
- ('27ZG100Z0000CH01', 'eic', 50.0, '2026-07-03 01:00:00+02', 'corediff', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
77
- ('27ZG100Z0000CH01', 'eic', 55.0, '2026-07-01 01:00:00+02', 'core2', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
78
- ('27ZG100Z0000CH01', 'eic', 55.0, '2026-07-02 01:00:00+02', 'core2', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
79
- ('27ZG100Z0000CH01', 'eic', 55.0, '2026-07-03 01:00:00+02', 'core2', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
80
- -- the cumulative meter state: present, and deliberately unused (max - min across an EIC's
81
- -- registrations is the distance between unrelated counters, so it is not a fallback)
82
- ('27ZG100Z0000CH01', 'eic', 900000.0, '2026-07-01 01:00:00+02', 'core', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
83
- ('27ZG100Z0000CH01', 'eic', 900150.0, '2026-07-03 01:00:00+02', 'core', 'ppas_ave_api', '9900001', 'GAS-CH-1');
84
-
85
- -- 2026-06: corediff has data, core2 is present but ALL ZERO. 0 is not NULL, so a bare COALESCE
86
- -- would return a flat-zero Nm3 series here - exactly what production does for some EICs. The
87
- -- expected result is 120 m3 from corediff.
88
- INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number) VALUES
89
- ('27ZG100Z0000CH01', 'eic', 60.0, '2026-06-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
90
- ('27ZG100Z0000CH01', 'eic', 60.0, '2026-06-02 01:00:00+02', 'corediff', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
91
- ('27ZG100Z0000CH01', 'eic', 0.0, '2026-06-01 01:00:00+02', 'core2', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
92
- ('27ZG100Z0000CH01', 'eic', 0.0, '2026-06-02 01:00:00+02', 'core2', 'ppas_ave_api', '9900001', 'GAS-CH-1');
77
+ ('27ZG100Z0000CH01', 'eic', 50.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900001', 'GAS-CH-1');
93
78
 
94
- -- 2026-05: only core2. The series falls back to Nm3 for this month, which means one point can change
95
- -- unit mid-window and therefore appears as two series - honest, and why `unit` is part of the key.
79
+ -- Core readings on the first day of each month. 2026-05-01 is deliberately missing, so both April
80
+ -- (missing its "next month" boundary) and May (missing its "current month" boundary) come back
81
+ -- null - a diff needs both sides.
96
82
  INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number) VALUES
97
- ('27ZG100Z0000CH01', 'eic', 30.0, '2026-05-01 01:00:00+02', 'core2', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
98
- ('27ZG100Z0000CH01', 'eic', 30.0, '2026-05-02 01:00:00+02', 'core2', 'ppas_ave_api', '9900001', 'GAS-CH-1');
83
+ ('27ZG100Z0000CH01', 'eic', 900000.0, '2026-04-01 01:00:00+02', 'core', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
84
+ ('27ZG100Z0000CH01', 'eic', 900060.0, '2026-06-01 01:00:00+02', 'core', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
85
+ ('27ZG100Z0000CH01', 'eic', 900180.0, '2026-07-01 01:00:00+02', 'core', 'ppas_ave_api', '9900001', 'GAS-CH-1'),
86
+ ('27ZG100Z0000CH01', 'eic', 900330.0, '2026-08-01 01:00:00+02', 'core', 'ppas_ave_api', '9900001', 'GAS-CH-1');
87
+ -- June = 900180 - 900060 = 120; July = 900330 - 900180 = 150.
99
88
 
100
89
  -- 27ZG100Z0000CH02, 2026-07: TWO place_ids, ONE meter serial - two live registrations of the same
101
- -- physical meter, both reporting the same intervals. Expected 100 m3, NOT 200: the aggregate picks
102
- -- one registration per serial (the most recently reporting one, here 9900012).
90
+ -- physical meter. gas_registration picks one per serial (the most recently reporting one, here
91
+ -- 9900012), so only that place_id's core readings feed the diff: 100 m3, NOT 200.
103
92
  INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number) VALUES
104
93
  ('27ZG100Z0000CH02', 'eic', 100.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900011', 'GAS-CH-DUP'),
105
- ('27ZG100Z0000CH02', 'eic', 100.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900012', 'GAS-CH-DUP');
94
+ ('27ZG100Z0000CH02', 'eic', 100.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900012', 'GAS-CH-DUP'),
95
+ ('27ZG100Z0000CH02', 'eic', 500000.0, '2026-07-01 01:00:00+02', 'core', 'ppas_ave_api', '9900012', 'GAS-CH-DUP'),
96
+ ('27ZG100Z0000CH02', 'eic', 500100.0, '2026-08-01 01:00:00+02', 'core', 'ppas_ave_api', '9900012', 'GAS-CH-DUP');
106
97
 
107
98
  -- 27ZG100Z0000CH03, 2026-07: TWO place_ids, TWO serials - genuinely two meters on one delivery
108
- -- point. Expected 200 m3: deltas from distinct meters do add up.
99
+ -- point. Each registration gets its own diff and they add up: 100 + 100 = 200 m3.
109
100
  INSERT INTO enapo_measurements (id, id_type, value, "timestamp", var, source, place_id, device_serial_number) VALUES
110
101
  ('27ZG100Z0000CH03', 'eic', 100.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900021', 'GAS-CH-A'),
111
- ('27ZG100Z0000CH03', 'eic', 100.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900022', 'GAS-CH-B');
102
+ ('27ZG100Z0000CH03', 'eic', 100.0, '2026-07-01 01:00:00+02', 'corediff', 'ppas_ave_api', '9900022', 'GAS-CH-B'),
103
+ ('27ZG100Z0000CH03', 'eic', 700000.0, '2026-07-01 01:00:00+02', 'core', 'ppas_ave_api', '9900021', 'GAS-CH-A'),
104
+ ('27ZG100Z0000CH03', 'eic', 700100.0, '2026-08-01 01:00:00+02', 'core', 'ppas_ave_api', '9900021', 'GAS-CH-A'),
105
+ ('27ZG100Z0000CH03', 'eic', 800000.0, '2026-07-01 01:00:00+02', 'core', 'ppas_ave_api', '9900022', 'GAS-CH-B'),
106
+ ('27ZG100Z0000CH03', 'eic', 800100.0, '2026-08-01 01:00:00+02', 'core', 'ppas_ave_api', '9900022', 'GAS-CH-B');
112
107
 
113
108
 
114
109
  -- =================================================================================================
@@ -73,19 +73,17 @@ gas_registration AS (
73
73
  GROUP BY em.id, em.device_serial_number, em.place_id
74
74
  ORDER BY em.id, em.device_serial_number, max(em."timestamp") DESC, em.place_id DESC
75
75
  ),
76
- gas_metered AS (
77
- SELECT
78
- em.id AS place_id,
79
- date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date AS period_start,
80
- sum(em.value) FILTER (WHERE em.var = 'corediff') AS m3_sum,
81
- sum(em.value) FILTER (WHERE em.var = 'core2') AS nm3_sum,
82
- -- Coverage is the max day-count across corediff/core2, not just the winning var's.
83
- GREATEST(
84
- count(DISTINCT (em."timestamp" AT TIME ZONE 'Europe/Prague')::date)
85
- FILTER (WHERE em.var = 'corediff'),
86
- count(DISTINCT (em."timestamp" AT TIME ZONE 'Europe/Prague')::date)
87
- FILTER (WHERE em.var = 'core2')
88
- ) AS days_covered
76
+ gas_core_month AS (
77
+ -- Cumulative 'core' reading (m3) at the first measured hour of each first-of-month day, kept
78
+ -- per (id, device_serial_number, place_id) - an id can carry several registrations (see
79
+ -- gas_registration) whose diffs must be summed separately, not collapsed into one reading.
80
+ SELECT DISTINCT ON (em.id, em.device_serial_number, em.place_id,
81
+ date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague'))
82
+ em.id,
83
+ em.device_serial_number,
84
+ em.place_id,
85
+ date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date AS month_start,
86
+ em.value
89
87
  FROM enapo_measurements em
90
88
  CROSS JOIN bounds b
91
89
  INNER JOIN gas_registration gr
@@ -93,10 +91,29 @@ gas_metered AS (
93
91
  AND gr.device_serial_number = em.device_serial_number
94
92
  AND gr.place_id = em.place_id
95
93
  WHERE em.source = 'ppas_ave_api'
96
- AND em.var IN ('core2', 'corediff')
94
+ AND em.var = 'core'
95
+ AND (em."timestamp" AT TIME ZONE 'Europe/Prague')::date
96
+ = date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date
97
97
  AND em."timestamp" >= b.month_from_tstz
98
- AND em."timestamp" < b.month_to_tstz
99
- GROUP BY em.id, date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague')::date
98
+ AND em."timestamp" < (b.month_to_tstz + interval '1 month')
99
+ ORDER BY em.id, em.device_serial_number, em.place_id,
100
+ date_trunc('month', em."timestamp" AT TIME ZONE 'Europe/Prague'), em."timestamp"
101
+ ),
102
+ gas_metered AS (
103
+ SELECT
104
+ cur.id AS place_id,
105
+ cur.month_start AS period_start,
106
+ -- A negative diff means the meter was exchanged/reset between the two boundary readings -
107
+ -- null rather than a nonsense negative volume.
108
+ (CASE WHEN sum(nxt.value - cur.value) < 0 THEN NULL ELSE sum(nxt.value - cur.value) END) AS m3_sum,
109
+ ((cur.month_start + interval '1 month')::date - cur.month_start) AS days_covered
110
+ FROM gas_core_month cur
111
+ INNER JOIN gas_core_month nxt
112
+ ON nxt.id = cur.id
113
+ AND nxt.device_serial_number = cur.device_serial_number
114
+ AND nxt.place_id = cur.place_id
115
+ AND nxt.month_start = (cur.month_start + interval '1 month')::date
116
+ GROUP BY cur.id, cur.month_start
100
117
  ),
101
118
 
102
119
  distribution_device AS (
@@ -253,15 +270,6 @@ invoice_year AS (
253
270
  GROUP BY source, place_id, date_trunc('year', period_start)::date
254
271
  ),
255
272
 
256
- gas_combustion_heat AS (
257
- SELECT DISTINCT ON (d.eic)
258
- d.eic,
259
- NULLIF(d.combustion_heat, 0)::numeric AS heat_kwh_per_m3
260
- FROM distribution_device d
261
- WHERE NULLIF(d.combustion_heat, 0) IS NOT NULL
262
- ORDER BY d.eic, d.date_to DESC
263
- ),
264
-
265
273
  porsenna_period AS (
266
274
  SELECT *
267
275
  FROM (
@@ -314,16 +322,15 @@ monthly AS (
314
322
  'ppas_ave_api',
315
323
  'monthly',
316
324
  g.period_start,
317
- CASE WHEN g.m3_sum IS NOT NULL THEN g.m3_sum ELSE NULLIF(g.nm3_sum, 0) END,
318
- CASE WHEN g.m3_sum IS NOT NULL THEN 'm3' ELSE 'Nm3' END,
319
- (NULLIF(g.nm3_sum, 0) * ch.heat_kwh_per_m3)::numeric,
320
- CASE WHEN g.m3_sum IS NOT NULL THEN 'corediff' ELSE 'core2' END,
325
+ g.m3_sum,
326
+ 'm3',
327
+ NULL::numeric,
328
+ 'core',
321
329
  g.days_covered,
322
330
  false,
323
331
  '{}'::text[]
324
332
  FROM point p
325
333
  INNER JOIN gas_metered g ON g.place_id = p.place_id
326
- LEFT JOIN gas_combustion_heat ch ON ch.eic = p.place_id
327
334
  WHERE p.commodity = 'gas'
328
335
 
329
336
  UNION ALL
@@ -438,7 +445,9 @@ FROM (
438
445
  SELECT * FROM yearly
439
446
  ) all_periods
440
447
  WHERE value IS NOT NULL
441
- GROUP BY gid, place_id, commodity, source, unit, period_type, period_start;
448
+ GROUP BY gid, place_id, commodity, source, unit, period_type, period_start
449
+ with no data;
450
+
442
451
 
443
452
  COMMENT ON MATERIALIZED VIEW mv_enapo_om_consumption IS
444
453
  '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).';
@@ -446,9 +455,9 @@ COMMENT ON MATERIALIZED VIEW mv_enapo_om_consumption IS
446
455
  COMMENT ON COLUMN mv_enapo_om_consumption.place_id IS
447
456
  '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.';
448
457
  COMMENT ON COLUMN mv_enapo_om_consumption.value IS
449
- 'Consumption for the period in `unit`. Electricity is kWh (the upstream T1_Wh_total naming is wrong); metered gas is m3 from corediff, or Nm3 from core2 when corediff is absent; invoiced gas follows the invoice unit; Porsenna carries its own unit.';
458
+ '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.';
450
459
  COMMENT ON COLUMN mv_enapo_om_consumption.value_kwh IS
451
- 'Derived comparable energy: for gas, normometers_nm3 * combustion_heat (verified exact). NULL when the conversion inputs are missing - `value` + `unit` stay authoritative.';
460
+ '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).';
452
461
  COMMENT ON COLUMN mv_enapo_om_consumption.internal_source_var IS
453
462
  'Which enapo_measurements.var produced the row; NULL for invoice and Porsenna rows. Provenance for debugging, not part of the public response.';
454
463
  COMMENT ON COLUMN mv_enapo_om_consumption.invoice_ids IS
@@ -740,9 +740,9 @@ Sloupec `value` míchá **kumulativní stavy měřidla** a **rozdíly za interva
740
740
 
741
741
  | `source` | komodita | `var` | granularita | význam | jednotka | agregace |
742
742
  | -------------- | ----------- | ---------------------------------- | ----------- | -------------------------------------------------- | -------- | --------------------------------------------------- |
743
- | `ppas_ave_api` | plyn | `corediff` | hodinová | **rozdíl** (`OperatingDifference`) | m³ | `sum` ← **primární** |
744
- | `ppas_ave_api` | plyn | `core2` | hodinová | **rozdíl**, přepočtený (`ConvertDifference`) | Nm³ | `sum`, ale v 81 % řádků nula → jen fallback |
745
- | `ppas_ave_api` | plyn | `core` | hodinová | **kumulativní** stav měřidla (`OperatingAmount`) | m³ | **nepoužívat** (viz níže) |
743
+ | `ppas_ave_api` | plyn | `corediff` | hodinová | **rozdíl** (`OperatingDifference`) | m³ | `sum`, jen registrace (viz níže) |
744
+ | `ppas_ave_api` | plyn | `core2` | hodinová | **rozdíl**, přepočtený (`ConvertDifference`) | Nm³ | `sum`, jen registrace (viz níže) |
745
+ | `ppas_ave_api` | plyn | `core` | hodinová | **kumulativní** stav měřidla (`OperatingAmount`) | m³ | rozdíl 1. dne měsíce a 1. dne dalšího ← **primární v `mv_enapo_om_consumption.gas_metered`** |
746
746
  | `predi_input` | elektřina | `EActi_VT_NT_DaySum` | denní | **odvozený součet** VT+NT | **kWh** | `sum` ← **primární** |
747
747
  | `predi_input` | elektřina | `EActi_VT_DaySum` / `_NT_DaySum` | denní | denní energie; NT je dnes všude nulové | **kWh** | `sum` (jen fallback) |
748
748
  | `predi_input` | elektřina | `EFwActi_VT` / `_NT` | 15 min | průměrný **výkon**, tedy 4× energie | kW | **nepoužívat** (`sum / 4` by byly kWh) |
@@ -757,7 +757,7 @@ Další ověřené vlastnosti dat, na které agregace spoléhá:
757
757
 
758
758
  - `id_type` **není spolehlivý** — v produkci je aspoň jedno elektroměrové EAN uložené s `id_type = 'eic'`, takže join na měření jde výhradně přes `enapo_measurements.id` a `id_type` se nefiltruje
759
759
  - `enapo_gid_mapping.place_id` je **kód EAN/EIC**, zatímco `enapo_measurements.place_id` je interní PPAS place id — join `place_id = place_id` vrací nula řádků a vypadá jako „nejsou data“
760
- - jedno EIC může mít několik `place_id`. Pokud jejich počet přesahuje počet různých `device_serial_number`, jde o dvě živé registrace jednoho fyzického měřidla a sečtení by spotřebu zdvojilo — agregace proto pracuje na granularitě `(id, device_serial_number)`. Ze stejného důvodu je `max(core) - min(core)` nepoužitelné: rozpětí mezi dvěma nesouvisejícími počítadly.
760
+ - jedno EIC může mít několik `place_id`. Pokud jejich počet přesahuje počet různých `device_serial_number`, jde o dvě živé registrace jednoho fyzického měřidla a sečtení by spotřebu zdvojilo — agregace proto pracuje na granularitě `(id, device_serial_number)`. `mv_enapo_om_consumption.gas_metered` proto nejdřív vybere aktivní registraci (CTE `gas_registration`, podle poslední aktivity v `corediff`/`core2`) a teprve pro tu spočítá `core(1. den dalšího měsíce) - core(1. den měsíce)`; napříč celým EIC (bez vazby na registraci) by `core` rozdíl byl rozpětím mezi dvěma nesouvisejícími počítadly.
761
761
 
762
762
  #### _PPAS Distribution API_
763
763
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@golemio/energetics",
3
- "version": "1.13.1-dev.2809036872",
3
+ "version": "1.14.0-rc.2809913491",
4
4
  "description": "Golemio Energetics Module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",