diff --git a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java index 16b2376fe88..22340def606 100644 --- a/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java +++ b/hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java @@ -29,11 +29,14 @@ import org.apache.hertzbeat.alert.service.AlertDefineImExportService; import org.apache.hertzbeat.alert.service.AlertDefineService; import org.apache.hertzbeat.alert.service.DataSourceService; +import org.apache.hertzbeat.base.dao.LabelDao; +import org.apache.hertzbeat.base.service.LabelService; import org.apache.hertzbeat.common.cache.CacheFactory; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.constants.ExportFileConstants; import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.alerter.AlertDefine; +import org.apache.hertzbeat.common.entity.manager.Label; import org.apache.hertzbeat.common.util.FileUtil; import org.apache.hertzbeat.common.util.JexlExpressionRunner; import org.springframework.beans.factory.annotation.Autowired; @@ -48,6 +51,7 @@ import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -58,6 +62,7 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import java.util.stream.Collectors; /** * Alarm definition management interface implementation @@ -69,10 +74,16 @@ public class AlertDefineServiceImpl implements AlertDefineService { @Autowired private AlertDefineDao alertDefineDao; - + @Autowired private PeriodicAlertRuleScheduler periodicAlertRuleScheduler; + @Resource + private LabelService labelService; + + @Resource + private LabelDao labelDao; + private final DataSourceService dataSourceService; private final Map alertDefineImExportServiceMap = new HashMap<>(); @@ -93,7 +104,7 @@ public void validate(AlertDefine alertDefine, boolean isModify) throws IllegalAr JexlExpressionRunner.compile(alertDefine.getExpr()); } catch (Exception e) { throw new IllegalArgumentException("alert expr error: " + e.getMessage()); - } + } } } // the name of the alarm rule is unique @@ -107,6 +118,7 @@ public void validate(AlertDefine alertDefine, boolean isModify) throws IllegalAr @Override public void addAlertDefine(AlertDefine alertDefine) throws RuntimeException { + saveNewCustomLabel(alertDefine); alertDefine = alertDefineDao.saveAndFlush(alertDefine); periodicAlertRuleScheduler.updateSchedule(alertDefine); CacheFactory.clearAlertDefineCache(); @@ -114,11 +126,31 @@ public void addAlertDefine(AlertDefine alertDefine) throws RuntimeException { @Override public void modifyAlertDefine(AlertDefine alertDefine) throws RuntimeException { + saveNewCustomLabel(alertDefine); alertDefineDao.saveAndFlush(alertDefine); periodicAlertRuleScheduler.updateSchedule(alertDefine); CacheFactory.clearAlertDefineCache(); } + private void saveNewCustomLabel(AlertDefine alertDefine) { + Map labels = alertDefine.getLabels(); + if (labels == null) { + labels = new HashMap<>(8); + alertDefine.setLabels(labels); + } + Map customLabels = labels.entrySet().stream() + .filter(entry -> !isSystemBuiltInLabel(entry.getKey())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + List