Skip to content

Commit

Permalink
refactor, get target instance from script environment at execution time
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Dec 6, 2024
1 parent 46555b2 commit a429795
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/main/java/io/cryostat/expressions/MatchExpressionEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,7 @@ void onMessage(TargetDiscovery event) {
}
}

// used only for validating script syntax without an actual Target to evaluate against
Script createScript(String matchExpression) throws ScriptCreateException {
return createScript(null, matchExpression);
}

Script createScript(Target target, String matchExpression) throws ScriptCreateException {
ScriptCreation evt = new ScriptCreation();
try {
evt.begin();
Expand All @@ -118,7 +113,7 @@ Script createScript(Target target, String matchExpression) throws ScriptCreateEx
Decls.newVar(
"target",
Decls.newObjectType(SimplifiedTarget.class.getName())))
.withLibraries(List.of(new EventTypesLibrary(connectionManager, target)))
.withLibraries(List.of(new EventTypesLibrary(connectionManager)))
.build();
} finally {
evt.end();
Expand All @@ -130,7 +125,7 @@ Script createScript(Target target, String matchExpression) throws ScriptCreateEx

@CacheResult(cacheName = CACHE_NAME)
boolean load(Target target, String matchExpression) throws ScriptException {
Script script = createScript(target, matchExpression);
Script script = createScript(matchExpression);
return script.execute(Boolean.class, Map.of("target", SimplifiedTarget.from(target)));
}

Expand Down Expand Up @@ -236,6 +231,7 @@ public static class ScriptCreation extends Event {}
* expression-relevant fields exposed, connection URI exposed as a String, etc.
*/
private static record SimplifiedTarget(
long id,
boolean agent,
String connectUrl,
String alias,
Expand All @@ -255,6 +251,7 @@ private static record SimplifiedTarget(

static SimplifiedTarget from(Target target) {
return new SimplifiedTarget(
target.id,
target.isAgent(),
target.connectUrl.toString(),
target.alias,
Expand All @@ -267,11 +264,9 @@ static SimplifiedTarget from(Target target) {
static class EventTypesLibrary implements Library {

private final TargetConnectionManager connectionManager;
private final Target target;

EventTypesLibrary(TargetConnectionManager connectionManager, Target target) {
EventTypesLibrary(TargetConnectionManager connectionManager) {
this.connectionManager = connectionManager;
this.target = target;
}

@Override
Expand All @@ -282,23 +277,27 @@ public List<EnvOption> getCompileOptions() {
"eventTypeIds",
Decls.newOverload(
"eventTypeIds_void",
Collections.emptyList(),
List.of(
Decls.newObjectType(
SimplifiedTarget.class.getName())),
Decls.newListType(Decls.String)))));
}

@Override
public List<ProgramOption> getProgramOptions() {
return List.of(
ProgramOption.functions(
Overload.function(
Overload.unary(
"eventTypeIds",
(args) -> ListT.newStringArrayList(getEventTypeIds()))));
(arg) ->
ListT.newStringArrayList(
getEventTypeIds(
(SimplifiedTarget) arg.value())))));
}

private String[] getEventTypeIds() {
if (target == null) {
return new String[0];
}
private String[] getEventTypeIds(SimplifiedTarget st) {
Log.infov("Target: {0}", st);
Target target = Target.find("id", st.id()).singleResult();
try {
return connectionManager.executeConnectedTask(
target,
Expand Down

0 comments on commit a429795

Please sign in to comment.