feat(fhir): publish optional category groups and stratum captions of compositions

This commit is contained in:
Angel Capetillo Zavala
2026-11-21 01:29:31 -06:00
parent c86c530703
commit c2b18d9969
3 changed files with 266 additions and 3 deletions
+18 -3
View File
@@ -11,7 +11,7 @@ import json
from datetime import datetime, timedelta
from pathlib import Path
from webapi.compositions import CompositionAnswer, CompositionDefinition
from webapi.compositions import CompositionAnswer, CompositionDefinition, category_groups
from webapi.durations import OBSERVATION, DurationAnswer, DurationDefinition
from webapi.fhir.common import CODE_SYSTEM, STRUCTURE, canonical, concept
from webapi.metrics import EVENT_CUTOFF, HISTORY_ENTRY, KNOWLEDGE_CUTOFF, POPULATION_SYSTEM, Answer, Counts
@@ -24,6 +24,7 @@ EXCLUDED = "measure-population-exclusion"
FIGURES = f"{STRUCTURE}/figuras"
PERIOD_TYPES = f"{STRUCTURE}/tipos-periodo"
CATEGORY_LABEL = f"{STRUCTURE}/categoria"
CATEGORY_GROUP = f"{STRUCTURE}/categoria-grupo"
CONTRACT = f"{STRUCTURE}/contrato"
REQUIRES_VALIDATION = f"{STRUCTURE}/requiere-validacion"
POPULATION_LABELS = {
@@ -87,9 +88,16 @@ def measure_resource(definition, title: str, description: str) -> dict:
*({"url": PERIOD_TYPES, "valueCode": t} for t in meta.get("tipos_periodo", ())),
*({"url": FIGURES, "valueString": f} for f in meta.get("figuras", ()))]
category = meta.get("categoria", {})
# Optional and additive: a category's group (`grupo`) and each group's caption (`categoria-grupo`), only when the
# composition declares them; a client that doesn't know them reads the labels alone.
extensions += [{"url": CATEGORY_LABEL, "extension": [
{"url": "codigo", "valueCode": code}, {"url": "etiqueta", "valueString": spec["etiqueta"]},
{"url": "color", "valueString": spec["color"]}]} for code, spec in category.get("etiquetas", {}).items()]
{"url": "color", "valueString": spec["color"]},
*([{"url": "grupo", "valueCode": spec["grupo"]}] if spec.get("grupo") else [])]}
for code, spec in category.get("etiquetas", {}).items()]
extensions += [{"url": CATEGORY_GROUP, "extension": [
{"url": "codigo", "valueCode": code}, {"url": "etiqueta", "valueString": caption}]}
for code, caption in category_groups(definition.url, category).items()]
if meta.get("requiere_validacion"):
extensions.append({"url": REQUIRES_VALIDATION, "valueCode": meta["requiere_validacion"]})
resource = {
@@ -141,13 +149,20 @@ def _groups(definition, result) -> list[dict]:
result.total)]}
labels_of = {c: s["etiqueta"] for c, s in definition.meta.get("categoria", {}).get("etiquetas", {}).items()}
category = definition.category.value
# A stratum value's caption, as a proportion's strata have it (`_label`), or from `meta.etiquetas_estrato`,
# which stays out of the strata's spec so the report's key doesn't change.
spec_of = {s.name: s.spec for s in definition.strata}
captions = definition.meta.get("etiquetas_estrato", {})
def caption(name: str, value: str) -> str | None:
return _label(spec_of.get(name, {}), value) or captions.get(name, {}).get(value)
stratifiers = [{"code": [concept(STRATIFIER_SYSTEM, category)], "stratum": [
{"value": _stratum_value(category, c, labels_of.get(c)), "population": [_count("initial-population", n)]}
for c, n in sorted(result.categories.items())]}]
for name, by_value in result.strata.items():
stratifiers.append({"code": [concept(STRATIFIER_SYSTEM, name), concept(STRATIFIER_SYSTEM, category)],
"stratum": [{"component": [
{"code": concept(STRATIFIER_SYSTEM, name), "value": _stratum_value(name, v)},
{"code": concept(STRATIFIER_SYSTEM, name), "value": _stratum_value(name, v, caption(name, v))},
{"code": concept(STRATIFIER_SYSTEM, category), "value": _stratum_value(category, c)},
], "population": [_count("initial-population", n)]}
for v, cs in sorted(by_value.items()) for c, n in sorted(cs.items())]})