From cb8b3fd70460e237a6c1d456656f74c1fd6f31cb Mon Sep 17 00:00:00 2001 From: tomsun28 Date: Fri, 10 Dec 2021 22:21:53 +0800 Subject: [PATCH] =?UTF-8?q?[alerter]=20=E5=91=8A=E8=AD=A6=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alert/controller/AlertsController.java | 86 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 alerter/src/main/java/com/usthe/alert/controller/AlertsController.java diff --git a/alerter/src/main/java/com/usthe/alert/controller/AlertsController.java b/alerter/src/main/java/com/usthe/alert/controller/AlertsController.java new file mode 100644 index 0000000..8a0ff82 --- /dev/null +++ b/alerter/src/main/java/com/usthe/alert/controller/AlertsController.java @@ -0,0 +1,86 @@ +package com.usthe.alert.controller; + +import com.usthe.alert.pojo.entity.Alert; +import com.usthe.alert.service.AlertService; +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.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.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 = "/alerts", produces = {APPLICATION_JSON_VALUE}) +public class AlertsController { + + @Autowired + private AlertService alertService; + + @GetMapping + @ApiOperation(value = "查询告警列表", notes = "根据查询过滤项获取告警信息列表") + public ResponseEntity>> getAlerts( + @ApiParam(value = "告警ID", example = "6565466456") @RequestParam(required = false) List ids, + @ApiParam(value = "告警监控对象ID", example = "6565463543") @RequestParam(required = false) Long monitorId, + @ApiParam(value = "告警级别", example = "6565463543") @RequestParam(required = false) Byte priority, + @ApiParam(value = "告警状态", example = "6565463543") @RequestParam(required = false) Byte status, + @ApiParam(value = "告警内容模糊查询", example = "linux") @RequestParam(required = false) String content, + @ApiParam(value = "排序字段,默认id", example = "name") @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 (monitorId != null) { + Predicate predicateApp = criteriaBuilder.equal(root.get("monitorId"), monitorId); + predicate = criteriaBuilder.and(predicateApp); + } + if (priority != null) { + Predicate predicateApp = criteriaBuilder.equal(root.get("priority"), priority); + predicate = criteriaBuilder.and(predicateApp); + } + if (status != null) { + Predicate predicateApp = criteriaBuilder.equal(root.get("status"), status); + predicate = criteriaBuilder.and(predicateApp); + } + if (content != null && !"".equals(content)) { + Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + content + "%"); + predicate = criteriaBuilder.and(predicateContent); + } + return predicate; + }; + Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort)); + PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp); + Page alertPage = alertService.getAlerts(specification, pageRequest); + Message> message = new Message<>(alertPage); + return ResponseEntity.ok(message); + } + +} diff --git a/alerter/src/main/resources/META-INF/spring.factories b/alerter/src/main/resources/META-INF/spring.factories index 83be499..fada5b9 100644 --- a/alerter/src/main/resources/META-INF/spring.factories +++ b/alerter/src/main/resources/META-INF/spring.factories @@ -7,4 +7,5 @@ com.usthe.alert.AlerterProperties,\ com.usthe.alert.AlerterDataQueue,\ com.usthe.alert.AlerterConfiguration,\ com.usthe.alert.entrance.KafkaDataConsume,\ -com.usthe.alert.calculate.CalculateAlarm \ No newline at end of file +com.usthe.alert.calculate.CalculateAlarm,\ +com.usthe.alert.controller.AlertsController \ No newline at end of file