use of org.hypertrace.gateway.service.entity.query.DataFetcherNode in project gateway-service by hypertrace.
the class ExecutionContextBuilderVisitorTest method shouldKeepPendingSelectionSourcesForOrderBy.
@Test
public void shouldKeepPendingSelectionSourcesForOrderBy() {
DataFetcherNode dataFetcherNode = new DataFetcherNode("QS", null);
// API.id -> ["QS", "EDS"]
// API.name -> ["QS", "EDS"]
when(expressionContext.getSourceToSelectionAttributeMap()).thenReturn(Map.of("QS", Set.of("API.id", "API.name"), "EDS", Set.of("API.id", "API.name")));
when(expressionContext.getSourceToSelectionOrderByAttributeMap()).thenReturn(Map.of("QS", Set.of("API.id"), "EDS", Set.of("API.status")));
when(executionContext.getPendingSelectionSources()).thenReturn(Set.of("EDS"));
when(executionContext.getPendingSelectionSourcesForOrderBy()).thenReturn(Set.of("EDS"));
dataFetcherNode.acceptVisitor(new ExecutionContextBuilderVisitor(executionContext));
verify(executionContext).removePendingSelectionSource("QS");
verify(executionContext).removePendingSelectionSourceForOrderBy("QS");
verify(executionContext).removePendingSelectionSource("EDS");
verify(executionContext, never()).removePendingSelectionSourceForOrderBy("EDS");
}
use of org.hypertrace.gateway.service.entity.query.DataFetcherNode in project gateway-service by hypertrace.
the class ExecutionVisitorTest method test_visitDataFetcherNodeWithoutPagination.
@Test
public void test_visitDataFetcherNodeWithoutPagination() {
long startTime = 0;
long endTime = 10;
String tenantId = "TENANT_ID";
Map<String, String> requestHeaders = Map.of("x-tenant-id", tenantId);
AttributeScope entityType = AttributeScope.API;
Expression selectionExpression = buildExpression(API_NAME_ATTR);
EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(entityType.name()).setStartTimeMillis(startTime).setEndTimeMillis(endTime).addSelection(selectionExpression).setFilter(generateEQFilter(API_DISCOVERY_STATE, "DISCOVERED")).build();
EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
Map<EntityKey, Builder> entityKeyBuilderResponseMap = Map.of(EntityKey.of("entity-id-0"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-0")), EntityKey.of("entity-id-1"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-1")), EntityKey.of("entity-id-2"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-2")));
EntityFetcherResponse entityFetcherResponse = new EntityFetcherResponse(entityKeyBuilderResponseMap);
when(expressionContext.getSourceToSelectionExpressionMap()).thenReturn(Map.of("QS", List.of(selectionExpression)));
when(executionContext.getEntitiesRequest()).thenReturn(entitiesRequest);
when(executionContext.getTenantId()).thenReturn(tenantId);
when(executionContext.getRequestHeaders()).thenReturn(requestHeaders);
when(executionContext.getTimestampAttributeId()).thenReturn("API.startTime");
when(queryServiceEntityFetcher.getEntities(eq(entitiesRequestContext), eq(entitiesRequest))).thenReturn(entityFetcherResponse);
when(queryServiceEntityFetcher.getTimeAggregatedMetrics(eq(entitiesRequestContext), eq(entitiesRequest))).thenReturn(new EntityFetcherResponse());
// no pagination in data fetcher node
DataFetcherNode dataFetcherNode = new DataFetcherNode("QS", entitiesRequest.getFilter());
compareEntityResponses(new EntityResponse(entityFetcherResponse, entityFetcherResponse.getEntityKeyBuilderMap().size()), executionVisitor.visit(dataFetcherNode));
verify(queryServiceEntityFetcher, times(1)).getEntities(any(), any());
}
use of org.hypertrace.gateway.service.entity.query.DataFetcherNode in project gateway-service by hypertrace.
the class ExecutionVisitorTest method test_visitPaginateOnlyNode.
@Test
public void test_visitPaginateOnlyNode() {
List<OrderByExpression> orderByExpressions = List.of(buildOrderByExpression(API_ID_ATTR));
int limit = 2;
int offset = 2;
long startTime = 0;
long endTime = 10;
String tenantId = "TENANT_ID";
Map<String, String> requestHeaders = Map.of("x-tenant-id", tenantId);
AttributeScope entityType = AttributeScope.API;
Expression selectionExpression = buildExpression(API_NAME_ATTR);
Expression metricExpression = buildAggregateExpression(API_DURATION_ATTR, FunctionType.AVG, "AVG_API.duration", List.of());
TimeAggregation timeAggregation = buildTimeAggregation(30, API_NUM_CALLS_ATTR, FunctionType.SUM, "SUM_API.numCalls", List.of());
EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(entityType.name()).setStartTimeMillis(startTime).setEndTimeMillis(endTime).addSelection(selectionExpression).addSelection(metricExpression).addTimeAggregation(timeAggregation).setFilter(generateEQFilter(API_DISCOVERY_STATE, "DISCOVERED")).addAllOrderBy(orderByExpressions).setLimit(limit).setOffset(offset).build();
EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
// Order matters since we will do the pagination ourselves. So we use a LinkedHashMap
Map<EntityKey, Builder> entityKeyBuilderResponseMap1 = new LinkedHashMap<>();
entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-0"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-0")));
entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-1"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-1")));
entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-2")));
entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-3")));
Map<EntityKey, Builder> entityKeyBuilderResponseMap2 = new LinkedHashMap<>();
entityKeyBuilderResponseMap2.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 14.0)));
entityKeyBuilderResponseMap2.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 15.0)));
Map<EntityKey, Builder> entityKeyBuilderResponseMap3 = new LinkedHashMap<>();
entityKeyBuilderResponseMap3.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
entityKeyBuilderResponseMap3.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
Map<EntityKey, Builder> expectedEntityKeyBuilderResponseMap = new LinkedHashMap<>();
expectedEntityKeyBuilderResponseMap.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-2")).putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 14.0)).putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
expectedEntityKeyBuilderResponseMap.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-3")).putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 15.0)).putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
when(executionContext.getEntityIdExpressions()).thenReturn(List.of(buildExpression(API_ID_ATTR)));
when(expressionContext.getSourceToSelectionExpressionMap()).thenReturn(Map.of("QS", List.of(selectionExpression)));
when(expressionContext.getSourceToMetricExpressionMap()).thenReturn(Map.of("QS", List.of(metricExpression)));
when(expressionContext.getSourceToTimeAggregationMap()).thenReturn(Map.of("QS", List.of(timeAggregation)));
when(executionContext.getEntitiesRequest()).thenReturn(entitiesRequest);
when(executionContext.getTenantId()).thenReturn(tenantId);
when(executionContext.getRequestHeaders()).thenReturn(requestHeaders);
when(executionContext.getTimestampAttributeId()).thenReturn("API.startTime");
EntitiesRequest entitiesRequestForAttributes = EntitiesRequest.newBuilder(entitiesRequest).clearSelection().clearTimeAggregation().addSelection(selectionExpression).setLimit(limit + offset).setOffset(0).build();
EntityFetcherResponse attributesResponse = new EntityFetcherResponse(entityKeyBuilderResponseMap1);
EntitiesRequest entitiesRequestForMetricAggregation = EntitiesRequest.newBuilder(entitiesRequest).clearLimit().clearOffset().clearOrderBy().clearSelection().clearTimeAggregation().addSelection(metricExpression).clearFilter().setFilter(generateInFilter(API_ID_ATTR, List.of("entity-id-3", "entity-id-2"))).build();
EntitiesRequest entitiesRequestForTimeAggregation = EntitiesRequest.newBuilder(entitiesRequest).clearSelection().clearLimit().clearOffset().clearOrderBy().clearFilter().setFilter(generateInFilter(API_ID_ATTR, List.of("entity-id-3", "entity-id-2"))).build();
when(queryServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequestForAttributes)).thenReturn(attributesResponse);
when(queryServiceEntityFetcher.getTotal(eq(entitiesRequestContext), eq(entitiesRequest))).thenReturn(100L);
when(queryServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequestForMetricAggregation)).thenReturn(new EntityFetcherResponse(entityKeyBuilderResponseMap2));
when(queryServiceEntityFetcher.getTimeAggregatedMetrics(entitiesRequestContext, entitiesRequestForTimeAggregation)).thenReturn(new EntityFetcherResponse(entityKeyBuilderResponseMap3));
DataFetcherNode dataFetcherNode = new DataFetcherNode("QS", entitiesRequest.getFilter(), limit + offset, 0, orderByExpressions, true);
PaginateOnlyNode paginateOnlyNode = new PaginateOnlyNode(dataFetcherNode, limit, offset);
SelectionNode childSelectionNode = new SelectionNode.Builder(paginateOnlyNode).setAggMetricSelectionSources(Set.of("QS")).build();
SelectionNode selectionNode = new SelectionNode.Builder(childSelectionNode).setTimeSeriesSelectionSources(Set.of("QS")).build();
compareEntityResponses(new EntityResponse(new EntityFetcherResponse(expectedEntityKeyBuilderResponseMap), 100), executionVisitor.visit(selectionNode));
}
use of org.hypertrace.gateway.service.entity.query.DataFetcherNode in project gateway-service by hypertrace.
the class FilterOptimizingVisitorTest method testSortAndPaginateNode.
@Test
public void testSortAndPaginateNode() {
OrderByExpression orderByExpression = buildOrderByExpression("api1");
DataFetcherNode dataFetcherNode = new DataFetcherNode("QS", Filter.getDefaultInstance());
SortAndPaginateNode sortAndPaginateNode = new SortAndPaginateNode(dataFetcherNode, 10, 20, List.of(orderByExpression));
FilterOptimizingVisitor filterOptimizingVisitor = new FilterOptimizingVisitor();
SortAndPaginateNode visitedSortAndPaginateNode = (SortAndPaginateNode) filterOptimizingVisitor.visit(sortAndPaginateNode);
assertEquals(sortAndPaginateNode.getChildNode(), visitedSortAndPaginateNode.getChildNode());
assertEquals(10, visitedSortAndPaginateNode.getLimit());
assertEquals(20, visitedSortAndPaginateNode.getOffset());
assertEquals(List.of(orderByExpression), visitedSortAndPaginateNode.getOrderByExpressionList());
}
use of org.hypertrace.gateway.service.entity.query.DataFetcherNode in project gateway-service by hypertrace.
the class FilterOptimizingVisitorTest method testAndNodes_dataFetcherNodes_sameSource.
@Test
public void testAndNodes_dataFetcherNodes_sameSource() {
Filter filter1 = generateEQFilter("API.name", "apiName1");
Filter filter2 = generateEQFilter("API.id", "apiId1");
Filter filter3 = generateEQFilter("API.type", "HTTP");
DataFetcherNode dataFetcherNode1 = new DataFetcherNode("QS", filter1);
DataFetcherNode dataFetcherNode2 = new DataFetcherNode("QS", filter2);
DataFetcherNode dataFetcherNode3 = new DataFetcherNode("QS", filter3);
AndNode andNode = new AndNode(List.of(dataFetcherNode1, dataFetcherNode2));
AndNode parentAndNode = new AndNode(List.of(andNode, dataFetcherNode3));
QueryNode queryNode = parentAndNode.acceptVisitor(new FilterOptimizingVisitor());
DataFetcherNode mergedDataFetcherNode = (DataFetcherNode) queryNode;
assertEquals("QS", mergedDataFetcherNode.getSource());
assertEquals(Filter.newBuilder().setOperator(Operator.AND).addChildFilter(Filter.newBuilder().setOperator(Operator.AND).addChildFilter(filter1).addChildFilter(filter2)).addChildFilter(filter3).build(), mergedDataFetcherNode.getFilter());
}
Aggregations