mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-21 00:01:01 +08:00
本次提交主要包含以下更新:
- **错误日志页面增强**:
- 重构了 [`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) 中移除了不必要的注释。
这些更改旨在提升错误日志模块的用户体验和功能性,并优化应用程序的启动和配置管理流程。
180 lines
5.7 KiB
Python
180 lines
5.7 KiB
Python
from datetime import datetime, timedelta, timezone
|
||
from typing import Any, Dict, List, Optional
|
||
|
||
from sqlalchemy import delete, func, select
|
||
|
||
from app.config.config import settings
|
||
from app.database import services as db_services
|
||
from app.database.connection import database
|
||
from app.database.models import ErrorLog
|
||
from app.log.logger import get_error_log_logger
|
||
|
||
logger = get_error_log_logger()
|
||
|
||
|
||
async def delete_old_error_logs():
|
||
"""
|
||
Deletes error logs older than a specified number of days,
|
||
based on the AUTO_DELETE_ERROR_LOGS_ENABLED and AUTO_DELETE_ERROR_LOGS_DAYS settings.
|
||
"""
|
||
if not settings.AUTO_DELETE_ERROR_LOGS_ENABLED:
|
||
logger.info("Auto-deletion of error logs is disabled. Skipping.")
|
||
return
|
||
|
||
days_to_keep = settings.AUTO_DELETE_ERROR_LOGS_DAYS
|
||
if not isinstance(days_to_keep, int) or days_to_keep <= 0:
|
||
logger.error(
|
||
f"Invalid AUTO_DELETE_ERROR_LOGS_DAYS value: {days_to_keep}. Must be a positive integer. Skipping deletion."
|
||
)
|
||
return
|
||
|
||
cutoff_date = datetime.now(timezone.utc) - timedelta(days=days_to_keep)
|
||
|
||
logger.info(
|
||
f"Attempting to delete error logs older than {days_to_keep} days (before {cutoff_date.strftime('%Y-%m-%d %H:%M:%S %Z')})."
|
||
)
|
||
|
||
try:
|
||
if not database.is_connected:
|
||
await database.connect()
|
||
logger.info("Database connection established for deleting error logs.")
|
||
|
||
# First, count how many logs will be deleted (optional, for logging)
|
||
count_query = select(func.count(ErrorLog.id)).where(
|
||
ErrorLog.request_time < cutoff_date
|
||
)
|
||
num_logs_to_delete = await database.fetch_val(count_query)
|
||
|
||
if num_logs_to_delete == 0:
|
||
logger.info(
|
||
"No error logs found older than the specified period. No deletion needed."
|
||
)
|
||
return
|
||
|
||
logger.info(f"Found {num_logs_to_delete} error logs to delete.")
|
||
|
||
# Perform the deletion
|
||
query = delete(ErrorLog).where(ErrorLog.request_time < cutoff_date)
|
||
await database.execute(query)
|
||
logger.info(
|
||
f"Successfully deleted {num_logs_to_delete} error logs older than {days_to_keep} days."
|
||
)
|
||
|
||
except Exception as e:
|
||
logger.error(
|
||
f"Error during automatic deletion of error logs: {e}", exc_info=True
|
||
)
|
||
|
||
|
||
async def process_get_error_logs(
|
||
limit: int,
|
||
offset: int,
|
||
key_search: Optional[str],
|
||
error_search: Optional[str],
|
||
error_code_search: Optional[str],
|
||
start_date: Optional[datetime],
|
||
end_date: Optional[datetime],
|
||
sort_by: str,
|
||
sort_order: str,
|
||
) -> Dict[str, Any]:
|
||
"""
|
||
处理错误日志的检索,支持分页和过滤。
|
||
"""
|
||
try:
|
||
logs_data = await db_services.get_error_logs(
|
||
limit=limit,
|
||
offset=offset,
|
||
key_search=key_search,
|
||
error_search=error_search,
|
||
error_code_search=error_code_search,
|
||
start_date=start_date,
|
||
end_date=end_date,
|
||
sort_by=sort_by,
|
||
sort_order=sort_order,
|
||
)
|
||
total_count = await db_services.get_error_logs_count(
|
||
key_search=key_search,
|
||
error_search=error_search,
|
||
error_code_search=error_code_search,
|
||
start_date=start_date,
|
||
end_date=end_date,
|
||
)
|
||
return {"logs": logs_data, "total": total_count}
|
||
except Exception as e:
|
||
logger.error(f"Service error in process_get_error_logs: {e}", exc_info=True)
|
||
raise
|
||
|
||
|
||
async def process_get_error_log_details(log_id: int) -> Optional[Dict[str, Any]]:
|
||
"""
|
||
处理特定错误日志详细信息的检索。
|
||
如果未找到,则返回 None。
|
||
"""
|
||
try:
|
||
log_details = await db_services.get_error_log_details(log_id=log_id)
|
||
return log_details
|
||
except Exception as e:
|
||
logger.error(
|
||
f"Service error in process_get_error_log_details for ID {log_id}: {e}",
|
||
exc_info=True,
|
||
)
|
||
raise
|
||
|
||
|
||
async def process_delete_error_logs_by_ids(log_ids: List[int]) -> int:
|
||
"""
|
||
按 ID 批量删除错误日志。
|
||
返回尝试删除的日志数量。
|
||
"""
|
||
if not log_ids:
|
||
return 0
|
||
try:
|
||
deleted_count = await db_services.delete_error_logs_by_ids(log_ids)
|
||
return deleted_count
|
||
except Exception as e:
|
||
logger.error(
|
||
f"Service error in process_delete_error_logs_by_ids for IDs {log_ids}: {e}",
|
||
exc_info=True,
|
||
)
|
||
raise
|
||
|
||
|
||
async def process_delete_error_log_by_id(log_id: int) -> bool:
|
||
"""
|
||
按 ID 删除单个错误日志。
|
||
如果删除成功(或找到日志并尝试删除),则返回 True,否则返回 False。
|
||
"""
|
||
try:
|
||
success = await db_services.delete_error_log_by_id(log_id)
|
||
return success
|
||
except Exception as e:
|
||
logger.error(
|
||
f"Service error in process_delete_error_log_by_id for ID {log_id}: {e}",
|
||
exc_info=True,
|
||
)
|
||
raise
|
||
|
||
|
||
async def process_delete_all_error_logs() -> int:
|
||
"""
|
||
处理删除所有错误日志的请求。
|
||
返回删除的日志数量。
|
||
"""
|
||
try:
|
||
# 确保数据库已连接 (如果适用,类似于 delete_old_error_logs)
|
||
# if not database.is_connected:
|
||
# await database.connect()
|
||
# logger.info("Database connection established for deleting all error logs.")
|
||
|
||
deleted_count = await db_services.delete_all_error_logs()
|
||
logger.info(
|
||
f"Successfully processed request to delete all error logs. Count: {deleted_count}"
|
||
)
|
||
return deleted_count
|
||
except Exception as e:
|
||
logger.error(
|
||
f"Service error in process_delete_all_error_logs: {e}",
|
||
exc_info=True,
|
||
)
|
||
raise
|