Update flask_server.py

This commit is contained in:
MacRimi
2026-03-08 00:30:05 +01:00
parent 673c206e02
commit 94670711e7

View File

@@ -631,7 +631,7 @@ def init_latency_db():
return False return False
def _measure_latency(target_ip: str) -> dict: def _measure_latency(target_ip: str) -> dict:
"""Ping a target and return latency stats.""" """Ping a target and return latency stats. Uses 3 pings and returns the average to avoid false positives."""
try: try:
result = subprocess.run( result = subprocess.run(
['ping', '-c', '3', '-W', '2', target_ip], ['ping', '-c', '3', '-W', '2', target_ip],
@@ -652,18 +652,16 @@ def _measure_latency(target_ip: str) -> dict:
return { return {
'success': True, 'success': True,
'avg': round(sum(latencies) / len(latencies), 1), 'avg': round(sum(latencies) / len(latencies), 1),
'min': round(min(latencies), 1),
'max': round(max(latencies), 1),
'packet_loss': round((3 - len(latencies)) / 3 * 100, 1) 'packet_loss': round((3 - len(latencies)) / 3 * 100, 1)
} }
# Ping failed - 100% packet loss # Ping failed - 100% packet loss
return {'success': False, 'avg': None, 'min': None, 'max': None, 'packet_loss': 100.0} return {'success': False, 'avg': None, 'packet_loss': 100.0}
except Exception: except Exception:
return {'success': False, 'avg': None, 'min': None, 'max': None, 'packet_loss': 100.0} return {'success': False, 'avg': None, 'packet_loss': 100.0}
def _record_latency(): def _record_latency():
"""Record latency to the default gateway.""" """Record latency to the default gateway. Only stores the average of 3 pings."""
try: try:
gateway = _get_default_gateway() gateway = _get_default_gateway()
stats = _measure_latency(gateway) stats = _measure_latency(gateway)
@@ -671,9 +669,9 @@ def _record_latency():
conn = _get_temp_db() conn = _get_temp_db()
conn.execute( conn.execute(
"""INSERT INTO latency_history """INSERT INTO latency_history
(timestamp, target, latency_avg, latency_min, latency_max, packet_loss) (timestamp, target, latency_avg, packet_loss)
VALUES (?, ?, ?, ?, ?, ?)""", VALUES (?, ?, ?, ?)""",
(int(time.time()), 'gateway', stats['avg'], stats['min'], stats['max'], stats['packet_loss']) (int(time.time()), 'gateway', stats['avg'], stats['packet_loss'])
) )
conn.commit() conn.commit()
conn.close() conn.close()
@@ -718,20 +716,18 @@ def get_latency_history(target='gateway', timeframe='hour'):
if interval is None: if interval is None:
cursor = conn.execute( cursor = conn.execute(
"""SELECT timestamp, latency_avg, latency_min, latency_max, packet_loss """SELECT timestamp, latency_avg, packet_loss
FROM latency_history FROM latency_history
WHERE timestamp >= ? AND target = ? WHERE timestamp >= ? AND target = ?
ORDER BY timestamp ASC""", ORDER BY timestamp ASC""",
(since, target) (since, target)
) )
rows = cursor.fetchall() rows = cursor.fetchall()
data = [{"timestamp": r[0], "value": r[1], "min": r[2], "max": r[3], "packet_loss": r[4]} for r in rows if r[1] is not None] data = [{"timestamp": r[0], "value": r[1], "packet_loss": r[2]} for r in rows if r[1] is not None]
else: else:
cursor = conn.execute( cursor = conn.execute(
"""SELECT (timestamp / ?) * ? as bucket, """SELECT (timestamp / ?) * ? as bucket,
ROUND(AVG(latency_avg), 1) as avg_val, ROUND(AVG(latency_avg), 1) as avg_val,
ROUND(MIN(latency_min), 1) as min_val,
ROUND(MAX(latency_max), 1) as max_val,
ROUND(AVG(packet_loss), 1) as avg_loss ROUND(AVG(packet_loss), 1) as avg_loss
FROM latency_history FROM latency_history
WHERE timestamp >= ? AND target = ? WHERE timestamp >= ? AND target = ?
@@ -740,19 +736,17 @@ def get_latency_history(target='gateway', timeframe='hour'):
(interval, interval, since, target) (interval, interval, since, target)
) )
rows = cursor.fetchall() rows = cursor.fetchall()
data = [{"timestamp": r[0], "value": r[1], "min": r[2], "max": r[3], "packet_loss": r[4]} for r in rows if r[1] is not None] data = [{"timestamp": r[0], "value": r[1], "packet_loss": r[2]} for r in rows if r[1] is not None]
conn.close() conn.close()
# Compute stats # Compute stats using the averaged values shown in the graph
if data: if data:
values = [d["value"] for d in data if d["value"] is not None] values = [d["value"] for d in data if d["value"] is not None]
if values: if values:
mins = [d["min"] for d in data if d.get("min") is not None]
maxs = [d["max"] for d in data if d.get("max") is not None]
stats = { stats = {
"min": round(min(mins) if mins else min(values), 1), "min": round(min(values), 1),
"max": round(max(maxs) if maxs else max(values), 1), "max": round(max(values), 1),
"avg": round(sum(values) / len(values), 1), "avg": round(sum(values) / len(values), 1),
"current": values[-1] if values else 0 "current": values[-1] if values else 0
} }