feat: filter exams by time window in SQL (last N minutes), default 10min

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-21 15:59:11 -05:00
co-authored by Claude Sonnet 4.6
parent 35859e8d75
commit a880b7fb81
2 changed files with 4 additions and 7 deletions
+3 -6
View File
@@ -33,7 +33,7 @@ WHERE CAST(r.FECHA_RECEPCION AS DATE) = CURRENT_DATE
AND r.NUM_FACTURA > 0
"""
_SQL_EXAMENES_HOY = """
_SQL_EXAMENES_VENTANA = """
SELECT
TRIM(p.DOCIDENT) AS DOCIDENT,
r.IDRECEPCION,
@@ -59,7 +59,7 @@ LEFT JOIN EMPRESA_SUB es ON TRIM(es.NIT_EMP) = TRIM(r.NIT_EMPRESA)
AND TRIM(es.SUBGRUPO) = TRIM(r.SUBGRUPO)
LEFT JOIN TARIFA t ON TRIM(t.COD_EXAMEN) = TRIM(rel.COD_EXAMEN)
AND t.TARIFA = COALESCE(es.TARIFA, e.TARIFA)
WHERE CAST(r.FECHA_RECEPCION AS DATE) = CURRENT_DATE
WHERE r.HORAINICIORECEPCION >= CAST('NOW' AS TIMESTAMP) - ? MINUTE
AND r.NUM_FACTURA > 0
ORDER BY r.IDRECEPCION
"""
@@ -127,7 +127,7 @@ async def sync_recientes(ventana_min: int = 2) -> dict:
return {"ok": False, "error": f"Firebird: {msg}"}
ok_pac, err_pac, rows_pac = fb.execute_query(_SQL_HOY, None)
ok_ex, _, rows_ex = fb.execute_query(_SQL_EXAMENES_HOY, None)
ok_ex, _, rows_ex = fb.execute_query(_SQL_EXAMENES_VENTANA, (ventana_min,))
fb.disconnect()
if not ok_pac:
@@ -148,9 +148,6 @@ async def sync_recientes(ventana_min: int = 2) -> dict:
examenes_map: dict[str, list] = {}
if ok_ex and rows_ex:
for ex in rows_ex:
h = _parse_hora(ex.get("HORAINICIORECEPCION"))
if h is not None and h < limite:
continue
doc = str(ex.get("DOCIDENT") or "").strip()
if not doc:
continue
+1 -1
View File
@@ -64,7 +64,7 @@ async def startup():
query_defaults()
contratos_defaults()
_scheduler.add_job(sync_recientes, "interval", minutes=1, id="wa_sync_recientes",
max_instances=1, coalesce=True)
kwargs={"ventana_min": 10}, max_instances=1, coalesce=True)
_scheduler.start()