49 lines
2.0 KiB
Python
49 lines
2.0 KiB
Python
"""CaCu's measures, evaluated on request over gold and served as FHIR R4B MeasureReports:
|
|
one Bundle per measure, one report per period asked for. Counting a few thousand rows is all
|
|
the request does; everything heavy was done by dbt."""
|
|
|
|
from datetime import date, datetime
|
|
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
from fhir.resources.R4B.bundle import Bundle
|
|
|
|
from core import cacu
|
|
from core import measures as m
|
|
from core.domains import Domain
|
|
from persistance import measures as reads
|
|
|
|
router = APIRouter(tags=["measures"])
|
|
DOMAINS = {Domain.CACU: m.MEASURES}
|
|
|
|
|
|
@router.get("/{domain}/measures")
|
|
def measures(domain: Domain) -> list[dict[str, str]]:
|
|
found = DOMAINS.get(domain, {})
|
|
return [{"id": x.id, "title": x.title, "scoring": x.rule.kind} for x in found.values()]
|
|
|
|
|
|
@router.get("/{domain}/measures/{measure_id}")
|
|
def measure(
|
|
domain: Domain,
|
|
measure_id: str,
|
|
periods: str = Query(description="comma-separated semesters (2025-S1) or months (2025-03)"),
|
|
as_of: datetime | None = Query(None, description="count only what was known by then"),
|
|
event_cutoff: date | None = Query(None, description="durations count to this day"),
|
|
) -> dict:
|
|
"""A Bundle (searchset) with the measure's MeasureReport for each period."""
|
|
if (found := DOMAINS.get(domain, {}).get(measure_id)) is None:
|
|
raise HTTPException(404, detail=f"no {domain.value} measure {measure_id}")
|
|
try:
|
|
asked = [m.Period.parse(p.strip()) for p in periods.split(",") if p.strip()]
|
|
except ValueError as e:
|
|
raise HTTPException(422, detail=f"periods: {e}") from e
|
|
params = {"model": cacu.RISK_BAND_MODEL} if found.rows == "risk" else {}
|
|
rows = reads.rows(found.rows, **params)
|
|
reports = [
|
|
m.report(found, p, m.evaluate(found, rows, p, as_of, event_cutoff), as_of, domain.value)
|
|
for p in asked
|
|
]
|
|
bundle = Bundle(type="searchset", total=len(reports),
|
|
entry=[{"resource": r} for r in reports]) # fmt: skip
|
|
return bundle.model_dump(mode="json", exclude_none=True)
|