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 4 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 @@ -30,6 +30,7 @@
import org.slf4j.LoggerFactory;

class GatewayServiceEntityEdgeFetcher {

private static final Logger LOG = LoggerFactory.getLogger(GatewayServiceEntityEdgeFetcher.class);
static final EdgeResultSet EMPTY_EDGE_RESULT_SET = new ConvertedEdgeResultSet(List.of());

Expand Down Expand Up @@ -119,16 +120,25 @@ private Maybe<Edge> buildEdge(

return zip(
this.attributeMapConverter.convert(
edgeSetGroupRequest.attributeRequests(), response.getAttributeMap()),
edgeSetGroupRequest
.edgeSetRequests()
.get(source.getEntityType())
.attributeRequests(),
response.getAttributeMap()),
this.baselineMetricAggregationContainerMapConverter.convert(
edgeSetGroupRequest.metricAggregationRequests(), response.getMetricsMap()),
edgeSetGroupRequest
.edgeSetRequests()
.get(source.getEntityType())
.metricAggregationRequests(),
response.getMetricsMap()),
(attributes, metrics) -> (Edge) new ConvertedEdge(neighbor, attributes, metrics))
.toMaybe();
}

@lombok.Value
@Accessors(fluent = true)
private static class ConvertedEdge implements Edge {

Entity neighbor;
Map<AttributeExpression, Object> attributeValues;
Map<AttributeExpression, BaselinedMetricAggregationContainer> metricContainers;
Expand All @@ -147,6 +157,7 @@ public BaselinedMetricAggregationContainer metric(AttributeExpression attributeE
@lombok.Value
@Accessors(fluent = true)
private static class ConvertedEdgeResultSet implements EdgeResultSet {

List<Edge> results;

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
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.entity.request.EdgeSetRequest;
import org.hypertrace.graphql.metric.request.MetricAggregationRequest;

class GatewayServiceEntityInteractionRequestBuilder {
Expand All @@ -44,7 +46,7 @@ class GatewayServiceEntityInteractionRequestBuilder {
}

Single<InteractionsRequest> build(EdgeSetGroupRequest edgeSetRequestGroup) {
if (edgeSetRequestGroup.entityTypes().isEmpty()) {
if (edgeSetRequestGroup.edgeSetRequests().isEmpty()) {
return Single.just(InteractionsRequest.getDefaultInstance());
}

Expand All @@ -61,40 +63,64 @@ Single<InteractionsRequest> build(EdgeSetGroupRequest edgeSetRequestGroup) {

private Single<Set<Expression>> collectSelectionsAndAggregations(EdgeSetGroupRequest request) {
return this.selectionConverter
.convert(request.attributeRequests())
.mergeWith(this.aggregationConverter.convert(request.metricAggregationRequests()))
.convert(getAllAttributeRequests(request))
.mergeWith(this.aggregationConverter.convert(getAllMetricAggregationRequests(request)))
.toObservable()
.flatMap(Observable::fromIterable)
.collect(Collectors.toUnmodifiableSet());
}

private Set<AttributeRequest> getAllAttributeRequests(EdgeSetGroupRequest request) {
return request.edgeSetRequests().values().stream()
.map(EdgeSetRequest::attributeRequests)
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableSet());
}

private Set<MetricAggregationRequest> getAllMetricAggregationRequests(
aman-bansal marked this conversation as resolved.
Show resolved Hide resolved
EdgeSetGroupRequest request) {
return request.edgeSetRequests().values().stream()
.map(EdgeSetRequest::metricAggregationRequests)
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableSet());
}

private Single<Filter> buildEntityInteractionFilter(EdgeSetGroupRequest request) {
return Observable.fromIterable(request.entityTypes()) // add entity types filter
.collect(Collectors.toUnmodifiableSet())
// Todo: we should be using converter taking argument as logical filters with filter arg schema
return Observable.fromIterable(request.edgeSetRequests().entrySet())
.map(
entry ->
Stream.concat(
Stream.of(buildEntityTypeFilter(request, entry.getKey())),
entry.getValue().filterArguments().stream())
.collect(Collectors.toUnmodifiableList()))
.flatMapSingle(this.filterConverter::convert)
.collect(Collectors.toUnmodifiableList())
.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())));
childFilters ->
Filter.newBuilder()
aman-bansal marked this conversation as resolved.
Show resolved Hide resolved
.setOperator(Operator.OR)
.addAllChildFilter(childFilters)
.build());
}

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

@Value
@Accessors(fluent = true)
private static class EntityNeighborTypeFilter implements FilterArgument {

FilterType type = FilterType.ATTRIBUTE;
String key = null;
AttributeExpression keyExpression;
FilterOperatorType operator = FilterOperatorType.IN;
Collection<String> value;
FilterOperatorType operator = FilterOperatorType.EQUALS;
String value;
AttributeScope idType = null;
String idScope = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
import org.hypertrace.core.graphql.utils.schema.SelectionQuery;
import org.hypertrace.graphql.entity.dao.EntityDao;
import org.hypertrace.graphql.entity.request.EdgeSetGroupRequest;
import org.hypertrace.graphql.entity.request.EdgeSetRequest;
import org.hypertrace.graphql.entity.request.EntityLabelRequest;
import org.hypertrace.graphql.entity.request.EntityLabelRequestBuilder;
import org.hypertrace.graphql.entity.request.EntityRequest;
import org.hypertrace.graphql.entity.schema.Entity;
import org.hypertrace.graphql.entity.schema.EntityJoinable;
import org.hypertrace.graphql.entity.schema.EntityResultSet;
import org.hypertrace.graphql.entity.schema.argument.EntityTypeStringArgument;
import org.hypertrace.graphql.metric.request.MetricAggregationRequest;
import org.hypertrace.graphql.metric.request.MetricRequest;
import org.hypertrace.graphql.metric.request.MetricRequestBuilder;
import org.hypertrace.graphql.metric.schema.argument.AggregatableOrderArgument;
Expand Down Expand Up @@ -317,12 +317,9 @@ private static class DefaultEntityRequest implements EntityRequest {
@Value
@Accessors(fluent = true)
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();
AttributeRequest neighborIdAttribute = null;
AttributeRequest neighborTypeAttribute = null;
Map<String, EdgeSetRequest> edgeSetRequests = Collections.emptyMap();

@Override
public Single<EntityRequest> buildNeighborRequest(
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,35 +90,18 @@ 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()
.flatMap(Collection::stream)
.collect(Collectors.toUnmodifiableSet());
Map<String, Set<SelectedField>> edgesSelectionsByType =
this.getEdgesSelectionByType(edgeFields.stream());

return zip(
this.getRequestedAndRequiredAttributes(context, allEdges, edgeType),
this.getNeighborIdAttribute(context, edgeType),
this.getNeighborTypeAttribute(context, edgeType),
this.metricAggregationRequestBuilder.build(
context, HypertraceAttributeScopeString.INTERACTION, allEdges.stream()),
this.filterRequestBuilder.build(
context, HypertraceAttributeScopeString.INTERACTION, filterArguments),
(attributeRequests, neighborIdRequest, neighborTypeRequest, metricRequests, filters) ->
this.getEntityTypeToEdgeSetRequest(context, edgeType, edgeFields),
(neighborIdRequest, neighborTypeRequest, edgeRequests) ->
new DefaultEdgeSetGroupRequest(
edgesByType.keySet(),
attributeRequests,
metricRequests,
neighborIdRequest,
neighborTypeRequest,
edgeRequests,
(entityType, neighborIds) ->
this.neighborEntitiesRequestBuilderProvider
.get()
Expand All @@ -127,12 +111,40 @@ private Single<EdgeSetGroupRequest> buildEdgeRequest(
timeRange,
space,
neighborIds,
edgesByType.get(entityType)),
filters));
edgesSelectionsByType.get(entityType))));
}

