diff --git a/pxy_dashboard/apps/views.py b/pxy_dashboard/apps/views.py index b4f3b09..475106a 100644 --- a/pxy_dashboard/apps/views.py +++ b/pxy_dashboard/apps/views.py @@ -348,3 +348,62 @@ def apps_whatsapp_bot(request): } return render(request, "pxy_dashboard/apps/apps-whatsapp-bot.html", context) +from django.contrib.auth.decorators import login_required +from django.shortcuts import render +from django.db.models import Avg +from django.utils import timezone +from datetime import timedelta +import json + +from pxy_bots.models import TelegramBot, TelegramConversation, TelegramMessage + +@login_required +def apps_telegram_bot(request): + # — Métricas globales — + total_conversations = TelegramConversation.objects.count() + messages_in = TelegramMessage.objects.filter(direction='in').count() + messages_out = TelegramMessage.objects.filter(direction='out').count() + avg_rt = ( + TelegramMessage.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), + } + + # — Información de cada bot — + bots_info = [] + for bot in TelegramBot.objects.all(): + bots_info.append({ + 'name': bot.name, + 'username': getattr(bot, 'username', '–'), + 'is_active': bot.is_active, + 'assistant': bot.assistant.name, + }) + + # — Datos de los últimos 7 días para scatter — + today = timezone.now().date() + start_date = today - timedelta(days=6) + weekly_data = {} + for bot in TelegramBot.objects.all(): + series = [] + for i in range(7): + day = start_date + timedelta(days=i) + cnt = TelegramMessage.objects.filter( + conversation__bot=bot, + timestamp__date=day + ).count() + series.append({'x': day.strftime('%Y-%m-%d'), 'y': cnt}) + weekly_data[bot.name] = series + + return render(request, 'pxy_dashboard/apps/apps-telegram-bot.html', { + 'stats': stats, + 'bots_info': bots_info, + 'weekly_data_json': json.dumps(weekly_data), + }) diff --git a/pxy_dashboard/templates/pxy_dashboard/apps/apps-telegram-bot.html b/pxy_dashboard/templates/pxy_dashboard/apps/apps-telegram-bot.html index 58db716..e39a3b0 100644 --- a/pxy_dashboard/templates/pxy_dashboard/apps/apps-telegram-bot.html +++ b/pxy_dashboard/templates/pxy_dashboard/apps/apps-telegram-bot.html @@ -1,5 +1,110 @@ {% extends "pxy_dashboard/partials/base.html" %} +{% load static %} + {% block content %} -
This is a placeholder page for apps-telegram-bot.html
Conversaciones totales
+Mensajes recibidos
+Mensajes enviados
+Tiempo respuesta promedio
+Nombre | +Username | +Estado | +Asistente AI | +
---|---|---|---|
{{ bot.name }} | +{{ bot.username }} | ++ {% if bot.is_active %} + Activo + {% else %} + Inactivo + {% endif %} + | +{{ bot.assistant }} | +
+ No hay bots configurados + | +