58 lines
2.5 KiB
Python
58 lines
2.5 KiB
Python
"""通知告警配置模型。"""
|
|
from sqlalchemy import Column, Integer, String, Text, Boolean, TIMESTAMP, func
|
|
from insurance.db.compat import db
|
|
|
|
|
|
class NotificationChannel(db.Model):
|
|
"""通知渠道配置表。"""
|
|
__tablename__ = "insurance_notification_channels"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
name = Column(String(128), nullable=False, comment="渠道名称")
|
|
type = Column(String(32), nullable=False, comment="渠道类型(email/wecom_webhook)")
|
|
config = Column(Text, default="{}", comment="渠道配置JSON")
|
|
enabled = Column(Boolean, default=True, comment="是否启用")
|
|
created_at = Column(TIMESTAMP, server_default=func.now())
|
|
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
|
|
|
|
def to_dict(self):
|
|
import json
|
|
return {
|
|
"id": f"channel-{self.id}",
|
|
"name": self.name,
|
|
"type": self.type,
|
|
"config": json.loads(self.config) if self.config else {},
|
|
"enabled": self.enabled,
|
|
"created_at": str(self.created_at) if self.created_at else None,
|
|
"updated_at": str(self.updated_at) if self.updated_at else None,
|
|
}
|
|
|
|
|
|
class AlertRule(db.Model):
|
|
"""告警规则配置表。"""
|
|
__tablename__ = "insurance_alert_rules"
|
|
|
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
|
name = Column(String(128), nullable=False, comment="规则名称")
|
|
description = Column(String(256), default="", comment="规则描述")
|
|
condition_type = Column(String(64), nullable=False, comment="触发条件类型")
|
|
condition_config = Column(Text, default="{}", comment="触发条件配置JSON")
|
|
channel_id = Column(Integer, comment="通知渠道ID")
|
|
enabled = Column(Boolean, default=True, comment="是否启用")
|
|
created_at = Column(TIMESTAMP, server_default=func.now())
|
|
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
|
|
|
|
def to_dict(self):
|
|
import json
|
|
return {
|
|
"id": f"rule-{self.id}",
|
|
"name": self.name,
|
|
"description": self.description,
|
|
"condition_type": self.condition_type,
|
|
"condition_config": json.loads(self.condition_config) if self.condition_config else {},
|
|
"channel_id": self.channel_id,
|
|
"enabled": self.enabled,
|
|
"created_at": str(self.created_at) if self.created_at else None,
|
|
"updated_at": str(self.updated_at) if self.updated_at else None,
|
|
}
|