CaCu $eligibility and $risk-series, current risks only, rebuild runs; gates carry the declared CaCu and CaMa differences

This commit is contained in:
Roberto Treviño
2026-12-05 19:42:07 -06:00
parent 05d0e4c278
commit c8472c705e
7 changed files with 163 additions and 21 deletions
+30 -6
View File
@@ -16,7 +16,7 @@ from fhir.resources.R4B.capabilitystatement import CapabilityStatement
from fhir.resources.R4B.operationoutcome import OperationOutcome
from fhir.resources.R4B.resource import Resource
from core import catalog
from core import catalog, models
from core import fhir as f
from core import measures as m
from core.domains import Domain, measures, part
@@ -54,6 +54,25 @@ def _clinical(domain: Domain):
return found
def _risks(domain: Domain, persons: list[str], cutoff: date) -> tuple[list[dict], dict]:
"""The served models' risks of these persons that still apply at the cut-off, and why
each person's others are withheld (a domain's `currency`, when it has one)."""
found = [r for r in reads.rows(domain.value, "risk", persons=persons)
if models.served(r["model"])] # fmt: skip
if (currency := part(domain, "currency")) is None:
return found, {}
history = reads.rows(domain.value, "risk_history", persons=persons)
return currency.applying(found, history, cutoff)
def _noted(domain: Domain, rows: list[dict], cutoff: date) -> list[dict]:
"""The persons, each with why some of her risks are withheld at the cut-off."""
if part(domain, "currency") is None:
return rows
_, why = _risks(domain, [r["person_id"] for r in rows], cutoff)
return [{**r, "risk_withheld": why.get(r["person_id"])} for r in rows]
def _cutoff(domain: Domain, asked: date | None) -> date:
"""The day asked for, or by default the last day with data in the domain (up to today)."""
if asked is not None:
@@ -106,8 +125,8 @@ def read_patient(domain: Domain, id: str) -> Response:
if link is None:
raise HTTPException(404, f"no Patient/{id}")
return fhir(f.replaced_patient(id, link["replaced_by"]))
status = fu.status(row, _cutoff(domain, None))
(found,) = _with_identity(domain, [{**row, "status": status}])
day = _cutoff(domain, None)
(found,) = _with_identity(domain, _noted(domain, [{**row, "status": fu.status(row, day)}], day))
return fhir(f.patient(found, fu.STATUS_SYSTEM))
@@ -213,7 +232,7 @@ def search_patient(
persons = [r["person_id"] for r in rows]
risks: dict[str, list[dict]] = {}
if window is not None and sort != "-recent" or "RiskAssessment:subject" in revinclude:
for r in reads.rows(domain.value, "risk", persons=persons):
for r in _risks(domain, persons, at)[0]:
risks.setdefault(r["person_id"], []).append(r)
if window is not None:
rows = fu.ordered(rows, sort, risks, horizon)
@@ -222,7 +241,8 @@ def search_patient(
included = _included(domain, revinclude, include, sorted(mine), at,
{p: v for p, v in risks.items() if p in mine}) # fmt: skip
applied = {"follow-up-cutoff": at.isoformat()} if window is not None else None
patients = [f.patient(r, fu.STATUS_SYSTEM) for r in _with_identity(domain, page)]
patients = [f.patient(r, fu.STATUS_SYSTEM)
for r in _with_identity(domain, _noted(domain, page, at))] # fmt: skip
return bundle(request, patients, len(rows), count, offset, included,
applied) # fmt: skip
@@ -302,7 +322,11 @@ def _clinical_search(kind: str, build, request, domain, subject, count, offset):
params["cutoff"] = _cutoff(domain, None).isoformat()
if kind == "procedure":
params["types"] = list(fu.PROCEDURES)
found = build(reads.rows(domain.value, kind, **params))
if kind == "risk":
rows = _risks(domain, params["persons"], _cutoff(domain, None))[0]
else:
rows = reads.rows(domain.value, kind, **params)
found = build(rows)
return bundle(request, _sliced(found, count, offset), len(found), count, offset)