mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-09-05 07:26:39 +08:00
feat: 改进错误日志功能并优化应用初始化流程
本次提交主要包含以下更新:
- **错误日志页面增强**:
- 重构了 [`app/static/js/error_logs.js`](app/static/js/error_logs.js) 中的分页逻辑,将样式控制移至 CSS,简化了 JavaScript 代码。
- 更新了 [`app/templates/error_logs.html`](app/templates/error_logs.html) 中的分页样式,使其与 `keys_status.html` 保持一致,提升了视觉统一性。
- 在错误日志页面新增了“清空全部”按钮,方便用户一键清除所有错误记录。
- 调整了错误日志表格头部的文本颜色为白色,以改善深色主题下的可读性。
- **应用初始化与配置优化**:
- 调整了 [`app/config/config.py`](app/config/config.py) 中日志记录器的获取方式,确保在配置加载早期即可用。
- 在 [`app/core/application.py`](app/core/application.py) 中引入了更明确的数据库连接管理(连接、断开、初始化)逻辑。
- 优化了 [`app/utils/helpers.py`](app/utils/helpers.py) 中项目路径和版本文件路径的定义方式,使其在模块级别初始化。
- **依赖清理**:
- 从 [`requirements.txt`](requirements.txt) 中移除了不必要的注释。
这些更改旨在提升错误日志模块的用户体验和功能性,并优化应用程序的启动和配置管理流程。
This commit is contained in:
@@ -41,7 +41,7 @@ class StatsService:
|
||||
),
|
||||
1,
|
||||
),
|
||||
(RequestLog.status_code is None, 1), # type: ignore
|
||||
(RequestLog.status_code is None, 1),
|
||||
else_=0,
|
||||
)
|
||||
).label("failure"),
|
||||
@@ -96,7 +96,7 @@ class StatsService:
|
||||
),
|
||||
1,
|
||||
),
|
||||
(RequestLog.status_code is None, 1), # type: ignore
|
||||
(RequestLog.status_code is None, 1),
|
||||
else_=0,
|
||||
)
|
||||
).label("failure"),
|
||||
@@ -166,25 +166,24 @@ class StatsService:
|
||||
RequestLog.request_time.label("timestamp"),
|
||||
RequestLog.api_key.label("key"),
|
||||
RequestLog.model_name.label("model"),
|
||||
RequestLog.status_code, # We might need to map this to 'success'/'failure' later
|
||||
RequestLog.status_code,
|
||||
)
|
||||
.where(RequestLog.request_time >= start_time)
|
||||
.order_by(RequestLog.request_time.desc())
|
||||
) # Order by most recent first
|
||||
)
|
||||
|
||||
results = await database.fetch_all(query)
|
||||
|
||||
# Convert results to list of dicts and map status_code
|
||||
details = []
|
||||
for row in results:
|
||||
status = "failure" # 默认状态为 failure,如果 status_code 有效且在 200-299 范围内则更新为 success
|
||||
if row["status_code"] is not None: # 检查 status_code 是否为空
|
||||
status = "failure"
|
||||
if row["status_code"] is not None:
|
||||
status = "success" if 200 <= row["status_code"] < 300 else "failure"
|
||||
details.append(
|
||||
{
|
||||
"timestamp": row[
|
||||
"timestamp"
|
||||
].isoformat(), # Use ISO format for JS compatibility
|
||||
].isoformat(),
|
||||
"key": row["key"],
|
||||
"model": row["model"],
|
||||
"status": status,
|
||||
@@ -197,7 +196,6 @@ class StatsService:
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to get API call details for period '{period}': {e}")
|
||||
# Re-raise the exception to be handled by the route
|
||||
raise
|
||||
|
||||
async def get_key_usage_details_last_24h(self, key: str) -> dict | None:
|
||||
@@ -225,10 +223,10 @@ class StatsService:
|
||||
.where(
|
||||
RequestLog.api_key == key,
|
||||
RequestLog.request_time >= cutoff_time,
|
||||
RequestLog.model_name.isnot(None), # Ensure model_name is not null
|
||||
RequestLog.model_name.isnot(None),
|
||||
)
|
||||
.group_by(RequestLog.model_name)
|
||||
.order_by(func.count(RequestLog.id).desc()) # Order by count descending
|
||||
.order_by(func.count(RequestLog.id).desc())
|
||||
)
|
||||
|
||||
results = await database.fetch_all(query)
|
||||
@@ -237,7 +235,7 @@ class StatsService:
|
||||
logger.info(
|
||||
f"No usage details found for key ending in ...{key[-4:]} in the last 24h."
|
||||
)
|
||||
return {} # Return empty dict if no records found
|
||||
return {}
|
||||
|
||||
usage_details = {row["model_name"]: row["call_count"] for row in results}
|
||||
logger.info(
|
||||
@@ -250,6 +248,4 @@ class StatsService:
|
||||
f"Failed to get key usage details for key ending in ...{key[-4:]}: {e}",
|
||||
exc_info=True,
|
||||
)
|
||||
# Depending on requirements, you might return None or raise the exception
|
||||
# Raising allows the route handler to return a 500 error.
|
||||
raise # Re-raise the exception
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user