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]auto config timezone for Jackson #2197

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package org.apache.hertzbeat.alert.reduce;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.dao.AlertSilenceDao;
import org.apache.hertzbeat.common.cache.CacheFactory;
import org.apache.hertzbeat.common.cache.CommonCacheService;
Expand All @@ -36,6 +38,7 @@
* silence alarm
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class AlarmSilenceReduce {

Expand Down Expand Up @@ -84,11 +87,11 @@ public boolean filterSilence(Alert alert) {
}
}
if (match) {
LocalDateTime nowDate = LocalDateTime.now();
ZonedDateTime nowDate = ZonedDateTime.now();
if (alertSilence.getType() == 0) {
// once time
boolean startMatch = alertSilence.getPeriodStart() == null || nowDate.isAfter(alertSilence.getPeriodStart().toLocalDateTime());
boolean endMatch = alertSilence.getPeriodEnd() == null || nowDate.isBefore(alertSilence.getPeriodEnd().toLocalDateTime());
boolean startMatch = alertSilence.getPeriodStart() == null || nowDate.isAfter(alertSilence.getPeriodStart());
boolean endMatch = alertSilence.getPeriodEnd() == null || nowDate.isBefore(alertSilence.getPeriodEnd());
if (startMatch && endMatch) {
int times = Optional.ofNullable(alertSilence.getTimes()).orElse(0);
alertSilence.setTimes(times + 1);
Expand All @@ -101,10 +104,13 @@ public boolean filterSilence(Alert alert) {
if (alertSilence.getDays() != null && !alertSilence.getDays().isEmpty()) {
boolean dayMatch = alertSilence.getDays().stream().anyMatch(item -> item == currentDayOfWeek);
if (dayMatch) {
LocalTime nowTime = nowDate.toLocalTime();
boolean startMatch = alertSilence.getPeriodStart() == null || nowTime.isAfter(alertSilence.getPeriodStart().toLocalTime());
boolean endMatch = alertSilence.getPeriodEnd() == null || nowTime.isBefore(alertSilence.getPeriodEnd().toLocalTime());
if (startMatch && endMatch) {
if (alertSilence.getPeriodStart() == null || alertSilence.getPeriodEnd() == null) {
continue;
}
LocalTime silentStart = alertSilence.getPeriodStart().toLocalTime();
LocalTime silentEnd = alertSilence.getPeriodEnd().toLocalTime();
// 判断是否为静默时间段
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be translated into Chinese.

if (isSilentPeriod(silentStart, silentEnd)) {
int times = Optional.ofNullable(alertSilence.getTimes()).orElse(0);
alertSilence.setTimes(times + 1);
alertSilenceDao.save(alertSilence);
Expand All @@ -117,4 +123,27 @@ public boolean filterSilence(Alert alert) {
}
return true;
}

/**
* 是否为静默时间段
*
* @param silentStart 静默开始时间
* @param silentEnd 静默结束时间
* @return 是/否
*/
private boolean isSilentPeriod(LocalTime silentStart, LocalTime silentEnd) {
if (null == silentStart || null == silentEnd) {
return false;
}
LocalTime nowLocalTime = ZonedDateTime.now().toLocalTime();
log.info("nowLocalTime:{}, silentStart:{}, silentEnd:{}, SystemDefaultTimeZoneId:{}", nowLocalTime, silentStart, silentEnd, ZoneId.systemDefault());
// 如果静默结束时间小于静默开始时间,意味着静默期跨越了午夜
if (silentEnd.isBefore(silentStart)) {
// 当前时间在午夜之前且大于等于静默开始时间,或者在午夜之后且小于静默结束时间
return nowLocalTime.isAfter(silentStart) || nowLocalTime.isBefore(silentEnd);
} else {
// 当前时间在静默开始和结束时间之间
return nowLocalTime.isAfter(silentStart) && nowLocalTime.isBefore(silentEnd);
Comment on lines +134 to +146
Copy link
Contributor

Choose a reason for hiding this comment

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

hi the code comments need to be in English, and suggest use log.debug or trace instead of log.info("nowLocalTime to aviod too many repeated, unimportant logs.

}
}
}
1 change: 1 addition & 0 deletions manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ spring:
static-path-pattern: /**
jackson:
default-property-inclusion: ALWAYS
time-zone: ${TZ:Asia/Shanghai}
Copy link
Contributor

Choose a reason for hiding this comment

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

due the #2122 support config time-zone in webui, can this be removed? @Calvin979 @cdphantom

Copy link
Contributor

Choose a reason for hiding this comment

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

due the #2122 support config time-zone in webui, can this be removed? @Calvin979 @cdphantom

Yes, it can be removed. @cdphantom

web:
resources:
static-locations:
Expand Down
Loading