55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
"""
|
|||
|
|
监控指标 API
|
||
|
|
============
|
||
|
|
|
||
|
|
【功能说明】监控仪表盘、成本统计、性能指标查询接口。
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import logging
|
||
|
|
|
||
|
|
from fastapi import APIRouter, Query
|
||
|
|
|
||
|
|
from app.schemas.project import ApiResponse
|
||
|
|
from app.services.metrics_service import get_dashboard
|
||
|
|
|
||
|
|
logger = logging.getLogger("agent.metrics.api")
|
||
|
|
|
||
|
|
router = APIRouter(prefix="/api/metrics", tags=["监控指标"])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/dashboard", response_model=ApiResponse[dict], summary="监控仪表盘")
|
||
|
|
def dashboard(hours: int = Query(default=24, ge=1, le=720, description="统计时间范围(小时)")) -> ApiResponse[dict]:
|
||
|
|
"""
|
||
|
|
获取监控仪表盘数据:模型 token/成本/延迟/调用次数 + 任务状态分布。
|
||
|
|
"""
|
||
|
|
result = get_dashboard(hours)
|
||
|
|
return ApiResponse(code=0, message=f"最近 {hours} 小时", data=result)
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/trend", response_model=ApiResponse[dict], summary="用量趋势")
|
||
|
|
def trend(days: int = Query(default=7, ge=1, le=90, description="统计天数")) -> ApiResponse[dict]:
|
||
|
|
"""
|
||
|
|
按天聚合用量趋势(最近 N 天)。
|
||
|
|
|
||
|
|
【返回】daily(每日用量)、provider_trend(各平台每日)、summary(日均/峰值)
|
||
|
|
"""
|
||
|
|
from app.services.metrics_service import get_trend
|
||
|
|
|
||
|
|
result = get_trend(days)
|
||
|
|
return ApiResponse(code=0, message=f"最近 {days} 天趋势", data=result)
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/forecast", response_model=ApiResponse[dict], summary="成本预测")
|
||
|
|
def forecast(days: int = Query(default=7, ge=1, le=90, description="预测依据天数")) -> ApiResponse[dict]:
|
||
|
|
"""
|
||
|
|
基于最近 N 天用量预测未来 30 天成本。
|
||
|
|
|
||
|
|
【算法】按日均成本线性外推 + 最近 3 天加权平均。
|
||
|
|
"""
|
||
|
|
from app.services.metrics_service import get_cost_forecast
|
||
|
|
|
||
|
|
result = get_cost_forecast(days)
|
||
|
|
return ApiResponse(code=0, message="成本预测完成", data=result)
|