fix FE 96h forecast
Some checks failed
CI and deploy / migration-check (push) Failing after 17s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-04-19 21:40:55 +02:00
parent 70d306961a
commit ee4355f17f
4 changed files with 234 additions and 7 deletions

View File

@@ -0,0 +1,67 @@
-- 15min sloty součtu FVE výkonů z posledních OK forecast runů (stejná logika jako fc_slot v fn_plan_current_bundle).
create or replace function ems.fn_forecast_pv_slots_range(
p_site_id int,
p_from timestamptz,
p_to timestamptz
)
returns jsonb
language sql
stable
as $fn$
with bounds as (
select
p_from as ts_from,
case
when p_to <= p_from then p_from + interval '15 minutes'
when p_to > p_from + interval '120 hours' then p_from + interval '120 hours'
else p_to
end as ts_to
),
slot_spine as (
select gs as interval_start
from bounds b,
generate_series(
b.ts_from,
(b.ts_to - interval '15 minutes')::timestamptz,
interval '15 minutes'
) as gs
),
fc as (
select
u.interval_start,
coalesce(sum(u.power_w), 0)::bigint as pv_forecast_total_w
from (
select distinct on (fpi.interval_start, fpr.pv_array_id)
fpi.interval_start,
fpi.power_w
from ems.forecast_pv_interval fpi
join ems.forecast_pv_run fpr on fpr.id = fpi.run_id
join ems.asset_pv_array apa
on apa.id = fpr.pv_array_id
and apa.site_id = fpr.site_id
cross join bounds b
where fpr.site_id = p_site_id
and fpr.status = 'ok'
and fpi.interval_start >= b.ts_from
and fpi.interval_start < b.ts_to
order by fpi.interval_start, fpr.pv_array_id, fpr.created_at desc
) u
group by u.interval_start
)
select coalesce(
jsonb_agg(
jsonb_build_object(
'interval_start', s.interval_start,
'pv_forecast_total_w', fc.pv_forecast_total_w
)
order by s.interval_start
),
'[]'::jsonb
)
from slot_spine s
left join fc on fc.interval_start = s.interval_start;
$fn$;
comment on function ems.fn_forecast_pv_slots_range(int, timestamptz, timestamptz) is
'JSON pole {interval_start, pv_forecast_total_w} po 15 min pro [p_from, p_to); doplnění grafu plánu za hranicí planning_interval.';