import enum from datetime import datetime from sqlalchemy import BigInteger, Boolean, Column, DateTime, Enum, Integer, String from .device import Base PRIMARY_KEY_TYPE = BigInteger().with_variant(Integer, "sqlite") class AlertTypeEnum(str, enum.Enum): offline = "offline" # 设备离线 degraded = "degraded" # 设备丢包故障 recovered = "recovered" # 设备恢复 system = "system" # 系统告警(如上游断网检测) class AlertEvent(Base): """告警事件表""" __tablename__ = "alert_events" id = Column(PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True) device_id = Column(Integer, nullable=False, index=True, comment="关联设备 ID") alert_type = Column(Enum(AlertTypeEnum), nullable=False, comment="告警类型") message = Column(String(1024), default="", comment="告警消息摘要") # 离线起止 start_at = Column(DateTime, nullable=False, comment="离线开始时间") end_at = Column(DateTime, nullable=True, comment="恢复时间") duration_minutes = Column(Integer, nullable=True, comment="离线时长(分钟)") is_resolved = Column(Boolean, default=False, comment="是否已恢复") notification_sent = Column(Boolean, default=False, comment="是否已发送通知") notification_attempts = Column(Integer, default=0, comment="通知尝试次数") last_notification_error = Column( String(512), nullable=True, comment="最近一次通知失败原因", ) related_event_id = Column( BigInteger, nullable=True, comment="关联的原始或升级告警事件 ID", ) previous_status = Column(String(16), nullable=True, comment="状态变更前状态") current_status = Column(String(16), nullable=True, comment="状态变更后状态") acknowledged_at = Column(DateTime, nullable=True, comment="用户确认时间") created_at = Column(DateTime, default=datetime.now, comment="创建时间") def __repr__(self): return f""