diff --git a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/JsonUtils.java b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/JsonUtils.java index 963b48a3..66931bd1 100644 --- a/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/JsonUtils.java +++ b/bigtop-manager-common/src/main/java/org/apache/bigtop/manager/common/utils/JsonUtils.java @@ -23,6 +23,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import lombok.extern.slf4j.Slf4j; import java.io.File; @@ -129,4 +130,19 @@ public static String writeAsString(T obj) { throw new RuntimeException(e); } } + + public static String indentWriteAsString(T obj) { + if (obj == null) { + return null; + } + + try { + OBJECTMAPPER.enable(SerializationFeature.INDENT_OUTPUT); + String result = OBJECTMAPPER.writeValueAsString(obj); + OBJECTMAPPER.disable(SerializationFeature.INDENT_OUTPUT); + return result; + } catch (Exception e) { + throw new RuntimeException(e); + } + } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/ClusterInfoFunctions.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/ClusterInfoFunctions.java index f64591cb..0ba264ae 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/ClusterInfoFunctions.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/ClusterInfoFunctions.java @@ -40,35 +40,39 @@ public class ClusterInfoFunctions { @Resource private ClusterService clusterService; - public Map list() { + public Map listCluster() { ToolSpecification toolSpecification = ToolSpecification.builder() - .name("list") + .name("listCluster") .description("Get cluster list") .build(); ToolExecutor toolExecutor = - (toolExecutionRequest, memoryId) -> clusterService.list().toString(); + (toolExecutionRequest, memoryId) -> JsonUtils.indentWriteAsString(clusterService.list()); return Map.of(toolSpecification, toolExecutor); } - public Map get() { + public Map getClusterById() { ToolSpecification toolSpecification = ToolSpecification.builder() - .name("get") + .name("getClusterById") .description("Get cluster information based on ID") .addParameter("clusterId", JsonSchemaProperty.NUMBER, JsonSchemaProperty.description("cluster id")) .build(); ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> { Map arguments = JsonUtils.readFromString(toolExecutionRequest.arguments()); Long clusterId = Long.valueOf(arguments.get("clusterId").toString()); - return clusterService.get(clusterId).toString(); + ClusterVO clusterVO = clusterService.get(clusterId); + if (clusterVO == null) { + return "Cluster not found"; + } + return JsonUtils.indentWriteAsString(clusterVO); }; return Map.of(toolSpecification, toolExecutor); } - public Map getByName() { + public Map getClusterByName() { ToolSpecification toolSpecification = ToolSpecification.builder() - .name("getByName") + .name("getClusterByName") .description("Get cluster information based on cluster name") .addParameter("clusterName", JsonSchemaProperty.STRING, JsonSchemaProperty.description("cluster name")) .build(); @@ -78,7 +82,7 @@ public Map getByName() { List clusterVOS = clusterService.list(); for (ClusterVO clusterVO : clusterVOS) { if (clusterVO.getName().equals(clusterName)) { - return clusterVO.toString(); + return JsonUtils.indentWriteAsString(clusterVO); } } return "Cluster not found"; @@ -89,9 +93,9 @@ public Map getByName() { public Map getAllFunctions() { Map functions = new HashMap<>(); - functions.putAll(list()); - functions.putAll(get()); - functions.putAll(getByName()); + functions.putAll(listCluster()); + functions.putAll(getClusterById()); + functions.putAll(getClusterByName()); return functions; } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/HostInfoFunctions.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/HostInfoFunctions.java index 13eae3f4..21018649 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/HostInfoFunctions.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/HostInfoFunctions.java @@ -41,24 +41,28 @@ public class HostInfoFunctions { @Resource private HostService hostService; - public Map get() { + public Map getHostById() { ToolSpecification toolSpecification = ToolSpecification.builder() - .name("get") + .name("getHostById") .description("Get host information based on ID") .addParameter("hostId", JsonSchemaProperty.NUMBER, JsonSchemaProperty.description("host id")) .build(); ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> { Map arguments = JsonUtils.readFromString(toolExecutionRequest.arguments()); Long hostId = Long.valueOf(arguments.get("hostId").toString()); - return hostService.get(hostId).toString(); + HostVO hostVO = hostService.get(hostId); + if (hostVO == null) { + return "Host not found"; + } + return JsonUtils.indentWriteAsString(hostVO); }; return Map.of(toolSpecification, toolExecutor); } - public Map getByName() { + public Map getHostByName() { ToolSpecification toolSpecification = ToolSpecification.builder() - .name("getByName") + .name("getHostByName") .description("Get host information based on cluster name") .addParameter("hostName", JsonSchemaProperty.STRING, JsonSchemaProperty.description("host name")) .build(); @@ -68,8 +72,7 @@ public Map getByName() { HostQuery hostQuery = new HostQuery(); hostQuery.setHostname(hostName); PageVO hostVO = hostService.list(hostQuery); - log.info(hostVO.toString()); - return hostVO.toString(); + return JsonUtils.indentWriteAsString(hostVO); }; return Map.of(toolSpecification, toolExecutor); @@ -77,8 +80,8 @@ public Map getByName() { public Map getAllFunctions() { Map functions = new HashMap<>(); - functions.putAll(get()); - functions.putAll(getByName()); + functions.putAll(getHostById()); + functions.putAll(getHostByName()); return functions; } } diff --git a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/StackInfoFunctions.java b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/StackInfoFunctions.java index c243a12d..a2c9c398 100644 --- a/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/StackInfoFunctions.java +++ b/bigtop-manager-server/src/main/java/org/apache/bigtop/manager/server/tools/functions/StackInfoFunctions.java @@ -44,9 +44,9 @@ public class StackInfoFunctions { @Resource private StackService stackService; - public Map list() { + public Map listStackAndService() { ToolSpecification toolSpecification = ToolSpecification.builder() - .name("list") + .name("listStackAndService") .description("Retrieve the list of services in each stack") .build(); ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> { @@ -58,7 +58,7 @@ public Map list() { } stackInfo.put(stackVO.getStackName(), services); } - return stackInfo.toString(); + return JsonUtils.indentWriteAsString(stackInfo); }; return Map.of(toolSpecification, toolExecutor); } @@ -85,8 +85,7 @@ public Map getServiceByName() { } } } - - return serviceVO.toString(); + return JsonUtils.indentWriteAsString(serviceVO); } } } @@ -98,7 +97,7 @@ public Map getServiceByName() { public Map getAllFunctions() { Map functions = new HashMap<>(); - functions.putAll(list()); + functions.putAll(listStackAndService()); functions.putAll(getServiceByName()); return functions; }