Files
WebApi/webapi/blueprints/reportes.py
T
Roberto Treviño Cervantes f65d5f3f4d preliminary release
2026-10-29 12:12:28 -06:00

50 lines
1.8 KiB
Python

"""Aggregate reports: the log of import attempts and the state of the live
indicator recomputation. Nothing here returns row-level data."""
from flask import Blueprint, Response, jsonify, request
from webapi.imports import audit as import_audit
from webapi.indicadores_vivos import DOMINIOS, ColaDeRecalculo
reportes_bp = Blueprint("reportes", __name__)
@reportes_bp.get("/importaciones")
def importaciones():
"""Totals per target over every logged attempt, plus the latest attempts
(`?limite=50`, newest first; `?dominio=cacu` to filter)."""
records = import_audit.default_sink().read()
domain = request.args.get("dominio")
if domain:
records = [r for r in records if r.get("dominio") == domain]
try:
limit = max(0, int(request.args.get("limite", 50)))
except ValueError:
return jsonify({"error": "limite debe ser un número"}), 400
return jsonify({"resumen": import_audit.summarize(records), "intentos": list(reversed(records))[:limit]})
@reportes_bp.get("/importaciones/descarga")
def importaciones_descarga():
"""The raw log, one JSON object per line."""
sink = import_audit.default_sink()
body = sink.path.read_bytes() if sink.path.exists() else b""
return Response(
body,
mimetype="application/x-ndjson",
headers={"Content-Disposition": "attachment; filename=importaciones.jsonl"},
)
@reportes_bp.get("/indicadores")
def indicadores_estado():
"""Per domain: state of the last recomputation and whether another is pending."""
return jsonify(ColaDeRecalculo().estado())
@reportes_bp.post("/indicadores/<dominio>/recalcular")
def indicadores_recalcular(dominio: str):
if dominio not in DOMINIOS:
return jsonify({"error": f"dominio desconocido: {dominio}"}), 404
ColaDeRecalculo().solicitar(dominio)
return jsonify({"pendiente": True}), 202