From c12cdbd328d702e12efaf2a82ff747cb8d3d031b Mon Sep 17 00:00:00 2001 From: mydq <42607771+mydq@users.noreply.github.com> Date: Thu, 21 Jul 2022 09:52:22 +0800 Subject: [PATCH] 1. When an alarm instance is deleted, the association in the alarm group is deleted in cascade. (#747) 2. When an alarm occurs, if there is a deleted instance in the alarm group instance id, npe will be reported. --- .../controller/AlertInstanceController.java | 17 +-- .../dlink/service/AlertInstanceService.java | 4 + .../impl/AlertInstanceServiceImpl.java | 106 ++++++++++++++++++ .../dlink/service/impl/TaskServiceImpl.java | 3 + 4 files changed, 114 insertions(+), 16 deletions(-) diff --git a/dlink-admin/src/main/java/com/dlink/controller/AlertInstanceController.java b/dlink-admin/src/main/java/com/dlink/controller/AlertInstanceController.java index a1bc4f03fc..03164cd961 100644 --- a/dlink-admin/src/main/java/com/dlink/controller/AlertInstanceController.java +++ b/dlink-admin/src/main/java/com/dlink/controller/AlertInstanceController.java @@ -73,22 +73,7 @@ public ProTableResult listAlertInstances(@RequestBody JsonNode pa */ @DeleteMapping public Result deleteMul(@RequestBody JsonNode para) { - if (para.size() > 0) { - List error = new ArrayList<>(); - for (final JsonNode item : para) { - Integer id = item.asInt(); - if (!alertInstanceService.removeById(id)) { - error.add(id); - } - } - if (error.size() == 0) { - return Result.succeed("删除成功"); - } else { - return Result.succeed("删除部分成功,但" + error.toString() + "删除失败,共" + error.size() + "次失败。"); - } - } else { - return Result.failed("请选择要删除的记录"); - } + return alertInstanceService.deleteAlertInstance(para); } /** diff --git a/dlink-admin/src/main/java/com/dlink/service/AlertInstanceService.java b/dlink-admin/src/main/java/com/dlink/service/AlertInstanceService.java index a88eca82d5..b97227bb6d 100644 --- a/dlink-admin/src/main/java/com/dlink/service/AlertInstanceService.java +++ b/dlink-admin/src/main/java/com/dlink/service/AlertInstanceService.java @@ -21,8 +21,10 @@ package com.dlink.service; import com.dlink.alert.AlertResult; +import com.dlink.common.result.Result; import com.dlink.db.service.ISuperService; import com.dlink.model.AlertInstance; +import com.fasterxml.jackson.databind.JsonNode; import java.util.List; @@ -37,4 +39,6 @@ public interface AlertInstanceService extends ISuperService { List listEnabledAll(); AlertResult testAlert(AlertInstance alertInstance); + + Result deleteAlertInstance(JsonNode para); } diff --git a/dlink-admin/src/main/java/com/dlink/service/impl/AlertInstanceServiceImpl.java b/dlink-admin/src/main/java/com/dlink/service/impl/AlertInstanceServiceImpl.java index 2b6a927c5c..d079d17815 100644 --- a/dlink-admin/src/main/java/com/dlink/service/impl/AlertInstanceServiceImpl.java +++ b/dlink-admin/src/main/java/com/dlink/service/impl/AlertInstanceServiceImpl.java @@ -20,20 +20,37 @@ package com.dlink.service.impl; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.dlink.alert.*; +import com.dlink.assertion.Asserts; +import com.dlink.common.result.Result; import com.dlink.db.service.impl.SuperServiceImpl; import com.dlink.mapper.AlertInstanceMapper; +import com.dlink.model.AlertGroup; import com.dlink.model.AlertInstance; +import com.dlink.service.AlertGroupService; import com.dlink.service.AlertInstanceService; import com.dlink.utils.JSONUtil; + +import com.fasterxml.jackson.databind.JsonNode; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.Calendar; +import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; /** * AlertInstanceServiceImpl @@ -43,6 +60,10 @@ **/ @Service public class AlertInstanceServiceImpl extends SuperServiceImpl implements AlertInstanceService { + + @Autowired + private AlertGroupService alertGroupService; + @Override public List listEnabledAll() { return list(new QueryWrapper().eq("enabled", 1)); @@ -79,4 +100,89 @@ public AlertResult testAlert(AlertInstance alertInstance) { String title = "任务【"+alertMsg.getJobName()+"】:" +alertMsg.getJobStatus() + "!"; return alert.send(title, alertMsg.toString()); } + + + @Override + public Result deleteAlertInstance(JsonNode para) { + if (para.size() > 0) { + final Map> alertGroupInformation = getAlertGroupInformation(); + final List error = new ArrayList<>(); + for (final JsonNode item : para) { + Integer id = item.asInt(); + if (!this.removeById(id)) { + error.add(id); + } + alertGroupInformation.remove(id); + } + writeBackGroupInformation(alertGroupInformation); + if (error.size() == 0) { + return Result.succeed("删除成功"); + } else { + return Result.succeed("删除部分成功,但" + error.toString() + "删除失败,共" + error.size() + "次失败。"); + } + } else { + return Result.failed("请选择要删除的记录"); + } + } + + private void writeBackGroupInformation(Map> alertGroupInformation){ + if (MapUtils.isEmpty(alertGroupInformation)){ + return; + } + final Map result = new HashMap<>(8); + for (Map.Entry> entry : alertGroupInformation.entrySet()) { + final Set groupIdSet = entry.getValue(); + for (Integer groupId : groupIdSet) { + final String instanceIdString = result.get(groupId); + result.put(groupId, instanceIdString == null ? "" + entry.getKey() + : instanceIdString + "," + entry.getKey()); + } + } + updateAlertGroupInformation(result); + } + + private void updateAlertGroupInformation(Map result) { + final LocalDateTime now = LocalDateTime.now(); + final List list = result.entrySet().stream().map(entry -> { + final AlertGroup alertGroup = new AlertGroup(); + alertGroup.setId(entry.getKey()); + alertGroup.setAlertInstanceIds(entry.getValue()); + alertGroup.setUpdateTime(now); + return alertGroup; + }).collect(Collectors.toList()); + alertGroupService.updateBatchById(list); + } + + + private Map> getAlertGroupInformation(){ + final LambdaQueryWrapper select = new LambdaQueryWrapper() + .select(AlertGroup::getId, AlertGroup::getAlertInstanceIds); + final List list = alertGroupService.list(select); + if (CollectionUtils.isEmpty(list)){ + return new HashMap<>(0); + } + final Map> map = new HashMap<>(list.size()); + for (AlertGroup alertGroup : list) { + buildGroup(map, alertGroup); + } + return map; + } + + private void buildGroup(Map> map, AlertGroup alertGroup) { + if (StringUtils.isBlank(alertGroup.getAlertInstanceIds())){ + return; + } + for (String instanceId : alertGroup.getAlertInstanceIds().split(",")) { + if (StringUtils.isBlank(instanceId)){ + continue; + } + final Integer instanceIdInt = Integer.valueOf(instanceId); + Set groupIdSet = map.get(instanceIdInt); + if (CollectionUtils.isEmpty(groupIdSet)){ + groupIdSet = new HashSet<>(); + map.put(instanceIdInt, groupIdSet); + } + groupIdSet.add(alertGroup.getId()); + } + } } diff --git a/dlink-admin/src/main/java/com/dlink/service/impl/TaskServiceImpl.java b/dlink-admin/src/main/java/com/dlink/service/impl/TaskServiceImpl.java index 01bbd9bd2d..9d9eddcca4 100644 --- a/dlink-admin/src/main/java/com/dlink/service/impl/TaskServiceImpl.java +++ b/dlink-admin/src/main/java/com/dlink/service/impl/TaskServiceImpl.java @@ -894,6 +894,9 @@ private void handleJobDone(JobInstance jobInstance) { String exceptionUrl = "http://" + jobManagerHost + "/#/job/" + jobInstance.getJid() + "/exceptions"; for (AlertInstance alertInstance : alertGroup.getInstances()) { + if (alertInstance == null){ + continue; + } Map map = JSONUtil.toMap(alertInstance.getParams()); if (map.get("msgtype").equals(ShowType.MARKDOWN.getValue())) { alertMsg.setLinkUrl("[跳转至该任务的 FlinkWeb](" + linkUrl + ")");