From 9028d0694549df46adf4cca71de823b5b35fc4c8 Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期三, 19 三月 2025 11:47:20 +0800 Subject: [PATCH] 调整速率有效期内,跳过执行 --- shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java | 44 +++++++++++++++------ shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java | 29 ++++++++++---- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java index cfb4427..9424085 100644 --- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java @@ -1,5 +1,6 @@ package com.iailab.module.shasteel.mq.consumer; +import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.iailab.framework.common.util.date.DateUtils; import com.iailab.module.model.api.mcs.McsApi; @@ -45,13 +46,16 @@ // 最早调度时间 private static Date earliestScheduleTime = null; + private static Date lastScheduleTime = null; + /** - * 监听报警信息,执行调度 + * 监听报警信息,执行煤气调度 * * @param message */ @RabbitListener(queues = QueueModelAlarmConfig.QUEUE_NAME) public void listen(Message message) { + log.info("监听报警信息,执行煤气调度"); String routingKey = message.getMessageProperties().getReceivedRoutingKey(); log.info("routingKey:" + routingKey); String messageBody = new String(message.getBody()); @@ -65,29 +69,41 @@ log.info("过滤旧消息"); return; } - - // 判断predictTime 是否大于outTime,否则不执行调度 - if (earliestScheduleTime != null && predictTime.compareTo(earliestScheduleTime) < 0) { + if (!ModelPredictFinishConsumer.finishModuleMap.containsKey(predictTime.getTime())) { + log.info("finishModuleMap not containsKey:" + predictTime.getTime()); return; } + List<String> finishModuleList = ModelPredictFinishConsumer.finishModuleMap.get(predictTime.getTime()); + if (CollectionUtils.isEmpty(finishModuleList)) { + log.info("finishModuleList is empty"); + return; + } + if (!finishModuleList.contains(CommonConstant.NET_BFG) || !finishModuleList.contains(CommonConstant.NET_COG) || + !finishModuleList.contains(CommonConstant.NET_LDG)) { + log.info("finishModuleList:" + JSONArray.toJSONString(finishModuleList)); + log.info("等待全部预测完成"); + return; + } + log.info("已全部预测完成,清空finishModuleMap"); + ModelPredictFinishConsumer.finishModuleMap.clear(); - List<AlarmMessageRespDTO> alarmList = JSONObject.parseArray(messageJson.get("alarmList").toString(),AlarmMessageRespDTO.class); - // 取出最小outTime - Date minOutTime = alarmList.stream().filter(e -> e.getOutTime() != null).map(AlarmMessageRespDTO::getOutTime).min(Date::compareTo).get(); - earliestScheduleTime = minOutTime; - - - // 查找需要执行的调度方案 + log.info("查找需要执行的调度方案"); List<StScheduleSchemeDTO> scheduleSchemeList = mcsApi.listScheduleScheme(ScheduleTriggerMethodEnum.EVENT.getCode(), routingKey); if (CollectionUtils.isEmpty(scheduleSchemeList)) { log.info("routingKey:" + routingKey + ",调度方案为空。"); return; } for (StScheduleSchemeDTO stScheduleSchemeDTO : scheduleSchemeList) { - String runKey = "GasSchedule_" + stScheduleSchemeDTO.getCode() + "_" + predictTime.getTime(); + if (lastScheduleTime != null && stScheduleSchemeDTO.getAdjustRate() != null && stScheduleSchemeDTO.getAdjustRate() > 0 && + (predictTime.getTime() - lastScheduleTime.getTime() > 1000 * 60 * stScheduleSchemeDTO.getAdjustRate().longValue()) ) { + log.info("调整速率有效期内,跳过执行"); + continue; + } + String runKey = "GasSchedule_" + stScheduleSchemeDTO.getCode() + "_" + predictTime.getTime(); log.info("runKey:" + runKey); log.info("lastRunGasSchedule:" + lastRunGasSchedule); if (lastRunGasSchedule.equals(runKey)) { + log.info("lastRunGasSchedule:" + lastRunGasSchedule + "已执行完成"); continue; } lastRunGasSchedule = runKey; @@ -128,7 +144,7 @@ Boolean simAdjustFlag = mdkApi.predictSimAdjust(simAdjustReqDTO); if (simAdjustFlag) { log.info("模拟调整成功,ScheduleCode:" + CommonConstant.GAS_MODEL_CODE); - }else { + } else { log.error("模拟调整失败,simAdjustReqDTO:" + simAdjustReqDTO); } @@ -218,6 +234,8 @@ break; } } + + lastScheduleTime = predictTime; } private void saveScheduleSuggest(String title, Object content, String scheduleObj, Date scheduleTime) { diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java index 422f03d..c6c647b 100644 --- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java @@ -20,6 +20,7 @@ import javax.annotation.Resource; import java.math.BigDecimal; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; /** @@ -40,9 +41,9 @@ @Resource private RabbitTemplate rabbitTemplate; - private static String lastRunAlarm = ""; + private static Map<String, Long> lastRunTime = new ConcurrentHashMap<>(); - private static Map<String, Long> lastRunTime = new HashMap<>(); + public static Map<Long, List<String>> finishModuleMap = new ConcurrentHashMap<>(); /** * 监听预测完成,产生预警消息 @@ -62,8 +63,15 @@ } // 预测时间 Date predictTime = DateUtils.parse(messageJson.get("predictTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND); - // 预测模块/预测管网 + // 预测管网 String moduleType = messageJson.get("moduleType").toString(); + if (!finishModuleMap.containsKey(predictTime.getTime())) { + List<String> mList = new ArrayList<>(); + finishModuleMap.put(predictTime.getTime(), mList); + } + finishModuleMap.get(predictTime.getTime()).add(moduleType); + log.info("finishModuleMap:" + JSONObject.toJSONString(finishModuleMap)); + if (moduleType.equals(CommonConstant.NET_BFG) || moduleType.equals(CommonConstant.NET_COG) || moduleType.equals(CommonConstant.NET_LDG) || moduleType.equals(CommonConstant.NET_LDG1) || @@ -186,13 +194,16 @@ } } - if (!CollectionUtils.isEmpty(alarmList)) { - log.info("发送预警消息"); - Map<String, Object> msg = new HashMap<>(2); - msg.put("predictTime", DateUtils.format(predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); - msg.put("alarmList", alarmList); - rabbitTemplate.convertAndSend(RoutingConstant.EXCHANGE, RoutingConstant.Iailab_Model_Alarm, msg); + if (CollectionUtils.isEmpty(alarmList)) { + log.info("alarmList is empty"); + return; } + log.info("发送预警消息"); + Map<String, Object> msg = new HashMap<>(2); + msg.put("predictTime", DateUtils.format(predictTime, DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); + msg.put("alarmList", alarmList); + msg.put("moduleType", moduleType); + rabbitTemplate.convertAndSend(RoutingConstant.EXCHANGE, RoutingConstant.Iailab_Model_Alarm, msg); } } catch (Exception e) { e.printStackTrace(); -- Gitblit v1.9.3