Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[refactor] move code from AlertsController to AlertService #2435

Merged
merged 24 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
75d2631
[bugfix]fixed "Alert Threshold Association with Monitors" is invalid …
pwallk Jul 4, 2024
7f9d7fc
Merge branch 'master' into master
pwallk Jul 4, 2024
2d0d315
[bugfix]fixed "Alert Threshold Association with Monitors" is invalid …
pwallk Jul 4, 2024
1df0b26
Merge branch 'master' of https://github.com/pwallk/hertzbeat
pwallk Jul 4, 2024
b9d30c7
remove unused method
pwallk Jul 4, 2024
af93d5c
Merge branch 'master' into master
tomsun28 Jul 4, 2024
ff5fbf2
Merge branch 'apache:master' into master
pwallk Jul 5, 2024
57cbeff
[bugfix] fixed "Unable to establish a connection to nginx's https por…
pwallk Jul 5, 2024
0a9ffcf
Merge branch 'master' of https://github.com/pwallk/hertzbeat
pwallk Jul 5, 2024
2feaf54
Merge branch 'master' of https://github.com/pwallk/hertzbeat
pwallk Jul 5, 2024
61b8601
xxx
pwallk Jul 19, 2024
9851e5c
Merge branch 'apache:master' into master
pwallk Jul 22, 2024
228eb5e
Merge branch 'apache:master' into master
pwallk Jul 23, 2024
6e8a208
Merge branch 'apache:master' into master
pwallk Jul 24, 2024
fb2f7bb
Merge branch 'apache:master' into master
pwallk Jul 25, 2024
6c04f3f
Merge branch 'apache:master' into master
pwallk Jul 27, 2024
26ab61d
Merge branch 'apache:master' into master
pwallk Jul 29, 2024
4bc2016
Merge branch 'apache:master' into master
pwallk Jul 31, 2024
5730b09
Merge branch 'apache:master' into master
pwallk Aug 1, 2024
5d07e5b
move code from AlertsController to AlertService
pwallk Aug 1, 2024
bb71c31
some changes
pwallk Aug 5, 2024
122739a
Merge branch 'apache:master' into master
pwallk Aug 5, 2024
7a1168f
Merge branch 'master' into refactor-AlertsController
pwallk Aug 5, 2024
0944a91
Merge branch 'master' into refactor-AlertsController
Calvin979 Aug 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
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.dto.AlertSummary;
Expand All @@ -32,9 +29,6 @@
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.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down Expand Up @@ -67,41 +61,8 @@ public ResponseEntity<Message<Page<Alert>>> getAlerts(
@Parameter(description = "Sort Type", example = "desc") @RequestParam(defaultValue = "desc") String order,
@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) {

Specification<Alert> 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 (monitorId != null) {
Predicate predicate = criteriaBuilder.like(root.get("tags").as(String.class), "%" + monitorId + "%");
andList.add(predicate);
}
if (priority != null) {
Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
andList.add(predicate);
}
if (status != null) {
Predicate predicate = criteriaBuilder.equal(root.get("status"), status);
andList.add(predicate);
}
if (content != null && !content.isEmpty()) {
Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + content + "%");
andList.add(predicateContent);
}
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<Alert> alertPage = alertService.getAlerts(specification, pageRequest);
Message<Page<Alert>> message = Message.success(alertPage);
return ResponseEntity.ok(message);
Page<Alert> alertPage = alertService.getAlerts(ids, monitorId, priority, status, content, sort, order, pageIndex, pageSize);
return ResponseEntity.ok(Message.success(alertPage));
}

@DeleteMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.hertzbeat.common.entity.alerter.Alert;
import org.apache.hertzbeat.common.entity.dto.AlertReport;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;

/**
Expand All @@ -40,11 +39,18 @@ public interface AlertService {

/**
* Dynamic conditional query
* @param specification Query conditions
* @param pageRequest pagination parameters
* @param alarmIds Alarm ID List
* @param monitorId Monitor ID
* @param priority Alarm level
* @param status Alarm Status
* @param content Alarm content fuzzy query
* @param sort Sort field
* @param order Sort Type
* @param pageIndex List current page
* @param pageSize Number of list pagination
* @return search result
*/
Page<Alert> getAlerts(Specification<Alert> specification, PageRequest pageRequest);
Page<Alert> getAlerts(List<Long> alarmIds, Long monitorId, Byte priority, Byte status, String content, String sort, String order, int pageIndex, int pageSize);

/**
* Delete alarms in batches according to the alarm ID list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package org.apache.hertzbeat.alert.service.impl;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Predicate;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -37,6 +40,7 @@
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;
Expand All @@ -61,7 +65,38 @@ public void addAlert(Alert alert) throws RuntimeException {
}

@Override
public Page<Alert> getAlerts(Specification<Alert> specification, PageRequest pageRequest) {
public Page<Alert> getAlerts(List<Long> alarmIds, Long monitorId, Byte priority, Byte status, String content, String sort, String order, int pageIndex, int pageSize) {
Specification<Alert> specification = (root, query, criteriaBuilder) -> {
List<Predicate> andList = new ArrayList<>();

if (alarmIds != null && !alarmIds.isEmpty()) {
CriteriaBuilder.In<Long> inPredicate = criteriaBuilder.in(root.get("id"));
for (long id : alarmIds) {
inPredicate.value(id);
}
andList.add(inPredicate);
}
if (monitorId != null) {
Predicate predicate = criteriaBuilder.like(root.get("tags").as(String.class), "%" + monitorId + "%");
andList.add(predicate);
}
if (priority != null) {
Predicate predicate = criteriaBuilder.equal(root.get("priority"), priority);
andList.add(predicate);
}
if (status != null) {
Predicate predicate = criteriaBuilder.equal(root.get("status"), status);
andList.add(predicate);
}
if (content != null && !content.isEmpty()) {
Predicate predicateContent = criteriaBuilder.like(root.get("content"), "%" + content + "%");
andList.add(predicateContent);
}
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 alertDao.findAll(specification, pageRequest);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
Expand Down Expand Up @@ -77,13 +76,7 @@ void getAlerts() throws Exception {
Page<Alert> alertPage = new PageImpl<>(Collections.singletonList(Alert.builder().build()));
Mockito.when(
alertService.getAlerts(
Mockito.any(Specification.class)
, Mockito.argThat(
argument ->
argument.getPageNumber() == pageRequest.getPageNumber()
&& argument.getPageSize() == pageRequest.getPageSize()
&& argument.getSort().equals(pageRequest.getSort())
)
ids, 1L, (byte) 1, (byte) 1, "test", sortField, orderType, pageIndex, pageSize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! Could you help fix this test. Unit tests are passed because this test method is not annotated with @Test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay

)
)
.thenReturn(alertPage);
Expand Down
2 changes: 1 addition & 1 deletion manager/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ spring:
application:
name: ${HOSTNAME:@hertzbeat@}${PID}
profiles:
active: prod
active: pg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why modify application.yml? Looks like it is a dev configuration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, forgot to rollback

mvc:
static-path-pattern: /**
jackson:
Expand Down