Skip to content

Commit

Permalink
ARTEMIS-4743 Split lines on ./artemis queue stat for large names
Browse files Browse the repository at this point in the history
  • Loading branch information
clebertsuconic committed Apr 23, 2024
1 parent 86f7250 commit c06e8a1
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.activemq.artemis.cli.commands.messages.ConnectionAbstract;
import org.apache.activemq.artemis.json.JsonArray;
import org.apache.activemq.artemis.json.JsonObject;
import org.apache.activemq.artemis.utils.TableOut;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

Expand Down Expand Up @@ -219,17 +220,18 @@ private void printStats(String result) {

FIELD[] fields = FIELD.values();
for (int i = 0; i < fields.length; i++) {
columnSizes[i] = fields[i].toString().length();
columnSizes[i] = Math.min(fields[i].toString().length(), 10);
}

for (int i = 0; i < array.size(); i++) {
getColumnSizes(array.getJsonObject(i), columnSizes);
}

printHeadings(columnSizes);
TableOut tableOut = new TableOut("|", columnSizes);
printHeadings(columnSizes, tableOut);

for (int i = 0; i < array.size(); i++) {
printQueueStats(array.getJsonObject(i), columnSizes);
printQueueStats(array.getJsonObject(i), columnSizes, tableOut);
statCount++;
}

Expand All @@ -252,19 +254,18 @@ private void getColumnSizes(JsonObject jsonObject, int[] columnSizes) {
}
}

