mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-07-12 16:02:44 +08:00
新增功能允许用户在 Keys 状态页面点击“详情”按钮,查看指定 API 密钥在过去 24 小时内按模型分类的请求次数统计。
主要变更包括:
后端:
- 新增 `app/router/stats_routes.py`,包含 `/api/key-usage-details/{key}` API 端点用于获取密钥使用详情。
- 重构 `app/service/stats_service.py`,将统计相关函数封装到 `StatsService` 类中,并添加 `get_key_usage_details_last_24h` 方法。
- 在 `app/router/routes.py` 中注册新的 `stats_routes`,并更新对 `stats_service` 的调用方式以使用类实例。
- 更新 `app/log/logger.py` 添加 `get_scheduler_routes` 日志记录器,并在 `app/router/scheduler_routes.py` 中使用它。
前端:
- 在 `app/templates/keys_status.html` 中为每个有效和无效密钥列表项添加“详情”按钮。
- 在 `app/templates/keys_status.html` 中添加用于显示密钥使用详情的模态框 HTML 结构。
- 在 `app/static/js/keys_status.js` 中添加 JavaScript 函数 (`showKeyUsageDetails`, `closeKeyUsageDetailsModal`, `renderKeyUsageDetails`) 来处理按钮点击事件、调用后端 API、控制模态框显示/隐藏以及渲染获取到的统计数据。
63 lines
2.5 KiB
Python
63 lines
2.5 KiB
Python
"""
|
|
定时任务控制路由模块
|
|
"""
|
|
|
|
from fastapi import APIRouter, Request, HTTPException, status # 移除 Depends, 添加 Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from app.core.security import verify_auth_token # 导入 verify_auth_token
|
|
from app.scheduler.key_checker import start_scheduler, stop_scheduler
|
|
from app.log.logger import get_scheduler_routes # 使用路由日志记录器
|
|
|
|
logger = get_scheduler_routes()
|
|
|
|
router = APIRouter(
|
|
prefix="/api/scheduler",
|
|
tags=["Scheduler"]
|
|
# 移除全局依赖
|
|
)
|
|
|
|
# 认证检查的辅助函数
|
|
async def verify_token(request: Request):
|
|
auth_token = request.cookies.get("auth_token")
|
|
if not auth_token or not verify_auth_token(auth_token):
|
|
logger.warning("Unauthorized access attempt to scheduler API")
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Not authenticated",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
|
|
@router.post("/start", summary="启动定时任务")
|
|
async def start_scheduler_endpoint(request: Request): # 添加 request 参数
|
|
"""Start the background scheduler task"""
|
|
"""
|
|
await verify_token(request) # 在函数开始处进行认证检查
|
|
"""
|
|
try:
|
|
logger.info("Received request to start scheduler.")
|
|
start_scheduler() # 调用 key_checker 中的函数
|
|
return JSONResponse(content={"message": "Scheduler started successfully."}, status_code=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
logger.error(f"Error starting scheduler: {str(e)}", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to start scheduler: {str(e)}"
|
|
)
|
|
|
|
@router.post("/stop", summary="停止定时任务")
|
|
async def stop_scheduler_endpoint(request: Request): # 添加 request 参数
|
|
"""Stop the background scheduler task"""
|
|
"""
|
|
await verify_token(request) # 在函数开始处进行认证检查
|
|
"""
|
|
try:
|
|
logger.info("Received request to stop scheduler.")
|
|
stop_scheduler() # 调用 key_checker 中的函数
|
|
return JSONResponse(content={"message": "Scheduler stopped successfully."}, status_code=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
logger.error(f"Error stopping scheduler: {str(e)}", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Failed to stop scheduler: {str(e)}"
|
|
) |