Feat: Añadir panel de métricas con estadísticas por idioma, plataforma y servidor
- Crear página dedicada /metrics con gráficos usando Chart.js - Implementar función get_translation_stats() en database.py - Añadir endpoint /api/stats en panel/main.py - Mostrar métricas de traducciones por idioma, plataforma y servidor Discord - Agregar tarjeta de acceso rápido a Métricas en el Dashboard - Actualizar action_plan_pro.md con el progreso completado
This commit is contained in:
@@ -974,3 +974,115 @@ def delete_discord_server(server_id: int):
|
||||
c.execute("DELETE FROM discord_servers WHERE server_id = ?", (server_id,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def get_translation_stats() -> dict:
|
||||
"""Obtiene estadísticas de traducciones totales, por idioma y por plataforma"""
|
||||
db_type = get_db_type()
|
||||
|
||||
if db_type == "mysql":
|
||||
conn = get_connection()
|
||||
cursor = conn.cursor(dictionary=True)
|
||||
|
||||
cursor.execute("SELECT COUNT(*) as total FROM translations")
|
||||
total_result = cursor.fetchone()
|
||||
total_translations = total_result['total'] if total_result else 0
|
||||
|
||||
cursor.execute("""
|
||||
SELECT target_lang, COUNT(*) as count
|
||||
FROM translations
|
||||
GROUP BY target_lang
|
||||
ORDER BY count DESC
|
||||
""")
|
||||
by_language = {row['target_lang']: row['count'] for row in cursor.fetchall()}
|
||||
|
||||
cursor.execute("""
|
||||
SELECT bot_type, COUNT(*) as count
|
||||
FROM translations t
|
||||
JOIN messages m ON t.message_id = m.message_id
|
||||
GROUP BY bot_type
|
||||
""")
|
||||
by_platform = {row['bot_type']: row['count'] for row in cursor.fetchall()}
|
||||
|
||||
cursor.execute("""
|
||||
SELECT DATE(created_at) as date, COUNT(*) as count
|
||||
FROM translations
|
||||
WHERE created_at >= DATE_SUB(NOW(), INTERVAL 30 DAY)
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY date
|
||||
""")
|
||||
by_day = [{'date': str(row['date']), 'count': row['count']} for row in cursor.fetchall()]
|
||||
|
||||
cursor.execute("""
|
||||
SELECT COALESCE(s.server_name, CONCAT('Servidor ', m.guild_id)) as server_name,
|
||||
COUNT(*) as count
|
||||
FROM translations t
|
||||
JOIN messages m ON t.message_id = m.message_id
|
||||
LEFT JOIN discord_servers s ON m.guild_id = s.server_id
|
||||
WHERE m.guild_id IS NOT NULL
|
||||
GROUP BY m.guild_id
|
||||
ORDER BY count DESC
|
||||
""")
|
||||
by_server = [{'server': row['server_name'], 'count': row['count']} for row in cursor.fetchall()]
|
||||
|
||||
cursor.close()
|
||||
|
||||
return {
|
||||
'total': total_translations,
|
||||
'by_language': by_language,
|
||||
'by_platform': by_platform,
|
||||
'by_day': by_day,
|
||||
'by_server': by_server
|
||||
}
|
||||
else:
|
||||
conn = get_connection()
|
||||
c = conn.cursor()
|
||||
|
||||
c.execute("SELECT COUNT(*) FROM translations")
|
||||
total_translations = c.fetchone()[0] or 0
|
||||
|
||||
c.execute("""
|
||||
SELECT target_lang, COUNT(*) as count
|
||||
FROM translations
|
||||
GROUP BY target_lang
|
||||
ORDER BY count DESC
|
||||
""")
|
||||
by_language = {row[0]: row[1] for row in c.fetchall()}
|
||||
|
||||
c.execute("""
|
||||
SELECT m.bot_type, COUNT(*) as count
|
||||
FROM translations t
|
||||
JOIN messages m ON t.message_id = m.message_id
|
||||
GROUP BY m.bot_type
|
||||
""")
|
||||
by_platform = {row[0]: row[1] for row in c.fetchall()}
|
||||
|
||||
c.execute("""
|
||||
SELECT DATE(created_at) as date, COUNT(*) as count
|
||||
FROM translations
|
||||
WHERE created_at >= DATE('now', '-30 days')
|
||||
GROUP BY DATE(created_at)
|
||||
ORDER BY date
|
||||
""")
|
||||
by_day = [{'date': row[0], 'count': row[1]} for row in c.fetchall()]
|
||||
|
||||
c.execute("""
|
||||
SELECT COALESCE(s.server_name, 'Servidor ' || m.guild_id) as server_name,
|
||||
COUNT(*) as count
|
||||
FROM translations t
|
||||
JOIN messages m ON t.message_id = m.message_id
|
||||
LEFT JOIN discord_servers s ON m.guild_id = s.server_id
|
||||
WHERE m.guild_id IS NOT NULL
|
||||
GROUP BY m.guild_id
|
||||
ORDER BY count DESC
|
||||
""")
|
||||
by_server = [{'server': row[0], 'count': row[1]} for row in c.fetchall()]
|
||||
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
'total': total_translations,
|
||||
'by_language': by_language,
|
||||
'by_platform': by_platform,
|
||||
'by_day': by_day,
|
||||
'by_server': by_server
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user