diff --git a/app/routes/logs.py b/app/routes/logs.py index a3cf046..8d987a9 100644 --- a/app/routes/logs.py +++ b/app/routes/logs.py @@ -1,5 +1,5 @@ import json as json_lib -from datetime import datetime +from datetime import datetime, date as date_cls import httpx from fastapi import APIRouter, Request, Depends from fastapi.responses import JSONResponse @@ -199,6 +199,60 @@ async def reenviar_envio(envio_id: int, request: Request, user: dict = Depends(g return JSONResponse({"success": new_status != "error", "message": msg}) +@router.get("/resumen") +async def resumen_page( + request: Request, + user: dict = Depends(get_current_user), + fecha: str = "", + tipo: str = "", +): + if not fecha: + fecha = date_cls.today().isoformat() + + conn = get_connection() + + where_extra = "" + params_q: list = [fecha] + if tipo: + where_extra = "AND tipo = ?" + params_q.append(tipo) + + rows = conn.execute(f""" + SELECT + factura, tipo, contrato, cedula, idrecepcion, + COUNT(*) AS intentos, + SUM(CASE WHEN status != 'error' THEN 1 ELSE 0 END) AS exitos, + MIN(created_at) AS primer_envio, + MAX(created_at) AS ultimo_envio, + MAX(CASE WHEN status = 'error' THEN id END) AS ultimo_error_id, + MAX(CASE WHEN status != 'error' THEN id END) AS exitoso_id, + CASE WHEN SUM(CASE WHEN status != 'error' THEN 1 ELSE 0 END) > 0 + THEN MAX(CASE WHEN status != 'error' THEN status END) + ELSE 'error' + END AS estado_final + FROM envios + WHERE DATE(created_at) = ? {where_extra} + GROUP BY factura, tipo, contrato, cedula, idrecepcion + ORDER BY estado_final, tipo, factura + """, params_q).fetchall() + + conn.close() + + total = len(rows) + exitosos = sum(1 for r in rows if r["estado_final"] != "error") + errores = sum(1 for r in rows if r["estado_final"] == "error") + + return request.app.state.templates.TemplateResponse("resumen.html", { + "request": request, "user": user, + "fecha": fecha, + "filtro_tipo": tipo, + "rows": rows, + "total": total, + "exitosos": exitosos, + "errores": errores, + }) + + @router.get("/detalle/{envio_id}") async def detalle_envio(envio_id: int, request: Request, user: dict = Depends(get_current_user)): conn = get_connection() diff --git a/app/templates/base.html b/app/templates/base.html index af2b933..29000e7 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -61,10 +61,13 @@ TNS 5 - {% if cur == '/envios/tns' %} + {% if cur in ('/envios/tns', '/logs/resumen') %}
{% endif %} @@ -84,6 +87,9 @@ Historial TNS + + Resumen Diario + Actividad diff --git a/app/templates/resumen.html b/app/templates/resumen.html new file mode 100644 index 0000000..cb38765 --- /dev/null +++ b/app/templates/resumen.html @@ -0,0 +1,274 @@ +{% extends "base.html" %} +{% block title %}Resumen Diario{% endblock %} +{% block header %}Resumen Diario TNS{% endblock %} +{% block content %} + + +| Tipo | +Factura | +IDRECEP. | +Contrato | +Cédula | +Intentos | +Estado final | +Primer envío | +Último envío | ++ |
|---|---|---|---|---|---|---|---|---|---|
| + + {{ r.tipo }} + + | +{{ r.factura or '—' }} | +{{ r.idrecepcion or '—' }} | +{{ r.contrato or '—' }} | +{{ r.cedula or '—' }} | ++ {{ r.intentos }} + {% if r.exitos > 0 and r.intentos > r.exitos %} + ({{ r.exitos }} ok) + {% endif %} + | ++ {% if r.estado_final == 'success' %} + ✓ Enviado + {% elif r.estado_final == 'warning' %} + ! Ya existe + {% else %} + ✗ Error persistente + {% endif %} + | +{{ r.primer_envio[:16] if r.primer_envio else '—' }} | +{{ r.ultimo_envio[:16] if r.ultimo_envio else '—' }} | ++ {% set ver_id = r.exitoso_id or r.ultimo_error_id %} + {% if ver_id %} + + {% endif %} + {% if r.estado_final == 'error' and r.ultimo_error_id %} + + {% endif %} + | +
Sin envíos registrados para {{ fecha }}
+