Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore | adding the support for querying multi interaction filters #203

Merged
merged 8 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -21,6 +22,7 @@
import org.hypertrace.core.graphql.common.utils.Converter;
import org.hypertrace.gateway.service.v1.common.Expression;
import org.hypertrace.gateway.service.v1.common.Filter;
import org.hypertrace.gateway.service.v1.common.Operator;
import org.hypertrace.gateway.service.v1.entity.InteractionsRequest;
import org.hypertrace.graphql.entity.request.EdgeSetGroupRequest;
import org.hypertrace.graphql.metric.request.MetricAggregationRequest;
Expand Down Expand Up @@ -69,22 +71,30 @@
}

private Single<Filter> buildEntityInteractionFilter(EdgeSetGroupRequest request) {
return Observable.fromIterable(request.entityTypes()) // add entity types filter
.collect(Collectors.toUnmodifiableSet())
return Observable.fromIterable(request.filterArguments().entrySet())

Check warning on line 74 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java#L74

Added line #L74 was not covered by tests
.map(
entityTypes ->
AttributeAssociation.<FilterArgument>of(
request.neighborTypeAttribute().attributeExpressionAssociation().attribute(),
new EntityNeighborTypeFilter(
request.neighborTypeAttribute().attributeExpressionAssociation().value(),
entityTypes)))
.flatMap(
filterAssociation ->
this.filterConverter.convert(
Stream.concat(
request.filterArguments().stream(), // add all other filters
Stream.of(filterAssociation))
.collect(Collectors.toUnmodifiableSet())));
entry ->
Stream.concat(
Stream.of(buildEntityTypeFilter(request, entry.getKey())),
entry.getValue().stream())
.collect(Collectors.toUnmodifiableList()))
.flatMapSingle(this.filterConverter::convert)
.collect(Collectors.toUnmodifiableList())
.map(

Check warning on line 83 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java#L77-L83

Added lines #L77 - L83 were not covered by tests
childFilters ->
Filter.newBuilder()
aman-bansal marked this conversation as resolved.
Show resolved Hide resolved
.setOperator(Operator.OR)
.addAllChildFilter(childFilters)
.build());

Check warning on line 88 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java#L85-L88

Added lines #L85 - L88 were not covered by tests
}

private AttributeAssociation<FilterArgument> buildEntityTypeFilter(
EdgeSetGroupRequest request, String entityType) {
return AttributeAssociation.of(
request.neighborTypeAttribute().attributeExpressionAssociation().attribute(),

Check warning on line 94 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java#L93-L94

Added lines #L93 - L94 were not covered by tests
new EntityNeighborTypeFilter(
request.neighborTypeAttribute().attributeExpressionAssociation().value(),
List.of(entityType)));

Check warning on line 97 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/GatewayServiceEntityInteractionRequestBuilder.java#L96-L97

Added lines #L96 - L97 were not covered by tests
aman-bansal marked this conversation as resolved.
Show resolved Hide resolved
}

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ private static class EmptyEdgeSetGroupRequest implements EdgeSetGroupRequest {
Set<String> entityTypes = Collections.emptySet();
Collection<AttributeRequest> attributeRequests = Collections.emptyList();
Collection<MetricAggregationRequest> metricAggregationRequests = Collections.emptyList();
Collection<AttributeAssociation<FilterArgument>> filterArguments = Collections.emptyList();
Map<String, Collection<AttributeAssociation<FilterArgument>>> filterArguments =
Collections.emptyMap();
AttributeRequest neighborIdAttribute = null;
AttributeRequest neighborTypeAttribute = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import static io.reactivex.rxjava3.core.Single.zip;

import graphql.schema.SelectedField;
import io.grpc.Status;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Single;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -89,31 +90,26 @@
Stream<SelectedField> edgeSetFields,
EdgeType edgeType) {
Set<SelectedField> edgeFields = edgeSetFields.collect(Collectors.toUnmodifiableSet());
List<FilterArgument> filterArguments = this.getFilters(edgeFields);

if (!filterArguments.isEmpty() && edgeFields.size() > 1) {
throw Status.UNIMPLEMENTED
.withDescription("Cannot specify more than one edge type with edge filters")
.asRuntimeException();
}

Map<String, Set<SelectedField>> edgesByType = this.getEdgesByType(edgeFields.stream());
Set<SelectedField> allEdges =
edgesByType.values().stream()
Map<String, Set<SelectedField>> edgesSelectionsByType =
this.getEdgesSelectionByType(edgeFields.stream());
Set<SelectedField> allEdgesSelections =
edgesSelectionsByType.values().stream()

Check warning on line 96 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L93-L96

Added lines #L93 - L96 were not covered by tests
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableSet());

Map<String, Set<FilterArgument>> edgesFiltersByType =
getEdgesFiltersByType(edgeFields.stream());

Check warning on line 101 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L100-L101

Added lines #L100 - L101 were not covered by tests

return zip(
this.getRequestedAndRequiredAttributes(context, allEdges, edgeType),
this.getRequestedAndRequiredAttributes(context, allEdgesSelections, edgeType),

Check warning on line 104 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L104

Added line #L104 was not covered by tests
aman-bansal marked this conversation as resolved.
Show resolved Hide resolved
this.getNeighborIdAttribute(context, edgeType),
this.getNeighborTypeAttribute(context, edgeType),
this.metricAggregationRequestBuilder.build(
context, HypertraceAttributeScopeString.INTERACTION, allEdges.stream()),
this.filterRequestBuilder.build(
context, HypertraceAttributeScopeString.INTERACTION, filterArguments),
context, HypertraceAttributeScopeString.INTERACTION, allEdgesSelections.stream()),
this.getFilterArguments(context, edgesFiltersByType),

