From 8528dae331e5c9bd993c5c75f0008925b0fc0aa7 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Wed, 18 Mar 2026 14:37:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=20operation=20?= =?UTF-8?q?=E4=B8=AD=E6=B2=A1=E6=9C=89=E4=BD=BF=E7=94=A8=E5=88=B0=E7=9A=84?= =?UTF-8?q?=20repo=20#567?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repositories/operation_log_repository.py | 45 ------------------- backend/server/utils/common_utils.py | 16 ++++--- 2 files changed, 10 insertions(+), 51 deletions(-) delete mode 100644 backend/package/yuxi/repositories/operation_log_repository.py diff --git a/backend/package/yuxi/repositories/operation_log_repository.py b/backend/package/yuxi/repositories/operation_log_repository.py deleted file mode 100644 index e2b0bb21..00000000 --- a/backend/package/yuxi/repositories/operation_log_repository.py +++ /dev/null @@ -1,45 +0,0 @@ -"""操作日志数据访问层 - Repository""" - -from typing import Any - -from sqlalchemy import select - -from yuxi.storage.postgres.manager import pg_manager -from yuxi.storage.postgres.models_business import OperationLog - - -class OperationLogRepository: - """操作日志数据访问层""" - - async def get_by_id(self, id: int) -> OperationLog | None: - """根据 ID 获取操作日志""" - async with pg_manager.get_async_session_context() as session: - result = await session.execute(select(OperationLog).where(OperationLog.id == id)) - return result.scalar_one_or_none() - - async def list_by_user(self, user_id: int, skip: int = 0, limit: int = 100) -> list[OperationLog]: - """获取用户的操作日志列表""" - async with pg_manager.get_async_session_context() as session: - result = await session.execute( - select(OperationLog) - .where(OperationLog.user_id == user_id) - .order_by(OperationLog.timestamp.desc()) - .offset(skip) - .limit(limit) - ) - return list(result.scalars().all()) - - async def create(self, data: dict[str, Any]) -> OperationLog: - """创建操作日志""" - async with pg_manager.get_async_session_context() as session: - log = OperationLog(**data) - session.add(log) - return log - - async def count_by_user(self, user_id: int) -> int: - """统计用户操作日志数量""" - from sqlalchemy import func - - async with pg_manager.get_async_session_context() as session: - result = await session.execute(select(func.count(OperationLog.id)).where(OperationLog.user_id == user_id)) - return result.scalar() or 0 diff --git a/backend/server/utils/common_utils.py b/backend/server/utils/common_utils.py index 70f21ed2..2eca72d3 100644 --- a/backend/server/utils/common_utils.py +++ b/backend/server/utils/common_utils.py @@ -32,13 +32,17 @@ def setup_logging(): async def log_operation(db: Session, user_id: int, operation: str, details: str = None, request: Request = None): """记录用户操作日志""" - ip_address = None - if request: - ip_address = request.client.host if request.client else None + try: + ip_address = None + if request: + ip_address = request.client.host if request.client else None - log = OperationLog(user_id=user_id, operation=operation, details=details, ip_address=ip_address) - db.add(log) - await db.commit() + log = OperationLog(user_id=user_id, operation=operation, details=details, ip_address=ip_address) + db.add(log) + await db.commit() + except Exception: + # 日志写入失败不影响主业务 + pass def get_user_dict(user: User, include_password: bool = False) -> dict: