Sync Loogle Bot 2025-12-26 15:02
This commit is contained in:
@@ -11,7 +11,7 @@ import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from typing import Optional
|
||||
from typing import Optional, List, Tuple
|
||||
|
||||
DEBUG = os.environ.get("DEBUG", "0").strip() == "1"
|
||||
|
||||
@@ -38,8 +38,14 @@ CONTAINER_DB_PATH = (os.environ.get("SPEEDTEST_CONTAINER_DB_PATH") or "/config/d
|
||||
WARN_DOWN = 400
|
||||
WARN_UP = 100
|
||||
|
||||
# Unità download/upload nel DB:
|
||||
# - default: byte/s => Mbps = (val*8)/1e6
|
||||
# - se già bit/s: SPEEDTEST_VALUES_ARE_BITS=1
|
||||
VALUES_ARE_BITS = os.environ.get("SPEEDTEST_VALUES_ARE_BITS", "0").strip() == "1"
|
||||
|
||||
# Quante righe recenti leggere dal DB (poi filtriamo in Python sulle ultime 24h)
|
||||
MAX_ROWS = int(os.environ.get("SPEEDTEST_MAX_ROWS", "2000").strip())
|
||||
|
||||
|
||||
def setup_logger() -> logging.Logger:
|
||||
logger = logging.getLogger("daily_report")
|
||||
@@ -127,22 +133,39 @@ def _to_mbps(val) -> float:
|
||||
return (v * 8.0) / 1_000_000.0
|
||||
|
||||
|
||||
def _parse_created_at_utc(created_at) -> datetime.datetime:
|
||||
if isinstance(created_at, datetime.datetime):
|
||||
dt = created_at
|
||||
else:
|
||||
s = str(created_at).replace("Z", "+00:00")
|
||||
try:
|
||||
dt = datetime.datetime.fromisoformat(s)
|
||||
except Exception:
|
||||
s2 = s.split(".")[0].replace("T", " ")
|
||||
dt = datetime.datetime.strptime(s2, "%Y-%m-%d %H:%M:%S")
|
||||
def _parse_created_at_utc(created_at) -> Optional[datetime.datetime]:
|
||||
"""
|
||||
Robust parsing:
|
||||
- ISO con Z / offset
|
||||
- "YYYY-MM-DD HH:MM:SS[.ms]" (assumiamo UTC se naive, perché spesso il DB è UTC)
|
||||
"""
|
||||
try:
|
||||
if isinstance(created_at, datetime.datetime):
|
||||
dt = created_at
|
||||
else:
|
||||
s = str(created_at).strip()
|
||||
if not s:
|
||||
return None
|
||||
|
||||
if dt.tzinfo is None:
|
||||
dt = dt.replace(tzinfo=datetime.timezone.utc)
|
||||
else:
|
||||
dt = dt.astimezone(datetime.timezone.utc)
|
||||
return dt
|
||||
# normalizza Z
|
||||
s = s.replace("Z", "+00:00")
|
||||
|
||||
# prova ISO (anche con T)
|
||||
try:
|
||||
dt = datetime.datetime.fromisoformat(s)
|
||||
except Exception:
|
||||
# fallback: "YYYY-MM-DD HH:MM:SS(.ms)"
|
||||
s2 = s.split(".")[0].replace("T", " ")
|
||||
dt = datetime.datetime.strptime(s2, "%Y-%m-%d %H:%M:%S")
|
||||
|
||||
if dt.tzinfo is None:
|
||||
# assumi UTC se naive
|
||||
dt = dt.replace(tzinfo=datetime.timezone.utc)
|
||||
else:
|
||||
dt = dt.astimezone(datetime.timezone.utc)
|
||||
return dt
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def find_local_db_path() -> str:
|
||||
@@ -186,20 +209,22 @@ def docker_copy_db_to_temp() -> str:
|
||||
def generate_report(db_path: str) -> Optional[str]:
|
||||
now_utc = datetime.datetime.now(datetime.timezone.utc)
|
||||
window_start_utc = now_utc - datetime.timedelta(hours=24)
|
||||
window_start_str = window_start_utc.isoformat(timespec="seconds")
|
||||
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
cursor = conn.cursor()
|
||||
|
||||
# NOTA: non filtriamo su created_at (string compare fragile).
|
||||
# Prendiamo le ultime MAX_ROWS righe completate e filtriamo in Python.
|
||||
query = """
|
||||
SELECT download, upload, ping, created_at
|
||||
FROM results
|
||||
WHERE created_at >= ?
|
||||
AND status = 'completed'
|
||||
ORDER BY created_at ASC
|
||||
WHERE status = 'completed'
|
||||
ORDER BY created_at DESC
|
||||
LIMIT ?
|
||||
"""
|
||||
cursor.execute(query, (window_start_str,))
|
||||
rows = cursor.fetchall()
|
||||
cursor.execute(query, (MAX_ROWS,))
|
||||
raw_rows = cursor.fetchall()
|
||||
except Exception as e:
|
||||
LOGGER.exception("Errore DB (%s): %s", db_path, e)
|
||||
return None
|
||||
@@ -209,8 +234,37 @@ def generate_report(db_path: str) -> Optional[str]:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not raw_rows:
|
||||
LOGGER.info("Nessun test trovato.")
|
||||
return None
|
||||
|
||||
# Filtra realmente per datetime (ultime 24h) e ordina crescente
|
||||
rows: List[Tuple[datetime.datetime, float, float, float]] = []
|
||||
for d_raw, u_raw, ping_raw, created_at in raw_rows:
|
||||
dt_utc = _parse_created_at_utc(created_at)
|
||||
if not dt_utc:
|
||||
continue
|
||||
if dt_utc < window_start_utc or dt_utc > now_utc:
|
||||
continue
|
||||
|
||||
d_mbps = _to_mbps(d_raw)
|
||||
u_mbps = _to_mbps(u_raw)
|
||||
try:
|
||||
ping_ms = float(ping_raw)
|
||||
except Exception:
|
||||
ping_ms = 0.0
|
||||
|
||||
rows.append((dt_utc, d_mbps, u_mbps, ping_ms))
|
||||
|
||||
rows.sort(key=lambda x: x[0])
|
||||
|
||||
LOGGER.debug("DB rows read=%s filtered_24h=%s (start=%s now=%s)",
|
||||
len(raw_rows), len(rows),
|
||||
window_start_utc.isoformat(timespec="seconds"),
|
||||
now_utc.isoformat(timespec="seconds"))
|
||||
|
||||
if not rows:
|
||||
LOGGER.info("Nessun test trovato nelle ultime 24h.")
|
||||
LOGGER.info("Nessun test nelle ultime 24h dopo filtro datetime.")
|
||||
return None
|
||||
|
||||
header = "ORA | Dn | Up | Pg |!"
|
||||
@@ -228,14 +282,7 @@ def generate_report(db_path: str) -> Optional[str]:
|
||||
msg += header + "\n"
|
||||
msg += sep + "\n"
|
||||
|
||||
for d_raw, u_raw, ping_raw, created_at in rows:
|
||||
d_mbps = _to_mbps(d_raw)
|
||||
u_mbps = _to_mbps(u_raw)
|
||||
try:
|
||||
ping_ms = float(ping_raw)
|
||||
except Exception:
|
||||
ping_ms = 0.0
|
||||
|
||||
for dt_utc, d_mbps, u_mbps, ping_ms in rows:
|
||||
total_down += d_mbps
|
||||
total_up += u_mbps
|
||||
count += 1
|
||||
@@ -245,29 +292,22 @@ def generate_report(db_path: str) -> Optional[str]:
|
||||
issues += 1
|
||||
flag = "!"
|
||||
|
||||
try:
|
||||
dt_utc = _parse_created_at_utc(created_at)
|
||||
dt_local = dt_utc.astimezone()
|
||||
time_str = dt_local.strftime("%H:%M")
|
||||
except Exception:
|
||||
time_str = "--:--"
|
||||
time_str = dt_utc.astimezone().strftime("%H:%M")
|
||||
|
||||
msg += f"{time_str:<5}|{int(round(d_mbps)):>5}|{int(round(u_mbps)):>5}|{int(round(ping_ms)):>4}|{flag}\n"
|
||||
|
||||
msg += "```\n"
|
||||
|
||||
if count > 0:
|
||||
avg_d = total_down / count
|
||||
avg_u = total_up / count
|
||||
avg_d = total_down / count
|
||||
avg_u = total_up / count
|
||||
|
||||
icon_d = "✅" if avg_d >= WARN_DOWN else "⚠️"
|
||||
icon_u = "✅" if avg_u >= WARN_UP else "⚠️"
|
||||
icon_d = "✅" if avg_d >= WARN_DOWN else "⚠️"
|
||||
icon_u = "✅" if avg_u >= WARN_UP else "⚠️"
|
||||
|
||||
# Ø al posto di "MEDIA:" per accorciare ed evitare wrap
|
||||
msg += f"Ø ⬇️{icon_d}`{avg_d:.0f} Mbps` ⬆️{icon_u}`{avg_u:.0f} Mbps`"
|
||||
msg += f"Ø ⬇️{icon_d}`{avg_d:.0f} Mbps` ⬆️{icon_u}`{avg_u:.0f} Mbps`"
|
||||
|
||||
if issues > 0:
|
||||
msg += f"\n\n⚠️ **{issues}** test sotto soglia (!)"
|
||||
if issues > 0:
|
||||
msg += f"\n\n⚠️ **{issues}** test sotto soglia (!)"
|
||||
|
||||
return msg
|
||||
|
||||
|
||||
Reference in New Issue
Block a user