M8-R03B: keep meter swap recompute in caller transaction

This commit is contained in:
2026-08-24 06:45:31 +02:00
parent 631b14e2ec
commit 3beeb5a461
4 changed files with 105 additions and 19 deletions
+17 -13
View File
@@ -151,7 +151,7 @@ def _trigger_recompute(db: Session, start: datetime, label: str) -> int:
# started_at is in the future — nothing to recompute.
logger.info("%s: started_at (%s) is in the future, skipping recompute.", label, start)
return 0
n = recompute_range(db, start, end)
n = recompute_range(db, start, end, commit=False)
logger.info(
"%s: recomputed %d period(s) in window [%s, %s).",
label,
@@ -352,23 +352,27 @@ def patch_energy_meter(
note=body.note,
started_at=new_started_at_utc,
)
# Retroactive recompute if started_at was changed.
if new_started_at_utc is not None and old_started_at is not None:
# Normalise old_started_at to UTC-aware for comparison.
if old_started_at.tzinfo is None:
old_started_at = old_started_at.replace(tzinfo=UTC)
# Window = [min(old, new), now) — covers all periods whose attribution
# may have changed due to the boundary shift in either direction.
window_start = min(old_started_at, new_started_at_utc)
_trigger_recompute(db, window_start, f"PATCH /api/energy/meters/{meter_id}")
db.commit()
except MeterIntervalError as exc:
db.rollback()
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
detail=str(exc),
)
# Retroactive recompute if started_at was changed.
if new_started_at_utc is not None and old_started_at is not None:
# Normalise old_started_at to UTC-aware for comparison.
if old_started_at.tzinfo is None:
old_started_at = old_started_at.replace(tzinfo=UTC)
# Window = [min(old, new), now) — covers all periods whose attribution
# may have changed due to the boundary shift in either direction.
window_start = min(old_started_at, new_started_at_utc)
_trigger_recompute(db, window_start, f"PATCH /api/energy/meters/{meter_id}")
db.commit()
except Exception:
db.rollback()
raise
db.refresh(meter)
# Trigger HA discovery re-publish so label renames on the active meter