Model/applications/condition/calico_runner.py
Khalim Conn-Kowlessar f1cf7babad Record the confirmed Calico UPRN-join mismatch and S3 source
Confirmed against the DB: Calico Asset References have zero overlap with
portfolio 824's landlord_property_id (addresses match; all 824 UPRNs non-null),
so the property-table lookup cannot resolve Calico. Documents the open
resolution (Calico-supplied ref->UPRN table or address matching) in ADR-0064,
the runner, and the README, and pins the confirmed S3 source location.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-13 15:34:23 +00:00

65 lines
2.5 KiB
Python

"""One-off runner for the Calico stock-condition (roof-covering) load.
Calico is a bulk one-off import, not a recurring feed, so it runs through this
runner rather than the SQS-Lambda path Peabody/LBWF use (ADR-0064). The raw
export lives in S3; references resolve to UPRNs via the ``property`` table for
Calico's portfolio.
Not exercised by the unit suite -- it is S3 + Postgres wiring around the
unit-tested ``run_calico_load``; run it where a database and S3 are available.
NOTE (2026-07-13): the ``PropertyUprnLookup`` wired below does **not** resolve
Calico yet -- Calico Asset References have zero overlap with portfolio 824's
stored ``landlord_property_id`` (ADR-0064 update). Swap in a CSV ``UprnLookup``
once Calico supplies an ``Asset Reference -> UPRN`` table, or add address
matching. Running as-is would drop every row as unmatched.
"""
import os
from io import BytesIO
from applications.condition.calico_load import run_calico_load
from applications.condition.load_report import LoadReport
from infrastructure.postgres.config import PostgresConfig
from infrastructure.postgres.engine import make_engine, transactional_session
from repositories.condition.condition_postgres import ConditionPostgres
from repositories.condition.property_reference_reader_postgres import (
PropertyReferenceReaderPostgres,
)
from repositories.condition.property_uprn_lookup import PropertyUprnLookup
from utils.logger import setup_logger
from utils.s3 import read_io_from_s3
logger = setup_logger()
CALICO_PORTFOLIO_ID = 824
# The export date the Calico file was generated (Roof Covering 2.6.26); anchors
# renewal years so they don't drift on re-run (ADR-0064).
CALICO_EXPORT_YEAR = 2026
# Confirmed source location.
CALICO_BUCKET = "condition-data-dev"
CALICO_KEY = "input/calico/Roof Covering 2.6.26.xlsx"
def run_calico_s3_load(
bucket: str = CALICO_BUCKET,
key: str = CALICO_KEY,
portfolio_id: int = CALICO_PORTFOLIO_ID,
export_year: int = CALICO_EXPORT_YEAR,
) -> LoadReport:
file_bytes: BytesIO = read_io_from_s3(bucket_name=bucket, file_key=key)
engine = make_engine(PostgresConfig.from_env(dict(os.environ)))
with transactional_session(engine) as session:
lookup = PropertyUprnLookup(
PropertyReferenceReaderPostgres(session), portfolio_id
)
report = run_calico_load(
file_stream=file_bytes,
uprn_lookup=lookup,
export_year=export_year,
writer=ConditionPostgres(),
)
logger.info(f"[calico_runner] {report}")
return report