From 2cf8d90d40907dee181feadf169d3a1fb850e55a Mon Sep 17 00:00:00 2001 From: huangfeng Date: Fri, 10 May 2024 11:35:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=A7=84=E5=88=99?= =?UTF-8?q?=E7=BC=93=E5=AD=98=EF=BC=8C=E5=88=86=E7=A6=BB=E8=A7=84=E5=88=99?= =?UTF-8?q?=E5=88=B7=E6=96=B0=E5=92=8C=E8=BF=90=E4=BD=9C=E8=A7=A6=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/xydl/cac/entity/WarnRule.java | 34 ++++++++++++++++++- .../cac/service/impl/WarnRuleServiceImpl.java | 3 ++ .../java/com/xydl/cac/task/CacheTask.java | 26 ++++++++++++++ .../java/com/xydl/cac/task/RuleCheckTask.java | 28 +++------------ 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/xydl/cac/entity/WarnRule.java b/src/main/java/com/xydl/cac/entity/WarnRule.java index 8bdb129..cdd10a7 100644 --- a/src/main/java/com/xydl/cac/entity/WarnRule.java +++ b/src/main/java/com/xydl/cac/entity/WarnRule.java @@ -2,12 +2,16 @@ package com.xydl.cac.entity; import com.fasterxml.jackson.annotation.JsonInclude; import com.xydl.cac.comparator.Comparator; +import com.xydl.cac.comparator.FloatCompare; +import com.xydl.cac.comparator.IntCompare; +import com.xydl.cac.entity.constants.Constants; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import javax.persistence.*; import javax.validation.constraints.NotBlank; @@ -22,6 +26,7 @@ import java.util.Date; @Entity @Table(name = "warn_rule") @ApiModel("告警规则表") +@Slf4j public class WarnRule { @Id @@ -73,5 +78,32 @@ public class WarnRule { @Transient private ModevTypePoint typePoint; @Transient - private Comparator comparat; + private Comparator actualComp; + + public void initComparator(String comparator, String operator) { + if (actualComp != null && this.comparator.equals(comparator)) { + this.operator = operator; + actualComp.setOperator(operator); + } else { + this.comparator = comparator; + this.operator = operator; + if (Constants.FloatCompare.equalsIgnoreCase(comparator)) { + actualComp = new FloatCompare(); + actualComp.setOperator(operator); + } else if (Constants.IntCompare.equalsIgnoreCase(comparator)) { + actualComp = new IntCompare(); + actualComp.setOperator(operator); + } else { + actualComp = null; + log.error("该规则(" + id + ")比较器(" + comparator + ")异常"); + } + } + } + + public boolean canDo() { + if (active != null && active.intValue() == 1 && actualComp != null) { + return true; + } + return false; + } } \ No newline at end of file diff --git a/src/main/java/com/xydl/cac/service/impl/WarnRuleServiceImpl.java b/src/main/java/com/xydl/cac/service/impl/WarnRuleServiceImpl.java index d14e55a..306c6b8 100644 --- a/src/main/java/com/xydl/cac/service/impl/WarnRuleServiceImpl.java +++ b/src/main/java/com/xydl/cac/service/impl/WarnRuleServiceImpl.java @@ -9,6 +9,7 @@ import com.xydl.cac.repository.WarnRuleRepository; import com.xydl.cac.service.DataService; import com.xydl.cac.service.NSensorService; import com.xydl.cac.service.WarnRuleService; +import com.xydl.cac.task.CacheTask; import com.xydl.cac.util.DateUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -79,10 +80,12 @@ public class WarnRuleServiceImpl implements WarnRuleService { rule.setLevel(item.getLevel()); rule.setActive(item.getActive()); repository.save(rule); + CacheTask.rule_Cache.remove(rule.getId()); } @Override public void delete(Integer id) throws Exception { repository.deleteById(id); + CacheTask.rule_Cache.remove(id); } } diff --git a/src/main/java/com/xydl/cac/task/CacheTask.java b/src/main/java/com/xydl/cac/task/CacheTask.java index 3a3d7ec..5c0d5c4 100644 --- a/src/main/java/com/xydl/cac/task/CacheTask.java +++ b/src/main/java/com/xydl/cac/task/CacheTask.java @@ -2,22 +2,30 @@ package com.xydl.cac.task; import com.xydl.cac.entity.Jg; import com.xydl.cac.entity.ModevType; +import com.xydl.cac.entity.WarnRule; import com.xydl.cac.entity.Zsb; +import com.xydl.cac.service.WarnRuleService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; @Service @Slf4j public class CacheTask { + @Resource + WarnRuleService ruleService; + public static HashMap unit_Cache = new HashMap<>(); public static List modevType_Cache = null; public static List jg_Cache = null; public static List zsb_Cache = null; + public static LinkedHashMap rule_Cache = new LinkedHashMap<>(); @Scheduled(cron = "0 1 * * * ?") private void clearCache() { @@ -25,5 +33,23 @@ public class CacheTask { modevType_Cache = null; jg_Cache = null; zsb_Cache = null; + rule_Cache.clear(); + } + + + @Scheduled(initialDelay = 60000, fixedDelay = 60000) + private void refreshRule() { + try { + List list = ruleService.listAll(); + for (WarnRule item : list) { + WarnRule rule = rule_Cache.get(item.getId()); + if (rule == null) { + item.initComparator(item.getComparator(), item.getOperator()); + rule_Cache.put(item.getId(), item); + } + } + } catch (Exception e) { + log.error("CacheTask.refreshRule error.", e); + } } } diff --git a/src/main/java/com/xydl/cac/task/RuleCheckTask.java b/src/main/java/com/xydl/cac/task/RuleCheckTask.java index 2c75832..e0b861d 100644 --- a/src/main/java/com/xydl/cac/task/RuleCheckTask.java +++ b/src/main/java/com/xydl/cac/task/RuleCheckTask.java @@ -1,16 +1,12 @@ package com.xydl.cac.task; -import com.xydl.cac.comparator.FloatCompare; -import com.xydl.cac.comparator.IntCompare; import com.xydl.cac.entity.ModevTypePoint; import com.xydl.cac.entity.NSensor; import com.xydl.cac.entity.WarnRule; import com.xydl.cac.entity.Warning; -import com.xydl.cac.entity.constants.Constants; import com.xydl.cac.repository.WarnRuleRepository; import com.xydl.cac.repository.WarningRepository; import com.xydl.cac.service.DataService; -import com.xydl.cac.service.WarnRuleService; import com.xydl.cac.util.DateUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; @@ -25,8 +21,6 @@ import java.util.Map; @Service @Slf4j public class RuleCheckTask { - @Resource - WarnRuleService ruleService; @Resource WarnRuleRepository ruleRepository; @Resource @@ -34,12 +28,11 @@ public class RuleCheckTask { @Resource WarningRepository warningRepository; - @Scheduled(initialDelay = 60000, fixedDelay = 60000) + @Scheduled(initialDelay = 90000, fixedDelay = 60000) private void checkAll() { try { - List ruleList = ruleService.listAll(); - for (WarnRule rule : ruleList) { - if (rule.getActive() != null && rule.getActive().intValue() == 1) { + for (WarnRule rule : CacheTask.rule_Cache.values()) { + if (rule.canDo()) { this.ruleCheck(rule); } } @@ -49,19 +42,6 @@ public class RuleCheckTask { } private void ruleCheck(WarnRule rule) { - if (Constants.FloatCompare.equalsIgnoreCase(rule.getComparator())) { - FloatCompare comparat = new FloatCompare(); - comparat.setOperator(rule.getOperator()); - rule.setComparat(comparat); - } else if (Constants.IntCompare.equalsIgnoreCase(rule.getComparator())) { - IntCompare comparat = new IntCompare(); - comparat.setOperator(rule.getOperator()); - rule.setComparat(comparat); - } else { - log.error("该规则(" + rule.getId() + ")缺少比较器"); - return; - } - try { NSensor sensor = rule.getNSensor(); ModevTypePoint typePoint = rule.getTypePoint(); @@ -72,7 +52,7 @@ public class RuleCheckTask { String str = (String) map.get("acquisitionTime"); date = DateUtil.parse(str); Object value = map.get(typePoint.getField()); - boolean r = rule.getComparat().compare(value, rule.getThreshold()); + boolean r = rule.getActualComp().compare(value, rule.getThreshold()); if (r) { this.sendWarning(rule, date, value); }