private void printHeadings(int[] columnSizes) {
// add 10 for the various '|' characters
StringBuilder stringBuilder = new StringBuilder(Arrays.stream(columnSizes).sum() + FIELD.values().length + 1).append('|');
private void printHeadings(int[] columnSizes, TableOut tableOut) {
String[] columns = new String[columnSizes.length];

int i = 0;
for (FIELD e: FIELD.values()) {
stringBuilder.append(paddingString(new StringBuilder(e.toString()), columnSizes[i++])).append('|');
columns[i++] = e.toString();
}

getActionContext().out.println(stringBuilder);
tableOut.print(getActionContext().out, columns);
}

private void printQueueStats(JsonObject jsonObject, int[] columnSizes) {
private void printQueueStats(JsonObject jsonObject, int[] columnSizes, TableOut tableOut) {

//should not happen but just in case..
if (jsonObject == null) {
Expand All @@ -274,15 +275,12 @@ private void printQueueStats(JsonObject jsonObject, int[] columnSizes) {
return;
}

// add 10 for the various '|' characters
StringBuilder stringBuilder = new StringBuilder(Arrays.stream(columnSizes).sum() + FIELD.values().length + 1).append('|');

int i = 0;
String[] columns = new String[columnSizes.length];
for (FIELD e: FIELD.values()) {
stringBuilder.append(paddingString(new StringBuilder(jsonObject.getString(e.jsonId)), columnSizes[i++])).append('|');
columns[i++] = jsonObject.getString(e.jsonId);
}

getActionContext().out.println(stringBuilder);
tableOut.print(getActionContext().out, columns);
}

private StringBuilder paddingString(StringBuilder value, int maxColumnSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1583,7 +1583,7 @@ public void testQstat() throws Exception {
statQueue.execute(context);
ArrayList<String> lines = getOutputLines(context, false);
// Header line + 3 queues
Assert.assertEquals("rows returned using queueName=Test1", 4, lines.size());
Assert.assertEquals("rows returned using queueName=Test1", 5, lines.size());

//check all queues are displayed when no Filter set
context = new TestActionContext();
Expand All @@ -1606,7 +1606,7 @@ public void testQstat() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + 3 queues
Assert.assertEquals("rows returned filtering by NAME ", 4, lines.size());
Assert.assertEquals("rows returned filtering by NAME ", 5, lines.size());

//check all queues NOT containing "management" are displayed using Filter field NAME
context = new TestActionContext();
Expand All @@ -1619,7 +1619,7 @@ public void testQstat() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + 6 queues (Test1/11/12/20+DLQ+ExpiryQueue, but not activemq.management.d6dbba78-d76f-43d6-a2c9-fc0575ed6f5d)
Assert.assertEquals("rows returned filtering by NAME operation NOT_CONTAINS", 7, lines.size());
Assert.assertEquals("rows returned filtering by NAME operation NOT_CONTAINS", 8, lines.size());

//check only queue named "Test1" is displayed using Filter field NAME and operation EQUALS
context = new TestActionContext();
Expand All @@ -1632,9 +1632,9 @@ public void testQstat() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
//Header line + 1 queue only
Assert.assertEquals("rows returned filtering by NAME operation EQUALS", 2, lines.size());
Assert.assertEquals("rows returned filtering by NAME operation EQUALS", 3, lines.size());
//verify contents of queue stat line is correct
String queueTest1 = lines.get(1);
String queueTest1 = lines.get(2);
String[] parts = queueTest1.split("\\|");
Assert.assertEquals("queue name", "Test1", parts[1].trim());
Assert.assertEquals("address name", "Test1", parts[2].trim());
Expand All @@ -1657,7 +1657,7 @@ public void testQstat() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + 3 queues
Assert.assertEquals("rows returned filtering by ADDRESS", 4, lines.size());
Assert.assertEquals("rows returned filtering by ADDRESS", 5, lines.size());

//check all queues containing address "Test1" are displayed using Filter field MESSAGE_COUNT
context = new TestActionContext();
Expand All @@ -1671,7 +1671,7 @@ public void testQstat() throws Exception {
lines = getOutputLines(context, false);

// Header line + 0 queues
Assert.assertEquals("rows returned filtering by MESSAGE_COUNT", 1, lines.size());
Assert.assertEquals("rows returned filtering by MESSAGE_COUNT", 2, lines.size());

//check all queues containing address "Test1" are displayed using Filter field MESSAGE_ADDED
context = new TestActionContext();
Expand All @@ -1684,7 +1684,7 @@ public void testQstat() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + 0 queues
Assert.assertEquals("rows returned filtering by MESSAGES_ADDED", 1, lines.size());
Assert.assertEquals("rows returned filtering by MESSAGES_ADDED", 2, lines.size());

//check queues with greater_than 19 MESSAGE_ADDED displayed
context = new TestActionContext();
Expand All @@ -1698,8 +1698,8 @@ public void testQstat() throws Exception {
lines = getOutputLines(context, false);

// Header line + 1 queues
Assert.assertEquals("rows returned filtering by MESSAGES_ADDED", 2, lines.size());
String[] columns = lines.get(1).split("\\|");
Assert.assertEquals("rows returned filtering by MESSAGES_ADDED", 3, lines.size());
String[] columns = lines.get(2).split("\\|");
Assert.assertEquals("queue name filtered by MESSAGES_ADDED GREATER_THAN ", "Test20", columns[2].trim());

//check queues with less_than 2 MESSAGE_ADDED displayed
Expand Down Expand Up @@ -1733,9 +1733,9 @@ public void testQstat() throws Exception {
statQueue.setValue("10");
statQueue.execute(context);
lines = getOutputLines(context, false);
columns = lines.get(1).split("\\|");
columns = lines.get(2).split("\\|");
// Header line + 1 queues
Assert.assertEquals("rows returned filtering by DELIVERING_COUNT", 2, lines.size());
Assert.assertEquals("rows returned filtering by DELIVERING_COUNT", 3, lines.size());
Assert.assertEquals("queue name filtered by DELIVERING_COUNT ", "Test1", columns[2].trim());

//check all queues containing address "Test1" are displayed using Filter field CONSUMER_COUNT
Expand All @@ -1748,9 +1748,9 @@ public void testQstat() throws Exception {
statQueue.setValue("2");
statQueue.execute(context);
lines = getOutputLines(context, false);
columns = lines.get(1).split("\\|");
columns = lines.get(2).split("\\|");
// Header line + 1 queues
Assert.assertEquals("rows returned filtering by CONSUMER_COUNT ", 2, lines.size());
Assert.assertEquals("rows returned filtering by CONSUMER_COUNT ", 3, lines.size());
Assert.assertEquals("queue name filtered by CONSUMER_COUNT ", "Test1", columns[2].trim());

//check all queues containing address "Test1" are displayed using Filter field MESSAGE_ACKED
Expand All @@ -1763,9 +1763,9 @@ public void testQstat() throws Exception {
statQueue.setValue("5");
statQueue.execute(context);
lines = getOutputLines(context, false);
columns = lines.get(1).split("\\|");
columns = lines.get(2).split("\\|");
// Header line + 1 queues
Assert.assertEquals("rows returned filtering by MESSAGE_ACKED ", 2, lines.size());
Assert.assertEquals("rows returned filtering by MESSAGE_ACKED ", 3, lines.size());
Assert.assertEquals("queue name filtered by MESSAGE_ACKED", "Test1", columns[2].trim());

//check no queues are displayed when name does not match
Expand All @@ -1777,7 +1777,7 @@ public void testQstat() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + 0 queues
Assert.assertEquals("rows returned by queueName for no Matching queue ", 1, lines.size());
Assert.assertEquals("rows returned by queueName for no Matching queue ", 2, lines.size());

//check maxrows is taking effect"
context = new TestActionContext();
Expand All @@ -1789,7 +1789,7 @@ public void testQstat() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + 1 queue only + warning line
Assert.assertEquals("rows returned by maxRows=1", 3, lines.size());
Assert.assertEquals("rows returned by maxRows=1", 4, lines.size());

} finally {
stopServer();
Expand Down Expand Up @@ -2035,7 +2035,7 @@ public void testQstatWarnings() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + DEFAULT_MAX_ROWS queues + warning line
Assert.assertEquals("rows returned using queueName=Test", 1 + StatQueue.DEFAULT_MAX_ROWS, lines.size());
Assert.assertEquals("rows returned using queueName=Test", 2 + StatQueue.DEFAULT_MAX_ROWS, lines.size());
Assert.assertFalse(lines.get(lines.size() - 1).startsWith("WARNING"));

//check all queues containing "Test" are displayed
Expand All @@ -2048,7 +2048,7 @@ public void testQstatWarnings() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + DEFAULT_MAX_ROWS queues
Assert.assertEquals("rows returned using queueName=Test", 1 + StatQueue.DEFAULT_MAX_ROWS, lines.size());
Assert.assertEquals("rows returned using queueName=Test", 2 + StatQueue.DEFAULT_MAX_ROWS, lines.size());
Assert.assertFalse(lines.get(lines.size() - 1).startsWith("WARNING"));

sendMessages(session, "Test" + StatQueue.DEFAULT_MAX_ROWS, 1);
Expand All @@ -2062,7 +2062,7 @@ public void testQstatWarnings() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + DEFAULT_MAX_ROWS queues + warning line
Assert.assertEquals("rows returned using queueName=Test", 1 + StatQueue.DEFAULT_MAX_ROWS + 1, lines.size());
Assert.assertEquals("rows returned using queueName=Test", 2 + StatQueue.DEFAULT_MAX_ROWS + 1, lines.size());
Assert.assertTrue(lines.get(lines.size() - 1).startsWith("WARNING"));

//check all queues containing "Test" are displayed
Expand All @@ -2075,7 +2075,7 @@ public void testQstatWarnings() throws Exception {
statQueue.execute(context);
lines = getOutputLines(context, false);
// Header line + DEFAULT_MAX_ROWS queues + warning line
Assert.assertEquals("rows returned using queueName=Test", 1 + StatQueue.DEFAULT_MAX_ROWS + 1, lines.size());
Assert.assertEquals("rows returned using queueName=Test", 2 + StatQueue.DEFAULT_MAX_ROWS + 1, lines.size());
Assert.assertTrue(lines.get(lines.size() - 1).startsWith("WARNING"));

} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.activemq.artemis.utils;

import java.io.PrintStream;
import java.util.ArrayList;

public class TableOut {

String separator;
int[] columnSizes;

public TableOut(String separator, int... columnSizes) {
this.separator = separator;
this.columnSizes = columnSizes;
}


public void print(PrintStream stream, String... columns) {
ArrayList<String>[] splitColumns = new ArrayList[columns.length];
for (int i = 0; i < columns.length; i++) {
splitColumns[i] = splitLine(columns[i], columnSizes[i]);
}


boolean hasMoreLines;
int lineNumber = 0;
do {
hasMoreLines = false;
stream.print(separator);
for (int column = 0; column < columns.length; column++) {
StringBuilder cell = new StringBuilder();
if (lineNumber < splitColumns[column].size()) {
cell.append(splitColumns[column].get(lineNumber));
}
if (lineNumber + 1 < splitColumns[column].size()) {
hasMoreLines = true;
}
while (cell.length() < columnSizes[column]) {
cell.append(" ");
}
stream.print(cell);
stream.print(separator);
}
stream.println();
lineNumber++;
} while (hasMoreLines);

}

public static ArrayList<String> splitLine(final String column, int size) {
ArrayList<String> cells = new ArrayList<>();

for (int position = 0; position < column.length();) {
int maxPosition = Math.min(size, column.length() - position);
cells.add(column.substring(position, position + maxPosition));
position += maxPosition;
}

return cells;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.activemq.artemis.utils;

import java.util.ArrayList;

import org.junit.Assert;
import org.junit.Test;

public class TableOutTest {

@Test
public void testSplitString() {
String bigCell = "1234554321321";
ArrayList<String> lines = TableOut.splitLine(bigCell, 5);
Assert.assertEquals(3, lines.size());
Assert.assertEquals("12345", lines.get(0));
Assert.assertEquals("54321", lines.get(1));
Assert.assertEquals("321", lines.get(2));
}

@Test
public void testOutLine() {

// the output is visual, however this test is good to make sure the output at least works without any issues
TableOut tableOut = new TableOut("|", 10, 3, 3);
tableOut.print(System.out, "This is a big title", "1234567", "1234");
}

}

0 comments on commit c06e8a1

Please sign in to comment.