This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 21a349659c9 CAMEL-21183: camel-jbang - Improve browse command
21a349659c9 is described below

commit 21a349659c997ae12b34e81a32afcbca0e8c8542
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Sep 10 06:50:23 2024 +0200

    CAMEL-21183: camel-jbang - Improve browse command
---
 .../camel/impl/console/BrowseDevConsole.java       | 27 ++++++++++++++++++++--
 .../camel/cli/connector/LocalCliConnector.java     |  1 +
 .../core/commands/action/CamelBrowseAction.java    | 16 +++++++++----
 3 files changed, 38 insertions(+), 6 deletions(-)

diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/BrowseDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/BrowseDevConsole.java
index 7d1507e4ea0..8c179f1ad8d 100644
--- 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/BrowseDevConsole.java
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/BrowseDevConsole.java
@@ -50,6 +50,11 @@ public class BrowseDevConsole extends AbstractDevConsole {
      */
     public static final String LIMIT = "limit";
 
+    /**
+     * To receive N last messages from the tail
+     */
+    public static final String TAIL = "tail";
+
     /**
      * Whether to include message dumps
      */
@@ -95,6 +100,8 @@ public class BrowseDevConsole extends AbstractDevConsole {
 
         String filter = (String) options.get(FILTER);
         String lim = (String) options.get(LIMIT);
+        String tail = (String) options.get(TAIL);
+        final int pos = tail == null ? 0 : Integer.parseInt(tail);
         final int max = lim == null ? limit : Integer.parseInt(lim);
         boolean dump = "true".equals(options.getOrDefault(DUMP, "true"));
         boolean includeBody = "true".equals(options.getOrDefault(INCLUDE_BODY, 
"true"));
@@ -106,9 +113,16 @@ public class BrowseDevConsole extends AbstractDevConsole {
             if (endpoint instanceof BrowsableEndpoint be
                     && (filter == null || 
PatternHelper.matchPattern(endpoint.getEndpointUri(), filter))) {
                 List<Exchange> list = be.getExchanges(max, null);
+                int queueSize = list != null ? list.size() : 0;
+                int begin = 0;
+                if (list != null && pos > 0 && pos < list.size()) {
+                    begin = list.size() - pos;
+                    list = list.subList(begin, list.size());
+                }
                 if (list != null) {
                     sb.append("\n");
-                    sb.append(String.format("Browse: %s (size: %d limit: 
%d)%n", endpoint.getEndpointUri(), list.size(), max));
+                    sb.append(String.format("Browse: %s (size: %d limit: %d 
position: %d)%n", endpoint.getEndpointUri(),
+                            queueSize, max, begin));
                     if (dump) {
                         for (Exchange e : list) {
                             String json
@@ -133,6 +147,8 @@ public class BrowseDevConsole extends AbstractDevConsole {
 
         String filter = (String) options.get(FILTER);
         String lim = (String) options.get(LIMIT);
+        String tail = (String) options.get(TAIL);
+        final int pos = tail == null ? 0 : Integer.parseInt(tail);
         final int max = lim == null ? limit : Integer.parseInt(lim);
         boolean dump = "true".equals(options.getOrDefault(DUMP, "true"));
         boolean includeBody = "true".equals(options.getOrDefault(INCLUDE_BODY, 
"true"));
@@ -144,11 +160,18 @@ public class BrowseDevConsole extends AbstractDevConsole {
             if (endpoint instanceof BrowsableEndpoint be
                     && (filter == null || 
PatternHelper.matchPattern(endpoint.getEndpointUri(), filter))) {
                 List<Exchange> list = be.getExchanges(max, null);
+                int queueSize = list != null ? list.size() : 0;
+                int begin = 0;
+                if (list != null && pos > 0 && pos < list.size()) {
+                    begin = list.size() - pos;
+                    list = list.subList(begin, list.size());
+                }
                 if (list != null) {
                     JsonObject jo = new JsonObject();
                     jo.put("endpointUri", endpoint.getEndpointUri());
-                    jo.put("queueSize", list.size());
+                    jo.put("queueSize", queueSize);
                     jo.put("limit", max);
+                    jo.put("position", begin);
                     arr.add(jo);
                     if (dump) {
                         JsonArray arr2 = new JsonArray();
diff --git 
a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
 
b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
index f9663d8f71b..047bc9d3fff 100644
--- 
a/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
+++ 
b/dsl/camel-cli-connector/src/main/java/org/apache/camel/cli/connector/LocalCliConnector.java
@@ -780,6 +780,7 @@ public class LocalCliConnector extends ServiceSupport 
implements CliConnector, C
             Map<String, Object> map = new HashMap<>();
             map.put("filter", root.getString("filter"));
             map.put("limit", root.getString("limit"));
+            map.put("tail", root.getString("tail"));
             map.put("dump", root.getString("dump"));
             map.put("includeBody", root.getString("includeBody"));
             String bodyMaxChars = root.getString("bodyMaxChars");
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java
index 56ebf965d17..6bf15aca63f 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelBrowseAction.java
@@ -67,13 +67,17 @@ public class CamelBrowseAction extends ActionBaseCommand {
                         description = "List endpoint URI in full details")
     boolean wideUri;
 
+    @CommandLine.Option(names = { "--dump" }, defaultValue = "false",
+                        description = "Whether to include message dumps")
+    boolean dump;
+
     @CommandLine.Option(names = { "--limit" }, defaultValue = "100",
                         description = "Limits the number of messages to dump 
per endpoint")
     int limit;
 
-    @CommandLine.Option(names = { "--dump" }, defaultValue = "false",
-                        description = "Whether to include message dumps")
-    boolean dump;
+    @CommandLine.Option(names = { "--tail" },
+                        description = "The number of messages from the end 
(latest) to dump")
+    int tail;
 
     @CommandLine.Option(names = { "--sort" }, completionCandidates = 
UriSizeCompletionCandidates.class,
                         description = "Sort by uri, or size", defaultValue = 
"uri")
@@ -129,6 +133,7 @@ public class CamelBrowseAction extends ActionBaseCommand {
         root.put("action", "browse");
         root.put("filter", endpoint == null ? "*" : endpoint);
         root.put("limit", limit);
+        root.put("tail", tail);
         root.put("dump", dump);
         root.put("includeBody", showBody);
         if (bodyMaxChars > 0) {
@@ -166,6 +171,7 @@ public class CamelBrowseAction extends ActionBaseCommand {
                     row.uri = o.getString("endpointUri");
                     row.queueSize = o.getInteger("queueSize");
                     row.limit = o.getInteger("limit");
+                    row.position = o.getInteger("position");
                     if (dump) {
                         row.messages = o.getCollection("messages");
                     }
@@ -217,7 +223,8 @@ public class CamelBrowseAction extends ActionBaseCommand {
                     JsonObject ep = new JsonObject();
                     ep.put("endpoint", row.uri);
                     String table = tableHelper.getDataAsTable(exchangeId, 
null, ep, null, message, null);
-                    String header = String.format("Browse Message: (%s/%s)", i 
+ 1, row.messages.size());
+                    String header = String.format("Browse Message: (%s/%s)", 
row.position + i + 1,
+                            row.position + row.messages.size());
                     if (loggingColor) {
                         
printer().println(Ansi.ansi().fgGreen().a(header).reset().toString());
                     } else {
@@ -282,6 +289,7 @@ public class CamelBrowseAction extends ActionBaseCommand {
         String uri;
         int queueSize;
         int limit;
+        int position;
         List<JsonObject> messages;
 
         Row copy() {

Reply via email to