Stats on view of dashboard
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
9f1a5ce51d
commit
30b8efac2f
@ -283,24 +283,37 @@ def dispatch_plan_view(request):
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
from django.db.models import Avg
|
||||||
|
from django.utils import timezone
|
||||||
|
from datetime import timedelta
|
||||||
|
import json, logging
|
||||||
|
from pxy_whatsapp.models import Conversation, Message
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def apps_whatsapp_bot(request):
|
def apps_whatsapp_bot(request):
|
||||||
stats = {}
|
# — 1) Calcular métricas directamente —
|
||||||
bots_info = []
|
|
||||||
|
|
||||||
# — 1) Cargar métricas —
|
|
||||||
try:
|
try:
|
||||||
resp = whatsapp_stats(request)
|
total_conversations = Conversation.objects.count()
|
||||||
if resp.status_code == 200:
|
messages_in = Message.objects.filter(direction="in").count()
|
||||||
stats = resp.json()
|
messages_out = Message.objects.filter(direction="out").count()
|
||||||
|
# Promedio de tiempo de respuesta (solo out con tiempo)
|
||||||
|
avg_rt = Message.objects.filter(
|
||||||
|
direction="out", response_time_ms__isnull=False
|
||||||
|
).aggregate(Avg("response_time_ms"))["response_time_ms__avg"] or 0
|
||||||
|
stats = {
|
||||||
|
"total_conversations": total_conversations,
|
||||||
|
"messages_in": messages_in,
|
||||||
|
"messages_out": messages_out,
|
||||||
|
"avg_response_time": int(avg_rt),
|
||||||
|
}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error loading WhatsApp stats: {e}")
|
logger.error(f"Error calculando métricas de WhatsApp: {e}")
|
||||||
|
stats = {"total_conversations": 0, "messages_in": 0, "messages_out": 0, "avg_response_time": 0}
|
||||||
|
|
||||||
# — 2) Cargar información de los bots —
|
# — 2) Cargar información de los bots —
|
||||||
|
bots_info = []
|
||||||
try:
|
try:
|
||||||
for bot in WhatsAppBot.objects.all():
|
for bot in WhatsAppBot.objects.all():
|
||||||
bots_info.append({
|
bots_info.append({
|
||||||
@ -310,7 +323,7 @@ def apps_whatsapp_bot(request):
|
|||||||
"assistant": bot.assistant.name,
|
"assistant": bot.assistant.name,
|
||||||
})
|
})
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error loading WhatsApp bots: {e}")
|
logger.error(f"Error cargando bots de WhatsApp: {e}")
|
||||||
|
|
||||||
# — 3) Prepare weekly data for scatter —
|
# — 3) Prepare weekly data for scatter —
|
||||||
today = timezone.now().date()
|
today = timezone.now().date()
|
||||||
@ -327,12 +340,11 @@ def apps_whatsapp_bot(request):
|
|||||||
points.append({"x": day.strftime("%Y-%m-%d"), "y": count})
|
points.append({"x": day.strftime("%Y-%m-%d"), "y": count})
|
||||||
weekly_data[bot.name] = points
|
weekly_data[bot.name] = points
|
||||||
|
|
||||||
# pass it as JSON
|
# 4) Render con todo el contexto
|
||||||
context = {
|
context = {
|
||||||
"stats": stats,
|
"stats": stats,
|
||||||
"bots_info": bots_info,
|
"bots_info": bots_info,
|
||||||
"weekly_data_json": json.dumps(weekly_data),
|
"weekly_data_json": json.dumps(weekly_data),
|
||||||
}
|
}
|
||||||
|
|
||||||
return render(request, "pxy_dashboard/apps/apps-whatsapp-bot.html", context)
|
return render(request, "pxy_dashboard/apps/apps-whatsapp-bot.html", context)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user