Skip to content

Commit

Permalink
bugfix alarm time span match in silence and notice (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 authored Nov 6, 2023
1 parent d90f500 commit bf6f47b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* silence alarm
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ public Map<String, String> getI18nResources(String lang) {
public List<Hierarchy> getAllAppHierarchy(String lang) {
List<Hierarchy> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.*;
Expand Down Expand Up @@ -148,6 +145,29 @@ public List<NoticeRule> 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();
Expand All @@ -158,36 +178,14 @@ public List<NoticeRule> 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());
}
Expand Down

0 comments on commit bf6f47b

Please sign in to comment.