This is an automated email from the ASF dual-hosted git repository.
zhengqiwei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 181ca5aa5 [refactor] move code from MonitorsController to
MonitorService (#2415)
181ca5aa5 is described below
commit 181ca5aa5e6ba67cfe7bab84c0c2222d08c6269f
Author: kangli <[email protected]>
AuthorDate: Thu Aug 1 00:01:11 2024 +0800
[refactor] move code from MonitorsController to MonitorService (#2415)
Co-authored-by: Calvin <[email protected]>
---
.../manager/controller/MonitorsController.java | 91 ++--------------------
.../hertzbeat/manager/service/MonitorService.java | 16 ++--
.../manager/service/impl/MonitorServiceImpl.java | 72 ++++++++++++++++-
.../manager/service/MonitorServiceTest.java | 8 +-
4 files changed, 92 insertions(+), 95 deletions(-)
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 46e1129b3..6bbe0a270 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 static
org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
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.common.entity.manager.Monitor;
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 @@ import org.springframework.web.multipart.MultipartFile;
@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 class MonitorsController {
@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<Monitor> specification = (root, query, criteriaBuilder)
-> {
- List<Predicate> andList = new ArrayList<>();
- if (ids != null && !ids.isEmpty()) {
- CriteriaBuilder.In<Long> 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<Monitor,
org.apache.hertzbeat.common.entity.manager.Tag> 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<Predicate> 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<Monitor> monitorPage = monitorService.getMonitors(specification,
pageRequest);
- Message<Page<Monitor>> message = Message.success(monitorPage);
- return ResponseEntity.ok(message);
+ Page<Monitor> 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 class MonitorsController {
description = "Filter all acquired monitoring information lists of
the specified monitoring type according to the query")
public ResponseEntity<Message<List<Monitor>>> getAppMonitors(
@Parameter(description = "en: Monitoring type", example = "linux")
@PathVariable(required = false) final String app) {
- List<Monitor> monitors = monitorService.getAppMonitors(app);
- Message<List<Monitor>> message = Message.success(monitors);
- return ResponseEntity.ok(message);
+ return
ResponseEntity.ok(Message.success(monitorService.getAppMonitors(app)));
}
@@ -162,8 +86,7 @@ public class MonitorsController {
if (ids != null && !ids.isEmpty()) {
monitorService.deleteMonitors(new HashSet<>(ids));
}
- Message<Void> message = Message.success();
- return ResponseEntity.ok(message);
+ return ResponseEntity.ok(Message.success());
}
@DeleteMapping("manage")
@@ -175,8 +98,7 @@ public class MonitorsController {
if (ids != null && !ids.isEmpty()) {
monitorService.cancelManageMonitors(new HashSet<>(ids));
}
- Message<Void> message = Message.success();
- return ResponseEntity.ok(message);
+ return ResponseEntity.ok(Message.success());
}
@GetMapping("manage")
@@ -188,8 +110,7 @@ public class MonitorsController {
if (ids != null && !ids.isEmpty()) {
monitorService.enableManageMonitors(new HashSet<>(ids));
}
- Message<Void> 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 1657b7a4d..0a3007e86 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.AppCount;
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<Monitor> getMonitors(Specification<Monitor> specification,
PageRequest pageRequest);
+ Page<Monitor> getMonitors(List<Long> 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 2684ea191..4cd7c888d 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.beans.factory.annotation.Autowired;
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 class MonitorServiceImpl implements MonitorService {
}
@Override
- public Page<Monitor> getMonitors(Specification<Monitor> specification,
PageRequest pageRequest) {
+ public Page<Monitor> getMonitors(List<Long> monitorIds, String app, String
name, String host, Byte status, String sort, String order, int pageIndex, int
pageSize, String tag) {
+ Specification<Monitor> specification = (root, query, criteriaBuilder)
-> {
+ List<Predicate> andList = new ArrayList<>();
+ if (monitorIds != null && !monitorIds.isEmpty()) {
+ CriteriaBuilder.In<Long> 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<Monitor, Tag> 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<Predicate> 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 595661977..fcb91ae6c 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.assertDoesNotThrow;
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 @@ class MonitorServiceTest {
@Test
void getMonitors() {
- Specification<Monitor> 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
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]