82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
import sqlite3
|
|
import datetime
|
|
import os
|
|
|
|
# Chemin de la base de données (dans un sous-dossier "data" pour garder le projet propre)
|
|
DB_PATH = "data/stats.db"
|
|
|
|
# Crée le dossier "data" si besoin
|
|
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
|
|
|
# Connexion à la base de données
|
|
conn = sqlite3.connect(DB_PATH)
|
|
cursor = conn.cursor()
|
|
|
|
# Création de la table si elle n'existe pas
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS commandes (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
nom TEXT,
|
|
user_id INTEGER,
|
|
guild_id INTEGER,
|
|
date TIMESTAMP
|
|
)
|
|
''')
|
|
conn.commit()
|
|
|
|
def enregistrer_utilisation(nom_commande, user_id, guild_id):
|
|
"""Enregistre l'utilisation d'une commande."""
|
|
cursor.execute(
|
|
"INSERT INTO commandes (nom, user_id, guild_id, date) VALUES (?, ?, ?, ?)",
|
|
(nom_commande, user_id, guild_id, datetime.datetime.now())
|
|
)
|
|
conn.commit()
|
|
|
|
def get_stats():
|
|
"""Retourne les statistiques d'utilisation du bot."""
|
|
cursor.execute("SELECT COUNT(*) FROM commandes")
|
|
total = cursor.fetchone()[0]
|
|
|
|
cursor.execute("SELECT nom, COUNT(*) as nb FROM commandes GROUP BY nom ORDER BY nb DESC LIMIT 5")
|
|
top_commandes = cursor.fetchall()
|
|
|
|
cursor.execute("SELECT user_id, COUNT(*) as nb FROM commandes GROUP BY user_id ORDER BY nb DESC LIMIT 1")
|
|
top_user = cursor.fetchone()
|
|
|
|
return total, top_commandes, top_user
|
|
|
|
|
|
def get_stats_detailed():
|
|
# Exemple de données simulées, à remplacer par tes vraies données
|
|
from datetime import datetime, timedelta
|
|
|
|
total_commandes = 1234
|
|
top_commandes = [("help", 250), ("stats_bot", 180), ("play", 160), ("ban", 100), ("kick", 90)]
|
|
top_utilisateurs = [(123456789012345678, 300), (987654321098765432, 220), (111222333444555666, 180), (444555666777888999, 160), (555666777888999000, 130)]
|
|
|
|
today = datetime.today()
|
|
evolution_commandes = []
|
|
for i in range(7): # dernière semaine
|
|
day = today - timedelta(days=6 - i)
|
|
evolution_commandes.append((day, 150 + i*10))
|
|
|
|
repartition_statuts = {"À faire": 50, "En cours": 30, "Terminée": 70, "Bloqué": 10}
|
|
|
|
commandes_par_jour = {
|
|
"Lundi": 180,
|
|
"Mardi": 210,
|
|
"Mercredi": 150,
|
|
"Jeudi": 200,
|
|
"Vendredi": 220,
|
|
"Samedi": 90,
|
|
"Dimanche": 40,
|
|
}
|
|
|
|
return {
|
|
"total_commandes": total_commandes,
|
|
"top_commandes": top_commandes,
|
|
"top_utilisateurs": top_utilisateurs,
|
|
"evolution_commandes": evolution_commandes,
|
|
"repartition_statuts": repartition_statuts,
|
|
"commandes_par_jour": commandes_par_jour
|
|
}
|