perf: 优化规则触发代码

dev
huangfeng 12 months ago
parent d06edce15b
commit cfe0c49fee

@ -111,9 +111,15 @@ public class WarnRule {
} }
public boolean canDo() { public boolean canDo() {
if (active != null && active.intValue() == 1 && actualComp != null && nSensor != null) { if (active != null && active.intValue() == 1 &&
actualComp != null && nSensor != null && typePoint != null) {
return true; return true;
} }
return false; return false;
} }
// 是否触发
public boolean triggerRule(Object value) {
return actualComp.compare(value, threshold);
}
} }

@ -11,8 +11,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
@Service @Service
@Slf4j @Slf4j
@ -25,7 +25,7 @@ public class CacheTask {
public static List<ModevType> modevType_Cache = null; public static List<ModevType> modevType_Cache = null;
public static List<Jg> jg_Cache = null; public static List<Jg> jg_Cache = null;
public static List<Zsb> zsb_Cache = null; public static List<Zsb> zsb_Cache = null;
public static LinkedHashMap<Integer, WarnRule> rule_Cache = new LinkedHashMap<>(); public static ConcurrentHashMap<Integer, WarnRule> rule_Cache = new ConcurrentHashMap<>();
@Scheduled(cron = "0 1 * * * ?") @Scheduled(cron = "0 1 * * * ?")
private void clearCache() { private void clearCache() {
@ -37,7 +37,7 @@ public class CacheTask {
} }
@Scheduled(initialDelay = 60 * 1000, fixedDelay = 60 * 1000) @Scheduled(initialDelay = 30 * 1000, fixedDelay = 60 * 1000)
private void refreshRule() { private void refreshRule() {
try { try {
List<WarnRule> list = ruleService.listAll(null); List<WarnRule> list = ruleService.listAll(null);

@ -16,10 +16,7 @@ import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service @Service
@Slf4j @Slf4j
@ -35,10 +32,13 @@ public class RuleCheckTask {
@Resource @Resource
WebSocketServer webSocketServer; WebSocketServer webSocketServer;
@Scheduled(initialDelay = 90 * 1000, fixedDelay = 3 * 60 * 1000) @Scheduled(initialDelay = 60 * 1000, fixedDelay = 3 * 60 * 1000)
private void checkAll() { private void checkAll() {
try { try {
for (WarnRule rule : CacheTask.rule_Cache.values()) { Iterator<Integer> it = CacheTask.rule_Cache.keySet().iterator();
while (it.hasNext()) {
Integer key = it.next();
WarnRule rule = CacheTask.rule_Cache.get(key);
if (rule.canDo()) { if (rule.canDo()) {
this.ruleCheck(rule); this.ruleCheck(rule);
} }
@ -61,7 +61,7 @@ public class RuleCheckTask {
String str = (String) map.get("acquisitionTime"); String str = (String) map.get("acquisitionTime");
date = DateUtil.parse(str); date = DateUtil.parse(str);
Object value = map.get(typePoint.getField()); Object value = map.get(typePoint.getField());
boolean r = rule.getActualComp().compare(value, rule.getThreshold()); boolean r = rule.triggerRule(value);
if (r) { if (r) {
this.sendWarning(rule, date, str, String.valueOf(value)); this.sendWarning(rule, date, str, String.valueOf(value));
} }

Loading…
Cancel
Save