Files
aiagent/backend/app/models/alert_rule.py
2026-01-19 00:09:36 +08:00

86 lines
3.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
告警规则模型
"""
from sqlalchemy import Column, String, Text, Integer, Boolean, DateTime, ForeignKey, 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邮件、webhookWebhook、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="更新时间")
# 关系
user = relationship("User", backref="alert_rules")
def __repr__(self):
return f"<AlertRule(id={self.id}, name={self.name}, alert_type={self.alert_type})>"
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")
# 关系
rule = relationship("AlertRule", backref="logs")
acknowledged_user = relationship("User", foreign_keys=[acknowledged_by])
def __repr__(self):
return f"<AlertLog(id={self.id}, rule_id={self.rule_id}, severity={self.severity})>"