Skip to content

Commit

Permalink
remove inner EnvironmentNode
Browse files Browse the repository at this point in the history
  • Loading branch information
aali309 committed Feb 28, 2024
1 parent 03da04d commit 6fca92c
Showing 1 changed file with 31 additions and 89 deletions.
120 changes: 31 additions & 89 deletions src/main/java/io/cryostat/graphql/EnvironmentNodes.java
Original file line number Diff line number Diff line change
@@ -1,95 +1,51 @@
/*
* 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 java.util.stream.Collectors;

import io.cryostat.discovery.DiscoveryNode;

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

import io.cryostat.discovery.DiscoveryNode;
import io.smallrye.graphql.api.Nullable;

@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;

}

public static class EnvironmentNode {
private Long id;
private String name;
private List<String> labels;
private String nodeType;
private List<EnvironmentNode> children;

public Long getId(){
return this.id;
}

public String getName(){
return this.name;
}

public List<String> getLabels(){
return this.labels;
}

public String getNodeType(){
return this.nodeType;
}

public List<EnvironmentNode> getChildren(){
return this.children;
}

public void setId(Long id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public void setLabels(List<String> labels) {
this.labels = labels;
}

public void setNodeType(String nodeType) {
this.nodeType = nodeType;
}

public void addChild(EnvironmentNode child) {
if (this.children == null) {
this.children = new ArrayList<>();
}
this.children.add(child);
}
@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<EnvironmentNode> environmentNodes(EnvironmentNodeFilterInput filter) {
public List<DiscoveryNode> environmentNodes(EnvironmentNodeFilterInput filter) {
DiscoveryNode rootNode = DiscoveryNode.getUniverse();
return filterAndTraverse(rootNode, filter).stream()
.map(EnvironmentNodes::convertDiscoveryNodeToEnvironmentNode)
.collect(Collectors.toList());
return filterAndTraverse(rootNode, filter);
}

private List<DiscoveryNode> filterAndTraverse(DiscoveryNode node, EnvironmentNodeFilterInput filter) {
private List<DiscoveryNode> filterAndTraverse(
DiscoveryNode node, EnvironmentNodeFilterInput filter) {
List<DiscoveryNode> filteredNodes = new ArrayList<>();
if (matchesFilter(node, filter)) {
filteredNodes.add(node);
Expand All @@ -103,31 +59,17 @@ private List<DiscoveryNode> filterAndTraverse(DiscoveryNode node, EnvironmentNod
}

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 -> node.labels.containsKey(label));
boolean matchesLabels =
filter.labels == null
|| filter.labels.stream().allMatch(label -> node.labels.containsKey(label));
boolean matchesNodeType = filter.nodeType == null || filter.nodeType.equals(node.nodeType);

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

private static EnvironmentNode convertDiscoveryNodeToEnvironmentNode(DiscoveryNode discoveryNode) {
EnvironmentNode envNode = new EnvironmentNode();
envNode.setId(discoveryNode.id);
envNode.setName(discoveryNode.name);
List<String> labelsList = discoveryNode.labels.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.toList());
envNode.setLabels(labelsList);
envNode.setNodeType(discoveryNode.nodeType);
if (discoveryNode.children != null) {
for (DiscoveryNode child : discoveryNode.children) {
envNode.addChild(convertDiscoveryNodeToEnvironmentNode(child));
}
}
return envNode;
}
}

0 comments on commit 6fca92c

Please sign in to comment.