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 111f0f6b9 [refactor] move code from AlertSilencesController to 
AlertSilenceService (#2436)
111f0f6b9 is described below

commit 111f0f6b99f607ed6f25a62fbf0c4c6cd1107493
Author: kangli <[email protected]>
AuthorDate: Tue Aug 6 00:03:43 2024 +0800

    [refactor] move code from AlertSilencesController to AlertSilenceService 
(#2436)
    
    Co-authored-by: Calvin <[email protected]>
---
 .../alert/controller/AlertSilencesController.java  | 36 ++--------------------
 .../alert/service/AlertSilenceService.java         | 13 +++++---
 .../service/impl/AlertSilenceServiceImpl.java      | 31 ++++++++++++++++++-
 .../alert/service/AlertSilenceServiceTest.java     | 30 ++++++++++++------
 4 files changed, 60 insertions(+), 50 deletions(-)

diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilencesController.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilencesController.java
index 60fa156ea..1b209b109 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilencesController.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/controller/AlertSilencesController.java
@@ -21,9 +21,6 @@ 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 java.util.ArrayList;
 import java.util.HashSet;
 import java.util.List;
 import org.apache.hertzbeat.alert.service.AlertSilenceService;
@@ -31,11 +28,7 @@ import 
org.apache.hertzbeat.common.entity.alerter.AlertSilence;
 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.RequestMapping;
@@ -63,33 +56,8 @@ public class AlertSilencesController {
             @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<AlertSilence> 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("name")),
-                                "%" + search.toLowerCase() + "%"
-                        )
-                );
-                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<AlertSilence> alertSilencePage = 
alertSilenceService.getAlertSilences(specification, pageRequest);
-        Message<Page<AlertSilence>> message = 
Message.success(alertSilencePage);
-        return ResponseEntity.ok(message);
+        Page<AlertSilence> alertSilencePage = 
alertSilenceService.getAlertSilences(ids, search, sort, order, pageIndex, 
pageSize);
+        return ResponseEntity.ok(Message.success(alertSilencePage));
     }
 
     @DeleteMapping
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertSilenceService.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertSilenceService.java
index 3e7e114e4..36846b6b4 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertSilenceService.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/AlertSilenceService.java
@@ -17,11 +17,10 @@
 
 package org.apache.hertzbeat.alert.service;
 
+import java.util.List;
 import java.util.Set;
 import org.apache.hertzbeat.common.entity.alerter.AlertSilence;
 import org.springframework.data.domain.Page;
-import org.springframework.data.domain.PageRequest;
-import org.springframework.data.jpa.domain.Specification;
 
 /**
  * management interface service for alert silence
@@ -67,9 +66,13 @@ public interface AlertSilenceService {
 
     /**
      * Dynamic conditional query
-     * @param specification Query conditions
-     * @param pageRequest   Paging parameters
+     * @param silenceIds    Alarm Silence ID
+     * @param search        Search Name
+     * @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<AlertSilence> getAlertSilences(Specification<AlertSilence> 
specification, PageRequest pageRequest);
+    Page<AlertSilence> getAlertSilences(List<Long> silenceIds, String search, 
String sort, String order, int pageIndex, int pageSize);
 }
diff --git 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java
 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java
index a8a347af5..9a960863a 100644
--- 
a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java
+++ 
b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertSilenceServiceImpl.java
@@ -17,7 +17,11 @@
 
 package org.apache.hertzbeat.alert.service.impl;
 
+import jakarta.persistence.criteria.CriteriaBuilder;
+import jakarta.persistence.criteria.Predicate;
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 import java.util.Set;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.hertzbeat.alert.dao.AlertSilenceDao;
@@ -29,9 +33,11 @@ import 
org.apache.hertzbeat.common.entity.alerter.AlertSilence;
 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.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.StringUtils;
 
 /**
  * management interface service implement for alert silence
@@ -77,7 +83,30 @@ public class AlertSilenceServiceImpl implements 
AlertSilenceService {
     }
 
     @Override
-    public Page<AlertSilence> getAlertSilences(Specification<AlertSilence> 
specification, PageRequest pageRequest) {
+    public Page<AlertSilence> getAlertSilences(List<Long> silenceIds, String 
search, String sort, String order, int pageIndex, int pageSize) {
+        Specification<AlertSilence> specification = (root, query, 
criteriaBuilder) -> {
+            List<Predicate> andList = new ArrayList<>();
+            if (silenceIds != null && !silenceIds.isEmpty()) {
+                CriteriaBuilder.In<Long> inPredicate = 
criteriaBuilder.in(root.get("id"));
+                for (long id : silenceIds) {
+                    inPredicate.value(id);
+                }
+                andList.add(inPredicate);
+            }
+            if (StringUtils.hasText(search)) {
+                Predicate predicate = criteriaBuilder.or(
+                        criteriaBuilder.like(
+                                criteriaBuilder.lower(root.get("name")),
+                                "%" + search.toLowerCase() + "%"
+                        )
+                );
+                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 alertSilenceDao.findAll(specification, pageRequest);
     }
 
diff --git 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertSilenceServiceTest.java
 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertSilenceServiceTest.java
index 93cff97be..2915b2875 100644
--- 
a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertSilenceServiceTest.java
+++ 
b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertSilenceServiceTest.java
@@ -17,9 +17,16 @@
 
 package org.apache.hertzbeat.alert.service;
 
+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.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 import java.util.Optional;
 import java.util.Set;
-
 import org.apache.hertzbeat.alert.dao.AlertSilenceDao;
 import org.apache.hertzbeat.alert.service.impl.AlertSilenceServiceImpl;
 import org.apache.hertzbeat.common.entity.alerter.AlertSilence;
@@ -28,15 +35,9 @@ import org.junit.jupiter.api.Test;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-
-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.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyLong;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.jpa.domain.Specification;
 
 /**
  * test case for {@link AlertSilenceServiceImpl}
@@ -106,6 +107,15 @@ class AlertSilenceServiceTest {
                verify(alertSilenceDao, times(1)).findById(1L);
        }
 
+       @Test
+       void testGetAlertSilences() {
+               when(alertSilenceDao.findAll(any(Specification.class), 
any(PageRequest.class))).thenReturn(Page.empty());
+               assertDoesNotThrow(() -> 
alertSilenceService.getAlertSilences(null, null, "id", "desc", 1, 10));
+               verify(alertSilenceDao, 
times(1)).findAll(any(Specification.class), any(PageRequest.class));
+
+               assertNotNull(alertSilenceService.getAlertSilences(null, null, 
"id", "desc", 1, 10));
+       }
+
        @Test
        void testDeleteAlertSilences() {
 


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

Reply via email to