数据 输入输出 处理
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.

105 lines
2.7 KiB

package adaptors
import (
"encoding/json"
"fmt"
"slices"
"goInOut/consumers/AlarmCombination"
"goInOut/models"
"log"
"strconv"
"strings"
"time"
)
// Adaptor_ZWY_AlarmCombin 知物云的告警数据 组合统计 触发后 发到 kafka
type Adaptor_ZWY_AlarmCombin struct {
//一些必要信息
Info map[string]string
}
func (the Adaptor_ZWY_AlarmCombin) Transform(config AlarmCombination.CombinationInfo, rawMsg string) []NeedPush {
//20250704苏玮确认 小条件有一个告警就算有效,至少2个小条件告警, 触发大告警
log.Printf("解析数据")
esAggData := AlarmCombination.EsAggAlarm{}
esAggData.Aggregations.GroupBySensor.Buckets = []AlarmCombination.BucketsSensorDataCount{}
var needPush []NeedPush
err := json.Unmarshal([]byte(rawMsg), &esAggData)
if err != nil {
log.Printf("解析 es proxy 数据异常: %s", err.Error())
return nil
}
esAggPointsCount := len(esAggData.Aggregations.GroupBySensor.Buckets)
detailMsg := ""
itemAlarmCounts := 0 // >=2有效
for _, esBucket := range esAggData.Aggregations.GroupBySensor.Buckets {
stationId, _ := strconv.Atoi(esBucket.Key)
for _, item := range config.ConfigItems {
found := slices.Contains(item.StationIds, stationId)
if found {
itemAlarmCounts += 1
if len(detailMsg) > 0 {
detailMsg += ","
}
detailMsg += fmt.Sprintf("测点[%d]触发[f=%d]有效告警组", stationId, item.FactorId)
log.Printf("")
break
}
}
}
if itemAlarmCounts < 2 {
log.Printf("es 聚集查询告警组数=%d < 2", esAggPointsCount)
return nil
}
msg := fmt.Sprintf("组合告警[%s]生效,有效触发的前2组:%s", config.Name, detailMsg)
log.Printf(msg)
prefix := "zh-"
sourceId := prefix + strconv.Itoa(config.Id)
alarmMsg := models.KafkaAlarm{
MessageMode: "AlarmGeneration",
StructureId: config.StructId,
StructureName: config.StructName,
SourceId: sourceId,
SourceName: config.Name,
AlarmTypeCode: "8003",
AlarmCode: "80030001",
Content: msg,
Time: time.Now().Format("2006-01-02T15:04:05+0800"),
SourceTypeId: 4, // 0:DTU, 1:传感器, 2:测点
Sponsor: "goInOut_zhgj",
Extras: nil,
SubDevices: nil,
}
Payload, _ := json.Marshal(alarmMsg)
needPush = append(needPush, NeedPush{
Payload: Payload,
})
return needPush
}
func (the Adaptor_ZWY_AlarmCombin) GetPointCodeFromLabel(label string) int64 {
//解析label {13010600001}
pointUniqueCode := int64(0)
if len(label) > 2 {
newLabel := strings.TrimLeft(label, "{")
str := strings.TrimRight(newLabel, "}")
codeInt64, err := strconv.ParseInt(str, 10, 64)
if err != nil {
log.Printf("测点标签转换异常[%s]", label)
}
pointUniqueCode = codeInt64
}
return pointUniqueCode
}