50 lines
2.5 KiB
Python
50 lines
2.5 KiB
Python
"""A CaCu patient's individual risk: `GET /cacu/pacientes/<pac_id>/riesgo`.
|
|
|
|
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, on simulated data: every response carries `simulado: true`. The route is always registered;
|
|
who may see it is an access rule still to be defined. `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"`. `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.cohort_repository.membership(pac_id, COHORT_URL)
|
|
risk = repositories.cohort_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]}
|