sjednoceni forecastu
Some checks failed
CI and deploy / migration-check (push) Failing after 13s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-05-05 10:42:49 +02:00
parent 459f33d55c
commit 5b383e9028
9 changed files with 461 additions and 253 deletions

View File

@@ -38,43 +38,87 @@ begin
where ab.site_id = p_site_id;
with fc_slot as (
-- Kanonický PV forecast pro UI = to, co solver používá (planning_interval.*_forecast_solver_w),
-- aby seděla bilance v tabulce slotů. Pro sloty mimo uložený plán doplníme forecast-only řádky.
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
where fpr.site_id = p_site_id
and fpr.status = 'ok'
order by fpi.interval_start, fpr.pv_array_id, fpr.created_at desc
) u
group by u.interval_start
c.interval_start,
(coalesce(c.pv_a_forecast_canonical_w, 0) + coalesce(c.pv_b_forecast_canonical_w, 0))::bigint as pv_forecast_total_w,
coalesce(c.pv_a_forecast_canonical_w, 0)::bigint as pv_a_forecast_solver_w,
coalesce(c.pv_b_forecast_canonical_w, 0)::bigint as pv_b_forecast_solver_w
from jsonb_to_recordset(
ems.fn_forecast_pv_slots_range_canonical_ab(
p_site_id,
(v_run->>'horizon_start')::timestamptz,
greatest((v_run->>'horizon_end')::timestamptz, (v_run->>'horizon_start')::timestamptz + interval '96 hours'),
now()
)
) as c(
interval_start timestamptz,
pv_a_forecast_canonical_w bigint,
pv_b_forecast_canonical_w bigint
)
),
joined as (
select
to_jsonb(pi.*)
|| jsonb_build_object(
'pv_power_w', ai.actual_pv_power_w,
'pv_forecast_total_w', fs.pv_forecast_total_w
'pv_forecast_total_w',
coalesce(pi.pv_a_forecast_solver_w, 0)
+ coalesce(pi.pv_b_forecast_solver_w, 0),
'pv_a_forecast_solver_w', pi.pv_a_forecast_solver_w,
'pv_b_forecast_solver_w', pi.pv_b_forecast_solver_w
) as j,
pi.interval_start,
pi.expected_cost_czk,
pi.pv_a_curtailed_w,
pi.battery_setpoint_w,
pi.grid_setpoint_w,
fs.pv_forecast_total_w
(coalesce(pi.pv_a_forecast_solver_w, 0) + coalesce(pi.pv_b_forecast_solver_w, 0))::bigint as pv_forecast_total_w
from ems.planning_interval pi
left join ems.audit_interval ai
on ai.site_id = p_site_id
and ai.interval_start = pi.interval_start
left join fc_slot fs on fs.interval_start = pi.interval_start
where pi.run_id = v_run_id
union all
select
jsonb_build_object(
'interval_start', fs.interval_start,
'battery_setpoint_w', null,
'battery_soc_target_pct', null,
'grid_setpoint_w', null,
'export_limit_w', null,
'export_mode', null,
'deye_physical_mode', null,
'ev1_setpoint_w', null,
'ev2_setpoint_w', null,
'heat_pump_enabled', null,
'pv_a_curtailed_w', null,
'expected_cost_czk', null,
'effective_buy_price', null,
'effective_sell_price', null,
'is_predicted_price', false,
'pv_power_w', null,
'pv_forecast_total_w', fs.pv_forecast_total_w,
'pv_a_forecast_solver_w', fs.pv_a_forecast_solver_w,
'pv_b_forecast_solver_w', fs.pv_b_forecast_solver_w,
'load_baseline_w', null
) as j,
fs.interval_start,
null::numeric as expected_cost_czk,
null::int as pv_a_curtailed_w,
null::int as battery_setpoint_w,
null::int as grid_setpoint_w,
fs.pv_forecast_total_w
from fc_slot fs
where fs.interval_start >= (v_run->>'horizon_start')::timestamptz
and fs.interval_start < greatest((v_run->>'horizon_end')::timestamptz, (v_run->>'horizon_start')::timestamptz + interval '96 hours')
and not exists (
select 1
from ems.planning_interval pi2
where pi2.run_id = v_run_id
and pi2.interval_start = fs.interval_start
)
),
agg as (
select