Merge pull request #1353 from Hestia-Homes/feature/dont-resave-epc-if-refetch-false

Don't re-save EPC data which has been read from database rather than fetched. Also turn on database insights
This commit is contained in:
Daniel Roth 2026-06-29 15:14:14 +01:00 committed by GitHub
commit 597510b996
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View file

@ -166,7 +166,9 @@ class _PropertyWrite:
scenario_id: int
is_default: bool
lodged_epc: Optional[EpcPropertyData]
lodged_epc_is_new: bool
predicted_epc: Optional[EpcPropertyData]
predicted_epc_is_new: bool
spatial: Optional[SpatialReference]
solar: Optional[_SolarWrite]
plan: Plan
@ -185,13 +187,13 @@ def _flush_writes(engine: Engine, writes: list[_PropertyWrite]) -> None:
before a write is ever queued."""
with PostgresUnitOfWork(lambda: Session(engine)) as uow:
for w in writes:
if w.lodged_epc is not None:
if w.lodged_epc is not None and w.lodged_epc_is_new:
uow.epc.save(
w.lodged_epc,
property_id=w.property_id,
portfolio_id=w.portfolio_id,
)
elif w.predicted_epc is not None:
elif w.predicted_epc is not None and w.predicted_epc_is_new:
# Persist the synthesised EPC in the predicted slot (ADR-0031), so
# the Baseline stage can re-hydrate it and downstream sees the
# picture the Plan was modelled from.
@ -542,8 +544,10 @@ def handler(
)
stored_lodged = stored_lodged_epcs.get(pid)
lodged_epc_is_new = False
if refetch_epc:
epc: Optional[EpcPropertyData] = epc_client.get_by_uprn(uprn)
lodged_epc_is_new = epc is not None
elif stored_lodged is not None:
logger.info(
f"property={pid} using stored lodged EPC (refetch_epc=False)"
@ -555,6 +559,7 @@ def handler(
)
overrides = overlays_from(overrides_reader.overrides_for(pid))
predicted_epc: Optional[EpcPropertyData] = None
predicted_epc_is_new = False
if epc is not None:
logger.info(f"property={pid} lodged EPC found")
@ -588,6 +593,7 @@ def handler(
broaden=_broaden,
predictor=predictor,
)
predicted_epc_is_new = True
effective_epc = Property(
identity=PropertyIdentity(
portfolio_id=portfolio_id,
@ -649,8 +655,8 @@ def handler(
# Queue this Property's writes rather than committing now — the
# whole batch is persisted in one Unit of Work after the loop
# (see _flush_writes). The EPC is saved in its lodged or predicted
# slot (ADR-0031) at flush time depending on which is set here.
# (see _flush_writes). The *_is_new flags gate EPC saves so that
# EPCs read from DB unchanged are not re-written.
accumulated.append(
_PropertyWrite(
property_id=pid,
@ -659,7 +665,9 @@ def handler(
scenario_id=scenario_id,
is_default=scenario.is_default,
lodged_epc=epc,
lodged_epc_is_new=lodged_epc_is_new,
predicted_epc=predicted_epc,
predicted_epc_is_new=predicted_epc_is_new,
spatial=spatial,
solar=solar_write,
plan=plan,

View file

@ -81,6 +81,10 @@ resource "aws_db_instance" "default" {
publicly_accessible = true
# Specify the CA certificate with the default RDS CA certificate
ca_cert_identifier = "rds-ca-rsa2048-g1"
# Performance Insights (7-day retention is free)
performance_insights_enabled = true
performance_insights_retention_period = 7
# Temporary to enfore immediate change
apply_immediately = true
# Set up storage type to gp3 for better performance