Unverified Commit c12cdbd3 authored by mydq's avatar mydq Committed by GitHub

1. When an alarm instance is deleted, the association in the alarm group is...

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.
parent 53485855
...@@ -73,22 +73,7 @@ public class AlertInstanceController { ...@@ -73,22 +73,7 @@ public class AlertInstanceController {
*/ */
@DeleteMapping @DeleteMapping
public Result deleteMul(@RequestBody JsonNode para) { public Result deleteMul(@RequestBody JsonNode para) {
if (para.size() > 0) { return alertInstanceService.deleteAlertInstance(para);
List<Integer> 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("请选择要删除的记录");
}
} }
/** /**
......
...@@ -21,8 +21,10 @@ ...@@ -21,8 +21,10 @@
package com.dlink.service; package com.dlink.service;
import com.dlink.alert.AlertResult; import com.dlink.alert.AlertResult;
import com.dlink.common.result.Result;
import com.dlink.db.service.ISuperService; import com.dlink.db.service.ISuperService;
import com.dlink.model.AlertInstance; import com.dlink.model.AlertInstance;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.List; import java.util.List;
...@@ -37,4 +39,6 @@ public interface AlertInstanceService extends ISuperService<AlertInstance> { ...@@ -37,4 +39,6 @@ public interface AlertInstanceService extends ISuperService<AlertInstance> {
List<AlertInstance> listEnabledAll(); List<AlertInstance> listEnabledAll();
AlertResult testAlert(AlertInstance alertInstance); AlertResult testAlert(AlertInstance alertInstance);
Result deleteAlertInstance(JsonNode para);
} }
...@@ -20,20 +20,37 @@ ...@@ -20,20 +20,37 @@
package com.dlink.service.impl; package com.dlink.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dlink.alert.*; 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.db.service.impl.SuperServiceImpl;
import com.dlink.mapper.AlertInstanceMapper; import com.dlink.mapper.AlertInstanceMapper;
import com.dlink.model.AlertGroup;
import com.dlink.model.AlertInstance; import com.dlink.model.AlertInstance;
import com.dlink.service.AlertGroupService;
import com.dlink.service.AlertInstanceService; import com.dlink.service.AlertInstanceService;
import com.dlink.utils.JSONUtil; 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 org.springframework.stereotype.Service;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
/** /**
* AlertInstanceServiceImpl * AlertInstanceServiceImpl
...@@ -43,6 +60,10 @@ import java.util.UUID; ...@@ -43,6 +60,10 @@ import java.util.UUID;
**/ **/
@Service @Service
public class AlertInstanceServiceImpl extends SuperServiceImpl<AlertInstanceMapper, AlertInstance> implements AlertInstanceService { public class AlertInstanceServiceImpl extends SuperServiceImpl<AlertInstanceMapper, AlertInstance> implements AlertInstanceService {
@Autowired
private AlertGroupService alertGroupService;
@Override @Override
public List<AlertInstance> listEnabledAll() { public List<AlertInstance> listEnabledAll() {
return list(new QueryWrapper<AlertInstance>().eq("enabled", 1)); return list(new QueryWrapper<AlertInstance>().eq("enabled", 1));
...@@ -79,4 +100,89 @@ public class AlertInstanceServiceImpl extends SuperServiceImpl<AlertInstanceMapp ...@@ -79,4 +100,89 @@ public class AlertInstanceServiceImpl extends SuperServiceImpl<AlertInstanceMapp
String title = "任务【"+alertMsg.getJobName()+"】:" +alertMsg.getJobStatus() + "!"; String title = "任务【"+alertMsg.getJobName()+"】:" +alertMsg.getJobStatus() + "!";
return alert.send(title, alertMsg.toString()); return alert.send(title, alertMsg.toString());
} }
@Override
public Result deleteAlertInstance(JsonNode para) {
if (para.size() > 0) {
final Map<Integer, Set<Integer>> alertGroupInformation = getAlertGroupInformation();
final List<Integer> 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<Integer, Set<Integer>> alertGroupInformation){
if (MapUtils.isEmpty(alertGroupInformation)){
return;
}
final Map<Integer, String> result = new HashMap<>(8);
for (Map.Entry<Integer, Set<Integer>> entry : alertGroupInformation.entrySet()) {
final Set<Integer> 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<Integer, String> result) {
final LocalDateTime now = LocalDateTime.now();
final List<AlertGroup> 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<Integer, Set<Integer>> getAlertGroupInformation(){
final LambdaQueryWrapper<AlertGroup> select = new LambdaQueryWrapper<AlertGroup>()
.select(AlertGroup::getId, AlertGroup::getAlertInstanceIds);
final List<AlertGroup> list = alertGroupService.list(select);
if (CollectionUtils.isEmpty(list)){
return new HashMap<>(0);
}
final Map<Integer, Set<Integer>> map = new HashMap<>(list.size());
for (AlertGroup alertGroup : list) {
buildGroup(map, alertGroup);
}
return map;
}
private void buildGroup(Map<Integer, Set<Integer>> 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<Integer> groupIdSet = map.get(instanceIdInt);
if (CollectionUtils.isEmpty(groupIdSet)){
groupIdSet = new HashSet<>();
map.put(instanceIdInt, groupIdSet);
}
groupIdSet.add(alertGroup.getId());
}
}
} }
...@@ -894,6 +894,9 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen ...@@ -894,6 +894,9 @@ public class TaskServiceImpl extends SuperServiceImpl<TaskMapper, Task> implemen
String exceptionUrl = "http://" + jobManagerHost + "/#/job/" + jobInstance.getJid() + "/exceptions"; String exceptionUrl = "http://" + jobManagerHost + "/#/job/" + jobInstance.getJid() + "/exceptions";
for (AlertInstance alertInstance : alertGroup.getInstances()) { for (AlertInstance alertInstance : alertGroup.getInstances()) {
if (alertInstance == null){
continue;
}
Map<String, String> map = JSONUtil.toMap(alertInstance.getParams()); Map<String, String> map = JSONUtil.toMap(alertInstance.getParams());
if (map.get("msgtype").equals(ShowType.MARKDOWN.getValue())) { if (map.get("msgtype").equals(ShowType.MARKDOWN.getValue())) {
alertMsg.setLinkUrl("[跳转至该任务的 FlinkWeb](" + linkUrl + ")"); alertMsg.setLinkUrl("[跳转至该任务的 FlinkWeb](" + linkUrl + ")");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment