diff --git a/alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java b/alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java index 6e39d07..75ac8f8 100644 --- a/alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java +++ b/alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java @@ -91,7 +91,7 @@ public class CalculateAlarm { } // 查出此监控类型下的此指标集合下关联配置的告警定义信息 // field - define[] - Map> defineMap = alertDefineService.getAlertDefines(monitorId, app, metrics); + Map> defineMap = alertDefineService.getMonitorBindAlertDefines(monitorId, app, metrics); if (defineMap == null || defineMap.isEmpty()) { return; } diff --git a/alerter/src/main/java/com/usthe/alert/controller/AlertDefinesController.java b/alerter/src/main/java/com/usthe/alert/controller/AlertDefinesController.java new file mode 100644 index 0000000..0d79fe5 --- /dev/null +++ b/alerter/src/main/java/com/usthe/alert/controller/AlertDefinesController.java @@ -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>> getAlertDefines( + @ApiParam(value = "告警定义ID", example = "6565463543") @RequestParam(required = false) List 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 specification = (root, query, criteriaBuilder) -> { + Predicate predicate = criteriaBuilder.conjunction(); + if (ids != null && !ids.isEmpty()) { + CriteriaBuilder.In 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 alertDefinePage = alertDefineService.getAlertDefines(specification,pageRequest); + Message> message = new Message<>(alertDefinePage); + return ResponseEntity.ok(message); + } + + @DeleteMapping + @ApiOperation(value = "批量删除告警定义", notes = "根据告警定义ID列表批量删除监控项") + public ResponseEntity> deleteAlertDefines( + @ApiParam(value = "告警定义IDs", example = "6565463543") @RequestParam(required = false) List ids + ) { + if (ids != null && !ids.isEmpty()) { + alertDefineService.deleteAlertDefines(new HashSet<>(ids)); + } + Message message = new Message<>(); + return ResponseEntity.ok(message); + } + +} diff --git a/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java b/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java index ccc2c28..c62d396 100644 --- a/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java +++ b/alerter/src/main/java/com/usthe/alert/pojo/entity/AlertDefine.java @@ -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; diff --git a/alerter/src/main/java/com/usthe/alert/service/AlertDefineService.java b/alerter/src/main/java/com/usthe/alert/service/AlertDefineService.java index 021e51a..b01b768 100644 --- a/alerter/src/main/java/com/usthe/alert/service/AlertDefineService.java +++ b/alerter/src/main/java/com/usthe/alert/service/AlertDefineService.java @@ -67,7 +67,7 @@ public interface AlertDefineService { * @param pageRequest 分页参数 * @return 查询结果 */ - Page getAlertDefines(Specification specification, PageRequest pageRequest); + Page getMonitorBindAlertDefines(Specification specification, PageRequest pageRequest); /** * 应用告警定于与监控关联关系 @@ -83,5 +83,13 @@ public interface AlertDefineService { * @param metrics 指标组 * @return field - define[] */ - Map> getAlertDefines(long monitorId, String app, String metrics); + Map> getMonitorBindAlertDefines(long monitorId, String app, String metrics); + + /** + * 动态条件查询 + * @param specification 查询条件 + * @param pageRequest 分页参数 + * @return 查询结果 + */ + Page getAlertDefines(Specification specification, PageRequest pageRequest); } diff --git a/alerter/src/main/java/com/usthe/alert/service/impl/AlertDefineServiceImpl.java b/alerter/src/main/java/com/usthe/alert/service/impl/AlertDefineServiceImpl.java index c340805..3082ccf 100644 --- a/alerter/src/main/java/com/usthe/alert/service/impl/AlertDefineServiceImpl.java +++ b/alerter/src/main/java/com/usthe/alert/service/impl/AlertDefineServiceImpl.java @@ -68,7 +68,7 @@ public class AlertDefineServiceImpl implements AlertDefineService { } @Override - public Page getAlertDefines(Specification specification, PageRequest pageRequest) { + public Page getMonitorBindAlertDefines(Specification specification, PageRequest pageRequest) { return alertDefineDao.findAll(specification, pageRequest); } @@ -87,7 +87,7 @@ public class AlertDefineServiceImpl implements AlertDefineService { } @Override - public Map> getAlertDefines(long monitorId, String app, String metrics) { + public Map> getMonitorBindAlertDefines(long monitorId, String app, String metrics) { List 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 getAlertDefines(Specification specification, PageRequest pageRequest) { + return alertDefineDao.findAll(specification, pageRequest); + } } diff --git a/alerter/src/main/resources/META-INF/spring.factories b/alerter/src/main/resources/META-INF/spring.factories index fada5b9..30adc51 100644 --- a/alerter/src/main/resources/META-INF/spring.factories +++ b/alerter/src/main/resources/META-INF/spring.factories @@ -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 \ No newline at end of file +com.usthe.alert.controller.AlertsController,\ +com.usthe.alert.controller.AlertDefinesController \ No newline at end of file