From 0bace60ae5a94bda15a652bf7735e1dfee3db685 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Mon, 22 Jul 2024 10:11:49 +0800 Subject: [PATCH 1/2] [Optimize] some optimize for alert module Signed-off-by: yuluo-yx --- .../hertzbeat/alert/service/AlertService.java | 10 ++++++++++ .../service/impl/AlertConvergeServiceImpl.java | 6 +++++- .../AlertDefineAbstractImExportServiceImpl.java | 15 ++++++++------- .../alert/service/impl/AlertServiceImpl.java | 11 +++++++++++ .../service/impl/AlertSilenceServiceImpl.java | 2 ++ .../collector/util/TimeExpressionUtil.java | 2 +- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java index 2bc6b182e64..a19e6a330d5 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java @@ -19,9 +19,11 @@ import java.util.HashSet; import java.util.List; + import org.apache.hertzbeat.alert.dto.AlertSummary; import org.apache.hertzbeat.common.entity.alerter.Alert; import org.apache.hertzbeat.common.entity.dto.AlertReport; + import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; @@ -31,6 +33,14 @@ */ public interface AlertService { + /** + * Verify the correctness of the request data parameters + * @param alert AlertSilence + * @param isModify whether modify + * @throws IllegalArgumentException A checksum parameter error is thrown + */ + void validate(Alert alert, boolean isModify) throws IllegalArgumentException; + /** * Add alarm record * @param alert Alert entity diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertConvergeServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertConvergeServiceImpl.java index a4d05b47e56..36294cd4f9d 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertConvergeServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertConvergeServiceImpl.java @@ -17,6 +17,7 @@ package org.apache.hertzbeat.alert.service.impl; +import java.util.Objects; import java.util.Set; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.alert.dao.AlertConvergeDao; @@ -45,7 +46,10 @@ public class AlertConvergeServiceImpl implements AlertConvergeService { @Override public void validate(AlertConverge alertConverge, boolean isModify) throws IllegalArgumentException { - // todo + // todo + if (Objects.isNull(alertConverge)) { + throw new IllegalArgumentException("alertConverge is null"); + } } @Override diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java index 2900b0fe3af..761704f9fda 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java @@ -17,22 +17,23 @@ package org.apache.hertzbeat.alert.service.impl; +import java.io.InputStream; +import java.io.OutputStream; +import java.time.LocalDate; +import java.util.List; + import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import jakarta.annotation.Resource; -import java.io.InputStream; -import java.io.OutputStream; -import java.time.LocalDate; -import java.util.List; -import java.util.stream.Collectors; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.alert.service.AlertDefineImExportService; import org.apache.hertzbeat.alert.service.AlertDefineService; import org.apache.hertzbeat.common.entity.alerter.AlertDefine; import org.apache.hertzbeat.common.entity.manager.TagItem; + import org.springframework.beans.BeanUtils; import org.springframework.context.annotation.Lazy; import org.springframework.util.CollectionUtils; @@ -51,7 +52,7 @@ public void importConfig(InputStream is) { var formList = parseImport(is) .stream() .map(this::convert) - .collect(Collectors.toUnmodifiableList()); + .toList(); if (!CollectionUtils.isEmpty(formList)) { formList.forEach(alertDefine -> { alertDefineService.validate(alertDefine, false); @@ -65,7 +66,7 @@ public void exportConfig(OutputStream os, List configList) { var monitorList = configList.stream() .map(it -> alertDefineService.getAlertDefine(it)) .map(this::convert) - .collect(Collectors.toUnmodifiableList()); + .toList(); writeOs(monitorList, os); } diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java index ffdca29f9a9..05472010681 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java @@ -25,6 +25,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; + import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.alert.dao.AlertDao; import org.apache.hertzbeat.alert.dto.AlertPriorityNum; @@ -34,6 +36,7 @@ import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.alerter.Alert; import org.apache.hertzbeat.common.entity.dto.AlertReport; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -55,6 +58,14 @@ public class AlertServiceImpl implements AlertService { @Autowired private AlarmCommonReduce alarmCommonReduce; + @Override + public void validate(Alert alert, boolean isModify) throws IllegalArgumentException { + // todo + if (Objects.isNull(alert)) { + throw new IllegalArgumentException("Alarm priority cannot be empty"); + } + } + @Override public void addAlert(Alert alert) throws RuntimeException { alertDao.save(alert); diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java index a8a347af5b4..931e8922508 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java @@ -50,6 +50,8 @@ public void validate(AlertSilence alertSilence, boolean isModify) throws Illegal // if the alarm silent selection date set in periodic situations is empty, it will be deemed to be all checked. if (alertSilence.getType() == 1 && alertSilence.getDays() == null) { alertSilence.setDays(Arrays.asList((byte) 7, (byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 6)); + } else { + throw new IllegalArgumentException("periodic silence must set the alarm silent selection date"); } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java b/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java index 69f16ca3c6c..a34d9afa24c 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/util/TimeExpressionUtil.java @@ -36,7 +36,7 @@ /** * time expression deal util */ -public class TimeExpressionUtil { +public final class TimeExpressionUtil { private TimeExpressionUtil() { } From 07a1c05cfd23b76487300c0994f9fdf2a5cbedbe Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Mon, 22 Jul 2024 10:19:16 +0800 Subject: [PATCH 2/2] fix: fix code style Signed-off-by: yuluo-yx --- .../apache/hertzbeat/alert/service/AlertService.java | 2 -- .../impl/AlertDefineAbstractImExportServiceImpl.java | 10 ++++------ .../hertzbeat/alert/service/impl/AlertServiceImpl.java | 2 -- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java index a19e6a330d5..34b16bcb02b 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertService.java @@ -19,11 +19,9 @@ import java.util.HashSet; import java.util.List; - import org.apache.hertzbeat.alert.dto.AlertSummary; import org.apache.hertzbeat.common.entity.alerter.Alert; import org.apache.hertzbeat.common.entity.dto.AlertReport; - import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.jpa.domain.Specification; diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java index 761704f9fda..e05592a1609 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java @@ -17,23 +17,21 @@ package org.apache.hertzbeat.alert.service.impl; -import java.io.InputStream; -import java.io.OutputStream; -import java.time.LocalDate; -import java.util.List; - import cn.afterturn.easypoi.excel.annotation.Excel; import cn.afterturn.easypoi.excel.annotation.ExcelTarget; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import jakarta.annotation.Resource; +import java.io.InputStream; +import java.io.OutputStream; +import java.time.LocalDate; +import java.util.List; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.alert.service.AlertDefineImExportService; import org.apache.hertzbeat.alert.service.AlertDefineService; import org.apache.hertzbeat.common.entity.alerter.AlertDefine; import org.apache.hertzbeat.common.entity.manager.TagItem; - import org.springframework.beans.BeanUtils; import org.springframework.context.annotation.Lazy; import org.springframework.util.CollectionUtils; diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java index 05472010681..715790a7325 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertServiceImpl.java @@ -26,7 +26,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; - import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.alert.dao.AlertDao; import org.apache.hertzbeat.alert.dto.AlertPriorityNum; @@ -36,7 +35,6 @@ import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.alerter.Alert; import org.apache.hertzbeat.common.entity.dto.AlertReport; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest;