web: status dash updates

This commit is contained in:
2025-10-22 20:27:30 -04:00
committed by sbcrumb
parent 413c8ad6f6
commit 68baa1766c
4 changed files with 252 additions and 65 deletions
+17 -30
View File
@@ -1375,48 +1375,35 @@ def register_web_routes(app, dependencies):
@app.get("/api/scan/status")
async def api_scan_status():
"""Check scan status using simple time-based tracking"""
"""Proxy scan status requests to core container for detailed progress"""
import urllib.request
import urllib.error
import json
import os
import socket
from datetime import datetime, timedelta
# Check if we have a recent scan
if scan_tracking["last_scan_time"]:
time_since_scan = datetime.now() - scan_tracking["last_scan_time"]
# Assume scan is active for up to 10 minutes after initiation
# This is a reasonable estimate for most scan operations
if time_since_scan < timedelta(minutes=10) and scan_tracking["scanning"]:
minutes_elapsed = int(time_since_scan.total_seconds() / 60)
seconds_elapsed = int(time_since_scan.total_seconds() % 60)
if minutes_elapsed > 0:
elapsed_str = f"{minutes_elapsed}m {seconds_elapsed}s"
else:
elapsed_str = f"{seconds_elapsed}s"
return {
"scanning": True,
"message": f"Scan in progress ({elapsed_str} elapsed) - check core logs for details"
}
# Check if core container is responsive
# Get core container connection details
core_host = os.environ.get("CORE_API_HOST", "nfoguard-core")
core_port = os.environ.get("CORE_API_PORT", "8080")
try:
health_url = f"http://{core_host}:{core_port}/health"
req = urllib.request.Request(health_url)
# Call core container's detailed scan status endpoint
status_url = f"http://{core_host}:{core_port}/api/scan/status"
req = urllib.request.Request(status_url)
with urllib.request.urlopen(req, timeout=5) as response:
# Mark scanning as false if we reach here and enough time has passed
scan_tracking["scanning"] = False
return {"scanning": False, "message": "No active scan detected"}
status_data = json.loads(response.read().decode())
return status_data
except urllib.error.HTTPError as e:
if e.code == 404:
# Core container doesn't have the endpoint, fallback to simple tracking
return {"scanning": False, "message": "Detailed status not available"}
else:
return {"scanning": False, "message": f"Core container error: {e.code}"}
except (urllib.error.URLError, socket.timeout):
return {"scanning": False, "message": "Core container unavailable"}
except Exception:
return {"scanning": False, "message": "Unable to check scan status"}
except json.JSONDecodeError:
return {"scanning": False, "message": "Invalid response from core container"}
except Exception as e:
return {"scanning": False, "message": f"Unable to check scan status: {str(e)}"}