excel-planner/scripts/diagnostico_pa.py

102 lines
3.2 KiB
Python

"""
Diagnostico del puente Power Automate: fechas, responsables y errores comunes.
Uso:
python scripts/diagnostico_pa.py
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from config import validate_config
from services.powerautomate_bridge import PowerAutomateBridge
def _pct(part: int, total: int) -> str:
if total <= 0:
return "0%"
return f"{round(100 * part / total)}%"
def main() -> int:
missing = validate_config()
if missing:
print("[ERROR] Faltan variables en .env:", ", ".join(missing))
return 1
bridge = PowerAutomateBridge()
print("=" * 50)
print(" Diagnostico Power Automate")
print("=" * 50)
print("[...] Llamando al flujo (POST listar_proyectos)...")
try:
raw = bridge.fetch_raw({"accion": "listar_proyectos"})
except Exception as exc:
print(f"\n[ERROR] {exc}")
print("\nSiguiente paso:")
print(" 1. make.powerautomate.com > tu flujo > Historial de ejecuciones")
print(" 2. Corrige la accion en rojo")
print(" 3. Copia expresiones desde docs/POWER_AUTOMATE_EXPRESIONES.md")
return 1
keys = list(raw.keys())
print(f"\n[OK] Respuesta recibida. Claves: {keys}")
if "todasLasTareas" in raw and "tareas" in raw:
print("[OK] Respuesta incluye todasLasTareas + tareas (fusion fechas/responsables).")
elif "tareas" in raw:
print("[AVISO] Solo llega 'tareas'. Agrega 'todasLasTareas' en la Respuesta HTTP.")
elif "todasLasTareas" in raw:
print("[AVISO] Solo llega 'todasLasTareas'. Agrega 'tareas' (tareasFinales) para responsables.")
try:
projects = bridge.get_all_projects()
except Exception as exc:
print(f"[ERROR] Al procesar proyectos: {exc}")
return 1
tasks: list[dict] = []
for project in projects:
tasks.extend(project.get("tareas") or [])
total = len(tasks)
with_date = sum(1 for t in tasks if bridge._extract_due_date(t))
with_assignee = sum(1 for t in tasks if bridge._extract_assignee(t))
print(f"\nProyectos: {len(projects)}")
print(f"Tareas: {total}")
print(f"Con fecha: {_pct(with_date, total)} ({with_date}/{total})")
print(f"Con resp.: {_pct(with_assignee, total)} ({with_assignee}/{total})")
if tasks:
sample = tasks[0]
print("\nMuestra (primera tarea):")
print(json.dumps({
"proyecto": sample.get("proyecto"),
"tarea": sample.get("tarea"),
"fecha_vencimiento": sample.get("fecha_vencimiento") or bridge._extract_due_date(sample) or "(vacia)",
"asignados": sample.get("asignados"),
}, ensure_ascii=False, indent=2))
if with_date == 0:
print("\n[AVISO] Sin fechas -> revisa Parte 1 (fecha_vencimiento) y Respuesta HTTP.")
if with_assignee == 0:
print("[AVISO] Sin responsables -> revisa Parte 2 y Obtener_el_perfil_de_usuario_(V2).")
if with_date > 0 and with_assignee > 0:
print("\n[OK] Fechas y responsables llegando correctamente.")
return 0
return 0 if total > 0 else 1
if __name__ == "__main__":
raise SystemExit(main())