mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-06-05 07:39:54 +08:00
本次提交主要包含以下更改: - 代码清理: - 移除了 `app/router/` 目录下多个路由文件 ([`config_routes.py`](app/router/config_routes.py:1), [`error_log_routes.py`](app/router/error_log_routes.py:1), [`gemini_routes.py`](app/router/gemini_routes.py:1), [`openai_compatiable_routes.py`](app/router/openai_compatiable_routes.py:1), [`openai_routes.py`](app/router/openai_routes.py:1), [`routes.py`](app/router/routes.py:1), [`scheduler_routes.py`](app/router/scheduler_routes.py:1), [`stats_routes.py`](app/router/stats_routes.py:1), [`version_routes.py`](app/router/version_routes.py:1)) 中的大量解释性注释、TODO 注释和多余的日志标记。 - 清理了 [`scheduler_routes.py`](app/router/scheduler_routes.py:31) 中被注释掉的认证逻辑。 - 这些清理旨在提高代码的整洁度和可维护性。 - UI 优化: - 在 [`app/templates/config_editor.html`](app/templates/config_editor.html:327) 中,为 Gemini 模型的安全过滤级别设置增加了一条重要的提示信息,建议用户将其设置为 "OFF" 以避免影响输出速度,并强调非必要不应随意改动。
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Request
|
||
from starlette import status
|
||
from app.core.security import verify_auth_token
|
||
from app.service.stats.stats_service import StatsService
|
||
from app.log.logger import get_stats_logger
|
||
|
||
logger = get_stats_logger()
|
||
|
||
|
||
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 = APIRouter(
|
||
prefix="/api",
|
||
tags=["stats"],
|
||
dependencies=[Depends(verify_token)]
|
||
)
|
||
|
||
stats_service = StatsService()
|
||
|
||
@router.get("/key-usage-details/{key}",
|
||
summary="获取指定密钥最近24小时的模型调用次数",
|
||
description="根据提供的 API 密钥,返回过去24小时内每个模型被调用的次数统计。")
|
||
async def get_key_usage_details(key: str):
|
||
"""
|
||
Retrieves the model usage count for a specific API key within the last 24 hours.
|
||
|
||
Args:
|
||
key: The API key to get usage details for.
|
||
|
||
Returns:
|
||
A dictionary with model names as keys and their call counts as values.
|
||
Example: {"gemini-pro": 10, "gemini-1.5-pro-latest": 5}
|
||
|
||
Raises:
|
||
HTTPException: If an error occurs during data retrieval.
|
||
"""
|
||
try:
|
||
usage_details = await stats_service.get_key_usage_details_last_24h(key)
|
||
if usage_details is None:
|
||
return {}
|
||
return usage_details
|
||
except Exception as e:
|
||
logger.error(f"Error fetching key usage details for key {key[:4]}...: {e}")
|
||
raise HTTPException(
|
||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||
detail=f"获取密钥使用详情时出错: {e}"
|
||
) |