2025-11-07 20:56:04 +08:00
|
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-11-07 22:19:55 +08:00
|
|
|
|
// AlarmSourceType 定义了告警的来源类型
|
|
|
|
|
|
type AlarmSourceType string
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
AlarmSourceTypeDevice AlarmSourceType = "普通设备"
|
|
|
|
|
|
AlarmSourceTypeAreaController AlarmSourceType = "区域主控"
|
|
|
|
|
|
AlarmSourceTypeSystem AlarmSourceType = "系统"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-11-09 21:37:35 +08:00
|
|
|
|
// AlarmCode 定义了标准化的告警类型标识
|
|
|
|
|
|
type AlarmCode string
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
// --- 设备相关告警 ---
|
|
|
|
|
|
AlarmCodeTemperature AlarmCode = "温度阈值"
|
|
|
|
|
|
AlarmCodeHumidity AlarmCode = "湿度阈值"
|
|
|
|
|
|
AlarmCodeWeight AlarmCode = "重量阈值"
|
|
|
|
|
|
AlarmCodeBatteryLevel AlarmCode = "电池电量阈值"
|
|
|
|
|
|
AlarmCodeSignalMetrics AlarmCode = "信号强度阈值"
|
|
|
|
|
|
AlarmCodeDeviceOffline AlarmCode = "设备离线"
|
|
|
|
|
|
|
|
|
|
|
|
// --- 区域主控相关告警 ---
|
|
|
|
|
|
AlarmCodeAreaControllerOffline AlarmCode = "区域主控离线"
|
|
|
|
|
|
|
|
|
|
|
|
// --- 系统相关告警 ---
|
|
|
|
|
|
// (可在此处预留或添加)
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-11-07 20:56:04 +08:00
|
|
|
|
// ActiveAlarm 活跃告警
|
2025-11-07 22:19:55 +08:00
|
|
|
|
// 活跃告警会被更新(如忽略状态),因此保留 gorm.Model 以包含所有标准字段。
|
2025-11-07 20:56:04 +08:00
|
|
|
|
type ActiveAlarm struct {
|
|
|
|
|
|
gorm.Model
|
2025-11-07 22:19:55 +08:00
|
|
|
|
|
2025-11-09 21:37:35 +08:00
|
|
|
|
SourceType AlarmSourceType `gorm:"type:varchar(50);not null;index:idx_alarm_uniqueness;comment:告警来源类型" json:"source_type"`
|
2025-11-07 22:19:55 +08:00
|
|
|
|
// SourceID 告警来源ID,其具体含义取决于 SourceType 字段 (例如:设备ID, 区域主控ID, 猪栏ID)。
|
2025-11-09 21:37:35 +08:00
|
|
|
|
SourceID uint `gorm:"not null;index:idx_alarm_uniqueness;comment:告警来源ID" json:"source_id"`
|
|
|
|
|
|
|
|
|
|
|
|
// AlarmCode 是一个机器可读的、标准化的告警类型标识。
|
|
|
|
|
|
// 它与 SourceType 和 SourceID 共同构成一个活跃告警的唯一标识。
|
|
|
|
|
|
AlarmCode AlarmCode `gorm:"type:varchar(100);not null;index:idx_alarm_uniqueness;comment:告警代码" json:"alarm_code"`
|
2025-11-07 22:19:55 +08:00
|
|
|
|
|
|
|
|
|
|
AlarmSummary string `gorm:"comment:告警简述" json:"alarm_summary"`
|
2025-11-09 21:37:35 +08:00
|
|
|
|
Level SeverityLevel `gorm:"type:varchar(10);not null;index:idx_notification_query;comment:严重性等级" json:"level"`
|
2025-11-07 22:19:55 +08:00
|
|
|
|
AlarmDetails string `gorm:"comment:告警详细内容" json:"alarm_details"`
|
|
|
|
|
|
TriggerTime time.Time `gorm:"not null;comment:告警触发时间" json:"trigger_time"`
|
|
|
|
|
|
|
2025-11-09 21:37:35 +08:00
|
|
|
|
// IsIgnored 字段加入到专为通知查询优化的复合索引中
|
|
|
|
|
|
IsIgnored bool `gorm:"default:false;index:idx_notification_query;comment:是否被手动忽略" json:"is_ignored"`
|
2025-11-07 22:19:55 +08:00
|
|
|
|
// IgnoredUntil 忽略截止时间。在此时间之前,即使告警持续,也不会发送通知。
|
|
|
|
|
|
// 使用指针类型 *time.Time 来表示可为空的时间。
|
|
|
|
|
|
IgnoredUntil *time.Time `gorm:"comment:忽略截止时间" json:"ignored_until"`
|
|
|
|
|
|
|
2025-11-09 21:37:35 +08:00
|
|
|
|
// LastNotifiedAt 字段加入到专为通知查询优化的复合索引中
|
|
|
|
|
|
LastNotifiedAt *time.Time `gorm:"index:idx_notification_query;comment:上次发送通知时间" json:"last_notified_at"`
|
2025-11-07 20:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TableName 指定 ActiveAlarm 结构体对应的数据库表名
|
|
|
|
|
|
func (ActiveAlarm) TableName() string {
|
|
|
|
|
|
return "active_alarms"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// HistoricalAlarm 历史告警
|
|
|
|
|
|
// 历史告警是不可变归档数据,我们移除 gorm.Model,并手动定义字段。
|
2025-11-07 22:19:55 +08:00
|
|
|
|
// ID 和 TriggerTime 共同构成联合主键,以满足 TimescaleDB 超表的要求。
|
2025-11-07 20:56:04 +08:00
|
|
|
|
type HistoricalAlarm struct {
|
|
|
|
|
|
// 手动定义主键,ID 仍然自增
|
2025-11-07 22:19:55 +08:00
|
|
|
|
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
|
|
|
|
|
|
|
|
|
|
|
|
SourceType AlarmSourceType `gorm:"type:varchar(50);not null;index;comment:告警来源类型" json:"source_type"`
|
|
|
|
|
|
// SourceID 告警来源ID,其具体含义取决于 SourceType 字段 (例如:设备ID, 区域主控ID, 猪栏ID)。
|
|
|
|
|
|
SourceID uint `gorm:"not null;index;comment:告警来源ID" json:"source_id"`
|
|
|
|
|
|
|
2025-11-09 21:37:35 +08:00
|
|
|
|
// AlarmCode 是一个机器可读的、标准化的告警类型标识。
|
|
|
|
|
|
AlarmCode AlarmCode `gorm:"type:varchar(100);not null;index;comment:告警代码" json:"alarm_code"`
|
|
|
|
|
|
|
2025-11-07 22:19:55 +08:00
|
|
|
|
AlarmSummary string `gorm:"comment:告警简述" json:"alarm_summary"`
|
|
|
|
|
|
Level SeverityLevel `gorm:"type:varchar(10);not null;comment:严重性等级" json:"level"`
|
|
|
|
|
|
AlarmDetails string `gorm:"comment:告警详细内容" json:"alarm_details"`
|
|
|
|
|
|
TriggerTime time.Time `gorm:"primaryKey;not null;comment:告警触发时间" json:"trigger_time"`
|
2025-11-07 21:39:24 +08:00
|
|
|
|
ResolveTime time.Time `gorm:"not null;comment:告警解决时间" json:"resolve_time"`
|
2025-11-07 22:19:55 +08:00
|
|
|
|
ResolveMethod string `gorm:"comment:告警解决方式" json:"resolve_method"`
|
|
|
|
|
|
|
|
|
|
|
|
// ResolvedBy 使用指针类型 *uint 来表示可为空解决人, 当字段为空时表示系统自动解决的
|
|
|
|
|
|
ResolvedBy *uint `gorm:"comment:告警解决人" json:"resolved_by"`
|
2025-11-07 20:56:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TableName 指定 HistoricalAlarm 结构体对应的数据库表名
|
|
|
|
|
|
func (HistoricalAlarm) TableName() string {
|
|
|
|
|
|
return "historical_alarms"
|
|
|
|
|
|
}
|