feat(error_log): 添加清空所有错误日志的功能

主要变更:
- 在数据库服务层 ([`app/database/services.py:364`](app/database/services.py:364)) 添加了 `delete_all_error_logs` 函数。
- 在错误日志路由 ([`app/router/error_log_routes.py:186`](app/router/error_log_routes.py:186)) 中添加了新的 `DELETE /api/logs/errors/all` API 端点。
- 在前端 ([`app/static/js/error_logs.js`](app/static/js/error_logs.js)) 添加了“清空全部”按钮和相应的处理逻辑,并重构了删除确认模态框以支持此新功能。
- 将 [`app/core/application.py:42`](app/core/application.py:42) 中的 `initialize_database()` 调用从异步更改为同步。
This commit is contained in:
snaily
2025-05-15 00:23:53 +08:00
parent 4becc8d4d4
commit e260ad02bf
4 changed files with 109 additions and 43 deletions

View File

@@ -360,7 +360,38 @@ async def delete_error_log_by_id(log_id: int) -> bool:
except Exception as e:
logger.error(f"Error deleting error log with ID {log_id}: {e}", exc_info=True)
raise
async def delete_all_error_logs() -> int:
"""
删除所有错误日志条目。
Returns:
int: 被删除的错误日志数量。
"""
try:
# 1. 获取删除前的总数
count_query = select(func.count()).select_from(ErrorLog)
# fetch_val() is suitable here as we expect a single scalar value
total_to_delete = await database.fetch_val(count_query)
if total_to_delete == 0:
logger.info("No error logs found to delete.")
return 0
# 2. 执行删除操作
# This creates a query like "DELETE FROM error_log"
delete_query = delete(ErrorLog)
await database.execute(delete_query)
logger.info(f"Successfully deleted all {total_to_delete} error logs.")
return total_to_delete
except Exception as e:
logger.error(f"Failed to delete all error logs: {str(e)}", exc_info=True)
# Re-raise the exception so it can be caught by the service layer or route handler
raise
# 新增函数:添加请求日志
async def add_request_log(
model_name: Optional[str],