GET /cacu/pacientes/<pac_id>/riesgo, only in DEV: the patient's cohort membership and risks in the current batch, in the WebApp's contract

This commit is contained in:
2026-11-06 00:37:44 -06:00
parent 01ac568c22
commit 72a8e76108
3 changed files with 154 additions and 1 deletions
+6 -1
View File
@@ -77,10 +77,15 @@ def create_app() -> Flask:
app.register_blueprint(data_bp, url_prefix="/data")
app.register_blueprint(governance_bp, url_prefix="/governance")
app.register_blueprint(reportes_bp, url_prefix="/reportes")
# Patient-level follow-up list: only in DEV, with simulated data.
# Patient-level follow-up list and individual risk: only in DEV, with simulated data.
if en_dev():
from persistance.db import get_cacu_session
app.register_blueprint(blueprint_de_seguimiento(get_cacu_session), url_prefix="/cacu")
from persistance import cacu as cacu_repositories
from webapi.patient_risk import patient_risk_blueprint
app.register_blueprint(patient_risk_blueprint(cacu_repositories), url_prefix="/cacu")
return app
+49
View File
@@ -0,0 +1,49 @@
"""A CaCu patient's individual risk: `GET /cacu/pacientes/<pac_id>/riesgo`, only in DEV.
The contract the WebApp's patient record reads (`RiesgoPacienteCacu`): the patient's membership in the surveillance
cohort's latest build, with its variables, and its risks in the current batch, the newest one with a confirmed
evaluation. Patient data: **registered only in DEV** (`DEV_REVISION`), on simulated data, and every response carries
`simulado: true`. `pac_id` is the internal id; whoever calls is already inside the secure environment. A field is never
removed or renamed, only added.
"""
from datetime import timezone
from flask import Blueprint, jsonify
COHORT_URL = "cacu/cohorte_vigilancia"
COHORT_VARIABLES = ("tipo_censura", "c", "l", "r", "pap_0", "hpv_0")
def patient_risk_blueprint(repositories) -> Blueprint:
"""The blueprint, to register with `url_prefix="/cacu"` only in DEV. `repositories` is `persistance.cacu`."""
blueprint = Blueprint("cacu_patient_risk", __name__)
@blueprint.get("/pacientes/<int:pac_id>/riesgo")
def get_patient_risk(pac_id: int):
"""A patient's cohort membership and risks in the current batch (DEV only, simulated data)."""
if repositories.persona_repository.get(pac_id) is None:
return jsonify({"error": f"no existe la paciente {pac_id}"}), 404
membership = repositories.cohorte_repository.membership(pac_id, COHORT_URL)
risk = repositories.cohorte_repository.current_risk(pac_id)
return jsonify({"simulado": True, "cohorte": _cohort(membership), "riesgo": _risk(risk)})
return blueprint
def _cohort(membership) -> dict | None:
if membership is None:
return None
return {"url": membership.url, "version": membership.version, "fecha_indice": membership.fecha_indice.isoformat(),
**{name: membership.variables.get(name) for name in COHORT_VARIABLES}}
def _risk(risk) -> dict | None:
if risk is None:
return None
evaluated = risk.evaluado_en if risk.evaluado_en.tzinfo else risk.evaluado_en.replace(tzinfo=timezone.utc)
return {"lote_id": risk.lote_id, "modelo_nombre": risk.modelo_nombre, "modelo_version": risk.modelo_version,
"evaluado_en": evaluated.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ"),
"estado_lote": risk.estado_lote,
"horizontes": [{"horizonte_anios": h.horizonte_anios, "probabilidad": h.probabilidad, "ic_bajo": h.ic_bajo,
"ic_alto": h.ic_alto, "estado": h.estado} for h in risk.horizontes]}