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

@@ -86,14 +86,6 @@ def _bundle_from_debug(payload: dict[str, Any]) -> PlanningBundleDto:
return _bundle_from_payload(payload, run_key="planning_run")
def _extract_run_id(bundle: PlanningBundleDto) -> int | None:
raw = bundle.run.get("id")
try:
return int(raw)
except (TypeError, ValueError):
return None
def _build_plan_diff(
active: PlanningBundleDto,
comparison: PlanningBundleDto,
@@ -194,59 +186,27 @@ async def get_plan_compare(
if not site_ok:
raise HTTPException(status_code=404, detail="Site not found")
active_raw = await fetch_json(
payload = await fetch_json(
conn,
"select ems.fn_plan_current_bundle($1::int)",
"select ems.fn_plan_compare_bundle($1::int)",
site_id,
)
if not isinstance(active_raw, dict):
active_raw = json.loads(active_raw)
if active_raw.get("error") == "no_active_plan":
raise HTTPException(status_code=404, detail="No active plan")
active = _bundle_from_current(active_raw)
active_run_id = _extract_run_id(active)
if active_run_id is None:
raise HTTPException(status_code=404, detail="No active plan")
compare_run_id = await conn.fetchval(
"""
select pr.id
from ems.planning_run pr
where pr.site_id = $1::int
and pr.status = 'comparison'
and (pr.solver_params->>'comparison_of_run_id')::int = $2::int
order by pr.created_at desc
limit 1
""",
site_id,
active_run_id,
)
if compare_run_id is None:
compare_run_id = await conn.fetchval(
"""
select pr.id
from ems.planning_run pr
where pr.site_id = $1::int
and pr.status = 'comparison'
order by pr.created_at desc
limit 1
""",
site_id,
)
if compare_run_id is None:
raise HTTPException(status_code=404, detail="No comparison plan")
compare_raw = await fetch_json(
conn,
"select ems.fn_planning_run_debug($1::int)",
int(compare_run_id),
)
if not isinstance(compare_raw, dict):
compare_raw = json.loads(compare_raw)
if compare_raw is None:
if not isinstance(payload, dict):
payload = json.loads(payload)
err = payload.get("error")
if err == "no_active_plan":
raise HTTPException(status_code=404, detail="No active plan")
if err == "no_comparison_plan":
raise HTTPException(status_code=404, detail="No comparison plan")
comparison = _bundle_from_debug(compare_raw)
active_raw = payload.get("active") or {}
compare_raw = payload.get("comparison")
if not isinstance(active_raw, dict):
active_raw = {}
if not isinstance(compare_raw, dict):
raise HTTPException(status_code=404, detail="No comparison plan")
active = _bundle_from_current(active_raw)
diff, slot_diffs = _build_plan_diff(active, comparison)
return PlanningCompareResponseModel(
active=active,

View File

@@ -147,6 +147,10 @@ async def patch_pv_forecast_calibration(
status_code=404,
detail="PV forecast calibration row missing; run migration V057",
)
await conn.execute(
"select ems.fn_refresh_site_pv_delta_profile_cache($1::int)",
site_id,
)
row = await conn.fetchrow(
"""
SELECT to_jsonb(c.*) AS j

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

View File

@@ -110,6 +110,7 @@ power_w = (poa_global * area_m2 * 0.20 * shading_factor).clip(
- Default je `7` dní.
- Endpoint `GET /api/v1/sites/{site_id}/forecast/pv?date=YYYY-MM-DD` vrací vždy poslední `ok` run per `(interval_start, pv_array_id)` (`DISTINCT ON`), takže UI nevidí duplikáty z historických běhů.
- **Kalibrace delty:** `GET /api/v1/sites/{site_id}/forecast/pv-delta-profile?from=…&to=…` vrací JSON z `ems.fn_pv_forecast_delta_profile` (`deltas`, `deltas_by_array`, `delta_learn_min_ts` z `ems.site_pv_forecast_calibration`). Volitelné query parametry: `half_life_days`, `threshold_w`, `top_n_days`, `non_top_day_factor`, `day_weight_gamma` (NULL u numerických přepsání = hodnota z kalibrační tabulky / default funkce).
- **Cache delty (V079):** sloupce `delta_profile_cache` / `delta_profile_cached_at` v `site_pv_forecast_calibration`; refresh `ems.fn_refresh_site_pv_delta_profile_cache(site_id)` po `fn_fill_forecast_accuracy` a po PATCH kalibrace; čtení pro plánování/UI přes `fn_pv_forecast_delta_profile_cached` (TTL 30 min, pak fallback na plný přepočet).
- **Úprava kalibrace z API:** `PATCH /api/v1/sites/{site_id}/configuration/pv-forecast-calibration` s JSON tělem (částečný update); odpověď je aktuální řádek kalibrace. Souhrn konfigurace v `GET …/configuration` obsahuje klíč `pv_forecast_calibration`.
- **Telemetrie pro učení delty:** `telemetry_collector` při Modbus poll čte reg. **145** a **178**; `fn_telemetry_inverter_sample` ukládá `is_export_limited` / `pv_derating_flags` (bity 1 = solar sell off, 2 = GEN/MI cut-off aktivní dle masky `(reg178 & 3) == 3`). `fn_fill_forecast_accuracy` sloty s těmito signály označí `telemetry_derating`.

View File

@@ -652,7 +652,8 @@ Planner v2 má do `planning_interval` zapisovat stejné základní položky jako
- přepnutí mezi v1 a v2 bude na úrovni orchestrace nebo konfigurace lokality
- exportér i control pipeline mají dál číst standardní výstup z `planning_interval`
- pokud je zapnuté `PLANNING_ENGINE_COMPARE_ENABLED`, backend spočítá obě verze nad stejným vstupem, aktivní verzi zapíše do plánu a druhou uloží i jako samostatný read-only `planning_run` se stavem `comparison`
- compare čtení jde přes `GET /api/v1/sites/{site_id}/plan/compare` a endpoint páruje compare run na aktivní run přes `comparison_of_run_id`
- compare čtení jde přes `GET /api/v1/sites/{site_id}/plan/compare` → jedno volání `ems.fn_plan_compare_bundle` (aktivní plán + `fn_planning_run_debug` comparison runu)
- **Výkon `/plan/current` a `/plan/compare` (V079+):** read-model `ems.fn_plan_current_bundle` dříve při každém HTTP requestu přepočítával `fn_pv_forecast_delta_profile` nad celou historií `forecast_accuracy` (~stovky tisíc řádků na site) a kanonický PV forecast na 96 h. Od **V079** se delta profil cacheuje v `site_pv_forecast_calibration.delta_profile_cache` (refresh po `fn_fill_forecast_accuracy` a po `PATCH …/pv-forecast-calibration` přes `fn_refresh_site_pv_delta_profile_cache`; čtení přes `fn_pv_forecast_delta_profile_cached`, TTL 30 min). Kanonický PV pro graf se počítá jen za horizontem uloženého plánu (`horizon_end``horizon_start + 96 h`), ne pro sloty už v `planning_interval`. Ověření: `curl -w '%{time_total}\n' http://…/plan/current` před/po migraci; první request po deployi může být pomalý dokud cache nezaplní job (15 min) nebo ručně `select ems.fn_refresh_site_pv_delta_profile_cache(<site_id>);`
- FE stránka `frontend/src/pages/Planning.tsx` ukazuje souhrn aktivní verze, compare verze, slotové rozdíly a compare křivku baterie v grafu
- fyzicky se na střídač aplikuje jen aktivní plán; compare běh slouží jen pro audit a vizualizaci