fix(compare): census-sourced FSM/EAL benchmarks; never fall back across measure definitions

The FSM chip anchored against disadvantaged_pct (a different measure,
FSM6+CLA) whenever fsm_pct was null — which it always was, since the
performance df has no fsm_pct. New fact_census_benchmarks mart supplies
pupil-weighted FSM/EAL means per phase; the KS2-column medians that
produced a bogus 50% 'secondary disadvantaged' anchor are gone.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0146VHeLAWjDVE2B5uU67jCB
This commit is contained in:
Tudor
2026-07-16 19:05:15 +01:00
co-authored by Claude Fable 5
parent 9773483221
commit 1d855f3c17
7 changed files with 142 additions and 24 deletions
+27 -1
View File
@@ -677,6 +677,7 @@ async def compare_schools(
"deprivation": None,
}
supplementary_by_urn: dict = {}
census_benchmarks = None
db = None
try:
db = database.SessionLocal()
@@ -688,6 +689,31 @@ async def compare_schools(
key: supp.get(key, default)
for key, default in _EMPTY_SUPPLEMENTARY.items()
}
# Import-time census context benchmarks (fact_census_benchmarks);
# absent mart → None, and compute_benchmarks leaves those fields null.
try:
from .models import CensusBenchmark
rows = db.query(CensusBenchmark).all()
by_phase = {
r.phase: {
"year": r.year,
"fsm_pct": r.fsm_pct,
"eal_pct": r.eal_pct,
"median_pupils": r.median_pupils,
}
for r in rows
if getattr(r, "phase", None) in ("primary", "secondary")
}
if by_phase:
census_benchmarks = by_phase
except Exception:
# Missing mart (or a stubbed session in tests) must never break
# the compare payload — and not every session has rollback().
try:
db.rollback()
except Exception:
pass
except Exception:
supplementary_by_urn = {}
finally:
@@ -728,7 +754,7 @@ async def compare_schools(
# Official DfE anchors + computed state-school benchmarks so the
# compare UI can label provenance correctly (spec §8.6).
"national_averages": _national_averages_payload(df),
"benchmarks": compute_benchmarks(df),
"benchmarks": compute_benchmarks(df, census_benchmarks=census_benchmarks),
}