Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix alarm time span match in silence and notice #1318

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,28 @@ 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 = alertSilence.getTimes() == null ? 0 : alertSilence.getTimes();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())) {
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 = alertSilence.getTimes() == null ? 0 : alertSilence.getTimes();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int times = Optional.ofNullable(alertSilence.getTimes()).orElse(0);

alertSilence.setTimes(times + 1);
alertSilenceDao.save(alertSilence);
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
Loading