Update modules/database.py

This commit is contained in:
Blackwhitebear8 2025-10-31 20:26:28 +01:00
parent a81ba6648a
commit a766b10061

View file

@ -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]