sql first refactor
Some checks failed
CI and deploy / migration-check (push) Successful in 5s
CI and deploy / deploy (push) Failing after 20s

This commit is contained in:
Dusan Vojacek
2026-04-19 20:02:20 +02:00
parent a02e11ee13
commit 93f883f5e0
74 changed files with 6022 additions and 4014 deletions

View File

@@ -0,0 +1,17 @@
drop view if exists ems.vw_asset_ev_charger_modbus_poll;
create view ems.vw_asset_ev_charger_modbus_poll as
select
ec.site_id,
ec.id as charger_id,
ec.code,
se.host,
se.port,
se.unit_id
from ems.asset_ev_charger ec
join ems.site_endpoint se on se.id = ec.endpoint_id
where se.enabled = true
and se.endpoint_type = 'modbus_tcp';
comment on view ems.vw_asset_ev_charger_modbus_poll is
'Nabíječky EV s Modbus TCP pro telemetry_collector.';

View File

@@ -0,0 +1,17 @@
drop view if exists ems.vw_asset_heat_pump_modbus_poll;
create view ems.vw_asset_heat_pump_modbus_poll as
select
hp.site_id,
hp.id as heat_pump_id,
hp.code,
se.host,
se.port,
se.unit_id
from ems.asset_heat_pump hp
join ems.site_endpoint se on se.id = hp.endpoint_id
where se.enabled = true
and se.endpoint_type = 'modbus_tcp';
comment on view ems.vw_asset_heat_pump_modbus_poll is
'TČ s Modbus TCP pro telemetry_collector.';

View File

@@ -0,0 +1,18 @@
drop view if exists ems.vw_asset_inverter_modbus_poll;
create view ems.vw_asset_inverter_modbus_poll as
select
ai.site_id,
ai.id as inverter_id,
ai.code,
se.host,
se.port,
se.unit_id
from ems.asset_inverter ai
join ems.site_endpoint se on se.id = ai.endpoint_id
where ai.active = true
and se.enabled = true
and se.endpoint_type = 'modbus_tcp';
comment on view ems.vw_asset_inverter_modbus_poll is
'Aktivní střídače s Modbus TCP endpointem pro telemetry_collector.';

View File

@@ -0,0 +1,23 @@
-- denní součet |battery_power_w|/60 Wh → poměr k 2× usable (orientační cykly/den)
create or replace view ems.vw_battery_cycle_daily as
select
ti.site_id,
(ti.measured_at at time zone 'Europe/Prague')::date as day_prague,
sum(abs(ti.battery_power_w::numeric) / 60.0) as throughput_wh,
max(ab.usable_wh) as usable_wh,
round(
(sum(abs(ti.battery_power_w::numeric) / 60.0)
/ nullif(max(ab.usable_wh) * 2, 0))::numeric,
4
) as equiv_full_cycles
from ems.telemetry_inverter ti
cross join lateral (
select usable_capacity_wh::numeric as usable_wh
from ems.asset_battery b
where b.site_id = ti.site_id
order by b.id
limit 1
) ab
where ti.battery_power_w is not null
group by ti.site_id, (ti.measured_at at time zone 'Europe/Prague')::date;

View File

@@ -0,0 +1,21 @@
-- poslední úspěšně ověřený zápis per (site, asset, register)
drop view if exists ems.vw_modbus_last_verified;
create view ems.vw_modbus_last_verified as
select distinct on (mc.site_id, mc.asset_type, mc.asset_id, mc.register)
mc.id,
mc.site_id,
mc.asset_type,
mc.asset_id,
mc.register,
mc.value_verified,
mc.verified_at,
mc.status
from ems.modbus_command mc
where mc.status = 'verified'
and mc.value_verified is not null
order by mc.site_id, mc.asset_type, mc.asset_id, mc.register, mc.verified_at desc nulls last, mc.id desc;
comment on view ems.vw_modbus_last_verified is
'DISTINCT ON (register) poslední verified řádek pro ověření / mapu registrů.';

View File

@@ -0,0 +1,19 @@
-- tenké čtení lokality pro API (Phase 1 místo select z ems.site v routerech)
drop view if exists ems.vw_site_directory;
create view ems.vw_site_directory as
select
s.id,
s.code,
s.name,
s.timezone,
s.latitude,
s.longitude,
s.active,
s.notes,
s.created_at
from ems.site s;
comment on view ems.vw_site_directory is
'Veřejná metadata lokality pro GET seznamů; čtení místo přímého dotazu na ems.site.';