diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java index 46e1129b3d7..6bbe0a270fb 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/controller/MonitorsController.java @@ -21,12 +21,7 @@ import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.persistence.criteria.CriteriaBuilder; -import jakarta.persistence.criteria.JoinType; -import jakarta.persistence.criteria.ListJoin; -import jakarta.persistence.criteria.Predicate; import jakarta.servlet.http.HttpServletResponse; -import java.util.ArrayList; import java.util.HashSet; import java.util.List; import org.apache.hertzbeat.common.entity.dto.Message; @@ -34,11 +29,7 @@ import org.apache.hertzbeat.manager.service.MonitorService; 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.util.StringUtils; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -56,10 +47,6 @@ @RequestMapping(path = "/api/monitors", produces = {APPLICATION_JSON_VALUE}) public class MonitorsController { - private static final byte ALL_MONITOR_STATUS = 9; - - private static final int TAG_LENGTH = 2; - @Autowired private MonitorService monitorService; @@ -77,69 +64,8 @@ public ResponseEntity>> getMonitors( @Parameter(description = "List current page", example = "0") @RequestParam(defaultValue = "0") int pageIndex, @Parameter(description = "Number of list pagination ", example = "8") @RequestParam(defaultValue = "8") int pageSize, @Parameter(description = "Monitor tag ", example = "env:prod") @RequestParam(required = false) final String tag) { - Specification specification = (root, query, criteriaBuilder) -> { - List andList = new ArrayList<>(); - if (ids != null && !ids.isEmpty()) { - CriteriaBuilder.In inPredicate = criteriaBuilder.in(root.get("id")); - for (long id : ids) { - inPredicate.value(id); - } - andList.add(inPredicate); - } - if (StringUtils.hasText(app)) { - Predicate predicateApp = criteriaBuilder.equal(root.get("app"), app); - andList.add(predicateApp); - } - if (status != null && status >= 0 && status < ALL_MONITOR_STATUS) { - Predicate predicateStatus = criteriaBuilder.equal(root.get("status"), status); - andList.add(predicateStatus); - } - - if (StringUtils.hasText(tag)) { - String[] tagArr = tag.split(":"); - String tagName = tagArr[0]; - ListJoin tagJoin = root - .join(root.getModel() - .getList("tags", org.apache.hertzbeat.common.entity.manager.Tag.class), JoinType.LEFT); - if (tagArr.length == TAG_LENGTH) { - String tagValue = tagArr[1]; - andList.add(criteriaBuilder.equal(tagJoin.get("name"), tagName)); - andList.add(criteriaBuilder.equal(tagJoin.get("tagValue"), tagValue)); - } else { - andList.add(criteriaBuilder.equal(tagJoin.get("name"), tag)); - } - } - Predicate[] andPredicates = new Predicate[andList.size()]; - Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates)); - - List orList = new ArrayList<>(); - if (StringUtils.hasText(host)) { - Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%"); - orList.add(predicateHost); - } - if (StringUtils.hasText(name)) { - Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%"); - orList.add(predicateName); - } - Predicate[] orPredicates = new Predicate[orList.size()]; - Predicate orPredicate = criteriaBuilder.or(orList.toArray(orPredicates)); - - if (andPredicates.length == 0 && orPredicates.length == 0) { - return query.where().getRestriction(); - } else if (andPredicates.length == 0) { - return orPredicate; - } else if (orPredicates.length == 0) { - return andPredicate; - } else { - return query.where(andPredicate, orPredicate).getRestriction(); - } - }; - // Pagination is a must - Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort)); - PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp); - Page monitorPage = monitorService.getMonitors(specification, pageRequest); - Message> message = Message.success(monitorPage); - return ResponseEntity.ok(message); + Page monitorPage = monitorService.getMonitors(ids, app, name, host, status, sort, order, pageIndex, pageSize, tag); + return ResponseEntity.ok(Message.success(monitorPage)); } @GetMapping(path = "/{app}") @@ -147,9 +73,7 @@ public ResponseEntity>> getMonitors( description = "Filter all acquired monitoring information lists of the specified monitoring type according to the query") public ResponseEntity>> getAppMonitors( @Parameter(description = "en: Monitoring type", example = "linux") @PathVariable(required = false) final String app) { - List monitors = monitorService.getAppMonitors(app); - Message> message = Message.success(monitors); - return ResponseEntity.ok(message); + return ResponseEntity.ok(Message.success(monitorService.getAppMonitors(app))); } @@ -162,8 +86,7 @@ public ResponseEntity> deleteMonitors( if (ids != null && !ids.isEmpty()) { monitorService.deleteMonitors(new HashSet<>(ids)); } - Message message = Message.success(); - return ResponseEntity.ok(message); + return ResponseEntity.ok(Message.success()); } @DeleteMapping("manage") @@ -175,8 +98,7 @@ public ResponseEntity> cancelManageMonitors( if (ids != null && !ids.isEmpty()) { monitorService.cancelManageMonitors(new HashSet<>(ids)); } - Message message = Message.success(); - return ResponseEntity.ok(message); + return ResponseEntity.ok(Message.success()); } @GetMapping("manage") @@ -188,8 +110,7 @@ public ResponseEntity> enableManageMonitors( if (ids != null && !ids.isEmpty()) { monitorService.enableManageMonitors(new HashSet<>(ids)); } - Message message = Message.success(); - return ResponseEntity.ok(message); + return ResponseEntity.ok(Message.success()); } @GetMapping("/export") diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/MonitorService.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/MonitorService.java index 1657b7a4da8..0a3007e868b 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/MonitorService.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/MonitorService.java @@ -28,8 +28,6 @@ import org.apache.hertzbeat.manager.pojo.dto.MonitorDto; import org.apache.hertzbeat.manager.support.exception.MonitorDetectException; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.jpa.domain.Specification; import org.springframework.web.multipart.MultipartFile; /** @@ -96,11 +94,19 @@ public interface MonitorService { /** * Dynamic conditional query - * @param specification Query conditions - * @param pageRequest Pagination parameters + * @param monitorIds Monitor ID List + * @param app Monitor Type + * @param name Monitor Name support fuzzy query + * @param host Monitor Host support fuzzy query + * @param status Monitor Status 0:no monitor,1:usable,2:disabled,9:all status + * @param sort Sort Field + * @param order Sort mode eg:asc desc + * @param pageIndex List current page + * @param pageSize Number of list pagination + * @param tag Monitor tag * @return Search Result */ - Page getMonitors(Specification specification, PageRequest pageRequest); + Page getMonitors(List monitorIds, String app, String name, String host, Byte status, String sort, String order, int pageIndex, int pageSize, String tag); /** * Unmanaged monitoring items in batches according to the monitoring ID list diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java index 2684ea1910c..4cd7c888dac 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -18,10 +18,15 @@ package org.apache.hertzbeat.manager.service.impl; import com.fasterxml.jackson.core.type.TypeReference; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.JoinType; +import jakarta.persistence.criteria.ListJoin; +import jakarta.persistence.criteria.Predicate; import jakarta.servlet.http.HttpServletResponse; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -71,6 +76,7 @@ import org.springframework.context.ApplicationContext; 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.HttpHeaders; import org.springframework.stereotype.Service; @@ -95,6 +101,10 @@ public class MonitorServiceImpl implements MonitorService { public static final String PATTERN_HTTP = "(?i)http://"; public static final String PATTERN_HTTPS = "(?i)https://"; + private static final byte ALL_MONITOR_STATUS = 9; + + private static final int TAG_LENGTH = 2; + @Autowired private AppService appService; @@ -642,7 +652,67 @@ public MonitorDto getMonitorDto(long id) throws RuntimeException { } @Override - public Page getMonitors(Specification specification, PageRequest pageRequest) { + public Page getMonitors(List monitorIds, String app, String name, String host, Byte status, String sort, String order, int pageIndex, int pageSize, String tag) { + Specification specification = (root, query, criteriaBuilder) -> { + List andList = new ArrayList<>(); + if (monitorIds != null && !monitorIds.isEmpty()) { + CriteriaBuilder.In inPredicate = criteriaBuilder.in(root.get("id")); + for (long id : monitorIds) { + inPredicate.value(id); + } + andList.add(inPredicate); + } + if (StringUtils.hasText(app)) { + Predicate predicateApp = criteriaBuilder.equal(root.get("app"), app); + andList.add(predicateApp); + } + if (status != null && status >= 0 && status < ALL_MONITOR_STATUS) { + Predicate predicateStatus = criteriaBuilder.equal(root.get("status"), status); + andList.add(predicateStatus); + } + + if (StringUtils.hasText(tag)) { + String[] tagArr = tag.split(":"); + String tagName = tagArr[0]; + ListJoin tagJoin = root + .join(root.getModel() + .getList("tags", org.apache.hertzbeat.common.entity.manager.Tag.class), JoinType.LEFT); + if (tagArr.length == TAG_LENGTH) { + String tagValue = tagArr[1]; + andList.add(criteriaBuilder.equal(tagJoin.get("name"), tagName)); + andList.add(criteriaBuilder.equal(tagJoin.get("tagValue"), tagValue)); + } else { + andList.add(criteriaBuilder.equal(tagJoin.get("name"), tag)); + } + } + Predicate[] andPredicates = new Predicate[andList.size()]; + Predicate andPredicate = criteriaBuilder.and(andList.toArray(andPredicates)); + + List orList = new ArrayList<>(); + if (StringUtils.hasText(host)) { + Predicate predicateHost = criteriaBuilder.like(root.get("host"), "%" + host + "%"); + orList.add(predicateHost); + } + if (StringUtils.hasText(name)) { + Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%"); + orList.add(predicateName); + } + Predicate[] orPredicates = new Predicate[orList.size()]; + Predicate orPredicate = criteriaBuilder.or(orList.toArray(orPredicates)); + + if (andPredicates.length == 0 && orPredicates.length == 0) { + return query.where().getRestriction(); + } else if (andPredicates.length == 0) { + return orPredicate; + } else if (orPredicates.length == 0) { + return andPredicate; + } else { + return query.where(andPredicate, orPredicate).getRestriction(); + } + }; + // Pagination is a must + Sort sortExp = Sort.by(new Sort.Order(Sort.Direction.fromString(order), sort)); + PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp); return monitorDao.findAll(specification, pageRequest); } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java index 5956619773f..fcb91ae6cbd 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java @@ -21,8 +21,9 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.any; import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.reset; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; @@ -652,9 +653,8 @@ void getMonitorDto() { @Test void getMonitors() { - Specification specification = mock(Specification.class); - when(monitorDao.findAll(specification, PageRequest.of(1, 1))).thenReturn(Page.empty()); - assertNotNull(monitorService.getMonitors(specification, PageRequest.of(1, 1))); + doReturn(Page.empty()).when(monitorDao).findAll(any(Specification.class), any(PageRequest.class)); + assertNotNull(monitorService.getMonitors(null, null, null, null, null, "gmtCreate", "desc", 1, 1, null)); } @Test