Files
WPSBot/routers/health.py
2025-10-28 13:00:35 +08:00

58 lines
1.4 KiB
Python

"""健康检查路由"""
import logging
import psutil
import os
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from core.database import get_db
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("/health")
async def health_check():
"""健康检查"""
return JSONResponse({
"status": "healthy",
"service": "WPS Bot Game"
})
@router.get("/stats")
async def system_stats():
"""系统资源统计(开发用)"""
try:
process = psutil.Process(os.getpid())
memory_mb = process.memory_info().rss / 1024 / 1024
# 数据库统计
db = get_db()
cursor = db.conn.cursor()
cursor.execute("SELECT COUNT(*) FROM users")
user_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM game_states")
active_games = cursor.fetchone()[0]
return JSONResponse({
"system": {
"memory_mb": round(memory_mb, 2),
"threads": process.num_threads(),
"cpu_percent": process.cpu_percent()
},
"database": {
"users": user_count,
"active_games": active_games
}
})
except Exception as e:
logger.error(f"获取系统统计失败: {e}", exc_info=True)
return JSONResponse(
status_code=500,
content={"error": str(e)}
)