Search in sources :

Example 1 with QueryRequestUtil

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();
}
Also used : EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) Filter(org.hypertrace.core.query.service.api.Filter) QueryRequestUtil.createFilter(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createFilter) QueryRequestUtil(org.hypertrace.gateway.service.common.converters.QueryRequestUtil)

Example 2 with QueryRequestUtil

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);
}
Also used : QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) LinkedHashMap(java.util.LinkedHashMap) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) MetricSeries(org.hypertrace.gateway.service.v1.common.MetricSeries) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) AttributeScope(org.hypertrace.core.attribute.service.v1.AttributeScope) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) EntitiesRequestAndResponseUtils.buildOrderByExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) EntitiesRequestAndResponseUtils.buildTimeAggregation(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildTimeAggregation) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) QueryRequestUtil(org.hypertrace.gateway.service.common.converters.QueryRequestUtil) QueryServiceRequestAndResponseUtils.getResultSetChunk(org.hypertrace.gateway.service.common.QueryServiceRequestAndResponseUtils.getResultSetChunk) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 3 with QueryRequestUtil

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);
}
Also used : QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) LinkedHashMap(java.util.LinkedHashMap) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) MetricSeries(org.hypertrace.gateway.service.v1.common.MetricSeries) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) AttributeScope(org.hypertrace.core.attribute.service.v1.AttributeScope) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) EntitiesRequestAndResponseUtils.buildOrderByExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) EntitiesRequestAndResponseUtils.buildTimeAggregation(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildTimeAggregation) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) QueryRequestUtil(org.hypertrace.gateway.service.common.converters.QueryRequestUtil) QueryServiceRequestAndResponseUtils.getResultSetChunk(org.hypertrace.gateway.service.common.QueryServiceRequestAndResponseUtils.getResultSetChunk) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Aggregations

QueryRequest (org.hypertrace.core.query.service.api.QueryRequest)3 QueryRequestUtil (org.hypertrace.gateway.service.common.converters.QueryRequestUtil)3 EntitiesRequest (org.hypertrace.gateway.service.v1.entity.EntitiesRequest)3 LinkedHashMap (java.util.LinkedHashMap)2 AttributeScope (org.hypertrace.core.attribute.service.v1.AttributeScope)2 ResultSetChunk (org.hypertrace.core.query.service.api.ResultSetChunk)2 EntitiesRequestAndResponseUtils.buildOrderByExpression (org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression)2 EntitiesRequestAndResponseUtils.buildTimeAggregation (org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildTimeAggregation)2 QueryServiceRequestAndResponseUtils.getResultSetChunk (org.hypertrace.gateway.service.common.QueryServiceRequestAndResponseUtils.getResultSetChunk)2 EntitiesRequestContext (org.hypertrace.gateway.service.entity.EntitiesRequestContext)2 EntityKey (org.hypertrace.gateway.service.entity.EntityKey)2 MetricSeries (org.hypertrace.gateway.service.v1.common.MetricSeries)2 OrderByExpression (org.hypertrace.gateway.service.v1.common.OrderByExpression)2 TimeAggregation (org.hypertrace.gateway.service.v1.common.TimeAggregation)2 Builder (org.hypertrace.gateway.service.v1.entity.Entity.Builder)2 Test (org.junit.jupiter.api.Test)2 Filter (org.hypertrace.core.query.service.api.Filter)1 QueryRequestUtil.createFilter (org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createFilter)1