MonitorsController.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package com.usthe.manager.controller;
  2. import com.usthe.common.entity.dto.Message;
  3. import com.usthe.common.entity.manager.Monitor;
  4. import com.usthe.manager.service.MonitorService;
  5. import io.swagger.annotations.Api;
  6. import io.swagger.annotations.ApiOperation;
  7. import io.swagger.annotations.ApiParam;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.data.domain.Page;
  10. import org.springframework.data.domain.PageRequest;
  11. import org.springframework.data.domain.Sort;
  12. import org.springframework.data.jpa.domain.Specification;
  13. import org.springframework.http.ResponseEntity;
  14. import org.springframework.web.bind.annotation.DeleteMapping;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PathVariable;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.RestController;
  20. import javax.persistence.criteria.CriteriaBuilder;
  21. import javax.persistence.criteria.Predicate;
  22. import java.util.ArrayList;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
  26. /**
  27. * Monitor and manage batch API
  28. * 监控管理批量API
  29. *
  30. * @author tom
  31. * @date 2021/12/1 20:43
  32. */
  33. @Api(tags = "en: Monitor and manage batch API,zh: 监控列表API")
  34. @RestController
  35. @RequestMapping(path = "/monitors", produces = {APPLICATION_JSON_VALUE})
  36. public class MonitorsController {
  37. private static final byte ALL_MONITOR_STATUS = 9;
  38. @Autowired
  39. private MonitorService monitorService;
  40. @GetMapping
  41. @ApiOperation(value = "Obtain a list of monitoring information based on query filter items",
  42. notes = "根据查询过滤项获取监控信息列表")
  43. public ResponseEntity<Message<Page<Monitor>>> getMonitors(
  44. @ApiParam(value = "en: Monitor ID,zh: 监控ID", example = "6565463543") @RequestParam(required = false) final List<Long> ids,
  45. @ApiParam(value = "en: Monitor Type,zh: 监控类型", example = "linux") @RequestParam(required = false) final String app,
  46. @ApiParam(value = "en: Monitor Name,zh: 监控名称,模糊查询", example = "linux-127.0.0.1") @RequestParam(required = false) final String name,
  47. @ApiParam(value = "en: Monitor Host,zh: 监控Host,模糊查询", example = "127.0.0.1") @RequestParam(required = false) final String host,
  48. @ApiParam(value = "en: Monitor Status,zh: 监控状态 0:未监控,1:可用,2:不可用,3:不可达,4:挂起,9:全部状态", example = "1") @RequestParam(required = false) final Byte status,
  49. @ApiParam(value = "en: Sort Field,default id,zh: 排序字段,默认id", example = "name") @RequestParam(defaultValue = "id") final String sort,
  50. @ApiParam(value = "en: Sort by,zh: 排序方式,asc:升序,desc:降序", example = "desc") @RequestParam(defaultValue = "desc") final String order,
  51. @ApiParam(value = "en: List current page,zh: 列表当前分页", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
  52. @ApiParam(value = "en: Number of list pagination,zh: 列表分页数量", example = "8") @RequestParam(defaultValue = "8") int pageSize) {
  53. Specification<Monitor> specification = (root, query, criteriaBuilder) -> {
  54. List<Predicate> andList = new ArrayList<>();
  55. if (ids != null && !ids.isEmpty()) {
  56. CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
  57. for (long id : ids) {
  58. inPredicate.value(id);
  59. }
  60. andList.add(inPredicate);
  61. }
  62. if (app != null && !"".equals(app)) {
  63. Predicate predicateApp = criteriaBuilder.equal(root.get("app"), app);
  64. andList.add(predicateApp);
  65. }
  66. if (status != null && status >= 0 && status < ALL_MONITOR_STATUS) {
  67. Predicate predicateStatus = criteriaBuilder.equal(root.get("status"), status);
  68. andList.add(predicateStatus);
  69. }
  70. Predicate[] andPredicates = new Predicate[andList.size()];
  71. Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates));
  72. List<Predicate> orList = new ArrayList<>();
  73. if (host != null && !"".equals(host)) {
  74. Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%");
  75. orList.add(predicateHost);
  76. }
  77. if (name != null && !"".equals(name)) {
  78. Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
  79. orList.add(predicateName);
  80. }
  81. Predicate[] orPredicates = new Predicate[orList.size()];
  82. Predicate orPredicate = criteriaBuilder.or(orList.toArray(orPredicates));
  83. if (andPredicate.getExpressions().isEmpty() && orPredicate.getExpressions().isEmpty()) {
  84. return query.where().getRestriction();
  85. } else if (andPredicate.getExpressions().isEmpty()) {
  86. return query.where(orPredicate).getRestriction();
  87. } else if (orPredicate.getExpressions().isEmpty()) {
  88. return query.where(andPredicate).getRestriction();
  89. } else {
  90. return query.where(andPredicate, orPredicate).getRestriction();
  91. }
  92. };
  93. // Pagination is a must 分页是必须的
  94. Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort));
  95. PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
  96. Page<Monitor> monitorPage = monitorService.getMonitors(specification, pageRequest);
  97. Message<Page<Monitor>> message = new Message<>(monitorPage);
  98. return ResponseEntity.ok(message);
  99. }
  100. @GetMapping(path = "/{app}")
  101. @ApiOperation(value = "Filter all acquired monitoring information lists of the specified monitoring type according to the query",
  102. notes = "根据查询过滤指定监控类型的所有获取监控信息列表")
  103. public ResponseEntity<Message<List<Monitor>>> getAppMonitors(
  104. @ApiParam(value = "en: Monitoring type,zh: 监控类型", example = "linux") @PathVariable(required = false) final String app) {
  105. List<Monitor> monitors = monitorService.getAppMonitors(app);
  106. Message<List<Monitor>> message = new Message<>(monitors);
  107. return ResponseEntity.ok(message);
  108. }
  109. @DeleteMapping
  110. @ApiOperation(value = "Delete monitoring items in batches according to the monitoring ID list",
  111. notes = "根据监控ID列表批量删除监控项")
  112. public ResponseEntity<Message<Void>> deleteMonitors(
  113. @ApiParam(value = "en: Monitoring ID List,zh: 监控ID列表", example = "6565463543") @RequestParam(required = false) List<Long> ids
  114. ) {
  115. if (ids != null && !ids.isEmpty()) {
  116. monitorService.deleteMonitors(new HashSet<>(ids));
  117. }
  118. Message<Void> message = new Message<>();
  119. return ResponseEntity.ok(message);
  120. }
  121. @DeleteMapping("manage")
  122. @ApiOperation(value = "Unmanaged monitoring items in batches according to the monitoring ID list",
  123. notes = "根据监控ID列表批量取消纳管监控项")
  124. public ResponseEntity<Message<Void>> cancelManageMonitors(
  125. @ApiParam(value = "en: Monitoring ID List,zh: 监控ID列表", example = "6565463543") @RequestParam(required = false) List<Long> ids
  126. ) {
  127. if (ids != null && !ids.isEmpty()) {
  128. monitorService.cancelManageMonitors(new HashSet<>(ids));
  129. }
  130. Message<Void> message = new Message<>();
  131. return ResponseEntity.ok(message);
  132. }
  133. @GetMapping("manage")
  134. @ApiOperation(value = "Start the managed monitoring items in batches according to the monitoring ID list",
  135. notes = "根据监控ID列表批量启动纳管监控项")
  136. public ResponseEntity<Message<Void>> enableManageMonitors(
  137. @ApiParam(value = "en: Monitor ID List,zh: 监控ID列表", example = "6565463543") @RequestParam(required = false) List<Long> ids
  138. ) {
  139. if (ids != null && !ids.isEmpty()) {
  140. monitorService.enableManageMonitors(new HashSet<>(ids));
  141. }
  142. Message<Void> message = new Message<>();
  143. return ResponseEntity.ok(message);
  144. }
  145. }