From 4092be012104cf7596d1e188a821bacece7f9137 Mon Sep 17 00:00:00 2001 From: Ekaropolus Date: Tue, 20 May 2025 03:44:37 -0600 Subject: [PATCH] Telegram Bots Dashboard --- pxy_dashboard/apps/views.py | 59 ++++++++++ .../pxy_dashboard/apps/apps-telegram-bot.html | 109 +++++++++++++++++- 2 files changed, 166 insertions(+), 2 deletions(-) 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 %} -

Telegram Bot

-

This is a placeholder page for apps-telegram-bot.html

+ {% include "pxy_dashboard/partials/dashboard/kpi_row.html" %} + +
+
+
+
+

{{ stats.total_conversations|default:"–" }}

+

Conversaciones totales

+
+
+
+
+
+
+

{{ stats.messages_in|default:"–" }}

+

Mensajes recibidos

+
+
+
+
+
+
+

{{ stats.messages_out|default:"–" }}

+

Mensajes enviados

+
+
+
+
+
+
+

{{ stats.avg_response_time|default:"–" }} ms

+

Tiempo respuesta promedio

+
+
+
+
+ + +
+
Bots de Telegram
+
+
+ + + + + + + + + + + {% for bot in bots_info %} + + + + + + + {% empty %} + + + + {% endfor %} + +
NombreUsernameEstadoAsistente AI
{{ bot.name }}{{ bot.username }} + {% if bot.is_active %} + Activo + {% else %} + Inactivo + {% endif %} + {{ bot.assistant }}
+ No hay bots configurados +
+
+
+
+ + +
+
Actividad Semanal de Mensajes por Bot
+
+
+
+
+{% endblock %} + +{% block extra_js %} + + {% endblock %}