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

feat: [refactor]move CollectorController to CollectorService #2495

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -67,32 +67,15 @@ public ResponseEntity<Message<Page<CollectorSummary>>> getCollectors(
@Parameter(description = "collector name", example = "tom") @RequestParam(required = false) final String name,
@Parameter(description = "List current page", example = "0") @RequestParam(defaultValue = "0") int pageIndex,
@Parameter(description = "Number of list pagination", example = "8") @RequestParam(required = false) Integer pageSize) {
if (pageSize == null) {
pageSize = Integer.MAX_VALUE;
}
Specification<Collector> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !name.isEmpty()) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
Page<CollectorSummary> receivers = collectorService.getCollectors(specification, pageRequest);
Message<Page<CollectorSummary>> message = Message.success(receivers);
return ResponseEntity.ok(message);
return ResponseEntity.ok(Message.success(collectorService.getCollectors(pageIndex, pageSize, name)));
}

@PutMapping("/online")
@Operation(summary = "Online collectors")
public ResponseEntity<Message<Void>> onlineCollector(
@Parameter(description = "collector name", example = "demo-collector")
@RequestParam(required = false) List<String> collectors) {
if (collectors != null) {
collectors.forEach(collector ->
this.manageServer.getCollectorAndJobScheduler().onlineCollector(collector));
}
collectorService.online(collectors);
return ResponseEntity.ok(Message.success("Online success"));
Copy link
Contributor

Choose a reason for hiding this comment

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

hi, Have you tested this interface? Does he return data properly?

}

