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 aee969a18 [refactor] move code from AlertDefinesController to 
AlertDefineService (#2429)
aee969a18 is described below

commit aee969a18a00d8294e54331d042659e57d624fb3
Author: kangli <[email protected]>
AuthorDate: Fri Aug 2 23:15:45 2024 +0800

    [refactor] move code from AlertDefinesController to AlertDefineService 
(#2429)
    
    Co-authored-by: Calvin <[email protected]>
---
 .../alert/controller/AlertDefinesController.java   | 53 +---------------------
 .../alert/service/AlertDefineService.java          | 11 +++--
 .../alert/service/impl/AlertDefineServiceImpl.java | 49 +++++++++++++++++++-
 .../controller/AlertDefinesControllerTest.java     | 26 +++++------
 .../alert/service/AlertDefineServiceTest.java      |  9 ++--
 5 files changed, 76 insertions(+), 72 deletions(-)

diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
index 2150f0680..7579c05df 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertDefinesController.java
@@ -21,10 +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.Predicate;
 import jakarta.servlet.http.HttpServletResponse;
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import org.apache.hertzbeat.alert.service.AlertDefineService;
@@ -32,11 +29,7 @@ import 
org.apache.hertzbeat.common.entity.alerter.AlertDefine;
 import org.apache.hertzbeat.common.entity.dto.Message;
 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.PostMapping;
@@ -67,51 +60,7 @@ public class AlertDefinesController {
             @Parameter(description = "Sort mode: asc: ascending, desc: 
descending", example = "desc") @RequestParam(defaultValue = "desc") String 
order,
             @Parameter(description = "List current page", example = "0") 
@RequestParam(defaultValue = "0") int pageIndex,
             @Parameter(description = "Number of list pages", example = "8") 
@RequestParam(defaultValue = "8") int pageSize) {
-
-        Specification<AlertDefine> 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(search)) {
-                Predicate predicate = criteriaBuilder.or(
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("app")),
-                                "%" + search.toLowerCase() + "%"
-                        ),
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("metric")),
-                                "%" + search.toLowerCase() + "%"
-                        ),
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("field")),
-                                "%" + search.toLowerCase() + "%"
-                        ),
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("expr")),
-                                "%" + search.toLowerCase() + "%"
-                        ),
-                        criteriaBuilder.like(
-                                criteriaBuilder.lower(root.get("template")),
-                                "%" + search.toLowerCase() + "%"
-                        )
-                );
-                andList.add(predicate);
-            }
-            if (priority != null) {
-                Predicate predicate = 
criteriaBuilder.equal(root.get("priority"), priority);
-                andList.add(predicate);
-            }
-            Predicate[] predicates = new Predicate[andList.size()];
-            return criteriaBuilder.and(andList.toArray(predicates));
-        };
-        Sort sortExp = Sort.by(new 
Sort.Order(Sort.Direction.fromString(order), sort));
-        PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
-        Page<AlertDefine> alertDefinePage = 
alertDefineService.getAlertDefines(specification, pageRequest);
+        Page<AlertDefine> alertDefinePage = 
alertDefineService.getAlertDefines(ids, search, priority, sort, order, 
pageIndex, pageSize);
         return ResponseEntity.ok(Message.success(alertDefinePage));
     }
 
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertDefineService.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertDefineService.java
index 74319167a..dabad05a3 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertDefineService.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertDefineService.java
@@ -113,11 +113,16 @@ public interface AlertDefineService {
 
     /**
      * Dynamic conditional query
-     * @param specification Query conditions 
-     * @param pageRequest Paging parameters
+     * @param defineIds     Alarm Definition ID List
+     * @param search        Search-Target Expr Template
+     * @param priority      Alarm Definition Severity
+     * @param sort          Sort field
+     * @param order         Sort mode: asc: ascending, desc: descending
+     * @param pageIndex     List current page
+     * @param pageSize      Number of list pages
      * @return The query results 
      */
-    Page<AlertDefine> getAlertDefines(Specification<AlertDefine> 
specification, PageRequest pageRequest);
+    Page<AlertDefine> getAlertDefines(List<Long> defineIds, String search, 
Byte priority, String sort, String order, int pageIndex, int pageSize);
 
     /**
      * Query the associated monitoring list information based on the alarm 
definition ID
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java
index b29add88a..624df4334 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java
@@ -17,9 +17,12 @@
 
 package org.apache.hertzbeat.alert.service.impl;
 
+import jakarta.persistence.criteria.CriteriaBuilder;
+import jakarta.persistence.criteria.Predicate;
 import jakarta.servlet.http.HttpServletResponse;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
@@ -41,6 +44,7 @@ import org.apache.hertzbeat.common.util.JexlExpressionRunner;
 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.HttpHeaders;
 import org.springframework.stereotype.Service;
@@ -147,7 +151,50 @@ public class AlertDefineServiceImpl implements 
AlertDefineService {
     }
 
     @Override
-    public Page<AlertDefine> getAlertDefines(Specification<AlertDefine> 
specification, PageRequest pageRequest) {
+    public Page<AlertDefine> getAlertDefines(List<Long> defineIds, String 
search, Byte priority, String sort, String order, int pageIndex, int pageSize) {
+        Specification<AlertDefine> specification = (root, query, 
criteriaBuilder) -> {
+            List<Predicate> andList = new ArrayList<>();
+            if (defineIds != null && !defineIds.isEmpty()) {
+                CriteriaBuilder.In<Long> inPredicate = 
criteriaBuilder.in(root.get("id"));
+                for (long id : defineIds) {
+                    inPredicate.value(id);
+                }
+                andList.add(inPredicate);
+            }
+            if (StringUtils.hasText(search)) {
+                Predicate predicate = criteriaBuilder.or(
+                        criteriaBuilder.like(
+                                criteriaBuilder.lower(root.get("app")),
+                                "%" + search.toLowerCase() + "%"
+                        ),
+                        criteriaBuilder.like(
+                                criteriaBuilder.lower(root.get("metric")),
+                                "%" + search.toLowerCase() + "%"
+                        ),
+                        criteriaBuilder.like(
+                                criteriaBuilder.lower(root.get("field")),
+                                "%" + search.toLowerCase() + "%"
+                        ),
+                        criteriaBuilder.like(
+                                criteriaBuilder.lower(root.get("expr")),
+                                "%" + search.toLowerCase() + "%"
+                        ),
+                        criteriaBuilder.like(
+                                criteriaBuilder.lower(root.get("template")),
+                                "%" + search.toLowerCase() + "%"
+                        )
+                );
+                andList.add(predicate);
+            }
+            if (priority != null) {
+                Predicate predicate = 
criteriaBuilder.equal(root.get("priority"), priority);
+                andList.add(predicate);
+            }
+            Predicate[] predicates = new Predicate[andList.size()];
+            return criteriaBuilder.and(andList.toArray(predicates));
+        };
+        Sort sortExp = Sort.by(new 
Sort.Order(Sort.Direction.fromString(order), sort));
+        PageRequest pageRequest = PageRequest.of(pageIndex, pageSize, sortExp);
         return alertDefineDao.findAll(specification, pageRequest);
     }
 
diff --git 
a/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertDefinesControllerTest.java
 
b/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertDefinesControllerTest.java
index 62ce3fb82..7f9ef2c25 100644
--- 
a/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertDefinesControllerTest.java
+++ 
b/alerter/src/test/java/org/apache/hertzbeat/alert/controller/AlertDefinesControllerTest.java
@@ -98,20 +98,20 @@ class AlertDefinesControllerTest {
 
         // Test the correctness of the mock
         // Although objects cannot be mocked, stubs can be stored using class 
files
-//        
Mockito.when(alertDefineService.getAlertDefines(Mockito.any(Specification.class),
 Mockito.argThat(new ArgumentMatcher<PageRequest>() {
-//            @Override
-//            public boolean matches(PageRequest pageRequestMidden) {
-//                // There are three methods in the source code that need to 
be compared, namely getPageNumber(), getPageSize(), getSort()
-//                if(pageRequestMidden.getPageSize() == 
pageRequest.getPageSize() &&
-//                        pageRequestMidden.getPageNumber() == 
pageRequest.getPageNumber() &&
-//                        
pageRequestMidden.getSort().equals(pageRequest.getSort())) {
-//                    return true;
-//                }
-//                return false;
-//            }
-//        }))).thenReturn(new PageImpl<AlertDefine>(new 
ArrayList<AlertDefine>()));
+        //        
Mockito.when(alertDefineService.getAlertDefines(Mockito.any(Specification.class),
 Mockito.argThat(new ArgumentMatcher<PageRequest>() {
+        //            @Override
+        //            public boolean matches(PageRequest pageRequestMidden) {
+        //                // There are three methods in the source code that 
need to be compared, namely getPageNumber(), getPageSize(), getSort()
+        //                if(pageRequestMidden.getPageSize() == 
pageRequest.getPageSize() &&
+        //                        pageRequestMidden.getPageNumber() == 
pageRequest.getPageNumber() &&
+        //                        
pageRequestMidden.getSort().equals(pageRequest.getSort())) {
+        //                    return true;
+        //                }
+        //                return false;
+        //            }
+        //        }))).thenReturn(new PageImpl<AlertDefine>(new 
ArrayList<AlertDefine>()));
         AlertDefine define = 
AlertDefine.builder().id(9L).app("linux").metric("disk").field("usage").expr("x").times(1).tags(new
 LinkedList<>()).build();
-        Mockito.when(alertDefineService.getAlertDefines(Mockito.any(), 
Mockito.any())).thenReturn(new PageImpl<>(Collections.singletonList(define)));
+        Mockito.when(alertDefineService.getAlertDefines(null, null, null, 
"id", "desc", 1, 10)).thenReturn(new 
PageImpl<>(Collections.singletonList(define)));
 
         mockMvc.perform(MockMvcRequestBuilders.get(
                 "/api/alert/defines")
diff --git 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineServiceTest.java
 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineServiceTest.java
index b3b317db7..549f98032 100644
--- 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineServiceTest.java
+++ 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineServiceTest.java
@@ -20,10 +20,13 @@ package org.apache.hertzbeat.alert.service;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 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.anySet;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -176,9 +179,9 @@ class AlertDefineServiceTest {
 
     @Test
     void getAlertDefines() {
-        Specification<AlertDefine> specification = mock(Specification.class);
-        when(alertDefineDao.findAll(specification, PageRequest.of(1, 
1))).thenReturn(Page.empty());
-        assertNotNull(alertDefineService.getAlertDefines(specification, 
PageRequest.of(1, 1)));
+        when(alertDefineDao.findAll(any(Specification.class), 
any(PageRequest.class))).thenReturn(Page.empty());
+        assertNotNull(alertDefineService.getAlertDefines(null, null, null, 
"id", "desc", 1, 10));
+        verify(alertDefineDao, times(1)).findAll(any(Specification.class), 
any(PageRequest.class));
     }
 
     @Test


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

Reply via email to