use of org.hypertrace.gateway.service.common.converters.QueryRequestUtil in project gateway-service by hypertrace.
the class QueryServiceEntityFetcher method buildTimeSeriesQueryRequest.
private QueryRequest buildTimeSeriesQueryRequest(EntitiesRequest entitiesRequest, EntitiesRequestContext context, long periodSecs, List<TimeAggregation> timeAggregationBatch, List<String> idColumns, String timeColumn) {
long alignedStartTime = QueryExpressionUtil.alignToPeriodBoundary(entitiesRequest.getStartTimeMillis(), periodSecs, true);
long alignedEndTime = QueryExpressionUtil.alignToPeriodBoundary(entitiesRequest.getEndTimeMillis(), periodSecs, false);
EntitiesRequest timeAlignedEntitiesRequest = EntitiesRequest.newBuilder(entitiesRequest).setStartTimeMillis(alignedStartTime).setEndTimeMillis(alignedEndTime).build();
QueryRequest.Builder builder = QueryRequest.newBuilder();
timeAggregationBatch.forEach(e -> builder.addSelection(QueryAndGatewayDtoConverter.convertToQueryExpression(e.getAggregation())));
Filter.Builder queryFilter = constructQueryServiceFilter(timeAlignedEntitiesRequest, context, idColumns);
builder.setFilter(queryFilter);
// First group by the id columns.
builder.addAllGroupBy(idColumns.stream().map(QueryRequestUtil::createAttributeExpression).collect(Collectors.toList()));
// Secondary grouping is on time.
builder.addGroupBy(createTimeColumnGroupByExpression(timeColumn, periodSecs));
// Pinot truncates the GroupBy results to 10 when there is no limit explicitly but
// here we neither want the results to be truncated nor apply the limit coming from client.
// We would like to get all entities based on filters so we set the limit to a high value.
// TODO: Figure out a reasonable computed limit instead of this hardcoded one. Probably
// requested limit * expected max number of time series buckets
builder.setLimit(QueryServiceClient.DEFAULT_QUERY_SERVICE_GROUP_BY_LIMIT);
return builder.build();
}
use of org.hypertrace.gateway.service.common.converters.QueryRequestUtil in project gateway-service by hypertrace.
the class QueryServiceEntityFetcherTests method testGetTimeAggregatedMetrics.
@Test
public void testGetTimeAggregatedMetrics() {
List<OrderByExpression> orderByExpressions = List.of(buildOrderByExpression(API_ID_ATTR));
long startTime = 1L;
long endTime = 10L;
int limit = 10;
int offset = 5;
String tenantId = "TENANT_ID";
Map<String, String> requestHeaders = Map.of("x-tenant-id", tenantId);
AttributeScope entityType = AttributeScope.API;
TimeAggregation timeAggregation1 = buildTimeAggregation(30, API_NUM_CALLS_ATTR, FunctionType.SUM, "SUM_API.numCalls", List.of());
TimeAggregation timeAggregation2 = buildTimeAggregation(30, API_NUM_CALLS_ATTR, FunctionType.AVGRATE, "AVGRATE_API.numCalls", List.of(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(Value.newBuilder().setLong(60).setValueType(ValueType.LONG))).build()));
EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(entityType.name()).setStartTimeMillis(startTime).setEndTimeMillis(endTime).addTimeAggregation(timeAggregation1).addTimeAggregation(timeAggregation2).setFilter(Filter.newBuilder().setOperator(AND).addChildFilter(EntitiesRequestAndResponseUtils.getTimeRangeFilter("API.startTime", startTime, endTime)).addChildFilter(generateEQFilter(API_DISCOVERY_STATE_ATTR, "DISCOVERED"))).addAllOrderBy(orderByExpressions).setLimit(limit).setOffset(offset).build();
EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
QueryRequest expectedQueryRequest = QueryRequest.newBuilder().addSelection(QueryAndGatewayDtoConverter.convertToQueryExpression(timeAggregation1.getAggregation())).addSelection(QueryAndGatewayDtoConverter.convertToQueryExpression(timeAggregation2.getAggregation())).setFilter(createQsRequestFilter(API_START_TIME_ATTR, API_ID_ATTR, startTime, endTime, createStringFilter(API_DISCOVERY_STATE_ATTR, Operator.EQ, "DISCOVERED"))).addAllGroupBy(List.of(API_ID_ATTR).stream().map(QueryRequestUtil::createAttributeExpression).collect(Collectors.toList())).addGroupBy(createTimeColumnGroupByExpression(API_START_TIME_ATTR, 30)).setOffset(0).setLimit(10000).build();
List<ResultSetChunk> resultSetChunks = List.of(getResultSetChunk(List.of("API.apiId", "timeColumn", "SUM_API.numCalls", "AVGRATE_API.numCalls"), new String[][] { { "apiId1", "10000", "34", "68" }, { "apiId2", "20000", "34", "68" } }));
when(queryServiceClient.executeQuery(eq(expectedQueryRequest), eq(requestHeaders), eq(500))).thenReturn(resultSetChunks.iterator());
EntityFetcherResponse response = queryServiceEntityFetcher.getTimeAggregatedMetrics(entitiesRequestContext, entitiesRequest);
assertEquals(2, response.size());
Map<String, MetricSeries> metricSeriesMap1 = new LinkedHashMap<>();
metricSeriesMap1.put("AVGRATE_API.numCalls", MetricSeries.newBuilder().addValue(Interval.newBuilder().setStartTimeMillis(10000).setEndTimeMillis(40000).setValue(Value.newBuilder().setValueType(ValueType.DOUBLE).setDouble(68.0))).setAggregation("AVGRATE").setPeriod(Period.newBuilder().setUnit("SECONDS").setValue(30).build()).build());
metricSeriesMap1.put("SUM_API.numCalls", MetricSeries.newBuilder().addValue(Interval.newBuilder().setStartTimeMillis(10000).setEndTimeMillis(40000).setValue(Value.newBuilder().setValueType(ValueType.LONG).setLong(34))).setAggregation("SUM").setPeriod(Period.newBuilder().setUnit("SECONDS").setValue(30).build()).build());
Map<String, MetricSeries> metricSeriesMap2 = new LinkedHashMap<>();
metricSeriesMap2.put("AVGRATE_API.numCalls", MetricSeries.newBuilder().addValue(Interval.newBuilder().setStartTimeMillis(20000).setEndTimeMillis(50000).setValue(Value.newBuilder().setValueType(ValueType.DOUBLE).setDouble(68.0))).setAggregation("AVGRATE").setPeriod(Period.newBuilder().setUnit("SECONDS").setValue(30).build()).build());
metricSeriesMap2.put("SUM_API.numCalls", MetricSeries.newBuilder().addValue(Interval.newBuilder().setStartTimeMillis(20000).setEndTimeMillis(50000).setValue(Value.newBuilder().setValueType(ValueType.LONG).setLong(34))).setAggregation("SUM").setPeriod(Period.newBuilder().setUnit("SECONDS").setValue(30).build()).build());
Map<EntityKey, Builder> expectedEntityKeyBuilderResponseMap = new LinkedHashMap<>();
expectedEntityKeyBuilderResponseMap.put(EntityKey.of("apiId1"), Entity.newBuilder().setId("apiId1").setEntityType("API").putAllMetricSeries(metricSeriesMap1).putAttribute("API.id", getStringValue("apiId1")));
expectedEntityKeyBuilderResponseMap.put(EntityKey.of("apiId2"), Entity.newBuilder().setId("apiId2").setEntityType("API").putAllMetricSeries(metricSeriesMap2).putAttribute("API.id", getStringValue("apiId2")));
compareEntityFetcherResponses(new EntityFetcherResponse(expectedEntityKeyBuilderResponseMap), response);
}
use of org.hypertrace.gateway.service.common.converters.QueryRequestUtil in project gateway-service by hypertrace.
the class QueryServiceEntityFetcherTests method testGetTimeAggregatedMetricsForAvgRateInIsoFormat.
@Test
public void testGetTimeAggregatedMetricsForAvgRateInIsoFormat() {
List<OrderByExpression> orderByExpressions = List.of(buildOrderByExpression(API_ID_ATTR));
long startTime = 1L;
long endTime = 10L;
int limit = 10;
int offset = 5;
String tenantId = "TENANT_ID";
Map<String, String> requestHeaders = Map.of("x-tenant-id", tenantId);
AttributeScope entityType = AttributeScope.API;
TimeAggregation timeAggregation = buildTimeAggregation(30, API_NUM_CALLS_ATTR, FunctionType.AVGRATE, "AVGRATE_API.numCalls", List.of(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(Value.newBuilder().setString("PT1M").setValueType(ValueType.STRING))).build()));
EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(entityType.name()).setStartTimeMillis(startTime).setEndTimeMillis(endTime).addTimeAggregation(timeAggregation).setFilter(Filter.newBuilder().setOperator(AND).addChildFilter(EntitiesRequestAndResponseUtils.getTimeRangeFilter("API.startTime", startTime, endTime)).addChildFilter(generateEQFilter(API_DISCOVERY_STATE_ATTR, "DISCOVERED"))).addAllOrderBy(orderByExpressions).setLimit(limit).setOffset(offset).build();
EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
QueryRequest expectedQueryRequest = QueryRequest.newBuilder().addSelection(QueryAndGatewayDtoConverter.convertToQueryExpression(timeAggregation.getAggregation())).setFilter(createQsRequestFilter(API_START_TIME_ATTR, API_ID_ATTR, startTime, endTime, createStringFilter(API_DISCOVERY_STATE_ATTR, Operator.EQ, "DISCOVERED"))).addAllGroupBy(List.of(API_ID_ATTR).stream().map(QueryRequestUtil::createAttributeExpression).collect(Collectors.toList())).addGroupBy(createTimeColumnGroupByExpression(API_START_TIME_ATTR, 30)).setOffset(0).setLimit(10000).build();
List<ResultSetChunk> resultSetChunks = List.of(getResultSetChunk(List.of("API.apiId", "timeColumn", "AVGRATE_API.numCalls"), new String[][] { { "apiId1", "10000", "34" }, { "apiId2", "20000", "34" } }));
when(queryServiceClient.executeQuery(eq(expectedQueryRequest), eq(requestHeaders), eq(500))).thenReturn(resultSetChunks.iterator());
EntityFetcherResponse response = queryServiceEntityFetcher.getTimeAggregatedMetrics(entitiesRequestContext, entitiesRequest);
assertEquals(2, response.size());
Map<String, MetricSeries> metricSeriesMap1 = new LinkedHashMap<>();
metricSeriesMap1.put("AVGRATE_API.numCalls", MetricSeries.newBuilder().addValue(Interval.newBuilder().setStartTimeMillis(10000).setEndTimeMillis(40000).setValue(Value.newBuilder().setValueType(ValueType.DOUBLE).setDouble(34.0))).setAggregation("AVGRATE").setPeriod(Period.newBuilder().setUnit("SECONDS").setValue(30).build()).build());
Map<String, MetricSeries> metricSeriesMap2 = new LinkedHashMap<>();
metricSeriesMap2.put("AVGRATE_API.numCalls", MetricSeries.newBuilder().addValue(Interval.newBuilder().setStartTimeMillis(20000).setEndTimeMillis(50000).setValue(Value.newBuilder().setValueType(ValueType.DOUBLE).setDouble(34.0))).setAggregation("AVGRATE").setPeriod(Period.newBuilder().setUnit("SECONDS").setValue(30).build()).build());
Map<EntityKey, Builder> expectedEntityKeyBuilderResponseMap = new LinkedHashMap<>();
expectedEntityKeyBuilderResponseMap.put(EntityKey.of("apiId1"), Entity.newBuilder().setId("apiId1").setEntityType("API").putAllMetricSeries(metricSeriesMap1).putAttribute("API.id", getStringValue("apiId1")));
expectedEntityKeyBuilderResponseMap.put(EntityKey.of("apiId2"), Entity.newBuilder().setId("apiId2").setEntityType("API").putAllMetricSeries(metricSeriesMap2).putAttribute("API.id", getStringValue("apiId2")));
compareEntityFetcherResponses(new EntityFetcherResponse(expectedEntityKeyBuilderResponseMap), response);
}
Aggregations