speedup zalozka planning
Some checks failed
CI and deploy / migration-check (push) Failing after 12s
CI and deploy / deploy (push) Has been skipped

This commit is contained in:
Dusan Vojacek
2026-05-21 10:37:32 +02:00
parent eb425a26f2
commit d984716f69
10 changed files with 248 additions and 74 deletions

View File

@@ -17,6 +17,12 @@ declare
v_cap numeric;
v_cov numeric;
v_scarcity numeric;
v_horizon_start timestamptz;
v_horizon_end timestamptz;
v_chart_end timestamptz;
v_fc_from timestamptz;
v_fc_to timestamptz;
v_fc_slots jsonb;
begin
select to_jsonb(pr)
into v_run
@@ -31,6 +37,23 @@ begin
end if;
v_run_id := (v_run->>'id')::int;
v_horizon_start := (v_run->>'horizon_start')::timestamptz;
v_horizon_end := (v_run->>'horizon_end')::timestamptz;
v_chart_end := greatest(v_horizon_end, v_horizon_start + interval '96 hours');
-- Kanonický PV forecast jen za horizontem uloženého plánu (graf až 96 h).
if v_horizon_end < v_chart_end then
v_fc_from := v_horizon_end;
v_fc_to := v_chart_end;
v_fc_slots := ems.fn_forecast_pv_slots_range_canonical_ab(
p_site_id,
v_fc_from,
v_fc_to,
now()
);
else
v_fc_slots := '[]'::jsonb;
end if;
select coalesce(sum(ab.usable_capacity_wh), 0)::float
into v_batt_wh
@@ -38,21 +61,12 @@ 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
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(
from jsonb_to_recordset(v_fc_slots) as c(
interval_start timestamptz,
pv_a_forecast_canonical_w bigint,
pv_b_forecast_canonical_w bigint
@@ -60,14 +74,30 @@ begin
),
joined as (
select
to_jsonb(pi.*)
|| jsonb_build_object(
jsonb_build_object(
'interval_start', pi.interval_start,
'battery_setpoint_w', pi.battery_setpoint_w,
'battery_soc_target_pct', pi.battery_soc_target_pct,
'grid_setpoint_w', pi.grid_setpoint_w,
'export_limit_w', pi.export_limit_w,
'export_mode', pi.export_mode,
'deye_physical_mode', pi.deye_physical_mode,
'deye_gen_cutoff_enabled', pi.deye_gen_cutoff_enabled,
'ev1_setpoint_w', pi.ev1_setpoint_w,
'ev2_setpoint_w', pi.ev2_setpoint_w,
'heat_pump_enabled', pi.heat_pump_enabled,
'pv_a_curtailed_w', pi.pv_a_curtailed_w,
'expected_cost_czk', pi.expected_cost_czk,
'effective_buy_price', pi.effective_buy_price,
'effective_sell_price', pi.effective_sell_price,
'is_predicted_price', coalesce(pi.is_predicted_price, false),
'pv_power_w', ai.actual_pv_power_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
'pv_b_forecast_solver_w', pi.pv_b_forecast_solver_w,
'load_baseline_w', pi.load_baseline_w
) as j,
pi.interval_start,
pi.expected_cost_czk,
@@ -90,6 +120,7 @@ begin
'export_limit_w', null,
'export_mode', null,
'deye_physical_mode', null,
'deye_gen_cutoff_enabled', null,
'ev1_setpoint_w', null,
'ev2_setpoint_w', null,
'heat_pump_enabled', null,
@@ -111,8 +142,8 @@ begin
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')
where fs.interval_start >= v_horizon_start
and fs.interval_start < v_chart_end
and not exists (
select 1
from ems.planning_interval pi2
@@ -218,4 +249,4 @@ end;
$fn$;
comment on function ems.fn_plan_current_bundle(int) is
'Aktivní planning_run + intervaly + souhrn (GET /plan/current).';
'Aktivní planning_run + intervaly + souhrn (GET /plan/current). PV za horizont plánu z canonical forecast; delta profil z cache.';