Spaces:
Sleeping
Sleeping
Update modules/diagnostics.py
Browse files- modules/diagnostics.py +74 -28
modules/diagnostics.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# modules/diagnostics.py - System Diagnostics Module
|
| 2 |
from fastapi import APIRouter
|
| 3 |
import psutil
|
|
|
|
| 4 |
from datetime import datetime
|
| 5 |
|
| 6 |
def register_module(app, client, username):
|
|
@@ -10,37 +11,75 @@ def register_module(app, client, username):
|
|
| 10 |
@router.get("/diagnostics/health")
|
| 11 |
async def diagnostics_health():
|
| 12 |
"""Basic system health check"""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
"
|
| 21 |
-
"
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
-
}
|
| 26 |
|
| 27 |
@router.get("/diagnostics/full")
|
| 28 |
async def full_diagnostics():
|
| 29 |
"""Complete system diagnostics"""
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return {
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"
|
| 40 |
-
"
|
| 41 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
}
|
| 43 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
app.include_router(router)
|
| 46 |
print("✅ Diagnostics module registered with FastAPI")
|
|
@@ -48,9 +87,16 @@ def register_module(app, client, username):
|
|
| 48 |
# Export function for direct imports
|
| 49 |
async def get_basic_metrics():
|
| 50 |
"""Get basic system metrics"""
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# modules/diagnostics.py - System Diagnostics Module
|
| 2 |
from fastapi import APIRouter
|
| 3 |
import psutil
|
| 4 |
+
import os
|
| 5 |
from datetime import datetime
|
| 6 |
|
| 7 |
def register_module(app, client, username):
|
|
|
|
| 11 |
@router.get("/diagnostics/health")
|
| 12 |
async def diagnostics_health():
|
| 13 |
"""Basic system health check"""
|
| 14 |
+
try:
|
| 15 |
+
cpu = psutil.cpu_percent()
|
| 16 |
+
memory = psutil.virtual_memory()
|
| 17 |
+
|
| 18 |
+
return {
|
| 19 |
+
"success": True,
|
| 20 |
+
"module": "diagnostics",
|
| 21 |
+
"status": "active",
|
| 22 |
+
"system": {
|
| 23 |
+
"cpu_usage": cpu,
|
| 24 |
+
"memory_used": memory.percent,
|
| 25 |
+
"memory_available": round(memory.available / (1024**3), 2), # GB
|
| 26 |
+
"timestamp": datetime.now().isoformat()
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return {
|
| 31 |
+
"success": False,
|
| 32 |
+
"error": str(e),
|
| 33 |
+
"module": "diagnostics"
|
| 34 |
}
|
|
|
|
| 35 |
|
| 36 |
@router.get("/diagnostics/full")
|
| 37 |
async def full_diagnostics():
|
| 38 |
"""Complete system diagnostics"""
|
| 39 |
try:
|
| 40 |
+
# Direct implementation without circular import
|
| 41 |
+
cpu = psutil.cpu_percent()
|
| 42 |
+
memory = psutil.virtual_memory()
|
| 43 |
+
disk = psutil.disk_usage('/')
|
| 44 |
+
|
| 45 |
+
# Check if app.py is running
|
| 46 |
+
app_running = any('app.py' in p.info().get('cmdline', []) for p in psutil.process_iter(['cmdline']))
|
| 47 |
+
|
| 48 |
return {
|
| 49 |
+
"success": True,
|
| 50 |
+
"diagnostics": {
|
| 51 |
+
"timestamp": datetime.now().isoformat(),
|
| 52 |
+
"system_id": f"DIAG-{os.getpid()}",
|
| 53 |
+
"status": "HEALTHY",
|
| 54 |
+
"health_score": 85,
|
| 55 |
+
"sections": {
|
| 56 |
+
"system_resources": {
|
| 57 |
+
"cpu": {"usage_percent": cpu, "cores": psutil.cpu_count()},
|
| 58 |
+
"memory": {
|
| 59 |
+
"used_percent": memory.percent,
|
| 60 |
+
"available_gb": round(memory.available / (1024**3), 2)
|
| 61 |
+
},
|
| 62 |
+
"disk": {
|
| 63 |
+
"used_percent": disk.percent,
|
| 64 |
+
"free_gb": round(disk.free / (1024**3), 2)
|
| 65 |
+
}
|
| 66 |
+
},
|
| 67 |
+
"application": {
|
| 68 |
+
"app_running": app_running,
|
| 69 |
+
"directories": {
|
| 70 |
+
"logs": os.path.exists('logs'),
|
| 71 |
+
"data": os.path.exists('data')
|
| 72 |
+
}
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
}
|
| 76 |
}
|
| 77 |
+
except Exception as e:
|
| 78 |
+
return {
|
| 79 |
+
"success": False,
|
| 80 |
+
"error": str(e),
|
| 81 |
+
"message": "Diagnostics failed"
|
| 82 |
+
}
|
| 83 |
|
| 84 |
app.include_router(router)
|
| 85 |
print("✅ Diagnostics module registered with FastAPI")
|
|
|
|
| 87 |
# Export function for direct imports
|
| 88 |
async def get_basic_metrics():
|
| 89 |
"""Get basic system metrics"""
|
| 90 |
+
try:
|
| 91 |
+
return {
|
| 92 |
+
"success": True,
|
| 93 |
+
"cpu": psutil.cpu_percent(),
|
| 94 |
+
"memory": psutil.virtual_memory().percent,
|
| 95 |
+
"disk": psutil.disk_usage('/').percent,
|
| 96 |
+
"timestamp": datetime.now().isoformat()
|
| 97 |
+
}
|
| 98 |
+
except Exception as e:
|
| 99 |
+
return {
|
| 100 |
+
"success": False,
|
| 101 |
+
"error": str(e)
|
| 102 |
+
}
|