""" 告警规则模型 """ from sqlalchemy import Column, String, Text, Integer, Boolean, DateTime, ForeignKey, Index, JSON, func from sqlalchemy.dialects.mysql import CHAR from sqlalchemy.orm import relationship from app.core.database import Base import uuid class AlertRule(Base): """告警规则表""" __tablename__ = "alert_rules" id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="告警规则ID") name = Column(String(100), nullable=False, comment="规则名称") description = Column(Text, comment="规则描述") # 告警类型:execution_failed(执行失败)、execution_timeout(执行超时)、error_rate(错误率)、resource_usage(资源使用) alert_type = Column(String(50), nullable=False, comment="告警类型") # 监控目标:workflow(工作流)、agent(智能体)、system(系统) target_type = Column(String(50), nullable=False, comment="监控目标类型") target_id = Column(CHAR(36), nullable=True, comment="监控目标ID(为空则监控所有)") # 告警条件(JSON格式) # 例如:{"threshold": 5, "time_window": 3600, "comparison": "gt"} 表示1小时内失败次数大于5 conditions = Column(JSON, nullable=False, comment="告警条件") # 通知方式:email(邮件)、webhook(Webhook)、internal(站内通知) notification_type = Column(String(50), nullable=False, comment="通知方式") notification_config = Column(JSON, nullable=True, comment="通知配置(如邮箱地址、Webhook URL等)") # 状态 enabled = Column(Boolean, default=True, comment="是否启用") # 统计信息 trigger_count = Column(Integer, default=0, comment="触发次数") last_triggered_at = Column(DateTime, comment="最后触发时间") # 用户关联 user_id = Column(CHAR(36), ForeignKey("users.id"), nullable=False, comment="创建者ID") # 时间戳 created_at = Column(DateTime, default=func.now(), comment="创建时间") updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment="更新时间") __table_args__ = ( Index("idx_ar_alert_type", "alert_type"), Index("idx_ar_enabled", "enabled"), Index("idx_ar_target_type", "target_type"), ) # 关系 user = relationship("User", backref="alert_rules") def __repr__(self): return f"" class AlertLog(Base): """告警日志表""" __tablename__ = "alert_logs" id = Column(CHAR(36), primary_key=True, default=lambda: str(uuid.uuid4()), comment="告警日志ID") rule_id = Column(CHAR(36), ForeignKey("alert_rules.id"), nullable=False, comment="告警规则ID") # 告警信息 alert_type = Column(String(50), nullable=False, comment="告警类型") severity = Column(String(20), default="warning", comment="严重程度:info/warning/error/critical") message = Column(Text, nullable=False, comment="告警消息") details = Column(JSON, comment="详细信息") # 状态 status = Column(String(20), default="pending", comment="状态:pending/sent/failed/acknowledged") # 通知信息 notification_type = Column(String(50), comment="通知方式") notification_result = Column(Text, comment="通知结果") # 时间戳 triggered_at = Column(DateTime, default=func.now(), comment="触发时间") acknowledged_at = Column(DateTime, comment="确认时间") acknowledged_by = Column(CHAR(36), ForeignKey("users.id"), nullable=True, comment="确认人ID") __table_args__ = ( Index("idx_al_rule_id", "rule_id"), Index("idx_al_status", "status"), Index("idx_al_triggered_at", "triggered_at"), ) # 关系 rule = relationship("AlertRule", backref="logs") acknowledged_user = relationship("User", foreign_keys=[acknowledged_by]) def __repr__(self): return f""