Skip to content

Commit

Permalink
feat(graphql): add top-level query for environmentNodes (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
aali309 authored Feb 29, 2024
1 parent 4190012 commit fcc623f
Showing 1 changed file with 80 additions and 0 deletions.
80 changes: 80 additions & 0 deletions src/main/java/io/cryostat/graphql/EnvironmentNodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed 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 io.cryostat.graphql;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.cryostat.discovery.DiscoveryNode;
import io.cryostat.graphql.matchers.LabelSelectorMatcher;

import io.smallrye.graphql.api.Nullable;
import org.eclipse.microprofile.graphql.Description;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

@GraphQLApi
public class EnvironmentNodes {

public static class EnvironmentNodeFilterInput {
@Nullable public Long id;
@Nullable public String name;
@Nullable public List<String> names;
@Nullable public String nodeType;
@Nullable public List<String> labels;
}

@Query("environmentNodes")
@Description("Get all environment nodes in the discovery tree with optional filtering")
public List<DiscoveryNode> environmentNodes(EnvironmentNodeFilterInput filter) {
DiscoveryNode rootNode = DiscoveryNode.getUniverse();
return filterAndTraverse(rootNode, filter);
}

private List<DiscoveryNode> filterAndTraverse(
DiscoveryNode node, EnvironmentNodeFilterInput filter) {
List<DiscoveryNode> filteredNodes = new ArrayList<>();
if (matchesFilter(node, filter)) {
filteredNodes.add(node);
}
if (node.children != null) {
for (DiscoveryNode child : node.children) {
filteredNodes.addAll(filterAndTraverse(child, filter));
}
}
return filteredNodes;
}

private static boolean matchesFilter(DiscoveryNode node, EnvironmentNodeFilterInput filter) {
if (node.target != null) return false;
if (filter == null) return true;

boolean matchesId = filter.id == null || filter.id.equals(node.id);
boolean matchesName = filter.name == null || Objects.equals(filter.name, node.name);
boolean matchesNames = filter.names == null || filter.names.contains(node.name);
boolean matchesLabels =
filter.labels == null
|| filter.labels.stream()
.allMatch(
label ->
LabelSelectorMatcher.parse(label)
.test(node.labels));
boolean matchesNodeType = filter.nodeType == null || filter.nodeType.equals(node.nodeType);

return matchesId && matchesName && matchesNames && matchesLabels && matchesNodeType;
}
}

0 comments on commit fcc623f

Please sign in to comment.