|
|
@ -1,13 +1,13 @@ |
|
|
|
package consumers |
|
|
|
|
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"fmt" |
|
|
|
"goInOut/adaptors" |
|
|
|
"goInOut/consumers/AlarmCombination" |
|
|
|
"goInOut/dbOperate" |
|
|
|
"goInOut/dbOperate/_kafka" |
|
|
|
"goInOut/monitors" |
|
|
|
"goInOut/utils" |
|
|
|
"gopkg.in/yaml.v3" |
|
|
|
"log" |
|
|
|
"time" |
|
|
@ -23,6 +23,8 @@ type consumerAlarmCombination struct { |
|
|
|
monitor *monitors.CommonMonitor |
|
|
|
infoRedis *dbOperate.RedisHelper |
|
|
|
infoPg *dbOperate.DBHelper |
|
|
|
//数据库配置信息
|
|
|
|
combinationInfo []AlarmCombination.CombinationInfo |
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerAlarmCombination) LoadConfigJson(cfgStr string) { |
|
|
@ -58,10 +60,8 @@ func (the *consumerAlarmCombination) InputInitial() error { |
|
|
|
the.monitor.Start() |
|
|
|
for taskName, cron := range the.Info.Monitor { |
|
|
|
switch taskName { |
|
|
|
case "cron_pg": |
|
|
|
case "cron": |
|
|
|
the.monitor.RegisterTask(cron, the.updateCombinationInfo) |
|
|
|
case "cron_redis": |
|
|
|
the.monitor.RegisterTask(cron, the.getEs1HourAggData) |
|
|
|
default: |
|
|
|
log.Printf("定时任务[%s],cron=[%s] 无匹配", taskName, cron) |
|
|
|
} |
|
|
@ -79,8 +79,6 @@ func (the *consumerAlarmCombination) OutputInitial() error { |
|
|
|
|
|
|
|
func (the *consumerAlarmCombination) infoComponentInitial() error { |
|
|
|
//数据出口
|
|
|
|
addr := the.Info.QueryComponent.Redis.Address |
|
|
|
the.infoRedis = dbOperate.NewRedisHelper("", addr) |
|
|
|
pgConnStr := the.Info.QueryComponent.Pg.Connect |
|
|
|
the.infoPg = dbOperate.NewDBHelper("postgres", pgConnStr) |
|
|
|
return nil |
|
|
@ -111,45 +109,71 @@ func (the *consumerAlarmCombination) Work() { |
|
|
|
}() |
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerAlarmCombination) getAdaptor() (adaptor adaptors.Adaptor_AXYES_HBGL) { |
|
|
|
func (the *consumerAlarmCombination) getAdaptor() (adaptor adaptors.Adaptor_ZWY_AlarmCombin) { |
|
|
|
|
|
|
|
return adaptors.Adaptor_AXYES_HBGL{ |
|
|
|
Redis: the.infoRedis, |
|
|
|
} |
|
|
|
return adaptors.Adaptor_ZWY_AlarmCombin{} |
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerAlarmCombination) getEs1HourAggData() { |
|
|
|
start, end := utils.GetTimeRangeByHour(-1) |
|
|
|
log.Printf("查询数据时间范围 %s - %s", start, end) |
|
|
|
hourFactorIds := []int{15, 18, 20} |
|
|
|
structIds := []int64{1, 2} |
|
|
|
for _, structId := range structIds { |
|
|
|
for _, factorId := range hourFactorIds { |
|
|
|
esQuery := the.getESQueryStrByHour(structId, factorId, start, end) |
|
|
|
auth := map[string]string{"Authorization": "Bear 85a441d4-022b-4613-abba-aaa8e2693bf7"} |
|
|
|
esAggResultStr := the.InHttp.HttpGetWithHeader(esQuery, auth) |
|
|
|
func (the *consumerAlarmCombination) getEs1minAlarmData() { |
|
|
|
adaptor := the.getAdaptor() |
|
|
|
|
|
|
|
adaptor := the.getAdaptor() |
|
|
|
needPushes := adaptor.Transform(structId, factorId, esAggResultStr) |
|
|
|
for _, comInfos := range the.combinationInfo { |
|
|
|
//满足条件 sourceTypeId: Int = 1, // 0:DTU, 1:传感器, 2:测点
|
|
|
|
//state 0 新告警 1次数更新 2 等级提升 3 自动恢复 4 手动恢复 5 已恢复待确认
|
|
|
|
|
|
|
|
if len(needPushes) > 0 { |
|
|
|
the.ch <- needPushes |
|
|
|
} |
|
|
|
esQuery := the.getESAlarmAggQueryStr(comInfos) |
|
|
|
auth := map[string]string{"Authorization": "Bear 85a441d4-022b-4613-abba-aaa8e2693bf7"} |
|
|
|
esAggResultStr := the.InHttp.HttpGetWithHeader(esQuery, auth) |
|
|
|
lenRes := len(esAggResultStr) |
|
|
|
if lenRes < 120 { |
|
|
|
log.Printf("[s=%d]es agg 返回 无 有效数据,resp=%s ", comInfos.StructId, esAggResultStr) |
|
|
|
continue |
|
|
|
} |
|
|
|
needPushes := adaptor.Transform(comInfos, esAggResultStr) |
|
|
|
|
|
|
|
if len(needPushes) > 0 { |
|
|
|
the.ch <- needPushes |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerAlarmCombination) updateCombinationInfo() { |
|
|
|
log.Printf("更新 数据库 组合配置信息") |
|
|
|
sql := `SELECT * FROM "t_alarm_combination"` |
|
|
|
var CombinationInfos []AlarmCombination.CombinationInfo |
|
|
|
the.infoPg.Query(&CombinationInfos, sql) |
|
|
|
println("======") |
|
|
|
sql := `SELECT a.*,s."name" as struct_name FROM "t_alarm_combination" as a |
|
|
|
left join t_structure as s |
|
|
|
ON a.struct_id=s."id"` |
|
|
|
sql += " where a.id=1" //测试用
|
|
|
|
//var CombinationInfos []AlarmCombination.CombinationInfo
|
|
|
|
err := the.infoPg.Query(&the.combinationInfo, sql) |
|
|
|
if err != nil { |
|
|
|
log.Printf("查询数据库异常:err-> %s", err.Error()) |
|
|
|
return |
|
|
|
} |
|
|
|
for i, info := range the.combinationInfo { |
|
|
|
err := json.Unmarshal([]byte(info.ConfigStr), &info.ConfigItems) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
the.combinationInfo[i].ConfigItems = info.ConfigItems |
|
|
|
} |
|
|
|
log.Printf("共刷新%d条配置", len(the.combinationInfo)) |
|
|
|
|
|
|
|
//立即触发
|
|
|
|
the.getEs1minAlarmData() |
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerAlarmCombination) getESQueryStrByHour(structureId int64, factorId int, start, end string) string { |
|
|
|
aggSubSql := utils.GetEsAggSubSqlByAxyFactorId(factorId) |
|
|
|
func (the *consumerAlarmCombination) getESAlarmAggQueryStr(onceConfig AlarmCombination.CombinationInfo) string { |
|
|
|
structureId := onceConfig.StructId |
|
|
|
var allStationId []int |
|
|
|
for _, item := range onceConfig.ConfigItems { |
|
|
|
if len(item.StationIds) > 0 { |
|
|
|
allStationId = append(allStationId, item.StationIds...) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
allStationIdStr, _ := json.Marshal(allStationId) |
|
|
|
esQuery := fmt.Sprintf(` |
|
|
|
{ |
|
|
|
"size": 0, |
|
|
@ -158,106 +182,47 @@ func (the *consumerAlarmCombination) getESQueryStrByHour(structureId int64, fact |
|
|
|
"must": [ |
|
|
|
{ |
|
|
|
"term": { |
|
|
|
"structure": { |
|
|
|
"value": %d |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"term": { |
|
|
|
"factor": { |
|
|
|
"structure_id": { |
|
|
|
"value": %d |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"range": { |
|
|
|
"collect_time": { |
|
|
|
"gte": "%s", |
|
|
|
"lt": "%s" |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
} |
|
|
|
}, |
|
|
|
"aggs": { |
|
|
|
"groupSensor": { |
|
|
|
"terms": { |
|
|
|
"field": "sensor" |
|
|
|
}, |
|
|
|
"aggs": { |
|
|
|
"groupDate": { |
|
|
|
"date_histogram": { |
|
|
|
"field": "collect_time", |
|
|
|
"interval": "1h", |
|
|
|
"time_zone": "Asia/Shanghai", |
|
|
|
"min_doc_count": 1 |
|
|
|
}, |
|
|
|
"aggs": %s |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
`, structureId, factorId, start, end, aggSubSql) |
|
|
|
|
|
|
|
return esQuery |
|
|
|
} |
|
|
|
|
|
|
|
func (the *consumerAlarmCombination) getESQueryStrBy10min(structureId int64, factorId int, start, end string) string { |
|
|
|
aggSubSql := utils.GetEsAggSubSqlByAxyFactorId(factorId) |
|
|
|
esQuery := fmt.Sprintf(` |
|
|
|
{ |
|
|
|
"size": 0, |
|
|
|
"query": { |
|
|
|
"bool": { |
|
|
|
"must": [ |
|
|
|
{ |
|
|
|
"term": { |
|
|
|
"structure": { |
|
|
|
"value": %d |
|
|
|
"source_type_id": { |
|
|
|
"value": 2 |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"term": { |
|
|
|
"factor": { |
|
|
|
"value": %d |
|
|
|
} |
|
|
|
"terms": { |
|
|
|
"state": [ |
|
|
|
0, |
|
|
|
1, |
|
|
|
2 |
|
|
|
] |
|
|
|
} |
|
|
|
}, |
|
|
|
{ |
|
|
|
"range": { |
|
|
|
"collect_time": { |
|
|
|
"gte": "%s", |
|
|
|
"lte": "%s" |
|
|
|
} |
|
|
|
"terms": { |
|
|
|
"source_id": %s |
|
|
|
} |
|
|
|
} |
|
|
|
] |
|
|
|
} |
|
|
|
}, |
|
|
|
"aggs": { |
|
|
|
"groupSensor": { |
|
|
|
"groupBySensor": { |
|
|
|
"terms": { |
|
|
|
"field": "sensor" |
|
|
|
}, |
|
|
|
"aggs": { |
|
|
|
"groupDate": { |
|
|
|
"date_histogram": { |
|
|
|
"field": "collect_time", |
|
|
|
"interval": "10m", |
|
|
|
"time_zone": "Asia/Shanghai", |
|
|
|
"min_doc_count": 1 |
|
|
|
}, |
|
|
|
"aggs": %s |
|
|
|
"field": "source_id", |
|
|
|
"order": { |
|
|
|
"_count": "DESC" |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
`, structureId, factorId, start, end, aggSubSql) |
|
|
|
`, structureId, allStationIdStr) |
|
|
|
|
|
|
|
return esQuery |
|
|
|
} |
|
|
|