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

@@ -0,0 +1,23 @@
-- Cache výsledku fn_pv_forecast_delta_profile per site (obnovuje job fn_fill_forecast_accuracy).
-- Zrychlení GET /plan/current a plánování (canonical PV forecast).
alter table ems.site_pv_forecast_calibration
add column if not exists delta_profile_cache jsonb null,
add column if not exists delta_profile_cached_at timestamptz null;
comment on column ems.site_pv_forecast_calibration.delta_profile_cache is
'Poslední JSON z fn_pv_forecast_delta_profile (120d lookback, now); NULL = ještě nepočítáno.';
comment on column ems.site_pv_forecast_calibration.delta_profile_cached_at is
'Čas posledního refresh cache (fn_refresh_site_pv_delta_profile_cache).';
create index if not exists idx_planning_run_site_comparison_of
on ems.planning_run (
site_id,
((solver_params->>'comparison_of_run_id')::int),
created_at desc
)
where status = 'comparison';
comment on index ems.idx_planning_run_site_comparison_of is
'Rychlé nalezení comparison runu pro GET /plan/compare (comparison_of_run_id v solver_params).';

View File

@@ -0,0 +1,90 @@
-- Cache delta profilu PV (těžká agregace forecast_accuracy) — refresh po fn_fill_forecast_accuracy.
-- Prefix R__018: musí běžet před R__022 (volá fn_refresh_site_pv_delta_profile_cache).
create or replace function ems.fn_refresh_site_pv_delta_profile_cache(p_site_id int)
returns void
language plpgsql
as $fn$
declare
v_profile jsonb;
begin
v_profile := ems.fn_pv_forecast_delta_profile(
p_site_id,
now() - interval '120 days',
now()
);
update ems.site_pv_forecast_calibration c
set
delta_profile_cache = v_profile,
delta_profile_cached_at = now(),
updated_at = now()
where c.site_id = p_site_id;
if not found then
insert into ems.site_pv_forecast_calibration (
site_id,
delta_learn_min_ts,
delta_profile_cache,
delta_profile_cached_at
)
values (
p_site_id,
timestamptz '2026-04-11T22:00:00Z',
v_profile,
now()
);
end if;
end;
$fn$;
comment on function ems.fn_refresh_site_pv_delta_profile_cache(int) is
'Přepočte a uloží delta_profile_cache pro site (volá fn_pv_forecast_delta_profile).';
create or replace function ems.fn_pv_forecast_delta_profile_cached(
p_site_id int,
p_data_from timestamptz default (now() - interval '120 days'),
p_data_to timestamptz default now(),
p_half_life_days numeric default 14,
p_threshold_w int default 150,
p_top_n_days int default 3,
p_non_top_day_factor numeric default 0.02,
p_day_weight_gamma numeric default 1.0,
p_max_age interval default interval '30 minutes'
)
returns jsonb
language plpgsql
stable
as $fn$
declare
v_cached jsonb;
v_cached_at timestamptz;
begin
select c.delta_profile_cache, c.delta_profile_cached_at
into v_cached, v_cached_at
from ems.site_pv_forecast_calibration c
where c.site_id = p_site_id;
if v_cached is not null
and v_cached_at is not null
and v_cached_at >= now() - p_max_age
and p_data_from >= (now() - interval '120 days')
and p_data_to <= now() + interval '5 minutes' then
return v_cached;
end if;
return ems.fn_pv_forecast_delta_profile(
p_site_id,
p_data_from,
p_data_to,
p_half_life_days,
p_threshold_w,
p_top_n_days,
p_non_top_day_factor,
p_day_weight_gamma
);
end;
$fn$;
comment on function ems.fn_pv_forecast_delta_profile_cached is
'Delta profil z cache (max 30 min) nebo přepočet; pro canonical PV a /plan/current.';

View File

@@ -185,6 +185,9 @@ BEGIN
learning_exclude_reason = EXCLUDED.learning_exclude_reason;
GET DIAGNOSTICS v_count = ROW_COUNT;
perform ems.fn_refresh_site_pv_delta_profile_cache(p_site_id);
RETURN v_count;
END;
$$;
@@ -194,6 +197,7 @@ COMMENT ON FUNCTION ems.fn_fill_forecast_accuracy(INT, INT) IS
learning_eligible / learning_exclude_reason: před delta_learn_min_ts (kalibrace site) se nepočítá do učení delty;
po pv_curtailment_policy_effective_from sloty s curtailment / gen cutoff / cutoff_switch_log (export off) mají NULL actual a jsou vyloučeny z učení;
telemetrie: is_export_limited nebo pv_derating_flags <> 0 v okně slotu → stejné vyloučení (telemetry_derating).
Po úspěšném INSERT volá fn_refresh_site_pv_delta_profile_cache (V079 cache pro /plan/current).
Volat každých 15 minut (spolu s audit_filler) pro inkrementální plnění.
p_lookback_hours: kolik hodin zpět zpracovat (default 48h pro catch-up).
Pro první backfill: SELECT ems.fn_fill_forecast_accuracy(2, 8760) -- 1 rok';

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.';

View File

@@ -78,7 +78,7 @@ as $fn$
from factor_raw
),
profile as (
select ems.fn_pv_forecast_delta_profile(
select ems.fn_pv_forecast_delta_profile_cached(
p_site_id,
p_delta_data_from,
p_delta_data_to,

View File

@@ -0,0 +1,60 @@
-- Jedno volání DB pro GET /plan/compare (aktivní bundle + comparison run debug).
create or replace function ems.fn_plan_compare_bundle(p_site_id int)
returns jsonb
language plpgsql
stable
as $fn$
declare
v_active jsonb;
v_active_run_id int;
v_compare_run_id int;
v_comparison jsonb;
begin
v_active := ems.fn_plan_current_bundle(p_site_id);
if v_active ? 'error' then
return v_active;
end if;
v_active_run_id := (v_active->'run'->>'id')::int;
if v_active_run_id is null then
return jsonb_build_object('error', 'no_active_plan');
end if;
select pr.id
into v_compare_run_id
from ems.planning_run pr
where pr.site_id = p_site_id
and pr.status = 'comparison'
and (pr.solver_params->>'comparison_of_run_id')::int = v_active_run_id
order by pr.created_at desc
limit 1;
if v_compare_run_id is null then
select pr.id
into v_compare_run_id
from ems.planning_run pr
where pr.site_id = p_site_id
and pr.status = 'comparison'
order by pr.created_at desc
limit 1;
end if;
if v_compare_run_id is null then
return jsonb_build_object('error', 'no_comparison_plan');
end if;
v_comparison := ems.fn_planning_run_debug(v_compare_run_id);
if v_comparison is null then
return jsonb_build_object('error', 'no_comparison_plan');
end if;
return jsonb_build_object(
'active', v_active,
'comparison', v_comparison
);
end;
$fn$;
comment on function ems.fn_plan_compare_bundle(int) is
'Aktivní plán + comparison planning_run (GET /plan/compare).';