speedup zalozka planning
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user