From a766b10061214bfd7757e9945a4f924059693bd7 Mon Sep 17 00:00:00 2001 From: Blackwhitebear8 Date: Fri, 31 Oct 2025 20:26:28 +0100 Subject: [PATCH] Update modules/database.py --- modules/database.py | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/modules/database.py b/modules/database.py index 37f6c93..5097acb 100644 --- a/modules/database.py +++ b/modules/database.py @@ -54,15 +54,29 @@ def add_peer_stats(stats_list): finally: conn.close() -def get_peer_history(neighbor_address, start_date, end_date): +def get_peer_history(neighbor_address, start_date, end_date, interval_minutes=10): conn = get_db_connection() cursor = conn.cursor() - cursor.execute(''' - SELECT timestamp, prefixes_received, prefixes_sent + + interval_seconds = interval_minutes * 60 + outlier_threshold_received = 10 + + query = f''' + SELECT + MAX(timestamp) as timestamp, + CAST(AVG(CASE + WHEN prefixes_received > ? THEN prefixes_received + ELSE NULL + END) AS INTEGER) as prefixes_received, + CAST(AVG(prefixes_sent) AS INTEGER) as prefixes_sent FROM peer_history WHERE neighbor_address = ? AND timestamp BETWEEN ? AND ? + GROUP BY + CAST(strftime('%s', timestamp) / ? AS INTEGER) ORDER BY timestamp ASC - ''', (neighbor_address, start_date, end_date)) + ''' + + cursor.execute(query, (outlier_threshold_received, neighbor_address, start_date, end_date, interval_seconds)) rows = cursor.fetchall() conn.close() return [dict(row) for row in rows] @@ -80,15 +94,30 @@ def add_total_routes_stats(stats_list): finally: conn.close() -def get_total_routes_history(start_date, end_date): +def get_total_routes_history(start_date, end_date, interval_minutes=10): conn = get_db_connection() cursor = conn.cursor() - cursor.execute(''' - SELECT timestamp, ip_version, total_routes + + interval_seconds = interval_minutes * 60 + outlier_threshold = 10000 + + query = f''' + SELECT + MAX(timestamp) as timestamp, + ip_version, + CAST(AVG(CASE + WHEN total_routes > ? THEN total_routes + ELSE NULL + END) AS INTEGER) as total_routes FROM total_routes_history WHERE timestamp BETWEEN ? AND ? + GROUP BY + ip_version, + CAST(strftime('%s', timestamp) / ? AS INTEGER) ORDER BY timestamp ASC - ''', (start_date, end_date)) + ''' + + cursor.execute(query, (outlier_threshold, start_date, end_date, interval_seconds)) rows = cursor.fetchall() conn.close() return [dict(row) for row in rows]