mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-06 16:16:37 +08:00
feat(stats): 添加 API 调用详情查看功能
- 在 keys_status 页面添加了 API 调用统计卡片(1分钟/1小时/24小时)的可点击功能。 - 点击卡片会弹出一个模态框,显示对应时间段内的详细 API 调用记录,包括时间戳、部分 API 密钥、模型名称和调用状态(成功/失败)。 - 后端新增 `/api/stats/details` API 端点,用于根据请求的时间段('1m', '1h', '24h')从数据库查询并返回调用详情。 - 新增 `stats_service.get_api_call_details` 服务函数处理数据查询和格式化逻辑。 - 前端 `keys_status.js` 添加了 fetch 调用、模态框显示/隐藏以及数据渲染逻辑。 - 为 `keys_status` 页面添加了每 60 秒自动刷新的功能。 - 优化数据库连接配置,在 `create_engine` 中添加 `pool_pre_ping=True` 以提高连接可靠性。
This commit is contained in:
+31
-1
@@ -10,7 +10,7 @@ from app.core.security import verify_auth_token
|
||||
from app.log.logger import get_routes_logger
|
||||
from app.router import gemini_routes, openai_routes, config_routes, log_routes, scheduler_routes # 新增导入
|
||||
from app.service.key.key_manager import get_key_manager_instance
|
||||
from app.service.stats_service import get_api_usage_stats # <-- Import stats service
|
||||
from app.service.stats_service import get_api_usage_stats, get_api_call_details # <-- Import stats service and details function
|
||||
|
||||
logger = get_routes_logger()
|
||||
|
||||
@@ -38,6 +38,7 @@ def setup_routers(app: FastAPI) -> None:
|
||||
|
||||
# 添加健康检查路由
|
||||
setup_health_routes(app)
|
||||
setup_api_stats_routes(app) # Add API stats routes
|
||||
|
||||
|
||||
def setup_page_routes(app: FastAPI) -> None:
|
||||
@@ -158,3 +159,32 @@ def setup_health_routes(app: FastAPI) -> None:
|
||||
"""健康检查端点"""
|
||||
logger.info("Health check endpoint called")
|
||||
return {"status": "healthy"}
|
||||
|
||||
|
||||
def setup_api_stats_routes(app: FastAPI) -> None:
|
||||
"""
|
||||
设置 API 统计相关的路由
|
||||
|
||||
Args:
|
||||
app: FastAPI应用程序实例
|
||||
"""
|
||||
@app.get("/api/stats/details")
|
||||
async def api_stats_details(request: Request, period: str):
|
||||
"""获取指定时间段内的 API 调用详情"""
|
||||
try:
|
||||
# 验证认证
|
||||
auth_token = request.cookies.get("auth_token")
|
||||
if not auth_token or not verify_auth_token(auth_token):
|
||||
logger.warning("Unauthorized access attempt to API stats details")
|
||||
# Returning JSON error instead of redirect for API endpoint
|
||||
return {"error": "Unauthorized"}, 401
|
||||
|
||||
logger.info(f"Fetching API call details for period: {period}")
|
||||
details = await get_api_call_details(period)
|
||||
return details
|
||||
except ValueError as e:
|
||||
logger.warning(f"Invalid period requested for API stats details: {period} - {str(e)}")
|
||||
return {"error": str(e)}, 400
|
||||
except Exception as e:
|
||||
logger.error(f"Error fetching API stats details for period {period}: {str(e)}")
|
||||
return {"error": "Internal server error"}, 500
|
||||
|
||||
Reference in New Issue
Block a user