Cursor Agent
commited on
Commit
·
85f07c7
1
Parent(s):
b5ac54c
fix: Make system_status_api resilient to missing psutil
Browse files- Add graceful fallback when psutil not available
- System status API won't crash if psutil missing
- Resources will show 0 values instead of failing
- Improves compatibility with different environments
backend/routers/system_status_api.py
CHANGED
|
@@ -1,16 +1,23 @@
|
|
| 1 |
"""
|
| 2 |
-
System Status API - Comprehensive system status for
|
| 3 |
-
Provides aggregated status of all services, endpoints, coins
|
| 4 |
All data is REAL and measured, no fake data.
|
| 5 |
"""
|
| 6 |
import logging
|
| 7 |
import time
|
| 8 |
-
import psutil
|
| 9 |
from datetime import datetime
|
| 10 |
from typing import Dict, Any, List, Optional
|
| 11 |
from fastapi import APIRouter, HTTPException
|
| 12 |
from pydantic import BaseModel
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
| 16 |
router = APIRouter()
|
|
@@ -63,51 +70,76 @@ class SystemStatusResponse(BaseModel):
|
|
| 63 |
@router.get("/api/system/status", response_model=SystemStatusResponse)
|
| 64 |
async def get_system_status():
|
| 65 |
"""
|
| 66 |
-
Get comprehensive system status for the
|
| 67 |
|
| 68 |
Returns:
|
| 69 |
- overall_health: Overall system health status
|
| 70 |
- services: Status of backend services and providers
|
| 71 |
- endpoints: Health of API endpoints
|
| 72 |
- coins: Status of cryptocurrency data feeds
|
| 73 |
-
- resources: System resource metrics
|
| 74 |
|
| 75 |
All data is REAL and measured, no fake data.
|
| 76 |
"""
|
| 77 |
try:
|
| 78 |
-
from
|
| 79 |
-
|
| 80 |
-
tracker = get_metrics_tracker()
|
| 81 |
-
|
| 82 |
-
# 1. Get system resources
|
| 83 |
-
cpu_percent = psutil.cpu_percent(interval=0.1)
|
| 84 |
-
memory = psutil.virtual_memory()
|
| 85 |
-
uptime = tracker.get_uptime()
|
| 86 |
-
|
| 87 |
try:
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
| 91 |
|
| 92 |
-
resources
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
-
#
|
| 102 |
services = await check_services_status()
|
| 103 |
|
| 104 |
-
#
|
| 105 |
endpoints = await check_endpoints_health()
|
| 106 |
|
| 107 |
-
#
|
| 108 |
coins = await check_coin_feeds()
|
| 109 |
|
| 110 |
-
#
|
| 111 |
overall_health = determine_overall_health(services, endpoints, resources)
|
| 112 |
|
| 113 |
return SystemStatusResponse(
|
|
|
|
| 1 |
"""
|
| 2 |
+
System Status API - Comprehensive system status for drawer display
|
| 3 |
+
Provides aggregated status of all services, endpoints, coins
|
| 4 |
All data is REAL and measured, no fake data.
|
| 5 |
"""
|
| 6 |
import logging
|
| 7 |
import time
|
|
|
|
| 8 |
from datetime import datetime
|
| 9 |
from typing import Dict, Any, List, Optional
|
| 10 |
from fastapi import APIRouter, HTTPException
|
| 11 |
from pydantic import BaseModel
|
| 12 |
|
| 13 |
+
# Try to import psutil, but don't fail if not available
|
| 14 |
+
try:
|
| 15 |
+
import psutil
|
| 16 |
+
PSUTIL_AVAILABLE = True
|
| 17 |
+
except ImportError:
|
| 18 |
+
PSUTIL_AVAILABLE = False
|
| 19 |
+
logging.warning("psutil not available - system resource metrics will be limited")
|
| 20 |
+
|
| 21 |
logger = logging.getLogger(__name__)
|
| 22 |
|
| 23 |
router = APIRouter()
|
|
|
|
| 70 |
@router.get("/api/system/status", response_model=SystemStatusResponse)
|
| 71 |
async def get_system_status():
|
| 72 |
"""
|
| 73 |
+
Get comprehensive system status for the drawer display
|
| 74 |
|
| 75 |
Returns:
|
| 76 |
- overall_health: Overall system health status
|
| 77 |
- services: Status of backend services and providers
|
| 78 |
- endpoints: Health of API endpoints
|
| 79 |
- coins: Status of cryptocurrency data feeds
|
| 80 |
+
- resources: System resource metrics (if available)
|
| 81 |
|
| 82 |
All data is REAL and measured, no fake data.
|
| 83 |
"""
|
| 84 |
try:
|
| 85 |
+
# Get uptime from metrics tracker if available
|
| 86 |
+
uptime_seconds = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
try:
|
| 88 |
+
from backend.routers.system_metrics_api import get_metrics_tracker
|
| 89 |
+
tracker = get_metrics_tracker()
|
| 90 |
+
uptime_seconds = tracker.get_uptime()
|
| 91 |
+
except:
|
| 92 |
+
uptime_seconds = 0
|
| 93 |
|
| 94 |
+
# Get system resources if psutil is available
|
| 95 |
+
if PSUTIL_AVAILABLE:
|
| 96 |
+
try:
|
| 97 |
+
cpu_percent = psutil.cpu_percent(interval=0.1)
|
| 98 |
+
memory = psutil.virtual_memory()
|
| 99 |
+
try:
|
| 100 |
+
load_avg = list(psutil.getloadavg())
|
| 101 |
+
except AttributeError:
|
| 102 |
+
load_avg = None
|
| 103 |
+
|
| 104 |
+
resources = SystemResources(
|
| 105 |
+
cpu_percent=round(cpu_percent, 2),
|
| 106 |
+
memory_percent=round(memory.percent, 2),
|
| 107 |
+
memory_used_mb=round(memory.used / (1024 * 1024), 2),
|
| 108 |
+
memory_total_mb=round(memory.total / (1024 * 1024), 2),
|
| 109 |
+
uptime_seconds=uptime_seconds,
|
| 110 |
+
load_avg=load_avg
|
| 111 |
+
)
|
| 112 |
+
except Exception as e:
|
| 113 |
+
logger.warning(f"Failed to get system resources: {e}")
|
| 114 |
+
resources = SystemResources(
|
| 115 |
+
cpu_percent=0.0,
|
| 116 |
+
memory_percent=0.0,
|
| 117 |
+
memory_used_mb=0.0,
|
| 118 |
+
memory_total_mb=0.0,
|
| 119 |
+
uptime_seconds=uptime_seconds,
|
| 120 |
+
load_avg=None
|
| 121 |
+
)
|
| 122 |
+
else:
|
| 123 |
+
# Fallback when psutil not available
|
| 124 |
+
resources = SystemResources(
|
| 125 |
+
cpu_percent=0.0,
|
| 126 |
+
memory_percent=0.0,
|
| 127 |
+
memory_used_mb=0.0,
|
| 128 |
+
memory_total_mb=0.0,
|
| 129 |
+
uptime_seconds=uptime_seconds,
|
| 130 |
+
load_avg=None
|
| 131 |
+
)
|
| 132 |
|
| 133 |
+
# Check services status
|
| 134 |
services = await check_services_status()
|
| 135 |
|
| 136 |
+
# Check endpoints health
|
| 137 |
endpoints = await check_endpoints_health()
|
| 138 |
|
| 139 |
+
# Check coin feeds
|
| 140 |
coins = await check_coin_feeds()
|
| 141 |
|
| 142 |
+
# Determine overall health
|
| 143 |
overall_health = determine_overall_health(services, endpoints, resources)
|
| 144 |
|
| 145 |
return SystemStatusResponse(
|