Files
WebApi/webapi/risk_batches.py
T
rockdrigo e2ecaa027c CaMa: individual risk and population curves served, and the cycle estimates with any scenario in production
webapi/fhir/risk.py builds a patient's RiskAssessment for CaCu and CaMa; GET /cama/modelo/curvas serves the production
version's survival curves; the cycle's estimator is generic by scenario; silver persona carries the date of death for CaMa;
the import schema accepts it; the CaMa risk batches can be confirmed and voided.
2026-11-21 02:07:34 -06:00

48 lines
2.3 KiB
Python

"""Risk batches waiting for a clinical decision (ADR-MD-008): list the ones no one has confirmed or voided yet,
confirm one (`preliminary` -> `final`, then served as the patient's FHIR `RiskAssessment`,
`/cacu/fhir/RiskAssessment?subject=Patient/<pac_id>`) or void it (`entered-in-error`).
`GET/POST /<domain>/riesgos/lotes...`. The route is always registered; who may act on it is an access rule still
to be defined.
"""
from flask import Blueprint, jsonify, request
from persistance.edges.gold import ERROR, FINAL
def risk_batches_blueprint(repositories, name: str = "risk_batches") -> Blueprint:
"""The blueprint, to register with `url_prefix="/<domain>"`. `repositories` is `persistance.<domain>`; `name` is
unique per domain registered."""
blueprint = Blueprint(name, __name__)
@blueprint.get("/riesgos/lotes")
def list_pending_batches():
"""Pending batches, newest first."""
return jsonify([_batch(b) for b in repositories.cohort_repository.pending_batches()])
@blueprint.post("/riesgos/lotes/<int:lote_id>/confirmar")
def confirm_batch(lote_id: int):
"""Moves every current evaluation of the batch to `final` (ADR-MD-008): a clinical decision, with its
reason. 404 if the batch doesn't exist or has no evaluation left to decide."""
return _decide(lote_id, FINAL)
@blueprint.post("/riesgos/lotes/<int:lote_id>/anular")
def void_batch(lote_id: int):
"""Moves every current evaluation of the batch to `entered-in-error`."""
return _decide(lote_id, ERROR)
def _decide(lote_id: int, estado: str):
reason = (request.get_json(silent=True) or {}).get("reason", "").strip()
if not reason:
return jsonify({"error": "se requiere un motivo"}), 400
changed = repositories.cohort_repository.change_state(lote_id, estado, reason)
if not changed:
return jsonify({"error": f"el lote {lote_id} no existe o ya no tiene evaluaciones vigentes"}), 404
return jsonify({"lote_id": lote_id, "evaluaciones_cambiadas": changed})
return blueprint
def _batch(batch) -> dict:
return {"lote_id": batch.lote_id, "modelo_nombre": batch.modelo_nombre, "modelo_version": batch.modelo_version,
"evaluado_en": batch.evaluado_en.isoformat(), "n_evaluaciones": batch.n_evaluaciones}