35 lines
1.6 KiB
Python
35 lines
1.6 KiB
Python
"""操作审计日志。由 middleware 在变更接口成功后异步写入。"""
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import JSON, DateTime, ForeignKey, Integer, String, func
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.db.base import Base
|
|
|
|
|
|
class OpLog(Base):
|
|
__tablename__ = "op_log"
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
operator_id: Mapped[int | None] = mapped_column(
|
|
ForeignKey("user.id", ondelete="SET NULL"), nullable=True, index=True
|
|
)
|
|
operator_name: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
# 业务模块:auth.user / auth.role / product ...
|
|
module: Mapped[str] = mapped_column(String(64), nullable=False, index=True)
|
|
# 动作:create / update / delete / reset_password / test ...
|
|
action: Mapped[str] = mapped_column(String(32), nullable=False)
|
|
method: Mapped[str] = mapped_column(String(8), nullable=False)
|
|
path: Mapped[str] = mapped_column(String(255), nullable=False)
|
|
status_code: Mapped[int] = mapped_column(Integer, nullable=False)
|
|
ip: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
|
user_agent: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
|
# 请求体 / 响应体(敏感字段需由写入方做脱敏)
|
|
payload: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
|
cost_ms: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False, index=True
|
|
)
|