[alerter] 告警定义批量接口

This commit is contained in:
tomsun28
2021-12-12 10:32:40 +08:00
parent 8076787e07
commit 4586eba054
6 changed files with 107 additions and 7 deletions

View File

@@ -91,7 +91,7 @@ public class CalculateAlarm {
}
// 查出此监控类型下的此指标集合下关联配置的告警定义信息
// field - define[]
Map<String, List<AlertDefine>> defineMap = alertDefineService.getAlertDefines(monitorId, app, metrics);
Map<String, List<AlertDefine>> defineMap = alertDefineService.getMonitorBindAlertDefines(monitorId, app, metrics);
if (defineMap == null || defineMap.isEmpty()) {
return;
}

View File

@@ -0,0 +1,86 @@
package com.usthe.alert.controller;
import com.usthe.alert.pojo.entity.AlertDefine;
import com.usthe.alert.service.AlertDefineService;
import com.usthe.common.entity.dto.Message;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import java.util.HashSet;
import java.util.List;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
/**
* 告警定义批量API
* @author tom
* @date 2021/12/9 10:32
*/
@Api(tags = "告警定义管理API")
@RestController
@RequestMapping(path = "/alert/defines", produces = {APPLICATION_JSON_VALUE})
public class AlertDefinesController {
@Autowired
private AlertDefineService alertDefineService;
@GetMapping
@ApiOperation(value = "查询告警定义列表", notes = "根据查询过滤项获取告警定义信息列表")
public ResponseEntity<Message<Page<AlertDefine>>> getAlertDefines(
@ApiParam(value = "告警定义ID", example = "6565463543") @RequestParam(required = false) List<Long> ids,
@ApiParam(value = "告警定义级别", example = "6565463543") @RequestParam(required = false) Byte priority,
@ApiParam(value = "排序字段默认id", example = "id") @RequestParam(defaultValue = "id") String sort,
@ApiParam(value = "排序方式asc:升序desc:降序", example = "desc") @RequestParam(defaultValue = "desc") String order,
@ApiParam(value = "列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
@ApiParam(value = "列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
Specification<AlertDefine> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (ids != null && !ids.isEmpty()) {
CriteriaBuilder.In<Long> inPredicate= criteriaBuilder.in(root.get("id"));
for (long id : ids) {
inPredicate.value(id);
}
predicate = criteriaBuilder.and(inPredicate);
}
if (priority != null) {
Predicate predicateApp = criteriaBuilder.equal(root.get("priority"), priority);
predicate = criteriaBuilder.and(predicateApp);
}
return predicate;
};
// 分页是必须的
Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
Page<AlertDefine> alertDefinePage = alertDefineService.getAlertDefines(specification,pageRequest);
Message<Page<AlertDefine>> message = new Message<>(alertDefinePage);
return ResponseEntity.ok(message);
}
@DeleteMapping
@ApiOperation(value = "批量删除告警定义", notes = "根据告警定义ID列表批量删除监控项")
public ResponseEntity<Message<Void>> deleteAlertDefines(
@ApiParam(value = "告警定义IDs", example = "6565463543") @RequestParam(required = false) List<Long> ids
) {
if (ids != null && !ids.isEmpty()) {
alertDefineService.deleteAlertDefines(new HashSet<>(ids));
}
Message<Void> message = new Message<>();
return ResponseEntity.ok(message);
}
}

View File

@@ -73,7 +73,7 @@ public class AlertDefine {
@ApiModelProperty(value = "告警阈值开关", example = "true", accessMode = READ_WRITE, position = 8)
private boolean enable = true;
@ApiModelProperty(value = "告警通知内容", example = "linux {monitor_name}: {monitor_id} cpu usage high",
@ApiModelProperty(value = "告警通知内容模版", example = "linux {monitor_name}: {monitor_id} cpu usage high",
accessMode = READ_WRITE, position = 10)
@Length(max = 1024)
private String template;

View File

@@ -67,7 +67,7 @@ public interface AlertDefineService {
* @param pageRequest 分页参数
* @return 查询结果
*/
Page<AlertDefine> getAlertDefines(Specification<AlertDefine> specification, PageRequest pageRequest);
Page<AlertDefine> getMonitorBindAlertDefines(Specification<AlertDefine> specification, PageRequest pageRequest);
/**
* 应用告警定于与监控关联关系
@@ -83,5 +83,13 @@ public interface AlertDefineService {
* @param metrics 指标组
* @return field - define[]
*/
Map<String, List<AlertDefine>> getAlertDefines(long monitorId, String app, String metrics);
Map<String, List<AlertDefine>> getMonitorBindAlertDefines(long monitorId, String app, String metrics);
/**
* 动态条件查询
* @param specification 查询条件
* @param pageRequest 分页参数
* @return 查询结果
*/
Page<AlertDefine> getAlertDefines(Specification<AlertDefine> specification, PageRequest pageRequest);
}

View File

@@ -68,7 +68,7 @@ public class AlertDefineServiceImpl implements AlertDefineService {
}
@Override
public Page<AlertDefine> getAlertDefines(Specification<AlertDefine> specification, PageRequest pageRequest) {
public Page<AlertDefine> getMonitorBindAlertDefines(Specification<AlertDefine> specification, PageRequest pageRequest) {
return alertDefineDao.findAll(specification, pageRequest);
}
@@ -87,7 +87,7 @@ public class AlertDefineServiceImpl implements AlertDefineService {
}
@Override
public Map<String, List<AlertDefine>> getAlertDefines(long monitorId, String app, String metrics) {
public Map<String, List<AlertDefine>> getMonitorBindAlertDefines(long monitorId, String app, String metrics) {
List<AlertDefine> defines = alertDefineDao.queryAlertDefinesByMonitor(monitorId, metrics);
if (defines == null || defines.isEmpty()) {
return null;
@@ -96,4 +96,9 @@ public class AlertDefineServiceImpl implements AlertDefineService {
return defines.stream().sorted(Comparator.comparing(AlertDefine::getPriority))
.collect(Collectors.groupingBy(AlertDefine::getField));
}
@Override
public Page<AlertDefine> getAlertDefines(Specification<AlertDefine> specification, PageRequest pageRequest) {
return alertDefineDao.findAll(specification, pageRequest);
}
}

View File

@@ -8,4 +8,5 @@ com.usthe.alert.AlerterDataQueue,\
com.usthe.alert.AlerterConfiguration,\
com.usthe.alert.entrance.KafkaDataConsume,\
com.usthe.alert.calculate.CalculateAlarm,\
com.usthe.alert.controller.AlertsController
com.usthe.alert.controller.AlertsController,\
com.usthe.alert.controller.AlertDefinesController