fix repeatable migrations

This commit is contained in:
Dusan Vojacek
2026-04-19 20:15:46 +02:00
parent 0c93f493a4
commit 22bca9cd9e
73 changed files with 22 additions and 15 deletions

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;