From 7ba65c9d96e828331798241fb8e2a0eae8858144 Mon Sep 17 00:00:00 2001 From: marco370 <48531002-marco370@users.noreply.replit.com> Date: Mon, 17 Nov 2025 18:18:30 +0000 Subject: [PATCH] Fix errors when retrieving statistics by handling empty results Update the get_stats function in main.py to safely fetch and process counts from the database, preventing potential errors when no records are found for total logs, recent logs, detections, blocked IPs, and active routers. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528 Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Event-Id: 853b2085-c74d-4b3d-adeb-9db4276a24aa Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/1P26v7M --- .replit | 4 ++++ python_ml/main.py | 15 ++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/.replit b/.replit index af7007c..1b4063e 100644 --- a/.replit +++ b/.replit @@ -14,6 +14,10 @@ run = ["npm", "run", "start"] localPort = 5000 externalPort = 80 +[[ports]] +localPort = 41327 +externalPort = 3000 + [env] PORT = "5000" diff --git a/python_ml/main.py b/python_ml/main.py index cbdbfb7..d576067 100644 --- a/python_ml/main.py +++ b/python_ml/main.py @@ -388,26 +388,31 @@ async def get_stats(): # Log stats cursor.execute("SELECT COUNT(*) as total FROM network_logs") - total_logs = cursor.fetchone()['total'] + result = cursor.fetchone() + total_logs = result['total'] if result else 0 cursor.execute(""" SELECT COUNT(*) as recent FROM network_logs WHERE timestamp >= NOW() - INTERVAL '1 hour' """) - recent_logs = cursor.fetchone()['recent'] + result = cursor.fetchone() + recent_logs = result['recent'] if result else 0 # Detection stats cursor.execute("SELECT COUNT(*) as total FROM detections") - total_detections = cursor.fetchone()['total'] + result = cursor.fetchone() + total_detections = result['total'] if result else 0 cursor.execute(""" SELECT COUNT(*) as blocked FROM detections WHERE blocked = true """) - blocked_ips = cursor.fetchone()['blocked'] + result = cursor.fetchone() + blocked_ips = result['blocked'] if result else 0 # Router stats cursor.execute("SELECT COUNT(*) as total FROM routers WHERE enabled = true") - active_routers = cursor.fetchone()['total'] + result = cursor.fetchone() + active_routers = result['total'] if result else 0 # Latest training cursor.execute("""