|
@@ -1,15 +1,32 @@
|
|
|
package com.persagy.apm.energyalarmstarter.alarmengine.jms;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.persagy.apm.energyalarmstarter.alarmdata.model.vo.AlarmConfigItem;
|
|
|
import com.persagy.apm.energyalarmstarter.alarmdata.mq.model.DmpMessage;
|
|
|
+import com.persagy.apm.energyalarmstarter.alarmengine.constants.RedisConstants;
|
|
|
import com.persagy.apm.energyalarmstarter.alarmengine.feign.AlarmCondition;
|
|
|
+import com.persagy.apm.energyalarmstarter.alarmengine.feign.ObjConditionRel;
|
|
|
+import com.persagy.apm.energyalarmstarter.alarmengine.jms.model.ItemCodeCondition;
|
|
|
+import com.persagy.apm.energyalarmstarter.alarmengine.jms.model.ObjConditionInfo;
|
|
|
+import com.persagy.apm.energyalarmstarter.alarmengine.redis.RedisUtil4EngineStater;
|
|
|
import com.persagy.apm.energyalarmstarter.alarmengine.service.AlarmRecordMsgHandler;
|
|
|
+import io.micrometer.core.instrument.util.JsonUtils;
|
|
|
import io.netty.channel.ChannelHandler;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
/**
|
|
|
* @description: 报警引擎消息处理类
|
|
|
* @author: lixing
|
|
@@ -23,11 +40,14 @@ public class AlarmEngineMsgHandler {
|
|
|
@Autowired
|
|
|
private AlarmRecordMsgHandler alarmRecordMsgHandler;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil4EngineStater redisUtil;
|
|
|
+
|
|
|
@SneakyThrows
|
|
|
public String getAlarmCategory(DmpMessage dmpMessage) {
|
|
|
JSONObject data = dmpMessage.getExts();
|
|
|
String itemCode = data.getString("itemCode");
|
|
|
- return alarmRecordMsgHandler.getAlarmCategory(alarmRecordMsgHandler.getAlarmUrlParam(data), itemCode);
|
|
|
+ return alarmRecordMsgHandler.getAlarmCategory(alarmRecordMsgHandler.getAlarmUrlParam(dmpMessage), itemCode);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -87,7 +107,7 @@ public class AlarmEngineMsgHandler {
|
|
|
*/
|
|
|
public void syncNewCondition(DmpMessage msg) {
|
|
|
AlarmCondition alarmCondition = JSONObject.parseObject(msg.getStr1(), AlarmCondition.class);
|
|
|
- // TODO: 2021/11/16 更新redis
|
|
|
+ redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), alarmCondition);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -99,7 +119,7 @@ public class AlarmEngineMsgHandler {
|
|
|
*/
|
|
|
public void syncUpdatedCondition(DmpMessage msg) {
|
|
|
AlarmCondition alarmCondition = JSONObject.parseObject(msg.getStr1(), AlarmCondition.class);
|
|
|
- // TODO: 2021/11/16 更新redis
|
|
|
+ redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), alarmCondition);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -110,9 +130,8 @@ public class AlarmEngineMsgHandler {
|
|
|
* @version V1.0 2021/10/26 7:51 下午
|
|
|
*/
|
|
|
public void syncDeletedCondition(DmpMessage msg) {
|
|
|
- AlarmCondition alarmCondition = new AlarmCondition();
|
|
|
- alarmCondition.setId(msg.getStr1());
|
|
|
- // TODO: 2021/11/16 更新redis
|
|
|
+ String conditionId = msg.getStr1();
|
|
|
+ redisUtil.delete(RedisConstants.ALARM_CONDITIONS, conditionId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -123,8 +142,63 @@ public class AlarmEngineMsgHandler {
|
|
|
* @version V1.0 2021/10/26 7:51 下午
|
|
|
*/
|
|
|
public void syncNewObjConditionRelList(DmpMessage msg) {
|
|
|
- JSONArray createdRelList = JSONObject.parseArray(msg.getStr1());
|
|
|
- // TODO: 2021/11/16 更新redis
|
|
|
+ List<ObjConditionRel> createdRelList = JSONObject.parseArray(msg.getStr1(), ObjConditionRel.class);
|
|
|
+ syncCreatedObjConditionRelList2Redis(createdRelList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新的设备与报警条件关联关系同步至redis
|
|
|
+ *
|
|
|
+ * @param createdRelList 新增的关联关系
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/11/20 5:29 下午
|
|
|
+ */
|
|
|
+ private void syncCreatedObjConditionRelList2Redis(List<ObjConditionRel> createdRelList) {
|
|
|
+ if (CollectionUtils.isEmpty(createdRelList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (ObjConditionRel objConditionRel : createdRelList) {
|
|
|
+ // 报警对象id
|
|
|
+ String objId = objConditionRel.getObjId();
|
|
|
+ // 报警对象和报警条件的关联信息
|
|
|
+ ObjConditionInfo objConditionInfo = redisUtil.get(RedisConstants.OBJ_CONDITION_REL, objId, ObjConditionInfo.class);
|
|
|
+ if (objConditionInfo == null) {
|
|
|
+ objConditionInfo = new ObjConditionInfo(objConditionRel.getProjectId());
|
|
|
+ }
|
|
|
+ ItemCodeCondition condition = new ItemCodeCondition();
|
|
|
+ condition.setConditionId(objConditionRel.getConditionId());
|
|
|
+ condition.setItemCode(objConditionRel.getItemCode());
|
|
|
+ // 对象内部自己做去重操作
|
|
|
+ objConditionInfo.addCondition(condition);
|
|
|
+ redisUtil.put(RedisConstants.OBJ_CONDITION_REL, objId, objConditionInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除的设备与报警条件关联关系同步至redis
|
|
|
+ *
|
|
|
+ * @param deletedRelList 删除的关联关系
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/11/20 5:29 下午
|
|
|
+ */
|
|
|
+ private void syncDeletedObjConditionRelList2Redis(List<ObjConditionRel> deletedRelList) {
|
|
|
+ if (CollectionUtils.isEmpty(deletedRelList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (ObjConditionRel objConditionRel : deletedRelList) {
|
|
|
+ // 报警对象id
|
|
|
+ String objId = objConditionRel.getObjId();
|
|
|
+ ObjConditionInfo objConditionInfo = redisUtil.get(RedisConstants.OBJ_CONDITION_REL, objId, ObjConditionInfo.class);
|
|
|
+ ItemCodeCondition condition = new ItemCodeCondition();
|
|
|
+ condition.setConditionId(objConditionRel.getConditionId());
|
|
|
+ condition.setItemCode(objConditionRel.getItemCode());
|
|
|
+ objConditionInfo.removeCondition(condition);
|
|
|
+ if (CollectionUtils.isEmpty(objConditionInfo.getConditions())) {
|
|
|
+ redisUtil.delete(RedisConstants.OBJ_CONDITION_REL, objId);
|
|
|
+ } else {
|
|
|
+ redisUtil.put(RedisConstants.OBJ_CONDITION_REL, objId, objConditionInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -135,14 +209,104 @@ public class AlarmEngineMsgHandler {
|
|
|
* @version V1.0 2021/10/26 7:51 下午
|
|
|
*/
|
|
|
public void syncDeletedObjConditionRelList(DmpMessage msg) {
|
|
|
- JSONArray deletedRelList = JSONObject.parseArray(msg.getStr1());
|
|
|
- // TODO: 2021/11/16 更新redis
|
|
|
+ List<ObjConditionRel> deletedRelList = JSONObject.parseArray(msg.getStr1(), ObjConditionRel.class);
|
|
|
+ syncDeletedObjConditionRelList2Redis(deletedRelList);
|
|
|
}
|
|
|
|
|
|
public void syncUpdateObjConditionRelList(DmpMessage msg) {
|
|
|
- JSONArray deletedRelList = JSONObject.parseArray(msg.getStr1());
|
|
|
- JSONArray createdRelList = JSONObject.parseArray(msg.getStr2());
|
|
|
+ List<ObjConditionRel> deletedRelList = JSONObject.parseArray(msg.getStr1(), ObjConditionRel.class);
|
|
|
+ List<ObjConditionRel> createdRelList = JSONObject.parseArray(msg.getStr2(), ObjConditionRel.class);
|
|
|
// 先删除,再创建
|
|
|
- // TODO: 2021/11/16 更新redis
|
|
|
+ syncDeletedObjConditionRelList2Redis(deletedRelList);
|
|
|
+ syncCreatedObjConditionRelList2Redis(createdRelList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 增量同步报警定义
|
|
|
+ *
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/11/20 3:08 下午
|
|
|
+ */
|
|
|
+ @SneakyThrows
|
|
|
+ public void incrementSyncAlarmConfig(DmpMessage dmpMessage) {
|
|
|
+ Map<String, JSONArray> changedAlarmConfigs = alarmRecordMsgHandler.queryChangedAlarmConfigs(dmpMessage);
|
|
|
+ JSONArray createdConfigUniques = changedAlarmConfigs.get("createdConfigUniques");
|
|
|
+ JSONArray deletedConfigUniques = changedAlarmConfigs.get("deletedConfigUniques");
|
|
|
+ if (!CollectionUtils.isEmpty(deletedConfigUniques)) {
|
|
|
+ // 处理删除的报警定义 // TODO: 2021/11/20
|
|
|
+ // 报警定义中不包含报警条件的id
|
|
|
+ // 需要根据报警定义中的报警类型,从设备与报警条件的关联关系中找到对应的报警条件id。
|
|
|
+ // 删除报警条件
|
|
|
+ // 删除关联关系
|
|
|
+ List<AlarmConfigItem> alarmConfigItems = deletedConfigUniques.toJavaList(AlarmConfigItem.class);
|
|
|
+ for (AlarmConfigItem alarmConfigItem : alarmConfigItems) {
|
|
|
+ //从redis拿到设备的报警条件信息
|
|
|
+ ObjConditionInfo objConditionInfo = redisUtil.get(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId(), ObjConditionInfo.class);
|
|
|
+ LinkedList<ItemCodeCondition> conditions = objConditionInfo.getConditions();
|
|
|
+ LinkedList<ItemCodeCondition> conditionsRemain = objConditionInfo.getConditions();
|
|
|
+ for (ItemCodeCondition condition : conditions) {
|
|
|
+ if (alarmConfigItem.getItemCode().equals(condition.getItemCode())) {
|
|
|
+ //删除报警条件
|
|
|
+ redisUtil.delete(RedisConstants.ALARM_CONDITIONS, condition.getConditionId());
|
|
|
+ } else {
|
|
|
+ conditionsRemain.add(condition);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (CollectionUtil.isEmpty(conditionsRemain)) {
|
|
|
+ redisUtil.delete(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId());
|
|
|
+ } else {
|
|
|
+ redisUtil.put(RedisConstants.OBJ_CONDITION_REL, alarmConfigItem.getObjId(), conditionsRemain);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(createdConfigUniques)) {
|
|
|
+ List<AlarmConfigItem> alarmConfigItems = createdConfigUniques.toJavaList(AlarmConfigItem.class);
|
|
|
+ List<ObjConditionRel> createdRelList = new ArrayList<>();
|
|
|
+ for (AlarmConfigItem alarmConfigItem : alarmConfigItems) {
|
|
|
+ // 数据中台报警条件转化为业务中台报警条件和设备与条件的关联关系
|
|
|
+ AlarmCondition alarmCondition = avgAlarmDmpCondition(alarmConfigItem);
|
|
|
+ if (alarmCondition == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 存储报警条件
|
|
|
+ redisUtil.put(RedisConstants.ALARM_CONDITIONS, alarmCondition.getId(), alarmCondition);
|
|
|
+ // 存储设备与条件的关联关系
|
|
|
+ ObjConditionRel objConditionRel = new ObjConditionRel();
|
|
|
+ objConditionRel.setConditionId(alarmCondition.getId());
|
|
|
+ objConditionRel.setObjId(alarmConfigItem.getObjId());
|
|
|
+ objConditionRel.setItemCode(alarmConfigItem.getItemCode());
|
|
|
+ objConditionRel.setProjectId(alarmConfigItem.getProjectId());
|
|
|
+ createdRelList.add(objConditionRel);
|
|
|
+ }
|
|
|
+ syncCreatedObjConditionRelList2Redis(createdRelList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private AlarmCondition avgAlarmDmpCondition(AlarmConfigItem alarmConfigItem){
|
|
|
+ AlarmCondition alarmCondition = new AlarmCondition();
|
|
|
+ alarmCondition.setId(alarmConfigItem.getId());
|
|
|
+ List<String> infoCodes = new ArrayList<>();
|
|
|
+ if (alarmConfigItem.getCondition() == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ for (String infoCode : alarmConfigItem.getCondition().getInfoCode()) {
|
|
|
+ infoCodes.add(infoCode.replaceAll(alarmConfigItem.getObjId()+"_",""));
|
|
|
+ }
|
|
|
+ alarmCondition.setInfoCodes(infoCodes);
|
|
|
+ alarmCondition.setConfigs(alarmConfigItem.getCondition().getConfigs());
|
|
|
+ alarmCondition.setTriggerBackend(alarmConfigItem.getCondition().getTrigger().replaceAll(alarmConfigItem.getObjId()+"_",""));
|
|
|
+ alarmCondition.setTriggerUphold(alarmConfigItem.getCondition().getTriggerUphold()/60);
|
|
|
+ alarmCondition.setAlarmLevel(alarmConfigItem.getLevel());
|
|
|
+ alarmCondition.setEndUphold(alarmConfigItem.getCondition().getEndUphold()/60);
|
|
|
+ //alarmCondition.setTriggerRules(alarmDmpCondition.getRules());
|
|
|
+ alarmCondition.setEndBackend(alarmConfigItem.getCondition().getEnd().replaceAll(alarmConfigItem.getObjId()+"_",""));
|
|
|
+ // alarmCondition.setEndRules(null);
|
|
|
+ String effectTime = alarmConfigItem.getCondition().getEffectTime();
|
|
|
+ if (StringUtils.isNotBlank(effectTime)) {
|
|
|
+ JSONObject effectTimeObj = JSONObject.parseObject(effectTime);
|
|
|
+ alarmCondition.setEffectTimeType(effectTimeObj.getString("type"));
|
|
|
+ alarmCondition.setEffectTimeDetail(effectTime);
|
|
|
+ }
|
|
|
+ return alarmCondition;
|
|
|
}
|
|
|
}
|