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
This commit is contained in:
marco370 2025-11-17 18:18:30 +00:00
parent 75e68982da
commit 7ba65c9d96
2 changed files with 14 additions and 5 deletions

View File

@ -14,6 +14,10 @@ run = ["npm", "run", "start"]
localPort = 5000 localPort = 5000
externalPort = 80 externalPort = 80
[[ports]]
localPort = 41327
externalPort = 3000
[env] [env]
PORT = "5000" PORT = "5000"

View File

@ -388,26 +388,31 @@ async def get_stats():
# Log stats # Log stats
cursor.execute("SELECT COUNT(*) as total FROM network_logs") 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(""" cursor.execute("""
SELECT COUNT(*) as recent FROM network_logs SELECT COUNT(*) as recent FROM network_logs
WHERE timestamp >= NOW() - INTERVAL '1 hour' WHERE timestamp >= NOW() - INTERVAL '1 hour'
""") """)
recent_logs = cursor.fetchone()['recent'] result = cursor.fetchone()
recent_logs = result['recent'] if result else 0
# Detection stats # Detection stats
cursor.execute("SELECT COUNT(*) as total FROM detections") 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(""" cursor.execute("""
SELECT COUNT(*) as blocked FROM detections WHERE blocked = true 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 # Router stats
cursor.execute("SELECT COUNT(*) as total FROM routers WHERE enabled = true") 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 # Latest training
cursor.execute(""" cursor.execute("""