Expand All @@ -101,9 +84,7 @@ public ResponseEntity<Message<Void>> onlineCollector(
public ResponseEntity<Message<Void>> offlineCollector(
@Parameter(description = "collector name", example = "demo-collector")
@RequestParam(required = false) List<String> collectors) {
if (collectors != null) {
collectors.forEach(collector -> this.manageServer.getCollectorAndJobScheduler().offlineCollector(collector));
}
collectorService.offline(collectors);
return ResponseEntity.ok(Message.success("Offline success"));
}

Expand All @@ -121,14 +102,7 @@ public ResponseEntity<Message<Void>> deleteCollector(
public ResponseEntity<Message<Map<String, String>>> generateCollectorDeployInfo(
@Parameter(description = "collector name", example = "demo-collector")
@PathVariable() String collector) {
if (this.collectorService.hasCollector(collector)) {
return ResponseEntity.ok(Message.fail(CommonConstants.FAIL_CODE, "There already has same collector name."));
}
String host = IpDomainUtil.getLocalhostIp();
Map<String, String> maps = new HashMap<>(6);
maps.put("identity", collector);
maps.put("host", host);
return ResponseEntity.ok(Message.success(maps));
return ResponseEntity.ok(Message.success(collectorService.generateCollectorDeployInfo(collector)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.apache.hertzbeat.manager.service;

import java.util.List;
import java.util.Map;

import org.apache.hertzbeat.common.entity.dto.CollectorSummary;
import org.apache.hertzbeat.common.entity.manager.Collector;
import org.springframework.data.domain.Page;
Expand All @@ -28,14 +30,15 @@
* collector service
*/
public interface CollectorService {

/**
* Dynamic conditional query
* @param specification Query conditions
* @param pageRequest pageIndex pageSize
* @return Search result
* @param pageIndex
* @param pageSize
* @param name
* @return
*/
Page<CollectorSummary> getCollectors(Specification<Collector> specification, PageRequest pageRequest);
Page<CollectorSummary> getCollectors(int pageIndex,Integer pageSize,final String name);

/**
* delete registered collectors
Expand All @@ -49,4 +52,25 @@ public interface CollectorService {
* @return return true if it has
*/
boolean hasCollector(String collector);


/**
* online collectors
* @param collectors
*/
void online(List<String> collectors);

/**
* offline collectors
* @param collectors
*/
void offline(List<String> collectors);

/**
* generate Collector Deploy Info
* @param collector
* @return
*/
Map<String, String> generateCollectorDeployInfo(String collector);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@

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

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import jakarta.persistence.criteria.Predicate;
import org.apache.hertzbeat.common.constants.CommonConstants;
import org.apache.hertzbeat.common.entity.dto.CollectorSummary;
import org.apache.hertzbeat.common.entity.dto.Message;
import org.apache.hertzbeat.common.entity.manager.Collector;
import org.apache.hertzbeat.common.entity.manager.CollectorMonitorBind;
import org.apache.hertzbeat.common.support.exception.CommonException;
import org.apache.hertzbeat.common.util.IpDomainUtil;
import org.apache.hertzbeat.manager.dao.CollectorDao;
import org.apache.hertzbeat.manager.dao.CollectorMonitorBindDao;
import org.apache.hertzbeat.manager.scheduler.AssignJobs;
Expand All @@ -34,6 +41,7 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -58,7 +66,19 @@ public class CollectorServiceImpl implements CollectorService {

@Override
@Transactional(readOnly = true)
public Page<CollectorSummary> getCollectors(Specification<Collector> specification, PageRequest pageRequest) {
public Page<CollectorSummary> getCollectors(int pageIndex,Integer pageSize,final String name) {
if (pageSize == null) {
pageSize = Integer.MAX_VALUE;
}
Specification<Collector> specification = (root, query, criteriaBuilder) -> {
Predicate predicate = criteriaBuilder.conjunction();
if (name != null && !name.isEmpty()) {
Predicate predicateName = criteriaBuilder.like(root.get("name"), "%" + name + "%");
predicate = criteriaBuilder.and(predicateName);
}
return predicate;
};
PageRequest pageRequest = PageRequest.of(pageIndex, pageSize);
Page<Collector> collectors = collectorDao.findAll(specification, pageRequest);
List<CollectorSummary> collectorSummaryList = new LinkedList<>();
for (Collector collector : collectors.getContent()) {
Expand Down Expand Up @@ -97,4 +117,31 @@ public void deleteRegisteredCollector(List<String> collectors) {
public boolean hasCollector(String collector) {
return this.collectorDao.findCollectorByName(collector).isPresent();
}

@Override
public void online(List<String> collectors) {
if (collectors != null) {
collectors.forEach(collector ->
this.manageServer.getCollectorAndJobScheduler().onlineCollector(collector));
}
}

@Override
public void offline(List<String> collectors) {
if (collectors != null) {
collectors.forEach(collector -> this.manageServer.getCollectorAndJobScheduler().offlineCollector(collector));
}
}

@Override
public Map<String, String> generateCollectorDeployInfo(String collector) {
if (this.hasCollector(collector)) {
throw new IllegalArgumentException("There already has same collector name.");
}
String host = IpDomainUtil.getLocalhostIp();
Map<String, String> maps = new HashMap<>(6);
maps.put("identity", collector);
maps.put("host", host);
return maps;
}
}
14 changes: 7 additions & 7 deletions 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: dev
Copy link
Contributor

Choose a reason for hiding this comment

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

Why change the configuration file here?

mvc:
static-path-pattern: /**
jackson:
Expand Down Expand Up @@ -63,20 +63,20 @@ sureness:
spring:
config:
activate:
on-profile: prod
on-profile: dev

datasource:
driver-class-name: org.h2.Driver
username: sa
password: 123456
url: jdbc:h2:./data/hertzbeat;MODE=MYSQL
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 1qw23e
url: jdbc:mysql://localhost:3306/hertzbeat?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
hikari:
max-lifetime: 120000

jpa:
show-sql: false
database-platform: org.eclipse.persistence.platform.database.MySQLPlatform
database: h2
database: mysql
properties:
eclipselink:
logging:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -67,7 +69,7 @@ public class CollectorServiceTest {
public void getCollectors() {
Specification<Collector> specification = mock(Specification.class);
when(collectorDao.findAll(specification, PageRequest.of(1, 1))).thenReturn(Page.empty());
assertDoesNotThrow(() -> collectorService.getCollectors(specification, PageRequest.of(1, 1)));
assertDoesNotThrow(() -> collectorService.getCollectors(1,1,""));
}

@Test
Expand All @@ -81,4 +83,19 @@ public void deleteRegisteredCollector() {
public void hasCollector() {
collectorService.hasCollector("test");
}

@Test
public void online() {
collectorService.online(Stream.of("test").collect(Collectors.toList()));
}

@Test
public void offline() {
collectorService.offline(Stream.of("test").collect(Collectors.toList()));
}

@Test
public void generateCollectorDeployInfo() {
collectorService.generateCollectorDeployInfo("test");
}
}