From b96361c407479818cd416ce6684a4d87d4ead198 Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Mon, 6 Nov 2023 23:19:57 +0800 Subject: [PATCH] bugfix alarm time span match in silence and notice (#1318) --- .../alert/reduce/AlarmSilenceReduce.java | 30 +++++---- .../manager/service/impl/AppServiceImpl.java | 4 ++ .../service/impl/NoticeConfigServiceImpl.java | 64 +++++++++---------- 3 files changed, 52 insertions(+), 46 deletions(-) diff --git a/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmSilenceReduce.java b/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmSilenceReduce.java index 780c7700053..84e90537615 100644 --- a/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmSilenceReduce.java +++ b/alerter/src/main/java/org/dromara/hertzbeat/alert/reduce/AlarmSilenceReduce.java @@ -14,6 +14,7 @@ import java.time.LocalTime; import java.util.List; import java.util.Map; +import java.util.Optional; /** * silence alarm @@ -72,26 +73,29 @@ public boolean filterSilence(Alert alert) { LocalDateTime nowDate = LocalDateTime.now(); if (alertSilence.getType() == 0) { // once time - if (alertSilence.getPeriodStart() != null && alertSilence.getPeriodEnd() != null) { - if (nowDate.isAfter(alertSilence.getPeriodStart().toLocalDateTime()) - && nowDate.isBefore(alertSilence.getPeriodEnd().toLocalDateTime())) { - int times = alertSilence.getTimes() == null ? 0 : alertSilence.getTimes(); - alertSilence.setTimes(times + 1); - alertSilenceDao.save(alertSilence); - return false; - } + boolean startMatch = alertSilence.getPeriodStart() == null || + nowDate.isAfter(alertSilence.getPeriodStart().toLocalDateTime()); + boolean endMatch = alertSilence.getPeriodEnd() == null || + nowDate.isBefore(alertSilence.getPeriodEnd().toLocalDateTime()); + if (startMatch && endMatch) { + int times = Optional.ofNullable(alertSilence.getTimes()).orElse(0); + alertSilence.setTimes(times + 1); + alertSilenceDao.save(alertSilence); + return false; } } else if (alertSilence.getType() == 1) { // cyc time int currentDayOfWeek = nowDate.toLocalDate().getDayOfWeek().getValue(); if (alertSilence.getDays() != null && !alertSilence.getDays().isEmpty()) { boolean dayMatch = alertSilence.getDays().stream().anyMatch(item -> item == currentDayOfWeek); - if (dayMatch && alertSilence.getPeriodStart() != null && alertSilence.getPeriodEnd() != null ) { + if (dayMatch) { LocalTime nowTime = nowDate.toLocalTime(); - - if (nowTime.isAfter(alertSilence.getPeriodStart().toLocalTime()) - && nowTime.isBefore(alertSilence.getPeriodEnd().toLocalTime())) { - int times = alertSilence.getTimes() == null ? 0 : alertSilence.getTimes(); + boolean startMatch = alertSilence.getPeriodStart() == null || + nowTime.isAfter(alertSilence.getPeriodStart().toLocalTime()); + boolean endMatch = alertSilence.getPeriodEnd() == null || + nowTime.isBefore(alertSilence.getPeriodEnd().toLocalTime()); + if (startMatch && endMatch) { + int times = Optional.ofNullable(alertSilence.getTimes()).orElse(0); alertSilence.setTimes(times + 1); alertSilenceDao.save(alertSilence); return false; diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/AppServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/AppServiceImpl.java index dd97457be0d..d4c16673128 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/AppServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/AppServiceImpl.java @@ -216,6 +216,10 @@ public Map getI18nResources(String lang) { public List getAllAppHierarchy(String lang) { List hierarchies = new LinkedList<>(); for (var job : appDefines.values()) { + // todo 暂时先过滤掉push以解决前端问题,待后续设计优化后放开 + if (DispatchConstants.PROTOCOL_PUSH.equalsIgnoreCase(job.getApp())) { + continue; + } var hierarchyApp = new Hierarchy(); hierarchyApp.setCategory(job.getCategory()); hierarchyApp.setValue(job.getApp()); diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java index 44dab59479e..81b448213b8 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/NoticeConfigServiceImpl.java @@ -22,7 +22,6 @@ import org.dromara.hertzbeat.common.cache.ICacheService; import org.dromara.hertzbeat.common.constants.CommonConstants; import org.dromara.hertzbeat.common.entity.alerter.Alert; -import org.dromara.hertzbeat.common.entity.job.Job; import org.dromara.hertzbeat.common.entity.manager.NoticeReceiver; import org.dromara.hertzbeat.common.entity.manager.NoticeRule; import org.dromara.hertzbeat.common.entity.manager.NoticeTemplate; @@ -42,11 +41,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.*; @@ -148,6 +145,29 @@ public List getReceiverFilterRule(Alert alert) { // 规则是全部转发, 告警状态选择, 监控类型选择等(按照tags标签和告警级别过滤匹配) return rules.stream() .filter(rule -> { + if (!rule.isFilterAll()) { + // filter priorities + if (rule.getPriorities() != null && !rule.getPriorities().isEmpty()) { + boolean priorityMatch = rule.getPriorities().stream().anyMatch(item -> item != null && item == alert.getPriority()); + if (!priorityMatch) { + return false; + } + } + // filter tags + if (rule.getTags() != null && !rule.getTags().isEmpty()) { + boolean tagMatch = rule.getTags().stream().anyMatch(tagItem -> { + if (!alert.getTags().containsKey(tagItem.getName())) { + return false; + } + String alertTagValue = alert.getTags().get(tagItem.getName()); + return Objects.equals(tagItem.getValue(), alertTagValue); + }); + if (!tagMatch) { + return false; + } + } + } + LocalDateTime nowDate = LocalDateTime.now(); // filter day int currentDayOfWeek = nowDate.toLocalDate().getDayOfWeek().getValue(); @@ -158,36 +178,14 @@ public List getReceiverFilterRule(Alert alert) { } } // filter time - if (rule.getPeriodStart() != null && rule.getPeriodEnd() != null) { - LocalTime nowTime = nowDate.toLocalTime(); - - if (nowTime.isBefore(rule.getPeriodStart().toLocalTime()) - || nowTime.isAfter(rule.getPeriodEnd().toLocalTime())) { - return false; - } - } - - if (rule.isFilterAll()) { - return true; - } - // filter priorities - if (rule.getPriorities() != null && !rule.getPriorities().isEmpty()) { - boolean priorityMatch = rule.getPriorities().stream().anyMatch(item -> item != null && item == alert.getPriority()); - if (!priorityMatch) { - return false; - } - } - // filter tags - if (rule.getTags() != null && !rule.getTags().isEmpty()) { - return rule.getTags().stream().anyMatch(tagItem -> { - if (!alert.getTags().containsKey(tagItem.getName())) { - return false; - } - String alertTagValue = alert.getTags().get(tagItem.getName()); - return Objects.equals(tagItem.getValue(), alertTagValue); - }); - } - return true; + LocalTime nowTime = nowDate.toLocalTime(); + boolean startMatch = rule.getPeriodStart() == null || + nowTime.isAfter(rule.getPeriodStart().toLocalTime()) || + (rule.getPeriodEnd() != null && rule.getPeriodStart().isAfter(rule.getPeriodEnd()) + && nowTime.isBefore(rule.getPeriodStart().toLocalTime())); + boolean endMatch = rule.getPeriodEnd() == null || + nowTime.isBefore(rule.getPeriodEnd().toLocalTime()); + return startMatch && endMatch; }) .collect(Collectors.toList()); }