You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
4.8 KiB

package common_models
import (
"fmt"
"gitea.anxinyun.cn/container/common_models/constant/redisKey"
"log"
"time"
)
const (
Iota_Alarm_Status_Resolved = "resolved"
Iota_Alarm_Status_Firing = "firing"
Iota_Alarm_OutOfRange = "OutOfRange"
Iota_Alarm_LinkStatus = "LinkStatus"
Alarm_Mode_Generation = "AlarmGeneration"
//自动恢复
Alarm_Mode_AutoElimination = "AlarmAutoElimination"
// 告警类型
Alarm_Type_Dtu_LinkStatus = "2001" // DTU下线
Alarm_Type_Device_Status = "3001"
Alarm_Type_Timeout = "3003"
Alarm_Type_Data_Parse_Error = "3004"
Alarm_Type_OutRange = "3005"
Alarm_Type_Data_Interupt = "3006"
// 超阈值
Alarm_Type_Over_Threshold = "3007"
// 变化速率超阈值
Alarm_Type_Over_ChangeRate_Threshold = "3008"
Alarm_Type_OutRange_Legacy = "5001"
// 告警码
Alarm_Code_OutRange = "30050001"
Alarm_Code_OutRange_Iota = "5001"
Alarm_Code_OffLine = "20010001"
// 告警源类型:DTU
Alarm_Source_DTU = 0
// 告警源类型: 设备
Alarm_Source_Device = 1
// 告警源类型: 测点
Alarm_Source_Station = 2
// 发起者: et-recv
Alarm_Sponsor_Recv = "et.recv"
// 发起者 et-analyze
Alarm_Sponsor_Threshold = "et.analyze"
Alarm_Sponsor_Operator = "operator"
// 智慧应用
Alarm_Sponsor_SmartApp = "smart.app"
// Alarm 缓存相关常量
Alarm_Cache_Prefix = "alarm"
Alarm_Cache_Prefix_Agg = "AggThreshold"
ALARM_SOURCE_DEVICE = 1
ALARM_SOURCE_STATION = 2
)
type Alarm struct {
IsAlarm bool
Code string
AlarmType string
Level int
Content string
Sponsor string
}
// NewAlarmOverThreshold 超阈值告警
func NewAlarmOverThreshold(level int, content string) *Alarm {
codeMap := map[int]string{
1: "30070001",
2: "30070002",
3: "30070003",
4: "30070004",
}
code, ok := codeMap[level]
if !ok {
log.Printf("告警内容[%s] 告警等级[%d], 未找到对应的告警码 \n", content, level)
return nil
}
return &Alarm{
IsAlarm: true,
Code: code,
AlarmType: Alarm_Type_Over_Threshold,
Level: level,
Content: content,
Sponsor: Alarm_Sponsor_Threshold,
}
}
// NewAlarmRecoverThreshold 阈值恢复告警
func NewAlarmRecoverThreshold() *Alarm {
alarm := Alarm{
IsAlarm: false,
Code: "30070001",
AlarmType: Alarm_Type_Over_Threshold,
Level: 0,
Content: "",
Sponsor: Alarm_Sponsor_Threshold,
}
return &alarm
}
// NewOverChangingRateThreshold 变化速率超阈值告警
func NewOverChangingRateThreshold(level int, content string) *Alarm {
codeMap := map[int]string{
1: "30080001",
2: "30080002",
3: "30080003",
4: "30080004",
}
code, ok := codeMap[level]
if !ok {
log.Printf("告警内容[%s] 告警等级[%d], 未找到对应的告警码 \n", content, level)
return nil
}
alarm := Alarm{
IsAlarm: true,
Code: code,
AlarmType: Alarm_Type_Over_ChangeRate_Threshold,
Level: level,
Content: content,
Sponsor: Alarm_Sponsor_Threshold,
}
return &alarm
}
// NewRecoverChangingRateThreshold 变化速率阈值恢复告警
func NewRecoverChangingRateThreshold() *Alarm {
alarm := Alarm{
IsAlarm: false,
Code: "30080001",
AlarmType: Alarm_Type_Over_ChangeRate_Threshold,
Level: 0,
Content: "",
Sponsor: Alarm_Sponsor_Threshold,
}
return &alarm
}
// AlarmRedisKey 格式:alarm:sourceType:sourceId, 如:alarm:2:100
func AlarmRedisKey(sourceType int, sourceId string) string {
return fmt.Sprintf("%s:%d:%s", redisKey.Threshold, sourceType, sourceId)
}
// AlarmRedisKey_Agg 格式:AggThreshold:structId:factorId, 如:AggThreshold:1:106
func AlarmRedisKey_Agg(structId int, factorId int) string {
return fmt.Sprintf("%s:%d:%d", redisKey.Agg_threshold, structId, factorId)
}
// ToAlarmMsg 将测点信息 -> 告警信息
func (a *Alarm) ToAlarmMsg(stationInfo StationInfo, acqTime time.Time) AlarmMsg {
if a.IsAlarm {
return AlarmMsg{
MessageMode: Alarm_Mode_Generation,
StructureId: stationInfo.StructureId,
StructureName: stationInfo.StructureName,
SourceId: fmt.Sprintf("%d", stationInfo.Id),
SourceName: stationInfo.Name,
AlarmTypeCode: a.AlarmType,
AlarmCode: a.Code,
Content: a.Content,
AcqTime: acqTime,
SourceTypeId: Alarm_Source_Station,
Sponsor: a.Sponsor,
}
} else {
return AlarmMsg{
MessageMode: Alarm_Mode_AutoElimination,
StructureId: stationInfo.StructureId,
StructureName: stationInfo.StructureName,
SourceId: fmt.Sprintf("%d", stationInfo.Id),
SourceName: "",
AlarmTypeCode: a.AlarmType,
AlarmCode: "",
Content: "",
AcqTime: acqTime,
SourceTypeId: Alarm_Source_Station,
Sponsor: a.Sponsor,
}
}
}
type ThresholdAlarmDetail struct {
Level int
Content string
}
func (t ThresholdAlarmDetail) ToString() string {
return t.Content
}