use of org.hypertrace.gateway.service.v1.common.DomainEntityType in project gateway-service by hypertrace.
the class EntityInteractionsFetcher method buildQueryRequests.
@VisibleForTesting
Map<String, QueryRequest> buildQueryRequests(long startTime, long endTime, String spaceId, String entityType, InteractionsRequest interactionsRequest, Set<EntityKey> entityIds, boolean incoming, RequestContext requestContext) {
Set<String> entityTypes = getOtherEntityTypes(interactionsRequest.getFilter());
if (entityTypes.isEmpty()) {
return Collections.emptyMap();
}
QueryRequest.Builder builder = QueryRequest.newBuilder();
// Filter should include the timestamp filters from parent request first
Filter.Builder filterBuilder = Filter.newBuilder().setOperator(Operator.AND).addChildFilter(QueryRequestUtil.createBetweenTimesFilter(AttributeMetadataUtil.getTimestampAttributeId(metadataProvider, requestContext, SCOPE), startTime, endTime));
this.buildSpaceQueryFilterIfNeeded(requestContext, spaceId).ifPresent(filterBuilder::addChildFilter);
List<String> idColumns = getEntityIdColumnsFromInteraction(DomainEntityType.valueOf(entityType), !incoming);
// Add a filter on the entityIds
filterBuilder.addChildFilter(createFilterForEntityKeys(idColumns, entityIds));
// Group by the entity id column first, then the other end entity type for the interaction.
List<org.hypertrace.core.query.service.api.Expression> idExpressions = idColumns.stream().map(QueryRequestUtil::createAttributeExpression).collect(Collectors.toList());
builder.addAllGroupBy(idExpressions);
List<org.hypertrace.core.query.service.api.Expression> selections = new ArrayList<>();
for (Expression expression : interactionsRequest.getSelectionList()) {
// Ignore the predefined selections because they're handled specially.
if (ExpressionReader.isSimpleAttributeSelection(expression) && SELECTIONS_TO_IGNORE.contains(ExpressionReader.getAttributeIdFromAttributeSelection(expression).orElseThrow())) {
continue;
}
// Selection should have metrics and attributes that were requested
selections.add(QueryAndGatewayDtoConverter.convertToQueryExpression(expression).build());
}
// so we add count(*) as a dummy placeholder if there are no explicit selectors.
if (selections.isEmpty()) {
selections.add(QueryRequestUtil.createCountByColumnSelection(Optional.ofNullable(idColumns.get(0)).orElseThrow()));
}
QueryRequest protoType = builder.build();
Filter protoTypeFilter = filterBuilder.build();
Map<String, QueryRequest> queryRequests = new HashMap<>();
// response time.
for (String e : entityTypes) {
DomainEntityType otherEntityType = DomainEntityType.valueOf(e.toUpperCase());
// Get the filters from the interactions request to 'AND' them with the timestamp filter.
Filter.Builder filterCopy = Filter.newBuilder(protoTypeFilter);
filterCopy.addChildFilter(convertToQueryFilter(interactionsRequest.getFilter(), otherEntityType));
QueryRequest.Builder builderCopy = QueryRequest.newBuilder(protoType);
builderCopy.setFilter(filterCopy);
List<String> otherEntityIdColumns = getEntityIdColumnsFromInteraction(otherEntityType, incoming);
List<org.hypertrace.core.query.service.api.Expression> otherIdExpressions = otherEntityIdColumns.stream().map(QueryRequestUtil::createAttributeExpression).collect(Collectors.toList());
builderCopy.addAllGroupBy(otherIdExpressions);
// Add all selections in the correct order. First id, then other entity id and finally
// the remaining selections.
builderCopy.addAllSelection(idExpressions);
builderCopy.addAllSelection(otherIdExpressions);
selections.forEach(builderCopy::addSelection);
int limit = interactionsRequest.getLimit();
if (limit > 0) {
builderCopy.setLimit(limit);
} else {
builderCopy.setLimit(QueryServiceClient.DEFAULT_QUERY_SERVICE_GROUP_BY_LIMIT);
}
queryRequests.put(e, builderCopy.build());
}
return queryRequests;
}
Aggregations