Skip to content

Commit

Permalink
CAMEL-21777: Add description output in camel-jbang. And fix descripti…
Browse files Browse the repository at this point in the history
…on in Java DSL
  • Loading branch information
davsclaus committed Feb 28, 2025
1 parent af32ee8 commit 4a948bb
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,84 @@ public Type nodePrefixId(String prefixId) {
return asType();
}

/**
* Sets the description of this node.
* <p/>
* <b>Important:</b> If you want to set the description of the route, then you <b>must</b> use {@link #routeDescription(String)}
* instead.
*
* @param description the description
* @return the builder
*/
@Override
public Type description(String description) {
// special for choice otherwise
if (this instanceof ChoiceDefinition cbr) {
if (cbr.getOtherwise() != null) {
if (cbr.getOtherwise().getOutputs().isEmpty()) {
cbr.getOtherwise().description(description);
} else {
var last = cbr.getOtherwise().getOutputs().get(cbr.getOtherwise().getOutputs().size() - 1);
last.description(description);
}
} else if (!cbr.getWhenClauses().isEmpty()) {
var last = cbr.getWhenClauses().get(cbr.getWhenClauses().size() - 1);
if (last.getOutputs().isEmpty()) {
last.setDescription(description);
} else {
var p = last.getOutputs().get(last.getOutputs().size() - 1);
p.description(description);
}
} else {
cbr.setDescription(description);
}
return asType();
}

if (this instanceof OutputNode && getOutputs().isEmpty()) {
// set description on this
setDescription(description);
} else {
List<ProcessorDefinition<?>> outputs = null;
if (this instanceof NoOutputDefinition<Type>) {
// this does not accept output so it should be on the parent
if (getParent() != null) {
outputs = getParent().getOutputs();
}
} else if (this instanceof OutputExpressionNode) {
outputs = getOutputs();
} else if (this instanceof ExpressionNode) {
// this does not accept output so it should be on the parent
if (getParent() != null) {
outputs = getParent().getOutputs();
}
} else {
outputs = getOutputs();
}

// set it on last output as this is what the user means to do
// for Block(s) with non empty getOutputs() the id probably refers
// to the last definition in the current Block
if (!blocks.isEmpty()) {
if (blocks.getLast() instanceof ProcessorDefinition) {
ProcessorDefinition<?> block = (ProcessorDefinition<?>) blocks.getLast();
if (!block.getOutputs().isEmpty()) {
outputs = block.getOutputs();
}
}
}
if (outputs != null && !outputs.isEmpty()) {
// set description on last output
outputs.get(outputs.size() - 1).setDescription(description);
} else {
// the output could be empty
setDescription(description);
}
}

return asType();
}

