Files
hertzbeat/alerter/src/main/java/com/usthe/alert/controller/AlertsController.java
2021-12-10 22:21:53 +08:00

87 lines
4.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<Message<Page<Alert>>> getAlerts(
@ApiParam(value = "告警ID", example = "6565466456") @RequestParam(required = false) List<Long> 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<Alert> 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 (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<Alert> alertPage = alertService.getAlerts(specification, pageRequest);
Message<Page<Alert>> message = new Message<>(alertPage);
return ResponseEntity.ok(message);
}
}