diff --git a/cli/src/main/java/com/automq/rocketmq/cli/ConsoleHelper.java b/cli/src/main/java/com/automq/rocketmq/cli/ConsoleHelper.java index 511331730..c107ae820 100644 --- a/cli/src/main/java/com/automq/rocketmq/cli/ConsoleHelper.java +++ b/cli/src/main/java/com/automq/rocketmq/cli/ConsoleHelper.java @@ -17,15 +17,65 @@ package com.automq.rocketmq.cli; +import apache.rocketmq.controller.v1.Cluster; import apache.rocketmq.controller.v1.MessageQueueAssignment; +import apache.rocketmq.controller.v1.Node; import apache.rocketmq.controller.v1.OngoingMessageQueueReassignment; import apache.rocketmq.controller.v1.Topic; +import com.google.protobuf.Timestamp; +import de.vandermeer.asciitable.AT_Cell; import de.vandermeer.asciitable.AT_Row; import de.vandermeer.asciitable.AsciiTable; +import de.vandermeer.asciitable.CWC_LongestLine; import de.vandermeer.skb.interfaces.transformers.textformat.TextAlignment; +import java.util.Calendar; +import java.util.Date; import java.util.List; +import java.util.concurrent.TimeUnit; public class ConsoleHelper { + + private static Date toDate(Timestamp timestamp) { + long millis = TimeUnit.SECONDS.toMillis(timestamp.getSeconds()) + TimeUnit.NANOSECONDS.toMillis(timestamp.getNanos()); + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(millis); + return calendar.getTime(); + } + + private static void alignCentral(AT_Row row) { + for (AT_Cell cell : row.getCells()) { + cell.getContext().setTextAlignment(TextAlignment.CENTER); + } + } + + public static void printCluster(Cluster cluster) { + if (null == cluster) { + return; + } + + AsciiTable nodeTable = new AsciiTable(); + nodeTable.addRule(); + AT_Row row = nodeTable.addRow("NODE ID", "NODE NAME", "TOPIC QUANTITY", "QUEUE QUANTITY", + "STREAM QUANTITY", "LAST HEARTBEAT", "ROLE", "EPOCH", "EXPIRATION"); + + alignCentral(row); + + for (Node node : cluster.getNodesList()) { + nodeTable.addRule(); + boolean isLeader = node.getId() == cluster.getLease().getNodeId(); + row = nodeTable.addRow(node.getId(), node.getName(), node.getTopicNum(), node.getQueueNum(), node.getStreamNum(), + toDate(node.getLastHeartbeat()), isLeader ? "Leader" : "Worker", isLeader ? cluster.getLease().getEpoch() : "", + isLeader ? toDate(cluster.getLease().getExpirationTimestamp()) : ""); + alignCentral(row); + } + nodeTable.addRule(); + + CWC_LongestLine cwc = new CWC_LongestLine(); + nodeTable.getRenderer().setCWC(cwc); + String render = nodeTable.render(); + System.out.println(render); + } + public static void printTable(Topic topic) { AsciiTable topicTable = new AsciiTable(); topicTable.addRule(); @@ -60,7 +110,8 @@ public static void printTable(Topic topic) { reassignmentTable.addRow("SRC NODE ID", "DST NODE ID", "QUEUE ID"); reassignmentTable.addRule(); for (OngoingMessageQueueReassignment reassignment : ongoing) { - reassignmentTable.addRow(reassignment.getSrcNodeId(), reassignment.getDstNodeId(), reassignment.getQueue().getQueueId()); + reassignmentTable.addRow(reassignment.getSrcNodeId(), reassignment.getDstNodeId(), + reassignment.getQueue().getQueueId()); reassignmentTable.addRule(); } } diff --git a/cli/src/main/java/com/automq/rocketmq/cli/DescribeCluster.java b/cli/src/main/java/com/automq/rocketmq/cli/DescribeCluster.java index 6b739a72a..3c7692079 100644 --- a/cli/src/main/java/com/automq/rocketmq/cli/DescribeCluster.java +++ b/cli/src/main/java/com/automq/rocketmq/cli/DescribeCluster.java @@ -21,7 +21,6 @@ import apache.rocketmq.controller.v1.DescribeClusterRequest; import com.automq.rocketmq.controller.metadata.ControllerClient; import com.automq.rocketmq.controller.metadata.GrpcControllerClient; -import com.google.protobuf.util.JsonFormat; import java.util.concurrent.Callable; import picocli.CommandLine; @@ -37,7 +36,7 @@ public Void call() throws Exception { DescribeClusterRequest request = DescribeClusterRequest.newBuilder() .build(); Cluster cluster = client.describeCluster(mqAdmin.endpoint, request).join(); - System.out.println(JsonFormat.printer().print(cluster)); + ConsoleHelper.printCluster(cluster); } return null; }