Skip to content

Commit

Permalink
chore | adding the support for querying multi interaction filters
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-bansal committed Nov 27, 2023
1 parent 4055aaf commit 342e110
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 51 deletions.
2 changes: 1 addition & 1 deletion hypertrace-core-graphql
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ protected void configure() {
new TypeLiteral<
Converter<Collection<AttributeAssociation<FilterArgument>>, Filter>>() {}));

requireBinding(
Key.get(

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

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/EntityDaoModule.java#L68-L69

Added lines #L68 - L69 were not covered by tests
new TypeLiteral<
Converter<
Collection<Collection<AttributeAssociation<FilterArgument>>>, Filter>>() {}));

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

View check run for this annotation

Codecov / codecov/patch

hypertrace-graphql-entity-schema/src/main/java/org/hypertrace/graphql/entity/dao/EntityDaoModule.java#L72

Added line #L72 was not covered by tests

requireBinding(
Key.get(
new TypeLiteral<
Expand Down
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 @@ -31,13 +32,15 @@ class GatewayServiceEntityInteractionRequestBuilder {
private final Converter<Collection<AttributeRequest>, Set<Expression>> selectionConverter;
private final Converter<Collection<MetricAggregationRequest>, Set<Expression>>
aggregationConverter;
private final Converter<Collection<AttributeAssociation<FilterArgument>>, Filter> filterConverter;
private final Converter<Collection<Collection<AttributeAssociation<FilterArgument>>>, Filter>
filterConverter;

@Inject
GatewayServiceEntityInteractionRequestBuilder(
Converter<Collection<AttributeRequest>, Set<Expression>> selectionConverter,
Converter<Collection<MetricAggregationRequest>, Set<Expression>> aggregationConverter,
Converter<Collection<AttributeAssociation<FilterArgument>>, Filter> filterConverter) {
Converter<Collection<Collection<AttributeAssociation<FilterArgument>>>, Filter>
filterConverter) {

Check warning on line 43 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#L43

Added line #L43 was not covered by tests
this.selectionConverter = selectionConverter;
this.aggregationConverter = aggregationConverter;
this.filterConverter = filterConverter;
Expand Down Expand Up @@ -69,22 +72,28 @@ private Single<Set<Expression>> collectSelectionsAndAggregations(EdgeSetGroupReq
}

private Single<Filter> buildEntityInteractionFilter(EdgeSetGroupRequest request) {
return Observable.fromIterable(request.entityTypes()) // add entity types filter
.collect(Collectors.toUnmodifiableSet())
return this.filterConverter.convert(this.buildFilterArguments(request));

Check warning on line 75 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#L75

Added line #L75 was not covered by tests
}

private Collection<Collection<AttributeAssociation<FilterArgument>>> buildFilterArguments(
EdgeSetGroupRequest request) {
return request.filterArguments().entrySet().stream()

Check warning on line 80 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#L80

Added line #L80 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()))
.collect(Collectors.toUnmodifiableList());

Check warning on line 87 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#L83-L87

Added lines #L83 - L87 were not covered by tests
}

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

Check warning on line 93 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#L92-L93

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

Check warning on line 96 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#L95-L96

Added lines #L95 - L96 were not covered by tests
}

@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,11 @@
import static io.reactivex.rxjava3.core.Single.zip;

import graphql.schema.SelectedField;
import io.grpc.Status;
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 +89,30 @@ private Single<EdgeSetGroupRequest> buildEdgeRequest(
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 95 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#L92-L95

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

Map<String, Set<FilterArgument>> edgesFiltersByType =
getEdgesFiltersByType(edgeFields.stream());
Map<String, Collection<AttributeAssociation<FilterArgument>>> filterArguments =
edgesFiltersByType.entrySet().stream()
.collect(
Collectors.toMap(
Entry::getKey, entry -> getFilterArguments(context, entry.getValue())));

Check warning on line 105 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#L99-L105

Added lines #L99 - L105 were not covered by tests
return zip(
this.getRequestedAndRequiredAttributes(context, allEdges, edgeType),
this.getRequestedAndRequiredAttributes(context, allEdgesSelections, edgeType),

Check warning on line 107 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#L107

Added line #L107 was not covered by tests
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()),
Single.just(filterArguments),

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#L111-L112

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

Check warning on line 115 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#L115

Added line #L115 was not covered by tests
attributeRequests,
metricRequests,
neighborIdRequest,
Expand All @@ -127,12 +126,12 @@ private Single<EdgeSetGroupRequest> buildEdgeRequest(
timeRange,
space,
neighborIds,
edgesByType.get(entityType)),
edgesSelectionsByType.get(entityType)),

Check warning on line 129 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#L129

Added line #L129 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 +182,26 @@ private Single<AttributeRequest> getNeighborIdAttribute(
}
}

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 List<AttributeAssociation<FilterArgument>> getFilterArguments(
GraphQlRequestContext context, Set<FilterArgument> edgeFields) {
return this.filterRequestBuilder
.build(context, HypertraceAttributeScopeString.INTERACTION, edgeFields)
.blockingGet();

Check warning on line 189 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#L187-L189

Added lines #L187 - L189 were not covered by tests
}

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

Check warning on line 195 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#L194-L195

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

Check warning on line 197 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#L197

Added line #L197 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 204 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#L201-L204

Added lines #L201 - L204 were not covered by tests
}

private Single<AttributeRequest> getNeighborTypeAttribute(
Expand Down Expand Up @@ -227,7 +237,7 @@ private static class DefaultEdgeSetGroupRequest implements EdgeSetGroupRequest {
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 240 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#L240

Added line #L240 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

0 comments on commit 342e110

Please sign in to comment.