CaMa follow-up list: the cohort of a revision is read from the views once

This commit is contained in:
Roberto Treviño Cervantes
2026-11-22 22:20:12 -06:00
parent f97d1202a9
commit 201ef4fb03
2 changed files with 47 additions and 1 deletions
+27 -1
View File
@@ -9,7 +9,10 @@ cohort that was counted.
import calendar
import hashlib
import json
from collections import OrderedDict
from datetime import date, timedelta
from threading import Lock
from sqlmodel import Session
@@ -145,6 +148,29 @@ def _item(f: Fila) -> dict:
if diagnostico and diagnostico >= f.mastografia_fecha else None}
# The cohorts already read, by database, revision and the parameters the cohort reads: the same data and cut-off give the same
# cohort, so only a new load, another cut-off or a changed parameter reads the views again.
_COHORTES: OrderedDict = OrderedDict()
_COHORTES_LOCK = Lock()
COHORTES_EN_MEMORIA = 4
PARAMETROS_DE_LA_COHORTE = ("cama.birads_4_a_6", "cama.biopsia_cancer", "cama.grupos_edad", "cama.birads_gravedad")
def _cohorte(sesion: Session, corte: date, revision: str) -> list[Fila]:
parametros = json.dumps([valor_de(sesion, k, tablas=TABLAS) for k in PARAMETROS_DE_LA_COHORTE], default=str)
clave = (str(sesion.get_bind().url), revision, parametros)
with _COHORTES_LOCK:
if clave in _COHORTES:
_COHORTES.move_to_end(clave)
return _COHORTES[clave]
filas = cohorte_al(sesion, corte)
with _COHORTES_LOCK:
_COHORTES[clave] = filas
while len(_COHORTES) > COHORTES_EN_MEMORIA:
_COHORTES.popitem(last=False)
return filas
def _consulta(sesion: Session, *, corte, meses, filtros, contexto, hoy, page, page_size) -> tuple[dict, list[Fila]]:
if not disponible(sesion):
raise CohorteNoDisponible("la cohorte aún no está disponible: falta correr el ciclo de indicadores de CaMa")
@@ -155,7 +181,7 @@ def _consulta(sesion: Session, *, corte, meses, filtros, contexto, hoy, page, pa
raise PedidoInvalido("el contexto de la cohorte lleva cohorte, escenario y revisión juntos.")
if any(contexto) and contexto != (COHORTE, ESCENARIO, actual):
raise RevisionDistinta("la cohorte, el escenario o la revisión no son los que sirve el servidor para este corte")
elegidas, desde = _selecciona(cohorte_al(sesion, corte), corte=corte, meses=meses, filtros=filtros)
elegidas, desde = _selecciona(_cohorte(sesion, corte, actual), corte=corte, meses=meses, filtros=filtros)
umbral = valor_de(sesion, "comun.umbral_supresion", tablas=TABLAS)
acotada = meses != 0 or any(v is not None for v in filtros.values())
if (elegidas and len(elegidas) < umbral) or (acotada and not elegidas):