mirror of
https://github.com/snailyp/gemini-balance.git
synced 2026-05-11 18:09:55 +08:00
为错误日志页面增加了按 ID 排序以及单条和批量删除日志的功能。
主要变更:
后端 (Python/FastAPI):
- `services.py`:
- `get_error_logs`: 添加 `sort_by` 和 `sort_order` 参数以支持排序。
- 新增 `delete_error_logs`: 实现基于 ID 列表的批量删除。
- 新增 `delete_error_log_by_id`: 实现基于单个 ID 的删除。
- `error_log_routes.py`:
- `GET /api/logs/errors`: 添加 `sortBy` 和 `sortOrder` 查询参数以支持前端排序请求。
- 新增 `DELETE /api/logs/errors`: 处理批量删除请求。
- 新增 `DELETE /api/logs/errors/{log_id}`: 处理单条删除请求。
- `connection.py`: 移除了不再使用的同步 SQLAlchemy Session 相关代码。
前端 (HTML/JavaScript):
- `error_logs.html`:
- 调整了搜索/操作区域布局,添加了批量删除按钮。
- ID 表头增加排序图标和点击事件。
- 表格行操作列添加了删除按钮。
- 新增了删除确认模态框。
- `error_logs.js`:
- 添加了处理 ID 排序点击的逻辑,更新排序状态并重新加载数据。
- 添加了处理单条和批量删除按钮点击的逻辑。
- 实现了删除确认模态框的显示/隐藏及确认逻辑。
- 修改 `loadErrorLogs` 以包含排序参数。
- 修改 `renderErrorLogs` 以添加行删除按钮和必要的 `data-log-id` 属性。
- 更新了全选/取消全选逻辑以同步批量删除按钮状态。
59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
"""
|
||
数据库连接池模块
|
||
"""
|
||
from databases import Database
|
||
from sqlalchemy import create_engine, MetaData
|
||
# from sqlalchemy.orm import sessionmaker # 不再需要
|
||
from sqlalchemy.ext.declarative import declarative_base
|
||
|
||
from app.config.config import settings
|
||
from app.log.logger import get_database_logger
|
||
|
||
logger = get_database_logger()
|
||
|
||
# 数据库URL
|
||
DATABASE_URL = f"mysql+pymysql://{settings.MYSQL_USER}:{settings.MYSQL_PASSWORD}@{settings.MYSQL_HOST}:{settings.MYSQL_PORT}/{settings.MYSQL_DATABASE}"
|
||
|
||
# 创建数据库引擎
|
||
# pool_pre_ping=True: 在从连接池获取连接前执行简单的 "ping" 测试,确保连接有效
|
||
engine = create_engine(DATABASE_URL, pool_pre_ping=True)
|
||
|
||
# 创建元数据对象
|
||
metadata = MetaData()
|
||
|
||
# 创建基类
|
||
Base = declarative_base(metadata=metadata)
|
||
|
||
# 创建数据库连接池,并配置连接池参数
|
||
# min_size/max_size: 连接池的最小/最大连接数
|
||
# pool_recycle=3600: 连接在池中允许存在的最大秒数(生命周期)。
|
||
# 设置为 3600 秒(1小时),确保在 MySQL 默认的 wait_timeout (通常8小时) 或其他网络超时之前回收连接。
|
||
# 如果遇到连接失效问题,可以尝试调低此值,使其小于实际的 wait_timeout 或网络超时时间。
|
||
# databases 库会自动处理连接失效后的重连尝试。
|
||
database = Database(DATABASE_URL, min_size=5, max_size=20, pool_recycle=1800) # Reduced recycle time to 30 mins
|
||
|
||
# 移除了 SessionLocal 和 get_db 函数
|
||
|
||
# --- Async connection functions for lifespan/async routes ---
|
||
async def connect_to_db():
|
||
"""
|
||
连接到数据库
|
||
"""
|
||
try:
|
||
await database.connect()
|
||
logger.info("Connected to database")
|
||
except Exception as e:
|
||
logger.error(f"Failed to connect to database: {str(e)}")
|
||
raise
|
||
|
||
|
||
async def disconnect_from_db():
|
||
"""
|
||
断开数据库连接
|
||
"""
|
||
try:
|
||
await database.disconnect()
|
||
logger.info("Disconnected from database")
|
||
except Exception as e:
|
||
logger.error(f"Failed to disconnect from database: {str(e)}")
|