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:
parent
75e68982da
commit
7ba65c9d96
4
.replit
4
.replit
@ -14,6 +14,10 @@ run = ["npm", "run", "start"]
|
||||
localPort = 5000
|
||||
externalPort = 80
|
||||
|
||||
[[ports]]
|
||||
localPort = 41327
|
||||
externalPort = 3000
|
||||
|
||||
[env]
|
||||
PORT = "5000"
|
||||
|
||||
|
||||
@ -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("""
|
||||
|
||||
Loading…
Reference in New Issue
Block a user