/**
* Disables this EIP from the route during build time. Once an EIP has been disabled then it cannot be enabled later
* at runtime.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public Iterator<String> iterator() {
description = "Filter processors that must be slower than the given time (ms)")
long mean;

@CommandLine.Option(names = { "--description" },
description = "Include description in the ID column (if available)")
boolean description;

public CamelProcessorStatus(CamelJBangMain main) {
super(main);
}
Expand Down Expand Up @@ -99,6 +103,7 @@ public Integer doProcessWatchCall() throws Exception {
}
row.pid = Long.toString(ph.pid());
row.routeId = o.getString("routeId");
row.description = o.getString("description");
row.nodePrefixId = o.getString("nodePrefixId");
row.processor = o.getString("from");
row.source = o.getString("source");
Expand Down Expand Up @@ -177,6 +182,7 @@ private void addProcessors(Row route, List<Row> rows, List<JsonObject> processor
row.processorId = o.getString("id");
row.nodePrefixId = o.getString("nodePrefixId");
row.processor = o.getString("processor");
row.description = o.getString("description");
row.level = o.getIntegerOrDefault("level", 0);
row.source = o.getString("source");
Map<String, ?> stats = o.getMap("statistics");
Expand Down Expand Up @@ -231,8 +237,12 @@ protected void printTable(List<Row> rows) {
new Column().header("PID").headerAlign(HorizontalAlign.CENTER).with(this::getPid),
new Column().header("NAME").dataAlign(HorizontalAlign.LEFT).maxWidth(30, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(this::getName),
new Column().header("ID").dataAlign(HorizontalAlign.LEFT).maxWidth(40, OverflowBehaviour.ELLIPSIS_RIGHT)
new Column().header("ID").visible(!description).dataAlign(HorizontalAlign.LEFT)
.maxWidth(40, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(this::getId),
new Column().header("ID").visible(description).dataAlign(HorizontalAlign.LEFT)
.maxWidth(60, OverflowBehaviour.NEWLINE)
.with(this::getIdAndDescription),
new Column().header("PROCESSOR").dataAlign(HorizontalAlign.LEFT).minWidth(25)
.maxWidth(45, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(this::getProcessor),
Expand Down Expand Up @@ -305,6 +315,18 @@ protected String getId(Row r) {
return answer;
}

protected String getIdAndDescription(Row r) {
String id = getId(r);
if (description && r.description != null) {
if (id != null) {
id = id + "\n " + r.description;
} else {
id = r.description;
}
}
return id;
}

protected String getPid(Row r) {
if (r.processorId == null) {
return r.pid;
Expand Down Expand Up @@ -334,6 +356,7 @@ static class Row {
String nodePrefixId;
String processorId;
String processor;
String description;
int level;
String source;
String state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ public class CamelRouteStatus extends ProcessWatchCommand {
description = "Shows detailed information for routes that has error status")
boolean error;

@CommandLine.Option(names = { "--description" },
description = "Include description in the ID column (if available)")
boolean description;

public CamelRouteStatus(CamelJBangMain main) {
super(main);
}
Expand Down Expand Up @@ -100,6 +104,7 @@ public Integer doProcessWatchCall() throws Exception {
}
row.pid = Long.toString(ph.pid());
row.routeId = o.getString("routeId");
row.description = o.getString("description");
row.from = o.getString("from");
Boolean bool = o.getBoolean("remote");
if (bool != null) {
Expand Down Expand Up @@ -212,8 +217,12 @@ protected void printTable(List<Row> rows, boolean remoteVisible) {
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("ID").dataAlign(HorizontalAlign.LEFT).maxWidth(20, OverflowBehaviour.ELLIPSIS_RIGHT)
new Column().header("ID").visible(!description).dataAlign(HorizontalAlign.LEFT)
.maxWidth(20, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(this::getId),
new Column().header("ID").visible(description).dataAlign(HorizontalAlign.LEFT)
.maxWidth(45, OverflowBehaviour.NEWLINE)
.with(this::getIdAndDescription),
new Column().header("FROM").visible(!wideUri).dataAlign(HorizontalAlign.LEFT)
.maxWidth(45, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(this::getFrom),
Expand Down Expand Up @@ -244,8 +253,12 @@ protected void printErrorTable(Row er, boolean remoteVisible) {
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("ID").dataAlign(HorizontalAlign.LEFT).maxWidth(20, OverflowBehaviour.ELLIPSIS_RIGHT)
new Column().header("ID").visible(!description).dataAlign(HorizontalAlign.LEFT)
.maxWidth(20, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(this::getId),
new Column().header("ID").visible(description).dataAlign(HorizontalAlign.LEFT)
.maxWidth(45, OverflowBehaviour.NEWLINE)
.with(this::getIdAndDescription),
new Column().header("FROM").visible(!wideUri).dataAlign(HorizontalAlign.LEFT)
.maxWidth(45, OverflowBehaviour.ELLIPSIS_RIGHT)
.with(this::getFrom),
Expand Down Expand Up @@ -354,6 +367,18 @@ protected String getId(Row r) {
}
}

protected String getIdAndDescription(Row r) {
String id = getId(r);
if (description && r.description != null) {
if (id != null) {
id = id + "\n " + r.description;
} else {
id = r.description;
}
}
return id;
}

protected String getDelta(Row r) {
if (r.delta != null) {
if (r.delta.startsWith("-")) {
Expand All @@ -371,6 +396,7 @@ static class Row {
String name;
long uptime;
String routeId;
String description;
String from;
boolean remote;
String source;
Expand Down

0 comments on commit 4a948bb

Please sign in to comment.