private Single<Map<String, EdgeSetRequest>> getEntityTypeToEdgeSetRequest(
GraphQlRequestContext context, EdgeType edgeType, Set<SelectedField> edgeFields) {
return Observable.fromIterable(edgeFields)
.collect(Collectors.groupingBy(this::getEntityType, Collectors.toUnmodifiableSet()))
.flatMap(entry -> this.getEdgeSetRequest(context, edgeType, entry));
aman-bansal marked this conversation as resolved.
Show resolved Hide resolved
}

private Map<String, Set<SelectedField>> getEdgesByType(Stream<SelectedField> edgeSetStream) {
private Single<Map<String, EdgeSetRequest>> getEdgeSetRequest(
GraphQlRequestContext context,
EdgeType edgeType,
Map<String, Set<SelectedField>> edgeFields) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general, be careful with the naming especial with fields since they can represent any part of the query. These, if I'm following correctly represent a map of entity types to a set of edge set fields (e.g. outgoingEdges( neighborScope: "SERVICE") would be one entry under the service key) rather than the fields that define an actual edge.

I missed this earlier but since we group at the level of edge scope + edge type (e.g. outgoing services), that means if I had two edge sets of the same type with different filters, the filters would be ANDed. Is that desirable? If yes, can leave. If no, you'd want to avoid the grouping by type and create a new EdgeSetRequest for every edge set field and hold them as a collection rather than mapped by entity type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any use case for having different filters for the same set of edges. We had the same issue when we built the use case of a third-party application flow and decided to make the filters as AND across all edges defined. I think we can leave like this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed other arguments to be more meaningful

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any use case for having different filters for the same set of edges.

Disagree - there's plenty of compelling use cases here. I want to get a set of by slow downstream services and a set of my high error rate downstream services, for example. It's not just about AND/OR - it's what the caller wants to separate e.g. downstream external APIs vs downstream internal APIs.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might eventually want to change this, but can defer until we have a need.

return Observable.fromIterable(edgeFields.entrySet())
.flatMapSingle(
entry ->
this.getEdgeSetRequestEntry(context, edgeType, entry.getKey(), entry.getValue()))
.collect(Collectors.toUnmodifiableMap(Entry::getKey, Entry::getValue));
}

