Files
pig-farm-controller/internal/infra/models/notify.go

58 lines
2.4 KiB
Go
Raw Normal View History

2025-10-25 13:28:19 +08:00
package models
import (
"time"
)
// NotifierType 定义了通知器的类型。
type NotifierType string
const (
// NotifierTypeSMTP 表示 SMTP 邮件通知器。
NotifierTypeSMTP NotifierType = "邮件"
// NotifierTypeWeChat 表示企业微信通知器。
NotifierTypeWeChat NotifierType = "企业微信"
// NotifierTypeLark 表示飞书通知器。
NotifierTypeLark NotifierType = "飞书"
// NotifierTypeLog 表示日志通知器,作为最终的告警记录渠道。
NotifierTypeLog NotifierType = "日志"
)
2025-10-25 14:15:17 +08:00
// NotificationStatus 定义了通知发送尝试的状态枚举。
type NotificationStatus string
const (
NotificationStatusSuccess NotificationStatus = "发送成功" // 通知已成功发送
NotificationStatusFailed NotificationStatus = "发送失败" // 通知发送失败
NotificationStatusSkipped NotificationStatus = "已跳过" // 通知因某些原因被跳过(例如:用户未配置联系方式)
)
2025-10-25 13:28:19 +08:00
// Notification 表示已发送或尝试发送的通知记录。
type Notification struct {
2025-11-10 22:23:31 +08:00
Model
2025-10-25 13:28:19 +08:00
// NotifierType 通知器类型 (例如:"邮件", "企业微信", "飞书", "日志")
NotifierType NotifierType `gorm:"type:varchar(20);not null;index" json:"notifier_type"`
2025-10-25 13:28:19 +08:00
// UserID 接收通知的用户ID用于追溯通知记录到特定用户
2025-11-10 22:23:31 +08:00
UserID uint32 `gorm:"index" json:"user_id"` // 增加 UserID 字段,并添加索引
2025-10-25 13:28:19 +08:00
// Title 通知标题
Title string `gorm:"type:varchar(255);not null" json:"title"`
// Message 通知内容
Message string `gorm:"type:text;not null" json:"message"`
// Level 通知级别 (例如INFO, WARN, ERROR)
Level SeverityLevel `gorm:"type:varchar(10);not null" json:"level"`
2025-10-25 13:28:19 +08:00
// AlarmTimestamp 通知内容生成时的时间戳,与 ID 构成复合主键
AlarmTimestamp time.Time `gorm:"primaryKey;not null" json:"alarm_timestamp"`
// ToAddress 接收地址 (例如:邮箱地址, 企业微信ID, 日志标识符)
ToAddress string `gorm:"type:varchar(255);not null" json:"to_address"`
2025-10-25 14:15:17 +08:00
// Status 通知发送尝试的状态 (例如:"待发送", "发送成功", "发送失败", "已跳过")
Status NotificationStatus `gorm:"type:varchar(20);not null;default:'待发送'" json:"status"`
2025-10-25 13:28:19 +08:00
// ErrorMessage 如果通知发送失败,此字段存储错误信息
ErrorMessage string `gorm:"type:text" json:"error_message"`
}
// TableName 指定 Notification 模型的表名。
func (Notification) TableName() string {
return "notifications"
}