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 bdca0517dd6 camel-core - Route coverage dev console
bdca0517dd6 is described below

commit bdca0517dd65fe6b791e808f579a3818f53ac72a
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Sep 13 14:12:32 2022 +0200

    camel-core - Route coverage dev console
---
 .../org/apache/camel/dev-console/route-coverage    |   2 +
 .../impl/console/RouteCoverageDevConsole.java      | 169 +++++++++++++++++++++
 .../apache/camel/main/http/VertxHttpServer.java    |   2 +-
 3 files changed, 172 insertions(+), 1 deletion(-)

diff --git 
a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/route-coverage
 
b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/route-coverage
new file mode 100644
index 00000000000..f1efbfc9882
--- /dev/null
+++ 
b/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/route-coverage
@@ -0,0 +1,2 @@
+# Generated by camel build tools - do NOT edit this file!
+class=org.apache.camel.impl.console.RouteCoverageDevConsole
diff --git 
a/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteCoverageDevConsole.java
 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteCoverageDevConsole.java
new file mode 100644
index 00000000000..a0ca28c43f2
--- /dev/null
+++ 
b/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteCoverageDevConsole.java
@@ -0,0 +1,169 @@
+/*
+ * 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.camel.impl.console;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Route;
+import org.apache.camel.api.management.ManagedCamelContext;
+import org.apache.camel.api.management.mbean.ManagedProcessorMBean;
+import org.apache.camel.api.management.mbean.ManagedRouteMBean;
+import org.apache.camel.spi.annotations.DevConsole;
+import org.apache.camel.support.PatternHelper;
+import org.apache.camel.util.StringHelper;
+import org.apache.camel.util.json.JsonObject;
+
+@DevConsole("route-coverage")
+public class RouteCoverageDevConsole extends AbstractDevConsole {
+
+    /**
+     * Filters the routes matching by route id, route uri, and source location
+     */
+    public static final String FILTER = "filter";
+
+    /**
+     * Limits the number of entries displayed
+     */
+    public static final String LIMIT = "limit";
+
+    public RouteCoverageDevConsole() {
+        super("camel", "route-coverage", "Route Coverage", "Route coverage 
information");
+    }
+
+    @Override
+    protected String doCallText(Map<String, Object> options) {
+        final StringBuilder sb = new StringBuilder();
+        Function<ManagedRouteMBean, Object> task = mrb -> {
+            if (sb.length() > 0) {
+                sb.append("\n");
+            }
+            sb.append(String.format("    Id: %s", mrb.getRouteId()));
+            sb.append(String.format("\n    From: %s", mrb.getEndpointUri()));
+            if (mrb.getSourceLocation() != null) {
+                sb.append(String.format("\n    Source: %s", 
mrb.getSourceLocation()));
+            }
+            sb.append(String.format("\n    Total: %s", 
mrb.getExchangesTotal()));
+            String coverage = calculateRouteCoverage(mrb);
+            if (coverage != null) {
+                sb.append(String.format("\n    Coverage: %s", coverage));
+            }
+            sb.append("\n");
+            return null;
+        };
+        doCall(options, task);
+        return sb.toString();
+    }
+
+    @Override
+    protected JsonObject doCallJson(Map<String, Object> options) {
+        final JsonObject root = new JsonObject();
+        final List<JsonObject> list = new ArrayList<>();
+
+        Function<ManagedRouteMBean, Object> task = mrb -> {
+            JsonObject jo = new JsonObject();
+            list.add(jo);
+            jo.put("routeId", mrb.getRouteId());
+            jo.put("from", mrb.getEndpointUri());
+            if (mrb.getSourceLocation() != null) {
+                jo.put("source", mrb.getSourceLocation());
+            }
+            jo.put("exchangesTotal", mrb.getExchangesTotal());
+            String coverage = calculateRouteCoverage(mrb);
+            if (coverage != null) {
+                jo.put("routeCoverage", coverage);
+            }
+            return null;
+        };
+        doCall(options, task);
+        root.put("routes", list);
+        return root;
+    }
+
+    protected void doCall(Map<String, Object> options, 
Function<ManagedRouteMBean, Object> task) {
+        String path = (String) options.get(Exchange.HTTP_PATH);
+        String subPath = path != null ? StringHelper.after(path, "/") : null;
+        String filter = (String) options.get(FILTER);
+        String limit = (String) options.get(LIMIT);
+        final int max = limit == null ? Integer.MAX_VALUE : 
Integer.parseInt(limit);
+
+        ManagedCamelContext mcc = 
getCamelContext().getExtension(ManagedCamelContext.class);
+        if (mcc != null) {
+            List<Route> routes = getCamelContext().getRoutes();
+            routes.sort((o1, o2) -> 
o1.getRouteId().compareToIgnoreCase(o2.getRouteId()));
+            routes.stream()
+                    .map(route -> mcc.getManagedRoute(route.getRouteId()))
+                    .filter(r -> accept(r, filter))
+                    .filter(r -> accept(r, subPath))
+                    .sorted(RouteCoverageDevConsole::sort)
+                    .limit(max)
+                    .forEach(task::apply);
+        }
+    }
+
+    private static boolean accept(ManagedRouteMBean mrb, String filter) {
+        if (filter == null || filter.isBlank()) {
+            return true;
+        }
+
+        return PatternHelper.matchPattern(mrb.getRouteId(), filter)
+                || PatternHelper.matchPattern(mrb.getEndpointUri(), filter)
+                || PatternHelper.matchPattern(mrb.getSourceLocationShort(), 
filter);
+    }
+
+    private static int sort(ManagedRouteMBean o1, ManagedRouteMBean o2) {
+        // sort by id
+        return o1.getRouteId().compareTo(o2.getRouteId());
+    }
+
+    private String calculateRouteCoverage(ManagedRouteMBean mrb) {
+        ManagedCamelContext mcc = 
getCamelContext().getExtension(ManagedCamelContext.class);
+
+        Collection<String> ids;
+        try {
+            ids = mrb.processorIds();
+        } catch (Exception e) {
+            return null;
+        }
+
+        int total = ids.size();
+        int covered = 0;
+
+        for (String id : ids) {
+            ManagedProcessorMBean mp = mcc.getManagedProcessor(id);
+            if (mp != null) {
+                if (mp.getExchangesTotal() > 0) {
+                    covered++;
+                }
+            }
+        }
+
+        double percent;
+        if (total > 0) {
+            percent = (covered / total) * 100;
+        } else {
+            percent = 0;
+        }
+        String f = String.format("%.0f", percent);
+        return covered + "/" + total + " (" + f + "%)";
+    }
+
+}
diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/http/VertxHttpServer.java
 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/http/VertxHttpServer.java
index a9b6ad6685d..0d10807095a 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/http/VertxHttpServer.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/http/VertxHttpServer.java
@@ -261,7 +261,7 @@ public final class VertxHttpServer {
                         int p2 = id.indexOf(o2.getId());
                         return Integer.compare(p1, p2);
                     }).forEach(c -> {
-                        boolean include = "all".equals(id) || 
id.contains(c.getId());
+                        boolean include = "all".equals(id) || 
c.getId().equalsIgnoreCase(id);
                         if (include && c.supportMediaType(mediaType)) {
                             Object out = c.call(mediaType, params);
                             if (out != null && mediaType == 
DevConsole.MediaType.TEXT) {

Reply via email to