Check warning on line 109 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L108-L109

Added lines #L108 - L109 were not covered by tests
(attributeRequests, neighborIdRequest, neighborTypeRequest, metricRequests, filters) ->
new DefaultEdgeSetGroupRequest(
edgesByType.keySet(),
edgesSelectionsByType.keySet(),

Check warning on line 112 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L112

Added line #L112 was not covered by tests
attributeRequests,
metricRequests,
neighborIdRequest,
Expand All @@ -127,12 +123,12 @@
timeRange,
space,
neighborIds,
edgesByType.get(entityType)),
edgesSelectionsByType.get(entityType)),

Check warning on line 126 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L126

Added line #L126 was not covered by tests
filters));
}

private Map<String, Set<SelectedField>> getEdgesByType(Stream<SelectedField> edgeSetStream) {

private Map<String, Set<SelectedField>> getEdgesSelectionByType(
Stream<SelectedField> edgeSetStream) {
return edgeSetStream.collect(
Collectors.groupingBy(
this::getEntityType,
Expand Down Expand Up @@ -183,15 +179,34 @@
}
}

private List<FilterArgument> getFilters(Set<SelectedField> selectedFields) {
return selectedFields.stream()
.map(
selectedField ->
this.argumentDeserializer.deserializeObjectList(
selectedField.getArguments(), FilterArgument.class))
.flatMap(Optional::stream)
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableList());
private Single<Map<String, Collection<AttributeAssociation<FilterArgument>>>> getFilterArguments(
GraphQlRequestContext context, Map<String, Set<FilterArgument>> edgesFiltersByType) {
return Observable.fromIterable(edgesFiltersByType.entrySet())
.flatMapSingle(entry -> getFilterArgumentsEntry(context, entry.getKey(), entry.getValue()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));

Check warning on line 186 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L184-L186

Added lines #L184 - L186 were not covered by tests
}

private Single<Map.Entry<String, List<AttributeAssociation<FilterArgument>>>>
getFilterArgumentsEntry(
GraphQlRequestContext context, String key, Set<FilterArgument> edgeFields) {
return this.filterRequestBuilder
.build(context, HypertraceAttributeScopeString.INTERACTION, edgeFields)
.map(arguments -> Map.entry(key, arguments));

Check warning on line 194 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L192-L194

Added lines #L192 - L194 were not covered by tests
}

private Map<String, Set<FilterArgument>> getEdgesFiltersByType(
Stream<SelectedField> edgeSetStream) {
return edgeSetStream.collect(
Collectors.groupingBy(

Check warning on line 200 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L199-L200

Added lines #L199 - L200 were not covered by tests
this::getEntityType,
Collectors.flatMapping(this::getFilter, Collectors.toUnmodifiableSet())));

Check warning on line 202 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L202

Added line #L202 was not covered by tests
}

private Stream<FilterArgument> getFilter(SelectedField selectedField) {
return this.argumentDeserializer
.deserializeObjectList(selectedField.getArguments(), FilterArgument.class)
.stream()
.flatMap(Collection::stream);

Check warning on line 209 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L206-L209

Added lines #L206 - L209 were not covered by tests
}

private Single<AttributeRequest> getNeighborTypeAttribute(
Expand Down Expand Up @@ -227,7 +242,7 @@
AttributeRequest neighborIdAttribute;
AttributeRequest neighborTypeAttribute;
BiFunction<String, Collection<String>, Single<EntityRequest>> neighborRequestBuilder;
Collection<AttributeAssociation<FilterArgument>> filterArguments;
Map<String, Collection<AttributeAssociation<FilterArgument>>> filterArguments;

Check warning on line 245 in hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/request/EdgeRequestBuilder.java#L245

Added line #L245 was not covered by tests

@Override
public Single<EntityRequest> buildNeighborRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.reactivex.rxjava3.core.Single;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.hypertrace.core.graphql.common.request.AttributeAssociation;
import org.hypertrace.core.graphql.common.request.AttributeRequest;
Expand All @@ -23,5 +24,6 @@ public interface EdgeSetGroupRequest {

Single<EntityRequest> buildNeighborRequest(String entityType, Collection<String> neighborIds);

Collection<AttributeAssociation<FilterArgument>> filterArguments();
// map of filters. All map values will become OR filter and all filter arguments will be AND
Map<String, Collection<AttributeAssociation<FilterArgument>>> filterArguments();
}
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ private static class EmptyEdgeSetGroupRequest implements EdgeSetGroupRequest {
Set<String> entityTypes = Collections.emptySet();
Collection<AttributeRequest> attributeRequests = Collections.emptyList();
Collection<MetricAggregationRequest> metricAggregationRequests = Collections.emptyList();
Collection<AttributeAssociation<FilterArgument>> filterArguments = Collections.emptyList();
Map<String, Collection<AttributeAssociation<FilterArgument>>> filterArguments =
Collections.emptyMap();
AttributeRequest neighborIdAttribute = null;
AttributeRequest neighborTypeAttribute = null;

Expand Down