From d1bd385db9f2d08fbce4f7bdc2d344a1198e4d95 Mon Sep 17 00:00:00 2001 From: catdrink <2738035238@qq.com> Date: Sun, 8 Dec 2024 08:34:10 +0800 Subject: [PATCH] fix BigtopProxy.java --- .../manager/server/proxy/BigtopProxy.java | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/proxy/BigtopProxy.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/proxy/BigtopProxy.java index 81e90aadd..0d7404996 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/proxy/BigtopProxy.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/proxy/BigtopProxy.java @@ -28,11 +28,10 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import reactor.core.publisher.Mono; -import java.util.ArrayList; - @Component public class BigtopProxy { @@ -42,12 +41,8 @@ public BigtopProxy(WebClient.Builder webClientBuilder, @Value("http://localhost: this.webClient = webClientBuilder.baseUrl(bigTopHost).build(); } - private MultiValueMap createFormData(String clusterId, Integer pageNum) { + private MultiValueMap createFormData(Integer pageNum) { MultiValueMap formData = new LinkedMultiValueMap<>(); - formData.add("hostnameLike", ""); - formData.add("clusterId", clusterId); - formData.add("ipv4Like", ""); - formData.add("status", ""); formData.add("pageNum", String.valueOf(pageNum)); formData.add("pageSize", "20"); formData.add("orderBy", "id"); @@ -55,28 +50,43 @@ private MultiValueMap createFormData(String clusterId, Integer p return formData; } - public JsonNode queryClusterAgentsList(String clusterId) { - ArrayList hosts = new ArrayList<>(); + public JsonNode queryHosts(int pageNum) { + Mono body = webClient + .post() + .uri(uriBuilder -> uriBuilder.path("/api/hosts").build()) + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .body(BodyInserters.fromFormData(createFormData(pageNum))) + .retrieve() + .bodyToMono(JsonNode.class); + JsonNode result = body.block(); + if (result == null || result.isEmpty() || !(result.get("failed").asBoolean())) { + return null; + } + return result; + } + + public JsonNode queryClusterAgents(String clusterId) { ObjectMapper objectMapper = new ObjectMapper(); + // query cluster name + Mono body = + webClient.get().uri("/api/clusters/{id}", clusterId).retrieve().bodyToMono(JsonNode.class); + JsonNode result = body.block(); + if (result == null || result.isEmpty() || !(result.get("failed").asBoolean())) return null; + String clusterName = result.get("data").get("name").asText(); + ObjectNode clusterAgents = objectMapper.createObjectNode(); + ArrayNode arrayNode = objectMapper.createArrayNode(); int pageNum = 1; while (true) { - Mono body = webClient - .post() - .uri(uriBuilder -> uriBuilder.path("/api/hosts").build()) - .contentType(MediaType.APPLICATION_FORM_URLENCODED) - .body(BodyInserters.fromFormData(createFormData(clusterId, pageNum++))) - .retrieve() - .bodyToMono(JsonNode.class); - JsonNode result = body.block(); - if (result == null || result.isEmpty() || !(result.get("failed").asBoolean())) { - break; - } - JsonNode agents = result.get("data").get("content"); - agents.forEach(agent -> hosts.add(agent.get("ipv4").asText())); + JsonNode agentsPage = queryHosts(pageNum++); + if (agentsPage == null) break; + JsonNode agentsContent = agentsPage.get("data").get("content"); + agentsContent.forEach(agent -> { + if (agent.get("clusterName").asText().equals(clusterName)) + arrayNode.add(agent.get("ipv4").asText()); + }); } - ObjectNode clusterAgents = objectMapper.createObjectNode(); - clusterAgents.put("agentsNum", hosts.size()); - clusterAgents.set("agents", objectMapper.valueToTree(hosts)); + clusterAgents.put("agentsNum", arrayNode.size()); + clusterAgents.set("agents", arrayNode); return clusterAgents; } }