mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-07-12 13:29:04 +00:00
PostgresUnitOfWork built its PropertyPostgresRepository without an overrides reader, so a Property re-hydrated through the unit silently dropped its Landlord Overrides (ADR-0032). The Baseline orchestrator runs through the UoW, so it scored the bare lodged EPC while the Plan modelled the override-folded Effective EPC — the two diverged (e.g. baseline effective 71/C vs plan baseline 62/D), producing "already at band C yet recommends reaching C". Wire PropertyOverridesPostgresReader into the unit's property repo (uow- independent committed reference data, read via the same session factory) so every re-hydration folds overrides, matching the live modelling path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Callable
|
|
from types import TracebackType
|
|
from typing import Optional
|
|
|
|
from sqlmodel import Session
|
|
|
|
from repositories.property_baseline.property_baseline_postgres_repository import (
|
|
PropertyBaselinePostgresRepository,
|
|
)
|
|
from repositories.epc.epc_postgres_repository import EpcPostgresRepository
|
|
from repositories.plan.plan_postgres_repository import PlanPostgresRepository
|
|
from repositories.product.composite_product_repository import (
|
|
catalogue_with_off_catalogue_overrides,
|
|
)
|
|
from repositories.property.property_overrides_postgres_reader import (
|
|
PropertyOverridesPostgresReader,
|
|
)
|
|
from repositories.property.property_postgres_repository import (
|
|
PropertyPostgresRepository,
|
|
)
|
|
from repositories.scenario.scenario_postgres_repository import (
|
|
ScenarioPostgresRepository,
|
|
)
|
|
from repositories.solar.solar_postgres_repository import SolarPostgresRepository
|
|
from repositories.spatial.spatial_postgres_repository import SpatialPostgresRepository
|
|
from repositories.unit_of_work import UnitOfWork
|
|
|
|
|
|
class PostgresUnitOfWork(UnitOfWork):
|
|
"""Postgres-backed Unit of Work: one ``Session``, all repos bound to it.
|
|
|
|
Built from a session factory (a module-scoped engine + sessionmaker in
|
|
production, ADR-0012) so the connection pool is reused across warm Lambda
|
|
invocations. The session is opened on ``__enter__`` and closed on
|
|
``__exit__``; a fresh instance is one single-use unit.
|
|
"""
|
|
|
|
def __init__(self, session_factory: Callable[[], Session]) -> None:
|
|
self._session_factory = session_factory
|
|
|
|
def __enter__(self) -> "PostgresUnitOfWork":
|
|
self._session = self._session_factory()
|
|
epc_repo = EpcPostgresRepository(self._session)
|
|
spatial_repo = SpatialPostgresRepository(self._session)
|
|
# Fold Landlord Overrides onto the Effective EPC on every re-hydration
|
|
# (ADR-0032), so what the Baseline orchestrator scores off ``uow.property``
|
|
# matches what the Plan was modelled from. The reader is uow-independent —
|
|
# ``property_overrides`` is committed reference data — so it opens its own
|
|
# short read session per call via the same session factory.
|
|
overrides_reader = PropertyOverridesPostgresReader(self._session_factory)
|
|
self.property = PropertyPostgresRepository(
|
|
self._session, epc_repo, spatial_repo, overrides_reader
|
|
)
|
|
self.epc = epc_repo
|
|
self.solar = SolarPostgresRepository(self._session)
|
|
self.spatial = spatial_repo
|
|
self.property_baseline = PropertyBaselinePostgresRepository(self._session)
|
|
self.scenario = ScenarioPostgresRepository(self._session)
|
|
self.product = catalogue_with_off_catalogue_overrides(self._session)
|
|
self.plan = PlanPostgresRepository(self._session)
|
|
return self
|
|
|
|
def __exit__(
|
|
self,
|
|
exc_type: Optional[type[BaseException]],
|
|
exc: Optional[BaseException],
|
|
tb: Optional[TracebackType],
|
|
) -> None:
|
|
try:
|
|
self._session.rollback()
|
|
finally:
|
|
self._session.close()
|
|
|
|
def commit(self) -> None:
|
|
self._session.commit()
|
|
|
|
def rollback(self) -> None:
|
|
self._session.rollback()
|