operation.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from datetime import datetime
  2. from zoneinfo import ZoneInfo
  3. from app.constants import OperationType, OperationStatus
  4. from app.models import db
  5. class Operation(db.Model):
  6. """
  7. 操作模型类,表示数据库中的 'operation' 表。
  8. 包括操作类型、描述、耗时、状态、用户信息等,用于系统的审计、监控和分析。
  9. Attributes:
  10. operation_id (int): 操作记录 ID,自动增长的主键。
  11. operation_type (str): 操作类型,使用枚举定义,如 'authenticate', 'create', 'read', 'update', 'delete' 等。
  12. description (str): 对操作的详细描述,帮助理解操作的背景和过程。
  13. duration (float): 操作耗时,单位为秒(s)。
  14. failure_message (str): 当操作失败时,记录失败信息,帮助诊断问题。
  15. ip_address (str): 用户进行操作时的 IP 地址,记录操作来源的网络地址。
  16. device_info (str): 用户操作时的设备信息,通常为浏览器类型、操作系统等。
  17. status (str): 操作状态,表示操作是否成功,使用枚举定义,'success' 或 'failure'。
  18. created_at (datetime): 操作记录的时间戳,自动记录每条操作的创建时间。
  19. owner_id (int): 操作执行者的用户 ID,外键关联用户表,标识执行该操作的用户。
  20. Relationships:
  21. owner (User): 每条操作日志记录一个用户,表示该操作由哪个用户执行。
  22. """
  23. __tablename__ = 'operation'
  24. operation_id = db.Column(db.Integer, primary_key=True, autoincrement=True) # 操作记录 ID
  25. operation_type = db.Column(db.Enum(OperationType), default=OperationType.READ, nullable=False) # 操作类型
  26. description = db.Column(db.Text, nullable=False) # 操作描述
  27. duration = db.Column(db.Float, default=0.0, nullable=False) # 操作耗时(s)
  28. failure_message = db.Column(db.Text, default='无') # 失败信息
  29. ip_address = db.Column(db.String(45), nullable=False) # IP 地址
  30. device_info = db.Column(db.String(255), nullable=False) # 设备信息
  31. status = db.Column(db.Enum(OperationStatus), default=OperationStatus.SUCCESS, nullable=False) # 操作状态
  32. created_at = db.Column(db.DateTime, default=lambda: datetime.now(ZoneInfo("Asia/Shanghai"))) # 操作时间
  33. owner_id = db.Column(db.Integer, db.ForeignKey('user.user_id')) # 所属用户 ID(外键)
  34. # 设置与 User 的关系:一个操作记录只属于一个用户
  35. owner = db.relationship('User', backref=db.backref('operations', lazy=True))
  36. def __repr__(self):
  37. return (f"Operation(operation_id: {self.operation_id}, "
  38. f"operation_type: {self.operation_type.name}, "
  39. f"description: {self.description}, "
  40. f"duration: {self.duration}, "
  41. f"failure_message: {self.failure_message}, "
  42. f"ip_address: {self.ip_address}, "
  43. f"device_info: {self.device_info}, "
  44. f"status: {self.status.name}, "
  45. f"created_at: {self.created_at}, "
  46. f"owner_id: {self.owner_id})")
  47. def to_dict(self):
  48. """
  49. 将 Operation 实例转化为字典。
  50. """
  51. return {
  52. 'operation_id': self.operation_id,
  53. 'operation_type': self.operation_type.name,
  54. 'description': self.description,
  55. 'duration': self.duration,
  56. 'failure_message': self.failure_message,
  57. 'ip_address': self.ip_address,
  58. 'device_info': self.device_info,
  59. 'status': self.status.name,
  60. 'created_at': self.created_at,
  61. 'owner_id': self.owner_id
  62. }