mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-09 11:12:02 +00:00
fix: 修复多项后端缺陷并加固安全与限流
- 修正 AI 会话表初始化及异步数据库 URI 处理 - 新增全局 API 与 WS 聊天限流,健康检查豁免 - 修复忘记密码接口防用户枚举与参数校验 - 修复 SSE 推送数据重复编码问题 - 修复 Cron 表达式 Quartz 问号兼容 - 修复存储模块权限标识前缀 - 新增过期日志清理与任务错误中文提示
This commit is contained in:
@@ -1,13 +1,19 @@
|
||||
from typing import Any
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, cast
|
||||
|
||||
from sqlalchemy import delete
|
||||
from sqlalchemy.engine import CursorResult
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.core.base_schema import AuthSchema, PageResultSchema
|
||||
from app.core.database import async_db_session
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import logger
|
||||
from app.utils.common_util import search_to_dict
|
||||
from app.utils.excel_util import ExcelUtil
|
||||
|
||||
from .crud import LoginLogCRUD, OperationLogCRUD
|
||||
from .model import LoginLogModel, OperationLogModel
|
||||
from .schema import (
|
||||
LoginLogDetailOutSchema,
|
||||
LoginLogOutSchema,
|
||||
@@ -123,3 +129,34 @@ class OperationLogService:
|
||||
"created_id": "操作用户ID",
|
||||
}
|
||||
return await ExcelUtil.aexport_list2excel(list_data=operation_log_list, mapping_dict=mapping_dict)
|
||||
|
||||
|
||||
async def cleanup_expired_logs(days: int = 90) -> dict:
|
||||
"""按保留天数物理清理过期日志(供定时任务调用:无请求上下文,自建会话)。
|
||||
|
||||
参数:
|
||||
- days (int): 日志保留天数,默认 90 天。
|
||||
|
||||
返回:
|
||||
- dict: 各表清理条数统计。
|
||||
"""
|
||||
if days <= 0:
|
||||
raise ValueError("日志保留天数必须大于 0")
|
||||
|
||||
cutoff = datetime.now() - timedelta(days=days)
|
||||
async with async_db_session() as session:
|
||||
# DML 语句运行时返回 CursorResult(含 rowcount),静态类型是 Result,需 cast 收窄
|
||||
op_result = cast(CursorResult, await session.execute(delete(OperationLogModel).where(OperationLogModel.created_time < cutoff)))
|
||||
login_result = cast(CursorResult, await session.execute(delete(LoginLogModel).where(LoginLogModel.created_time < cutoff)))
|
||||
await session.commit()
|
||||
|
||||
# rowcount 类型为 int | None,未命中行时驱动可能返回 None,兜底为 0
|
||||
stats = {
|
||||
"days": days,
|
||||
"operation_deleted": op_result.rowcount or 0,
|
||||
"login_deleted": login_result.rowcount or 0,
|
||||
}
|
||||
logger.info(
|
||||
f"日志保留清理完成(保留 {days} 天): 操作日志 {stats['operation_deleted']} 条,登录日志 {stats['login_deleted']} 条"
|
||||
)
|
||||
return stats
|
||||
|
||||
Reference in New Issue
Block a user