Skip to content

Commit

Permalink
1. When an alarm instance is deleted, the association in the alarm gr…
Browse files Browse the repository at this point in the history
…oup is deleted in cascade. (DataLinkDC#747)

2. When an alarm occurs, if there is a deleted instance in the alarm group instance id, npe will be reported.
  • Loading branch information
mydq authored Jul 21, 2022
1 parent 5348585 commit c12cdbd
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,7 @@ public ProTableResult<AlertInstance> listAlertInstances(@RequestBody JsonNode pa
*/
@DeleteMapping
public Result deleteMul(@RequestBody JsonNode para) {
if (para.size() > 0) {
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("请选择要删除的记录");
}
return alertInstanceService.deleteAlertInstance(para);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,4 +39,6 @@ public interface AlertInstanceService extends ISuperService<AlertInstance> {
List<AlertInstance> listEnabledAll();

AlertResult testAlert(AlertInstance alertInstance);

Result deleteAlertInstance(JsonNode para);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +60,10 @@
**/
@Service
public class AlertInstanceServiceImpl extends SuperServiceImpl<AlertInstanceMapper, AlertInstance> implements AlertInstanceService {

@Autowired
private AlertGroupService alertGroupService;

@Override
public List<AlertInstance> listEnabledAll() {
return list(new QueryWrapper<AlertInstance>().eq("enabled", 1));
Expand Down Expand Up @@ -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<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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> map = JSONUtil.toMap(alertInstance.getParams());
if (map.get("msgtype").equals(ShowType.MARKDOWN.getValue())) {
alertMsg.setLinkUrl("[跳转至该任务的 FlinkWeb](" + linkUrl + ")");
Expand Down

0 comments on commit c12cdbd

Please sign in to comment.