102 lines
2.5 KiB
Python
102 lines
2.5 KiB
Python
"""权限系统监控API
|
||
|
||
提供权限系统的监控数据和告警信息。
|
||
"""
|
||
|
||
from typing import Annotated
|
||
|
||
from fastapi import APIRouter, Depends, status
|
||
|
||
from app.core.deps import get_current_user
|
||
from app.core.permission_monitor import evaluate_permission_system_health, get_permission_monitor
|
||
|
||
router = APIRouter(prefix="/permission-monitoring", tags=["permission-monitoring"])
|
||
|
||
|
||
@router.get("/metrics", status_code=status.HTTP_200_OK)
|
||
async def get_permission_metrics(
|
||
_=Depends(get_current_user),
|
||
) -> dict:
|
||
"""获取权限系统指标
|
||
|
||
返回权限检查、缓存等运行指标。
|
||
"""
|
||
monitor = get_permission_monitor()
|
||
return monitor.get_metrics()
|
||
|
||
|
||
@router.get("/cache-stats", status_code=status.HTTP_200_OK)
|
||
async def get_cache_statistics(
|
||
_=Depends(get_current_user),
|
||
) -> dict:
|
||
"""获取缓存统计信息
|
||
|
||
返回缓存命中率、缓存项目数等信息。
|
||
"""
|
||
monitor = get_permission_monitor()
|
||
return monitor.get_cache_stats()
|
||
|
||
|
||
@router.get("/alerts", status_code=status.HTTP_200_OK)
|
||
async def get_alerts(
|
||
level: str | None = None,
|
||
limit: int = 100,
|
||
_=Depends(get_current_user),
|
||
) -> dict:
|
||
"""获取告警列表
|
||
|
||
Args:
|
||
level: 告警级别过滤(info, warning, error)
|
||
limit: 返回的最大告警数
|
||
|
||
Returns:
|
||
告警列表
|
||
"""
|
||
monitor = get_permission_monitor()
|
||
alerts = monitor.get_alerts(level=level, limit=limit)
|
||
return {
|
||
"total": len(alerts),
|
||
"alerts": alerts,
|
||
}
|
||
|
||
|
||
@router.post("/reset-metrics", status_code=status.HTTP_200_OK)
|
||
async def reset_metrics(
|
||
_=Depends(get_current_user),
|
||
) -> dict:
|
||
"""重置监控指标
|
||
|
||
清除所有累积的指标数据,重新开始统计。
|
||
"""
|
||
monitor = get_permission_monitor()
|
||
monitor.reset_metrics()
|
||
return {"message": "指标已重置"}
|
||
|
||
|
||
@router.post("/clear-alerts", status_code=status.HTTP_200_OK)
|
||
async def clear_alerts(
|
||
_=Depends(get_current_user),
|
||
) -> dict:
|
||
"""清除所有告警
|
||
|
||
删除所有累积的告警记录。
|
||
"""
|
||
monitor = get_permission_monitor()
|
||
monitor.clear_alerts()
|
||
return {"message": "告警已清除"}
|
||
|
||
|
||
@router.get("/health", status_code=status.HTTP_200_OK)
|
||
async def permission_system_health(
|
||
_=Depends(get_current_user),
|
||
) -> dict:
|
||
"""权限系统健康检查
|
||
|
||
返回权限系统的健康状态。
|
||
"""
|
||
monitor = get_permission_monitor()
|
||
metrics = monitor.get_metrics()
|
||
cache_stats = monitor.get_cache_stats()
|
||
|
||
return evaluate_permission_system_health(metrics, cache_stats)
|