Files
ems/db/views/R__097_vw_pool_pump.sql
Dusan Vojacek f71bc944b4 fix(db): čtecí cesty telemetrie robustní vůči řídkým řádkům (idle-skip)
- fn_fill_audit_interval: EV a TČ agregace sum(power_w)/15 místo avg přes
  přítomné řádky — avg by při řídké telemetrii nadhodnotil aktivitu části
  slotu; chybějící minuta = 0 W (idle). TČ drží NULL bez power_w (MIM-B19N).
- fn_update_tuv_usage_stats: delta TUV normalizovaná na °C/min délkou mezery
  mezi řádky (gap_min), mezery > 30 min vyloučeny; pro hustá 1min data
  numericky identické s původním LAG.
- vw_pool_pump_day_energy: komentář — on_minutes drží invariant „zapnuté
  čerpadlo se ukládá každou minutu".

Pro hustá 1min data beze změny výsledků; připravuje idle-skip zápisů
v telemetry_collector (navazující commit).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-12 19:06:23 +02:00

50 lines
1.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Bazénové čerpadlo: poslední stav + denní spotřeba pro UI (PostgREST).
create or replace view ems.vw_latest_pool_pump
with (security_invoker = false)
as
select
pp.site_id,
pp.id as pump_id,
pp.code as pump_code,
pp.rated_power_w,
pp.schedulable,
t.measured_at,
t.is_on,
t.power_w,
t.energy_wh_total,
now() - t.measured_at as data_age
from ems.asset_pool_pump pp
left join lateral (
select tp.measured_at, tp.is_on, tp.power_w, tp.energy_wh_total
from ems.telemetry_pool_pump tp
where tp.pump_id = pp.id
order by tp.measured_at desc
limit 1
) t on true;
comment on view ems.vw_latest_pool_pump is
'Poslední telemetrie bazénového čerpadla (LATERAL per pump). Dashboard karta.';
-- Denní spotřeba z čítače Shelly (delta maxmin za pražský den, posledních 8 dní).
create or replace view ems.vw_pool_pump_day_energy
with (security_invoker = false)
as
select
tp.site_id,
tp.pump_id,
(tp.measured_at at time zone 'Europe/Prague')::date as day,
round((max(tp.energy_wh_total) - min(tp.energy_wh_total)) / 1000.0, 2) as kwh,
sum(case when tp.is_on then 1 else 0 end) as on_minutes
from ems.telemetry_pool_pump tp
where tp.measured_at >= now() - interval '8 days'
group by 1, 2, 3;
comment on view ems.vw_pool_pump_day_energy is
'Denní kWh čerpadla (delta čítače energy_wh_total) a minuty běhu, 8 dní zpět.
on_minutes = počet ON řádků: drží invariant idle-skip telemetrie (zapnuté
čerpadlo se ukládá každou minutu, vypnuté jen změna/heartbeat).';
grant select on ems.vw_latest_pool_pump to ems_anon;
grant select on ems.vw_pool_pump_day_energy to ems_anon;