Skip to content

Commit

Permalink
add indent json
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq committed Dec 20, 2024
1 parent 67cfc4f commit 39437a8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -129,4 +130,19 @@ public static <T> String writeAsString(T obj) {
throw new RuntimeException(e);
}
}

public static <T> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,39 @@ public class ClusterInfoFunctions {
@Resource
private ClusterService clusterService;

public Map<ToolSpecification, ToolExecutor> list() {
public Map<ToolSpecification, ToolExecutor> 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<ToolSpecification, ToolExecutor> get() {
public Map<ToolSpecification, ToolExecutor> 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<String, Object> 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<ToolSpecification, ToolExecutor> getByName() {
public Map<ToolSpecification, ToolExecutor> 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();
Expand All @@ -78,7 +82,7 @@ public Map<ToolSpecification, ToolExecutor> getByName() {
List<ClusterVO> clusterVOS = clusterService.list();
for (ClusterVO clusterVO : clusterVOS) {
if (clusterVO.getName().equals(clusterName)) {
return clusterVO.toString();
return JsonUtils.indentWriteAsString(clusterVO);
}
}
return "Cluster not found";
Expand All @@ -89,9 +93,9 @@ public Map<ToolSpecification, ToolExecutor> getByName() {

public Map<ToolSpecification, ToolExecutor> getAllFunctions() {
Map<ToolSpecification, ToolExecutor> functions = new HashMap<>();
functions.putAll(list());
functions.putAll(get());
functions.putAll(getByName());
functions.putAll(listCluster());
functions.putAll(getClusterById());
functions.putAll(getClusterByName());
return functions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,28 @@ public class HostInfoFunctions {
@Resource
private HostService hostService;

public Map<ToolSpecification, ToolExecutor> get() {
public Map<ToolSpecification, ToolExecutor> 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<String, Object> 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<ToolSpecification, ToolExecutor> getByName() {
public Map<ToolSpecification, ToolExecutor> 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();
Expand All @@ -68,17 +72,16 @@ public Map<ToolSpecification, ToolExecutor> getByName() {
HostQuery hostQuery = new HostQuery();
hostQuery.setHostname(hostName);
PageVO<HostVO> hostVO = hostService.list(hostQuery);
log.info(hostVO.toString());
return hostVO.toString();
return JsonUtils.indentWriteAsString(hostVO);
};

return Map.of(toolSpecification, toolExecutor);
}

public Map<ToolSpecification, ToolExecutor> getAllFunctions() {
Map<ToolSpecification, ToolExecutor> functions = new HashMap<>();
functions.putAll(get());
functions.putAll(getByName());
functions.putAll(getHostById());
functions.putAll(getHostByName());
return functions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class StackInfoFunctions {
@Resource
private StackService stackService;

public Map<ToolSpecification, ToolExecutor> list() {
public Map<ToolSpecification, ToolExecutor> listStackAndService() {
ToolSpecification toolSpecification = ToolSpecification.builder()
.name("list")
.name("listStackAndService")
.description("Retrieve the list of services in each stack")
.build();
ToolExecutor toolExecutor = (toolExecutionRequest, memoryId) -> {
Expand All @@ -58,7 +58,7 @@ public Map<ToolSpecification, ToolExecutor> list() {
}
stackInfo.put(stackVO.getStackName(), services);
}
return stackInfo.toString();
return JsonUtils.indentWriteAsString(stackInfo);
};
return Map.of(toolSpecification, toolExecutor);
}
Expand All @@ -85,8 +85,7 @@ public Map<ToolSpecification, ToolExecutor> getServiceByName() {
}
}
}

return serviceVO.toString();
return JsonUtils.indentWriteAsString(serviceVO);
}
}
}
Expand All @@ -98,7 +97,7 @@ public Map<ToolSpecification, ToolExecutor> getServiceByName() {

public Map<ToolSpecification, ToolExecutor> getAllFunctions() {
Map<ToolSpecification, ToolExecutor> functions = new HashMap<>();
functions.putAll(list());
functions.putAll(listStackAndService());
functions.putAll(getServiceByName());
return functions;
}
Expand Down

0 comments on commit 39437a8

Please sign in to comment.