Backup automatico script del 2025-12-01 16:49
This commit is contained in:
@@ -41,9 +41,8 @@ def generate_report():
|
||||
try:
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
cursor = conn.cursor()
|
||||
# Calcola l'orario UTC di 24h fa (perché il DB è in UTC)
|
||||
# Timezone UTC per la query
|
||||
yesterday = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(hours=24)
|
||||
|
||||
query = "SELECT download, upload, ping, created_at FROM results WHERE created_at > ? AND status = 'completed' ORDER BY created_at ASC"
|
||||
cursor.execute(query, (yesterday,))
|
||||
rows = cursor.fetchall()
|
||||
@@ -57,23 +56,24 @@ def generate_report():
|
||||
print("ℹ️ Nessun test trovato.")
|
||||
return None
|
||||
|
||||
# Variabili per statistiche
|
||||
# Variabili
|
||||
total_down = 0
|
||||
total_up = 0
|
||||
count = 0
|
||||
issues = 0
|
||||
|
||||
# Intestazione Messaggio
|
||||
# Usiamo l'ora locale del sistema per l'intestazione
|
||||
# Intestazione
|
||||
now_local = datetime.datetime.now()
|
||||
msg = f"📊 **REPORT VELOCITÀ 24H**\n📅 {now_local.strftime('%d/%m/%Y')}\n\n"
|
||||
|
||||
# Inizio Tabella Monospazio
|
||||
# Allarghiamo le colonne per farci stare "Mb"
|
||||
# ORA (5) | DOWN (6) | UP (5)
|
||||
msg += "```text\n"
|
||||
msg += "ORA | DOWN | UP \n"
|
||||
msg += "------+------+-----\n"
|
||||
msg += "ORA | DOWN | UP \n"
|
||||
msg += "------+--------+------\n"
|
||||
|
||||
for row in rows:
|
||||
# Conversione Bit -> Mbps
|
||||
d_val = (int(row[0]) * 8) / 1000000
|
||||
u_val = (int(row[1]) * 8) / 1000000
|
||||
|
||||
@@ -86,21 +86,22 @@ def generate_report():
|
||||
issues += 1
|
||||
marker = "!"
|
||||
|
||||
# Parsing Data e CONVERSIONE TIMEZONE
|
||||
try:
|
||||
d_str = row[3].split(".")[0].replace("T", " ")
|
||||
# 1. Creiamo l'oggetto datetime e gli diciamo "Tu sei UTC"
|
||||
dt_utc = datetime.datetime.strptime(d_str, '%Y-%m-%d %H:%M:%S').replace(tzinfo=datetime.timezone.utc)
|
||||
|
||||
# 2. Convertiamo nell'orario locale del sistema (definito da TZ nel docker-compose)
|
||||
dt_local = dt_utc.astimezone()
|
||||
|
||||
time_str = dt_local.strftime('%H:%M')
|
||||
except:
|
||||
time_str = "--:--"
|
||||
|
||||
# Formattazione tabella
|
||||
row_str = f"{time_str} | {d_val:>4.0f} | {u_val:>3.0f} {marker}"
|
||||
# FORMATTAZIONE CON UNITÀ
|
||||
# Creiamo stringhe tipo "850Mb"
|
||||
d_text = f"{int(d_val)}Mb"
|
||||
u_text = f"{int(u_val)}Mb"
|
||||
|
||||
# Allineamento:
|
||||
# :>6 significa "occupa 6 spazi allineato a destra"
|
||||
row_str = f"{time_str} | {d_text:>6} | {u_text:>5} {marker}"
|
||||
msg += f"{row_str}\n"
|
||||
|
||||
msg += "```\n"
|
||||
@@ -112,7 +113,8 @@ def generate_report():
|
||||
icon_d = "✅" if avg_d >= WARN_DOWN else "⚠️"
|
||||
icon_u = "✅" if avg_u >= WARN_UP else "⚠️"
|
||||
|
||||
msg += f"📈 **MEDIA:**\n⬇️ {icon_d} `{avg_d:.0f}` Mbps\n⬆️ {icon_u} `{avg_u:.0f}` Mbps"
|
||||
# Media su una riga sola con "Mb"
|
||||
msg += f"📈 **MEDIA:** ⬇️{icon_d}`{avg_d:.0f}Mb` ⬆️{icon_u}`{avg_u:.0f}Mb`"
|
||||
|
||||
if issues > 0:
|
||||
msg += f"\n\n⚠️ **{issues}** test sotto soglia (!)"
|
||||
|
||||
Reference in New Issue
Block a user