Calvin979 commented on code in PR #2415:
URL: https://github.com/apache/hertzbeat/pull/2415#discussion_r1697227430


##########
manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java:
##########
@@ -642,7 +652,69 @@ public MonitorDto getMonitorDto(long id) throws 
RuntimeException {
     }
 
     @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
+        order = order == null ? "desc" : order;
+        sort = sort == null ? "gmtCreate" : sort;

Review Comment:
   These two lines can be removed once you correct your test case to match 
param list.



##########
manager/src/test/java/org/apache/hertzbeat/manager/service/MonitorServiceTest.java:
##########
@@ -652,9 +653,8 @@ void getMonitorDto() {
 
     @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, 
null, null, 1, 1, null));

Review Comment:
   Seems this test case is not match param list of `getMonitors`. Some params 
have a default value.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to