Init
This commit is contained in:
57
CoreRouters/health.py
Normal file
57
CoreRouters/health.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""健康检查路由"""
|
||||
import psutil
|
||||
import os
|
||||
from fastapi import APIRouter
|
||||
from fastapi.responses import JSONResponse
|
||||
from CoreModules.database import get_db
|
||||
from Convention.Runtime.GlobalConfig import ProjectConfig, ConsoleFrontColor
|
||||
|
||||
config = ProjectConfig()
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/health")
|
||||
async def health_check():
|
||||
"""健康检查"""
|
||||
return JSONResponse({
|
||||
"status": "healthy",
|
||||
"service": config.FindItem("app_name", "Application")
|
||||
})
|
||||
|
||||
|
||||
@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:
|
||||
config.Log("Error", f"{ConsoleFrontColor.RED}获取系统统计失败: {e}{ConsoleFrontColor.RESET}", exc_info=True)
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"error": str(e)}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user