Spaces:
Sleeping
Sleeping
| # modules/sys_diagnostics.py - System Diagnostics Module | |
| from fastapi import APIRouter | |
| import psutil | |
| import os | |
| from datetime import datetime | |
| def register_module(app, client, username): | |
| """Register diagnostics module with FastAPI app""" | |
| router = APIRouter(prefix="/system") | |
| async def diagnostics_health(): | |
| """Basic system health check""" | |
| try: | |
| cpu = psutil.cpu_percent() | |
| memory = psutil.virtual_memory() | |
| return { | |
| "success": True, | |
| "module": "diagnostics", | |
| "status": "active", | |
| "system": { | |
| "cpu_usage": cpu, | |
| "memory_used": memory.percent, | |
| "memory_available": round(memory.available / (1024**3), 2), | |
| "timestamp": datetime.now().isoformat() | |
| } | |
| } | |
| except Exception as e: | |
| return { | |
| "success": False, | |
| "error": str(e), | |
| "module": "diagnostics" | |
| } | |
| async def full_diagnostics(): | |
| """Complete system diagnostics""" | |
| try: | |
| cpu = psutil.cpu_percent() | |
| memory = psutil.virtual_memory() | |
| disk = psutil.disk_usage('/') | |
| app_running = any('app.py' in p.info().get('cmdline', []) for p in psutil.process_iter(['cmdline'])) | |
| return { | |
| "success": True, | |
| "diagnostics": { | |
| "timestamp": datetime.now().isoformat(), | |
| "system_id": f"DIAG-{os.getpid()}", | |
| "status": "HEALTHY", | |
| "health_score": 85, | |
| "sections": { | |
| "system_resources": { | |
| "cpu": {"usage_percent": cpu, "cores": psutil.cpu_count()}, | |
| "memory": { | |
| "used_percent": memory.percent, | |
| "available_gb": round(memory.available / (1024**3), 2) | |
| }, | |
| "disk": { | |
| "used_percent": disk.percent, | |
| "free_gb": round(disk.free / (1024**3), 2) | |
| } | |
| }, | |
| "application": { | |
| "app_running": app_running, | |
| "directories": { | |
| "logs": os.path.exists('logs'), | |
| "data": os.path.exists('data') | |
| } | |
| } | |
| } | |
| } | |
| } | |
| except Exception as e: | |
| return { | |
| "success": False, | |
| "error": str(e), | |
| "message": "Diagnostics failed" | |
| } | |
| app.include_router(router) | |
| print("✅ Diagnostics module registered with FastAPI") | |
| return {"status": "registered"} | |
| async def get_basic_metrics(): | |
| """Get basic system metrics""" | |
| try: | |
| return { | |
| "success": True, | |
| "cpu": psutil.cpu_percent(), | |
| "memory": psutil.virtual_memory().percent, | |
| "disk": psutil.disk_usage('/').percent, | |
| "timestamp": datetime.now().isoformat() | |
| } | |
| except Exception as e: | |
| return { | |
| "success": False, | |
| "error": str(e) | |
| } |