Files
dashboard-leads/backend/diag_dayana_grafica_6_7.py
2026-08-18 11:48:08 -05:00

103 lines
3.9 KiB
Python

# diag_dayana_grafica_6_7.py
# Replica EXACTA de las dos series del grafico para DAYANA dias 6 y 7:
# NARANJA = por fecha de ASIGNACION (todos, con -45 refrigerio)
# AZUL = por fecha de RESPUESTA (solo respondidos, -45 refrigerio, y EXCLUYE >960 min)
from datetime import datetime, timedelta, date
from data_manager_v2 import DataManager
ANO, MES = 2026, 8
DIAS = [6, 7]
NOMBRE = "DAYANA"
LIMITE = 960
dm = DataManager()
asig = dm.traer_asignacion_respuesta()
act_rows = dm.traer_actividad_asesores()
uid2name = {r["user_id"]: (r.get("user_name") or "") for r in asig if r.get("user_id") is not None}
from collections import defaultdict
act = defaultdict(lambda: defaultdict(int))
for a in act_rows:
uid=a.get("user_id"); ts=a.get("created_at")
if uid not in uid2name or not isinstance(ts,datetime): continue
nom=uid2name[uid].upper(); d=ts.date(); h=ts.hour+ts.minute/60.0; key=(nom,d)
if d.weekday()==6: continue
if d.weekday()==5:
if 9<=h<13: act[key]["sab_manana"]+=1
elif 14<=h<18: act[key]["sab_tarde"]+=1
else:
if 13<=h<18: act[key]["tarde"]+=1
elif 18<=h<22: act[key]["noche"]+=1
def turno_dia(nom,d):
if d.weekday()==6: return None
fr=act.get((nom,d))
if not fr: return None
if d.weekday()==5:
m=fr.get("sab_manana",0); t=fr.get("sab_tarde",0)
if m==0 and t==0: return None
return [(9,13)] if m>=t else [(14,18)]
t=fr.get("tarde",0); n=fr.get("noche",0)
if t==0 and n==0: return None
return [(9,13),(18,22)] if n>t else [(9,13),(14,18)]
def es_t1(nom,d):
fr=act.get((nom,d))
if not fr or d.weekday()>=5: return False
t=fr.get("tarde",0); n=fr.get("noche",0)
return (not (n>t)) and (t>0 or n>0)
def _dt(d,hh): return datetime(d.year,d.month,d.day)+timedelta(hours=hh)
def mins_lab(nom,t1,t2):
if not isinstance(t1,datetime) or not isinstance(t2,datetime) or t2<=t1: return 0
total=0.0; cur=t1; s=0
while cur<t2 and s<60:
d=cur.date(); bl=turno_dia(nom,d)
if not bl: cur=_dt(d,24); s+=1; continue
for (hi,hf) in bl:
ini=_dt(d,hi); fin=_dt(d,hf)
if t2<=ini: break
if cur>=fin: continue
a=max(cur,ini); b=min(t2,fin)
if b>a: total+=(b-a).total_seconds()/60.0
cur=fin
if cur>=t2: break
if cur.date()==d: cur=_dt(d,24); s+=1
return round(total)
def calc_min(nom,t1,t2,d1):
h=t1.hour+t1.minute/60.0
if es_t1(nom,d1) and (13<=h<15):
dif=(t2-t1).total_seconds()/60.0
m=round(dif-45) if dif>45 else round(dif)
return max(m,0)
return mins_lab(nom,t1,t2)
for DIA in DIAS:
d1=date(ANO,MES,DIA)
print(f"\n{'#'*72}")
print(f"DAYANA — DIA {DIA:02d}/{MES:02d}/{ANO}")
print(f"{'#'*72}")
# ---- AZUL: respondidos ese dia (excluye >960) ----
print(f"\n>>> GRAFICO AZUL (por fecha de RESPUESTA): respondidos el {DIA} (excluye >960 min)")
print(f"{'telefono':13} {'asignado':14} {'respondio':14} {'min':>6} nota")
print("-"*60)
az=[]; tard=0
for r in asig:
if NOMBRE not in (r.get("user_name") or "").upper(): continue
t1=r.get("created_at"); t2=r.get("respuesta_fecha")
if not isinstance(t1,datetime) or not isinstance(t2,datetime): continue
if t2.date()!=d1: continue
m=calc_min((r.get('user_name') or '').upper(),t1,t2,t1.date())
nota = " (asignado otro dia)" if t1.date()!=d1 else ""
if m>LIMITE:
tard+=1
print(f"{r.get('telefono',''):13} {t1.strftime('%d/%m %H:%M'):14} {t2.strftime('%d/%m %H:%M'):14} {m:>6} TARDIO(>960) fuera{nota}")
continue
az.append(m)
print(f"{r.get('telefono',''):13} {t1.strftime('%d/%m %H:%M'):14} {t2.strftime('%d/%m %H:%M'):14} {m:>6}{nota}")
print("-"*60)
print(f" {len(az)} respondidos | promedio {round(sum(az)/len(az)) if az else 0} min | tardios(>960): {tard}")