private Single<Map.Entry<String, EdgeSetRequest>> getEdgeSetRequestEntry(
GraphQlRequestContext context, EdgeType edgeType, String key, Set<SelectedField> edgeFields) {
aaron-steinfeld marked this conversation as resolved.
Show resolved Hide resolved
return zip(
this.getRequestedAndRequiredAttributes(context, edgeFields, edgeType),
this.getMetricAggregationRequestAttributes(context, edgeFields, edgeType),
this.getFilterArguments(context, edgeFields),
(requestAttributes, metricAttributes, filters) ->
Map.entry(
key, new DefaultEdgeSetRequest(requestAttributes, metricAttributes, filters)));
}

private Map<String, Set<SelectedField>> getEdgesSelectionByType(
Stream<SelectedField> edgeSetStream) {
return edgeSetStream.collect(
Collectors.groupingBy(
this::getEntityType,
Expand All @@ -155,11 +167,25 @@ private String getEntityType(SelectedField edgeSetField) {
.orElseThrow();
}

private Single<List<MetricAggregationRequest>> getMetricAggregationRequestAttributes(
GraphQlRequestContext context, Collection<SelectedField> edges, EdgeType edgeType) {
Set<SelectedField> selections =
edges.stream()
.collect(
Collectors.flatMapping(this::getEdgesForEdgeSet, Collectors.toUnmodifiableSet()));
return this.metricAggregationRequestBuilder.build(
context, HypertraceAttributeScopeString.INTERACTION, selections.stream());
}

private Single<List<AttributeRequest>> getRequestedAndRequiredAttributes(
GraphQlRequestContext context, Collection<SelectedField> edges, EdgeType edgeType) {
Set<SelectedField> selections =
edges.stream()
.collect(
Collectors.flatMapping(this::getEdgesForEdgeSet, Collectors.toUnmodifiableSet()));
return this.attributeRequestBuilder
.buildForAttributeQueryableFields(
context, HypertraceAttributeScopeString.INTERACTION, edges.stream())
context, HypertraceAttributeScopeString.INTERACTION, selections.stream())
.mergeWith(this.getNeighborIdAttribute(context, edgeType))
.mergeWith(this.getNeighborTypeAttribute(context, edgeType))
.collect(Collectors.toUnmodifiableList());
Expand All @@ -183,15 +209,21 @@ 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 Single<List<AttributeAssociation<FilterArgument>>> getFilterArguments(
GraphQlRequestContext context, Set<SelectedField> edgeFields) {
Set<FilterArgument> filterArguments =
edgeFields.stream()
.collect(Collectors.flatMapping(this::getFilter, Collectors.toUnmodifiableSet()));

return this.filterRequestBuilder.build(
context, HypertraceAttributeScopeString.INTERACTION, filterArguments);
}

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

private Single<AttributeRequest> getNeighborTypeAttribute(
Expand Down Expand Up @@ -220,19 +252,23 @@ private enum EdgeType {
@Value
@Accessors(fluent = true)
private static class DefaultEdgeSetGroupRequest implements EdgeSetGroupRequest {

Set<String> entityTypes;
Collection<AttributeRequest> attributeRequests;
Collection<MetricAggregationRequest> metricAggregationRequests;
AttributeRequest neighborIdAttribute;
AttributeRequest neighborTypeAttribute;
Map<String, EdgeSetRequest> edgeSetRequests;
BiFunction<String, Collection<String>, Single<EntityRequest>> neighborRequestBuilder;
Collection<AttributeAssociation<FilterArgument>> filterArguments;

@Override
public Single<EntityRequest> buildNeighborRequest(
String entityType, Collection<String> neighborIds) {
return this.neighborRequestBuilder.apply(entityType, neighborIds);
}
}

@Value
@Accessors(fluent = true)
private static class DefaultEdgeSetRequest implements EdgeSetRequest {
Collection<AttributeRequest> attributeRequests;
Collection<MetricAggregationRequest> metricAggregationRequests;
Collection<AttributeAssociation<FilterArgument>> filterArguments;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@

import io.reactivex.rxjava3.core.Single;
import java.util.Collection;
import java.util.Set;
import org.hypertrace.core.graphql.common.request.AttributeAssociation;
import java.util.Map;
import org.hypertrace.core.graphql.common.request.AttributeRequest;
import org.hypertrace.core.graphql.common.schema.results.arguments.filter.FilterArgument;
import org.hypertrace.graphql.metric.request.MetricAggregationRequest;

public interface EdgeSetGroupRequest {

Set<String> entityTypes();

// Includes neighbor id and type
Collection<AttributeRequest> attributeRequests();

Collection<MetricAggregationRequest> metricAggregationRequests();

AttributeRequest neighborIdAttribute();

AttributeRequest neighborTypeAttribute();

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

Collection<AttributeAssociation<FilterArgument>> filterArguments();
Map<String, EdgeSetRequest> edgeSetRequests();
}
Loading