You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/09/14 05:21:06 UTC
[camel] branch main updated: camel-jbang - Add route coverage to camel get route
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 7e142a7ce71 camel-jbang - Add route coverage to camel get route
7e142a7ce71 is described below
commit 7e142a7ce718f5e4ee4687466060e407fda84ace
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Sep 14 07:20:52 2022 +0200
camel-jbang - Add route coverage to camel get route
---
.../org/apache/camel/dev-console/route-coverage | 2 -
.../impl/console/RouteCoverageDevConsole.java | 169 ---------------------
.../apache/camel/impl/console/RouteDevConsole.java | 46 ++++++
.../core/commands/process/CamelRouteStatus.java | 14 ++
.../jbang/core/commands/process/CamelRouteTop.java | 3 +
5 files changed, 63 insertions(+), 171 deletions(-)
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
deleted file mode 100644
index f1efbfc9882..00000000000
--- a/core/camel-console/src/generated/resources/META-INF/services/org/apache/camel/dev-console/route-coverage
+++ /dev/null
@@ -1,2 +0,0 @@
-# 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
deleted file mode 100644
index a0ca28c43f2..00000000000
--- a/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteCoverageDevConsole.java
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * 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/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java b/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java
index cb66d5bff8f..c788bf04cd8 100644
--- a/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java
+++ b/core/camel-console/src/main/java/org/apache/camel/impl/console/RouteDevConsole.java
@@ -17,6 +17,7 @@
package org.apache.camel.impl.console;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -25,6 +26,7 @@ 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;
@@ -63,6 +65,10 @@ public class RouteDevConsole extends AbstractDevConsole {
}
sb.append(String.format("\n State: %s", mrb.getState()));
sb.append(String.format("\n Uptime: %s", mrb.getUptime()));
+ String coverage = calculateRouteCoverage(mrb, true);
+ if (coverage != null) {
+ sb.append(String.format("\n Coverage: %s", coverage));
+ }
String load1 = getLoad1(mrb);
String load5 = getLoad5(mrb);
String load15 = getLoad15(mrb);
@@ -117,6 +123,10 @@ public class RouteDevConsole extends AbstractDevConsole {
jo.put("state", mrb.getState());
jo.put("uptime", mrb.getUptime());
JsonObject stats = new JsonObject();
+ String coverage = calculateRouteCoverage(mrb, false);
+ if (coverage != null) {
+ stats.put("coverage", coverage);
+ }
String load1 = getLoad1(mrb);
String load5 = getLoad5(mrb);
String load15 = getLoad15(mrb);
@@ -222,4 +232,40 @@ public class RouteDevConsole extends AbstractDevConsole {
return s;
}
+ private String calculateRouteCoverage(ManagedRouteMBean mrb, boolean percent) {
+ 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++;
+ }
+ }
+ }
+
+ if (percent) {
+ double p;
+ if (total > 0) {
+ p = (covered / total) * 100;
+ } else {
+ p = 0;
+ }
+ String f = String.format("%.0f", p);
+ return covered + "/" + total + " (" + f + "%)";
+ } else {
+ return covered + "/" + total;
+ }
+ }
+
}
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java
index 1c5b8d38b4c..59424264b48 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteStatus.java
@@ -102,6 +102,10 @@ public class CamelRouteStatus extends ProcessBaseCommand {
if (thp != null) {
row.throughput = thp.toString();
}
+ Object coverage = stats.get("coverage");
+ if (coverage != null) {
+ row.coverage = coverage.toString();
+ }
row.total = stats.get("exchangesTotal").toString();
row.inflight = stats.get("exchangesInflight").toString();
row.failed = stats.get("exchangesFailed").toString();
@@ -161,6 +165,7 @@ public class CamelRouteStatus extends ProcessBaseCommand {
new Column().header("STATUS").headerAlign(HorizontalAlign.CENTER)
.with(r -> r.state),
new Column().header("AGE").headerAlign(HorizontalAlign.CENTER).with(r -> r.age),
+ new Column().header("COVER").dataAlign(HorizontalAlign.CENTER).with(this::getCoverage),
new Column().header("MSG/S").with(this::getThroughput),
new Column().header("TOTAL").with(r -> r.total),
new Column().header("FAIL").with(r -> r.failed),
@@ -199,6 +204,14 @@ public class CamelRouteStatus extends ProcessBaseCommand {
return s;
}
+ protected String getCoverage(Row r) {
+ String s = r.coverage;
+ if (s == null || s.isEmpty()) {
+ s = "";
+ }
+ return s;
+ }
+
protected String getId(Row r) {
if (source && r.source != null) {
return sourceLocLine(r.source);
@@ -216,6 +229,7 @@ public class CamelRouteStatus extends ProcessBaseCommand {
String source;
String state;
String age;
+ String coverage;
String throughput;
String total;
String failed;
diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteTop.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteTop.java
index 3f94ceec9fa..942af6036d5 100644
--- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteTop.java
+++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/process/CamelRouteTop.java
@@ -70,6 +70,9 @@ public class CamelRouteTop extends CamelRouteStatus {
if ("0.00".equals(s3)) {
s3 = "-";
}
+ if (s1.equals("-") && s2.equals("-") && s3.equals("-")) {
+ return "0/0/0";
+ }
return s1 + "/" + s2 + "/" + s3;
}