Skip to content

Commit

Permalink
CAMEL-20419: Add get transformer jbang command
Browse files Browse the repository at this point in the history
  • Loading branch information
davsclaus committed Feb 14, 2024
1 parent d9949df commit 10d9e95
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,13 @@ protected void statusTask() {
root.put("variables", json);
}
}
DevConsole dc16 = dcr.resolveById("transformers");
if (dc16 != null) {
JsonObject json = (JsonObject) dc16.call(DevConsole.MediaType.JSON);
if (json != null && !json.isEmpty()) {
root.put("transformers", json);
}
}
}
// various details
JsonObject services = collectServices();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public static void run(CamelJBangMain main, String... args) {
.addSubcommand("inflight", new CommandLine(new ListInflight(main)))
.addSubcommand("blocked", new CommandLine(new ListBlocked(main)))
.addSubcommand("route-controller", new CommandLine(new RouteControllerAction(main)))
.addSubcommand("transformer", new CommandLine(new ListTransformer(main)))
.addSubcommand("circuit-breaker", new CommandLine(new ListCircuitBreaker(main)))
.addSubcommand("metric", new CommandLine(new ListMetric(main)))
.addSubcommand("service", new CommandLine(new ListService(main)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* 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.dsl.jbang.core.commands.process;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.github.freva.asciitable.AsciiTable;
import com.github.freva.asciitable.Column;
import com.github.freva.asciitable.HorizontalAlign;
import com.github.freva.asciitable.OverflowBehaviour;
import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
import org.apache.camel.dsl.jbang.core.common.ProcessHelper;
import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.json.JsonArray;
import org.apache.camel.util.json.JsonObject;
import picocli.CommandLine;
import picocli.CommandLine.Command;

@Command(name = "transformer", description = "Get list of data type transformers", sortOptions = false)
public class ListTransformer extends ProcessBaseCommand {

public static class PidNameAgeTotalCompletionCandidates implements Iterable<String> {

public PidNameAgeTotalCompletionCandidates() {
}

@Override
public Iterator<String> iterator() {
return List.of("pid", "name", "age", "total").iterator();
}

}

@CommandLine.Parameters(description = "Name or pid of running Camel integration", arity = "0..1")
String name = "*";

@CommandLine.Option(names = { "--sort" }, completionCandidates = PidNameAgeTotalCompletionCandidates.class,
description = "Sort by pid, name, age or total", defaultValue = "pid")
String sort;

public ListTransformer(CamelJBangMain main) {
super(main);
}

@Override
public Integer doCall() throws Exception {
List<Row> rows = new ArrayList<>();

List<Long> pids = findPids(name);
ProcessHandle.allProcesses()
.filter(ph -> pids.contains(ph.pid()))
.forEach(ph -> {
JsonObject root = loadStatus(ph.pid());
if (root != null) {
JsonObject context = (JsonObject) root.get("context");
JsonObject jo = (JsonObject) root.get("transformers");
if (context != null && jo != null) {
JsonArray array = (JsonArray) jo.get("transformers");
for (int i = 0; i < array.size(); i++) {
JsonObject o = (JsonObject) array.get(i);
Row row = new Row();
row.name = context.getString("name");
if ("CamelJBang".equals(row.name)) {
row.name = ProcessHelper.extractName(root, ph);
}
row.pid = Long.toString(ph.pid());
row.dataTypeName = o.getString("name");
row.dataTypeFrom = o.getString("from");
row.dataTypeTo = o.getString("to");
row.uptime = extractSince(ph);
row.age = TimeUtils.printSince(row.uptime);
rows.add(row);
}
}
}
});

// sort rows
rows.sort(this::sortRow);

if (!rows.isEmpty()) {
printTable(rows);
}

return 0;
}

protected void printTable(List<Row> rows) {
printer().println(AsciiTable.getTable(AsciiTable.NO_BORDERS, rows, Arrays.asList(
new Column().header("PID").headerAlign(HorizontalAlign.CENTER).with(r -> r.pid),
new Column().header("NAME").dataAlign(HorizontalAlign.LEFT).maxWidth(30, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(r -> r.name),
new Column().header("AGE").headerAlign(HorizontalAlign.CENTER).with(r -> r.age),
new Column().header("DATA-TYPE").dataAlign(HorizontalAlign.LEFT).with(r -> r.dataTypeName),
new Column().header("FROM").dataAlign(HorizontalAlign.LEFT).with(r -> r.dataTypeFrom),
new Column().header("TO").dataAlign(HorizontalAlign.LEFT).with(r -> r.dataTypeTo))));
}

protected int sortRow(Row o1, Row o2) {
String s = sort;
int negate = 1;
if (s.startsWith("-")) {
s = s.substring(1);
negate = -1;
}
switch (s) {
case "pid":
return Long.compare(Long.parseLong(o1.pid), Long.parseLong(o2.pid)) * negate;
case "name":
return o1.name.compareToIgnoreCase(o2.name) * negate;
case "age":
return Long.compare(o1.uptime, o2.uptime) * negate;
default:
return 0;
}
}

static class Row {
String pid;
String name;
long uptime;
String age;
String dataTypeName;
String dataTypeFrom;
String dataTypeTo;
}

}

0 comments on commit 10d9e95

Please sign in to comment.