feat: nbi
This commit is contained in:
78
features/fm/relation.go
Normal file
78
features/fm/relation.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package fm
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"be.ems/lib/config"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func JudgeDerived(alarm *Alarm, session *xorm.Session) (parentId string, found bool) {
|
||||
for _, rule := range config.RelationRules.Derived {
|
||||
if alarm.AlarmCode == rule.ChildCode {
|
||||
// 查询是否有 parent_code 的告警
|
||||
var parent Alarm
|
||||
has, _ := session.Table("alarm").
|
||||
Where("alarm_code=? AND ne_id=? AND alarm_status=1", rule.ParentCode, alarm.NeId).
|
||||
Get(&parent)
|
||||
if has {
|
||||
return parent.AlarmId, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func JudgeRelated(alarm *Alarm, session *xorm.Session, rules []config.RelatedRule) (relatedId string, found bool) {
|
||||
for _, rule := range rules {
|
||||
// 1. 检查 ne_type 是否匹配
|
||||
if rule.NeType != "" && alarm.NeType != rule.NeType {
|
||||
continue
|
||||
}
|
||||
// 2. 检查 code 是否在 codes 列表
|
||||
codeMatch := false
|
||||
for _, c := range rule.Codes {
|
||||
if alarm.AlarmCode == c {
|
||||
codeMatch = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !codeMatch {
|
||||
continue
|
||||
}
|
||||
// 3. 查询同一ne_id、codes列表内、时间窗口内的其他告警
|
||||
var brother Alarm
|
||||
parsedTime, err := time.Parse(time.RFC3339, alarm.EventTime)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
timeStart := parsedTime.Add(-time.Duration(rule.TimeWindow) * time.Second)
|
||||
has, _ := session.Table("alarm").
|
||||
Where("ne_id=? AND alarm_code IN (?) AND event_time BETWEEN ? AND ? AND alarm_id!=?",
|
||||
alarm.NeId, rule.Codes, timeStart, alarm.EventTime, alarm.AlarmId).
|
||||
Get(&brother)
|
||||
if has {
|
||||
return brother.AlarmId, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
type AlarmRelation struct {
|
||||
Id int64 `xorm:"pk autoincr 'id'"`
|
||||
AlarmId string `xorm:"alarm_id"`
|
||||
RelatedAlarmId string `xorm:"related_alarm_id"`
|
||||
RelationType string `xorm:"relation_type"`
|
||||
CreateAt time.Time `xorm:"create_at"`
|
||||
}
|
||||
|
||||
func InsertAlarmRelation(session *xorm.Session, alarmId, relatedId, relationType string) (int64, error) {
|
||||
relation := &AlarmRelation{
|
||||
AlarmId: alarmId,
|
||||
RelatedAlarmId: relatedId,
|
||||
RelationType: relationType,
|
||||
CreateAt: time.Now(),
|
||||
}
|
||||
_, err := session.Insert(relation)
|
||||
return relation.Id, err // 返回插入的ID
|
||||
}
|
||||
Reference in New Issue
Block a user