add statistics

This commit is contained in:
vrubelroman 2025-11-13 13:32:46 +03:00
parent 23de80f94d
commit ceb62b408a
6 changed files with 318 additions and 3 deletions

View file

@ -1,7 +1,9 @@
from flask import Flask, jsonify, render_template
from flask_cors import CORS
import sqlite3
from datetime import datetime
import sys
import os
from datetime import datetime, date
app = Flask(__name__)
CORS(app)
@ -9,6 +11,14 @@ CORS(app)
# Путь к базе данных бота
DB_PATH = "/app/data/lichess_bot.db"
# Add parent directory to path to import message_counters
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'LichessClientTG_bot'))
try:
from message_counters import MessageCounters
except ImportError:
# Fallback if import fails
MessageCounters = None
@app.route('/')
def index():
"""Главная страница"""
@ -60,11 +70,32 @@ def get_users():
''')
total_gamers = cursor.fetchone()[0]
# Get message counters statistics
message_stats = {}
if MessageCounters:
try:
counters = MessageCounters(db_path=DB_PATH)
message_stats = counters.get_stats_summary()
except Exception as e:
print(f"Error getting message counters: {e}")
message_stats = {
'total_all_time': 0,
'total_today': 0,
'by_command': {}
}
else:
message_stats = {
'total_all_time': 0,
'total_today': 0,
'by_command': {}
}
return jsonify({
'success': True,
'users': users,
'total_users': len(users),
'total_gamers': total_gamers
'total_gamers': total_gamers,
'message_stats': message_stats
})
except Exception as e:

View file

@ -250,6 +250,20 @@
Кол-во игроков: <strong id="total-gamers">0</strong>
</div>
<div class="stats" style="margin-top: 15px; padding: 15px; background: #f5f5f5; border-radius: 8px;">
<h3 style="margin-top: 0; margin-bottom: 10px; font-size: 16px;">📨 Счетчики сообщений</h3>
<div style="margin-bottom: 8px;">
Всего отправлено: <strong id="total-messages-all">0</strong>
</div>
<div style="margin-bottom: 8px;">
Сегодня отправлено: <strong id="total-messages-today">0</strong>
</div>
<div id="message-stats-by-command" style="margin-top: 10px; font-size: 12px; color: #666;">
<div style="margin-bottom: 5px;"><strong>По командам:</strong></div>
<div id="command-stats-list"></div>
</div>
</div>
<div class="search-box">
<input type="text" id="search-input" placeholder="Поиск по имени или никнейму...">
</div>
@ -292,6 +306,45 @@
users = data.users;
document.getElementById('total-users').textContent = data.total_users;
document.getElementById('total-gamers').textContent = data.total_gamers;
// Update message counters
if (data.message_stats) {
document.getElementById('total-messages-all').textContent = data.message_stats.total_all_time || 0;
document.getElementById('total-messages-today').textContent = data.message_stats.total_today || 0;
// Render command stats
const commandStatsList = document.getElementById('command-stats-list');
if (data.message_stats.by_command && Object.keys(data.message_stats.by_command).length > 0) {
const commandNames = {
'addgamer': 'Add Gamer',
'addtoken': 'Add Token',
'getgamers': 'Get Gamers',
'delgamer': 'Del Gamer',
'today': 'Today',
'yesterday': 'Yesterday',
'week': 'Week',
'setperiod': 'Set Period',
'periodic_notification': 'Periodic Notifications'
};
// Filter out excluded commands
const excludedCommands = ['start', 'lang', 'resetlang'];
const filteredCommands = Object.entries(data.message_stats.by_command)
.filter(([cmd]) => !excludedCommands.includes(cmd));
commandStatsList.innerHTML = filteredCommands
.sort((a, b) => b[1].total - a[1].total)
.map(([cmd, stats]) => {
const cmdName = commandNames[cmd] || cmd;
return `<div style="margin-bottom: 3px;">
${cmdName}: <strong>${stats.total}</strong> (сегодня: <strong>${stats.today}</strong>)
</div>`;
}).join('');
} else {
commandStatsList.innerHTML = '<div style="color: #999;">Нет данных</div>';
}
}
filteredUsers = users;
renderUsers();