Search in sources :

Example 1 with ColumnMetadata

use of org.hypertrace.core.query.service.api.ColumnMetadata in project gateway-service by hypertrace.

the class EntityInteractionsFetcher method parseResultSet.

private void parseResultSet(String entityType, String otherEntityType, Collection<Expression> selections, Map<String, FunctionExpression> metricToAggFunction, Iterator<ResultSetChunk> resultset, boolean incoming, Map<EntityKey, Builder> entityIdToBuilders, RequestContext requestContext) {
    Map<String, AttributeMetadata> attributeMetadataMap = metadataProvider.getAttributesMetadata(requestContext, SCOPE);
    Map<String, AttributeKind> aliasToAttributeKind = MetricAggregationFunctionUtil.getValueTypeForFunctionType(metricToAggFunction, attributeMetadataMap);
    while (resultset.hasNext()) {
        ResultSetChunk chunk = resultset.next();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Received chunk: " + chunk.toString());
        }
        if (chunk.getRowCount() < 1) {
            break;
        }
        for (Row row : chunk.getRowList()) {
            // Construct the from/to EntityKeys from the columns
            List<String> idColumns = getEntityIdColumnsFromInteraction(DomainEntityType.valueOf(entityType.toUpperCase()), // Note: We add the selections it in this order
            !incoming);
            EntityKey entityId = EntityKey.of(IntStream.range(0, idColumns.size()).mapToObj(value -> row.getColumn(value).getString()).toArray(String[]::new));
            List<String> otherIdColumns = getEntityIdColumnsFromInteraction(DomainEntityType.valueOf(otherEntityType.toUpperCase()), incoming);
            EntityKey otherEntityId = EntityKey.of(IntStream.range(idColumns.size(), idColumns.size() + otherIdColumns.size()).mapToObj(value -> row.getColumn(value).getString()).toArray(String[]::new));
            EntityInteraction.Builder interaction = EntityInteraction.newBuilder();
            addInteractionEdges(interaction, selections, incoming ? otherEntityType : entityType, incoming ? otherEntityId : entityId, incoming ? entityType : otherEntityType, incoming ? entityId : otherEntityId);
            for (int i = idColumns.size() + otherIdColumns.size(); i < chunk.getResultSetMetadata().getColumnMetadataCount(); i++) {
                ColumnMetadata metadata = chunk.getResultSetMetadata().getColumnMetadata(i);
                // Ignore the count column since we introduced that ourselves into the query.
                if (StringUtils.equalsIgnoreCase(COUNT_COLUMN_NAME, metadata.getColumnName())) {
                    continue;
                }
                // Check if this is an attribute vs metric and set it accordingly on the interaction.
                if (metricToAggFunction.containsKey(metadata.getColumnName())) {
                    Value value = QueryAndGatewayDtoConverter.convertToGatewayValueForMetricValue(aliasToAttributeKind, attributeMetadataMap, metadata, row.getColumn(i));
                    interaction.putMetrics(metadata.getColumnName(), AggregatedMetricValue.newBuilder().setValue(value).setFunction(metricToAggFunction.get(metadata.getColumnName()).getFunction()).build());
                } else {
                    interaction.putAttribute(metadata.getColumnName(), QueryAndGatewayDtoConverter.convertQueryValueToGatewayValue(row.getColumn(i), attributeMetadataMap.get(metadata.getColumnName())));
                }
            }
            if (incoming) {
                entityIdToBuilders.get(entityId).addIncomingInteraction(interaction);
            } else {
                entityIdToBuilders.get(entityId).addOutgoingInteraction(interaction);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug(interaction.build().toString());
            }
        }
    }
}
Also used : ColumnMetadata(org.hypertrace.core.query.service.api.ColumnMetadata) AttributeKind(org.hypertrace.core.attribute.service.v1.AttributeKind) EntityInteraction(org.hypertrace.gateway.service.v1.entity.EntityInteraction) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) Value(org.hypertrace.gateway.service.v1.common.Value) AggregatedMetricValue(org.hypertrace.gateway.service.v1.common.AggregatedMetricValue) Row(org.hypertrace.core.query.service.api.Row) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk)

Example 2 with ColumnMetadata

use of org.hypertrace.core.query.service.api.ColumnMetadata in project gateway-service by hypertrace.

the class QueryServiceEntityFetcher method getEntities.

@Override
public EntityFetcherResponse getEntities(EntitiesRequestContext requestContext, EntitiesRequest entitiesRequest) {
    Map<String, AttributeMetadata> attributeMetadataMap = attributeMetadataProvider.getAttributesMetadata(requestContext, entitiesRequest.getEntityType());
    Map<String, AttributeMetadata> resultKeyToAttributeMetadataMap = this.remapAttributeMetadataByResultName(entitiesRequest, attributeMetadataMap);
    // Validate EntitiesRequest
    entitiesRequestValidator.validate(entitiesRequest, attributeMetadataMap);
    List<String> entityIdAttributeIds = AttributeMetadataUtil.getIdAttributeIds(attributeMetadataProvider, entityIdColumnsConfigs, requestContext, entitiesRequest.getEntityType());
    List<org.hypertrace.gateway.service.v1.common.Expression> aggregates = ExpressionReader.getFunctionExpressions(entitiesRequest.getSelectionList());
    Map<String, List<String>> requestedAliasesByEntityIdAttributeIds = getExpectedResultNamesForEachAttributeId(entitiesRequest.getSelectionList(), entityIdAttributeIds);
    QueryRequest.Builder builder = constructSelectionQuery(requestContext, entitiesRequest, entityIdAttributeIds, aggregates);
    adjustLimitAndOffset(builder, entitiesRequest.getLimit(), entitiesRequest.getOffset());
    if (!entitiesRequest.getOrderByList().isEmpty()) {
        // Order by from the request.
        builder.addAllOrderBy(QueryAndGatewayDtoConverter.convertToQueryOrderByExpressions(entitiesRequest.getOrderByList()));
    }
    QueryRequest queryRequest = builder.build();
    LOG.debug("Sending Query to Query Service ======== \n {}", queryRequest);
    Iterator<ResultSetChunk> resultSetChunkIterator = queryServiceClient.executeQuery(queryRequest, requestContext.getHeaders(), requestTimeout);
    // We want to retain the order as returned from the respective source. Hence using a
    // LinkedHashMap
    Map<EntityKey, Entity.Builder> entityBuilders = new LinkedHashMap<>();
    while (resultSetChunkIterator.hasNext()) {
        ResultSetChunk chunk = resultSetChunkIterator.next();
        LOG.debug("Received chunk: {}", chunk);
        if (chunk.getRowCount() < 1) {
            break;
        }
        for (Row row : chunk.getRowList()) {
            // Construct the entity id from the entityIdAttributeIds columns
            EntityKey entityKey = EntityKey.of(IntStream.range(0, entityIdAttributeIds.size()).mapToObj(value -> row.getColumn(value).getString()).toArray(String[]::new));
            Builder entityBuilder = entityBuilders.computeIfAbsent(entityKey, k -> Entity.newBuilder());
            entityBuilder.setEntityType(entitiesRequest.getEntityType());
            entityBuilder.setId(entityKey.toString());
            // as post processing.
            for (int i = 0; i < entityIdAttributeIds.size(); i++) {
                entityBuilder.putAttribute(entityIdAttributeIds.get(i), Value.newBuilder().setString(entityKey.getAttributes().get(i)).setValueType(ValueType.STRING).build());
            }
            requestedAliasesByEntityIdAttributeIds.forEach((attributeId, requestedAliasList) -> requestedAliasList.forEach(requestedAlias -> entityBuilder.putAttribute(requestedAlias, entityBuilder.getAttributeOrThrow(attributeId))));
            for (int i = entityIdAttributeIds.size(); i < chunk.getResultSetMetadata().getColumnMetadataCount(); i++) {
                ColumnMetadata metadata = chunk.getResultSetMetadata().getColumnMetadata(i);
                org.hypertrace.core.query.service.api.Value columnValue = row.getColumn(i);
                buildEntity(entityBuilder, requestContext, metadata, columnValue, resultKeyToAttributeMetadataMap, aggregates.isEmpty());
            }
        }
    }
    return new EntityFetcherResponse(entityBuilders);
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) QueryAndGatewayDtoConverter(org.hypertrace.gateway.service.common.converters.QueryAndGatewayDtoConverter) StringUtils(org.apache.commons.lang3.StringUtils) MetricSeries(org.hypertrace.gateway.service.v1.common.MetricSeries) QueryRequestUtil.createTimeColumnGroupByExpression(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createTimeColumnGroupByExpression) EntityIdColumnsConfigs(org.hypertrace.gateway.service.entity.config.EntityIdColumnsConfigs) Duration(java.time.Duration) Map(java.util.Map) Objects.isNull(java.util.Objects.isNull) Operator(org.hypertrace.core.query.service.api.Operator) Row(org.hypertrace.core.query.service.api.Row) Filter(org.hypertrace.core.query.service.api.Filter) QueryAndGatewayDtoConverter.convertToQueryExpression(org.hypertrace.gateway.service.common.converters.QueryAndGatewayDtoConverter.convertToQueryExpression) Collection(java.util.Collection) Value(org.hypertrace.gateway.service.v1.common.Value) ColumnMetadata(org.hypertrace.core.query.service.api.ColumnMetadata) Interval(org.hypertrace.gateway.service.v1.common.Interval) Streams(com.google.common.collect.Streams) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) ValueType(org.hypertrace.gateway.service.v1.common.ValueType) Collectors(java.util.stream.Collectors) List(java.util.List) QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) QueryRequestUtil(org.hypertrace.gateway.service.common.converters.QueryRequestUtil) Optional(java.util.Optional) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk) AttributeMetadataProvider(org.hypertrace.gateway.service.common.AttributeMetadataProvider) IntStream(java.util.stream.IntStream) QueryExpressionUtil(org.hypertrace.gateway.service.common.util.QueryExpressionUtil) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) EntitiesRequestValidator(org.hypertrace.gateway.service.entity.EntitiesRequestValidator) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) AggregatedMetricValue(org.hypertrace.gateway.service.v1.common.AggregatedMetricValue) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) ExpressionReader.getExpectedResultNamesForEachAttributeId(org.hypertrace.gateway.service.common.util.ExpressionReader.getExpectedResultNamesForEachAttributeId) ExpressionReader(org.hypertrace.gateway.service.common.util.ExpressionReader) LinkedHashMap(java.util.LinkedHashMap) Period(org.hypertrace.gateway.service.v1.common.Period) QueryRequestContext(org.hypertrace.gateway.service.common.QueryRequestContext) QueryRequestUtil.createCountByColumnSelection(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createCountByColumnSelection) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Health(org.hypertrace.gateway.service.v1.common.Health) QueryRequestUtil.createStringNullLiteralExpression(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createStringNullLiteralExpression) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) MetricAggregationFunctionUtil(org.hypertrace.gateway.service.common.util.MetricAggregationFunctionUtil) QueryServiceClient(org.hypertrace.core.query.service.client.QueryServiceClient) AttributeMetadataUtil(org.hypertrace.gateway.service.common.util.AttributeMetadataUtil) TimeUnit(java.util.concurrent.TimeUnit) Entity(org.hypertrace.gateway.service.v1.entity.Entity) ChronoUnit(java.time.temporal.ChronoUnit) Expression(org.hypertrace.core.query.service.api.Expression) FunctionExpression(org.hypertrace.gateway.service.v1.common.FunctionExpression) QueryRequestUtil.createDistinctCountByColumnSelection(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createDistinctCountByColumnSelection) Preconditions(com.google.common.base.Preconditions) FunctionType(org.hypertrace.gateway.service.v1.common.FunctionType) QueryRequestUtil.createFilter(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createFilter) ColumnMetadata(org.hypertrace.core.query.service.api.ColumnMetadata) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) LinkedHashMap(java.util.LinkedHashMap) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) List(java.util.List) QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) QueryRequestUtil.createTimeColumnGroupByExpression(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createTimeColumnGroupByExpression) QueryAndGatewayDtoConverter.convertToQueryExpression(org.hypertrace.gateway.service.common.converters.QueryAndGatewayDtoConverter.convertToQueryExpression) QueryRequestUtil.createStringNullLiteralExpression(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createStringNullLiteralExpression) Expression(org.hypertrace.core.query.service.api.Expression) FunctionExpression(org.hypertrace.gateway.service.v1.common.FunctionExpression) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) Row(org.hypertrace.core.query.service.api.Row) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk)

Example 3 with ColumnMetadata

use of org.hypertrace.core.query.service.api.ColumnMetadata in project gateway-service by hypertrace.

the class QueryServiceEntityFetcher method getTimeAggregatedMetrics.

@Override
public EntityFetcherResponse getTimeAggregatedMetrics(EntitiesRequestContext requestContext, EntitiesRequest entitiesRequest) {
    // No need to make execute the rest of this if there are no TimeAggregations in the request.
    if (entitiesRequest.getTimeAggregationCount() == 0) {
        return new EntityFetcherResponse();
    }
    // Only supported filter is entityIds IN ["id1", "id2", "id3"]
    List<String> idColumns = AttributeMetadataUtil.getIdAttributeIds(attributeMetadataProvider, entityIdColumnsConfigs, requestContext, entitiesRequest.getEntityType());
    String timeColumn = AttributeMetadataUtil.getTimestampAttributeId(attributeMetadataProvider, requestContext, entitiesRequest.getEntityType());
    Map<String, AttributeMetadata> attributeMetadataMap = attributeMetadataProvider.getAttributesMetadata(requestContext, entitiesRequest.getEntityType());
    Map<String, AttributeMetadata> resultKeyToAttributeMetadataMap = this.remapAttributeMetadataByResultName(entitiesRequest, attributeMetadataMap);
    entitiesRequestValidator.validate(entitiesRequest, attributeMetadataMap);
    entitiesRequest.getTimeAggregationList().forEach(timeAggregation -> requestContext.mapAliasToTimeAggregation(timeAggregation.getAggregation().getFunction().getAlias(), timeAggregation));
    // First group the Aggregations based on the period so that we can issue separate queries
    // to QueryService for each different Period.
    Collection<List<TimeAggregation>> result = entitiesRequest.getTimeAggregationList().stream().collect(Collectors.groupingBy(TimeAggregation::getPeriod)).values();
    Map<EntityKey, Map<String, MetricSeries.Builder>> entityMetricSeriesMap = new LinkedHashMap<>();
    for (List<TimeAggregation> batch : result) {
        Period period = batch.get(0).getPeriod();
        ChronoUnit unit = ChronoUnit.valueOf(period.getUnit());
        long periodSecs = Duration.of(period.getValue(), unit).getSeconds();
        QueryRequest request = buildTimeSeriesQueryRequest(entitiesRequest, requestContext, periodSecs, batch, idColumns, timeColumn);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sending time series queryRequest to query service: ======== \n {}", request.toString());
        }
        Iterator<ResultSetChunk> resultSetChunkIterator = queryServiceClient.executeQuery(request, requestContext.getHeaders(), requestTimeout);
        while (resultSetChunkIterator.hasNext()) {
            ResultSetChunk chunk = resultSetChunkIterator.next();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Received chunk: " + chunk.toString());
            }
            if (chunk.getRowCount() < 1) {
                break;
            }
            if (!chunk.hasResultSetMetadata()) {
                LOG.warn("Chunk doesn't have result metadata so couldn't process the response.");
                break;
            }
            for (Row row : chunk.getRowList()) {
                // Construct the entity id from the entityIdAttributeIds columns
                EntityKey entityKey = EntityKey.of(IntStream.range(0, idColumns.size()).mapToObj(value -> row.getColumn(value).getString()).toArray(String[]::new));
                Map<String, MetricSeries.Builder> metricSeriesMap = entityMetricSeriesMap.computeIfAbsent(entityKey, k -> new LinkedHashMap<>());
                Interval.Builder intervalBuilder = Interval.newBuilder();
                // Second column is the time column
                Value value = QueryAndGatewayDtoConverter.convertQueryValueToGatewayValue(row.getColumn(idColumns.size()));
                if (value.getValueType() == ValueType.STRING) {
                    long startTime = Long.parseLong(value.getString());
                    long endTime = startTime + TimeUnit.SECONDS.toMillis(periodSecs);
                    intervalBuilder.setStartTimeMillis(startTime);
                    intervalBuilder.setEndTimeMillis(endTime);
                    for (int i = idColumns.size() + 1; i < chunk.getResultSetMetadata().getColumnMetadataCount(); i++) {
                        ColumnMetadata metadata = chunk.getResultSetMetadata().getColumnMetadata(i);
                        TimeAggregation timeAggregation = requestContext.getTimeAggregationByAlias(metadata.getColumnName());
                        if (timeAggregation == null) {
                            LOG.warn("Couldn't find an aggregate for column: {}", metadata.getColumnName());
                            continue;
                        }
                        FunctionType functionType = timeAggregation.getAggregation().getFunction().getFunction();
                        AttributeMetadata functionAttributeMetadata = resultKeyToAttributeMetadataMap.get(metadata.getColumnName());
                        Value convertedValue = QueryAndGatewayDtoConverter.convertToGatewayValueForMetricValue(MetricAggregationFunctionUtil.getValueTypeForFunctionType(functionType, functionAttributeMetadata), resultKeyToAttributeMetadataMap, metadata, row.getColumn(i));
                        List<org.hypertrace.gateway.service.v1.common.Expression> healthExpressions = timeAggregation.getAggregation().getFunction().getArgumentsList().stream().filter(org.hypertrace.gateway.service.v1.common.Expression::hasHealth).collect(Collectors.toList());
                        Preconditions.checkArgument(healthExpressions.size() <= 1);
                        Health health = Health.NOT_COMPUTED;
                        MetricSeries.Builder seriesBuilder = metricSeriesMap.computeIfAbsent(metadata.getColumnName(), k -> getMetricSeriesBuilder(timeAggregation));
                        seriesBuilder.addValue(Interval.newBuilder(intervalBuilder.build()).setValue(convertedValue).setHealth(health));
                    }
                } else {
                    LOG.warn("Was expecting STRING values only but received valueType: {}", value.getValueType());
                }
            }
        }
    }
    Map<EntityKey, Entity.Builder> resultMap = new LinkedHashMap<>();
    for (Map.Entry<EntityKey, Map<String, MetricSeries.Builder>> entry : entityMetricSeriesMap.entrySet()) {
        Entity.Builder entityBuilder = Entity.newBuilder().setEntityType(entitiesRequest.getEntityType()).setId(entry.getKey().toString()).putAllMetricSeries(entry.getValue().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getSortedMetricSeries(e.getValue()))));
        for (int i = 0; i < idColumns.size(); i++) {
            entityBuilder.putAttribute(idColumns.get(i), Value.newBuilder().setString(entry.getKey().getAttributes().get(i)).setValueType(ValueType.STRING).build());
        }
        resultMap.put(entry.getKey(), entityBuilder);
    }
    return new EntityFetcherResponse(resultMap);
}
Also used : Entity(org.hypertrace.gateway.service.v1.entity.Entity) ColumnMetadata(org.hypertrace.core.query.service.api.ColumnMetadata) 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) List(java.util.List) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) Health(org.hypertrace.gateway.service.v1.common.Health) FunctionType(org.hypertrace.gateway.service.v1.common.FunctionType) Period(org.hypertrace.gateway.service.v1.common.Period) QueryRequestUtil.createTimeColumnGroupByExpression(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createTimeColumnGroupByExpression) QueryAndGatewayDtoConverter.convertToQueryExpression(org.hypertrace.gateway.service.common.converters.QueryAndGatewayDtoConverter.convertToQueryExpression) QueryRequestUtil.createStringNullLiteralExpression(org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createStringNullLiteralExpression) Expression(org.hypertrace.core.query.service.api.Expression) FunctionExpression(org.hypertrace.gateway.service.v1.common.FunctionExpression) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) Value(org.hypertrace.gateway.service.v1.common.Value) AggregatedMetricValue(org.hypertrace.gateway.service.v1.common.AggregatedMetricValue) Row(org.hypertrace.core.query.service.api.Row) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ChronoUnit(java.time.temporal.ChronoUnit) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk) Interval(org.hypertrace.gateway.service.v1.common.Interval)

Example 4 with ColumnMetadata

use of org.hypertrace.core.query.service.api.ColumnMetadata in project gateway-service by hypertrace.

the class SpanService method filterSpanEvents.

@VisibleForTesting
List<SpanEvent> filterSpanEvents(RequestContext context, SpansRequest request, Map<String, AttributeMetadata> attributeMetadataMap) {
    Map<String, AttributeMetadata> resultKeyToAttributeMetadataMap = AttributeMetadataUtil.remapAttributeMetadataByResultKey(request.getSelectionList(), attributeMetadataMap);
    QueryRequest.Builder queryBuilder = createQueryWithFilter(request, context);
    if (!request.getSelectionList().isEmpty()) {
        request.getSelectionList().forEach(exp -> queryBuilder.addSelection(QueryAndGatewayDtoConverter.convertToQueryExpression(exp)));
    }
    addSortLimitAndOffset(request, queryBuilder);
    List<SpanEvent> spanEventsResult = new ArrayList<>();
    QueryRequest queryRequest = queryBuilder.build();
    Iterator<ResultSetChunk> resultSetChunkIterator = queryServiceClient.executeQuery(queryRequest, context.getHeaders(), requestTimeout);
    while (resultSetChunkIterator.hasNext()) {
        ResultSetChunk chunk = resultSetChunkIterator.next();
        LOG.debug("Received chunk: {}", chunk);
        if (chunk.getRowCount() < 1) {
            break;
        }
        for (Row row : chunk.getRowList()) {
            SpanEvent.Builder spanEventBuilder = SpanEvent.newBuilder();
            for (int i = 0; i < chunk.getResultSetMetadata().getColumnMetadataCount(); i++) {
                ColumnMetadata metadata = chunk.getResultSetMetadata().getColumnMetadata(i);
                String attrName = metadata.getColumnName();
                spanEventBuilder.putAttributes(metadata.getColumnName(), QueryAndGatewayDtoConverter.convertToGatewayValue(attrName, row.getColumn(i), resultKeyToAttributeMetadataMap));
            }
            spanEventsResult.add(spanEventBuilder.build());
        }
    }
    return spanEventsResult;
}
Also used : ColumnMetadata(org.hypertrace.core.query.service.api.ColumnMetadata) QueryRequest(org.hypertrace.core.query.service.api.QueryRequest) ArrayList(java.util.ArrayList) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) SpanEvent(org.hypertrace.gateway.service.v1.span.SpanEvent) Row(org.hypertrace.core.query.service.api.Row) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with ColumnMetadata

use of org.hypertrace.core.query.service.api.ColumnMetadata in project gateway-service by hypertrace.

the class BaselineServiceQueryParser method parseQueryResponse.

public BaselineEntitiesResponse parseQueryResponse(Iterator<ResultSetChunk> resultSetChunkIterator, BaselineRequestContext requestContext, int idColumnsSize, String entityType, long startTime, long endTime) {
    Map<String, AttributeMetadata> attributeMetadataMap = attributeMetadataProvider.getAttributesMetadata(requestContext, entityType);
    Map<EntityKey, Map<String, BaselineMetricSeries.Builder>> entityMetricSeriesMap = new LinkedHashMap<>();
    boolean isFirstChunk = true;
    ResultSetMetadata resultMetadata = null;
    while (resultSetChunkIterator.hasNext()) {
        ResultSetChunk chunk = resultSetChunkIterator.next();
        LOG.debug("Received chunk: {} ", chunk);
        if (chunk.getRowCount() < 1) {
            break;
        }
        if (isFirstChunk) {
            resultMetadata = chunk.getResultSetMetadata();
            isFirstChunk = false;
        }
        for (Row row : chunk.getRowList()) {
            EntityKey entityKey = EntityKey.of(IntStream.range(0, idColumnsSize).mapToObj(value -> row.getColumn(value).getString()).toArray(String[]::new));
            Map<String, BaselineMetricSeries.Builder> metricSeriesMap = entityMetricSeriesMap.computeIfAbsent(entityKey, k -> new LinkedHashMap<>());
            BaselineInterval.Builder intervalBuilder = BaselineInterval.newBuilder();
            Value value = QueryAndGatewayDtoConverter.convertQueryValueToGatewayValue(row.getColumn(idColumnsSize));
            if (value.getValueType() == ValueType.STRING) {
                for (int i = idColumnsSize + 1; i < resultMetadata.getColumnMetadataCount(); i++) {
                    ColumnMetadata metadata = resultMetadata.getColumnMetadata(i);
                    BaselineTimeAggregation timeAggregation = requestContext.getTimeAggregationByAlias(metadata.getColumnName());
                    if (timeAggregation == null) {
                        LOG.warn("Couldn't find an aggregate for column: {}", metadata.getColumnName());
                        continue;
                    }
                    Value convertedValue = QueryAndGatewayDtoConverter.convertToGatewayValueForMetricValue(MetricAggregationFunctionUtil.getValueTypeForFunctionType(timeAggregation.getAggregation(), attributeMetadataMap), attributeMetadataMap, metadata, row.getColumn(i));
                    BaselineMetricSeries.Builder seriesBuilder = metricSeriesMap.computeIfAbsent(metadata.getColumnName(), k -> BaselineMetricSeries.newBuilder());
                    seriesBuilder.addBaselineValue(BaselineInterval.newBuilder(intervalBuilder.build()).setBaseline(Baseline.newBuilder().setValue(convertedValue).build()).build());
                }
            } else {
                LOG.warn("Was expecting STRING values only but received valueType: {}", value.getValueType());
            }
        }
    }
    List<BaselineEntity> baselineEntities = new ArrayList<>();
    for (Map.Entry<EntityKey, Map<String, BaselineMetricSeries.Builder>> entry : entityMetricSeriesMap.entrySet()) {
        BaselineEntity.Builder entityBuilder = BaselineEntity.newBuilder().setEntityType(entityType).setId(entry.getKey().toString()).putAllBaselineMetricSeries(entry.getValue().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getSortedMetricSeries(e.getValue()))));
        baselineEntities.add(entityBuilder.build());
    }
    return BaselineEntitiesResponse.newBuilder().addAllBaselineEntity(baselineEntities).build();
}
Also used : ColumnMetadata(org.hypertrace.core.query.service.api.ColumnMetadata) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) BaselineMetricSeries(org.hypertrace.gateway.service.v1.baseline.BaselineMetricSeries) BaselineTimeAggregation(org.hypertrace.gateway.service.v1.baseline.BaselineTimeAggregation) ResultSetMetadata(org.hypertrace.core.query.service.api.ResultSetMetadata) BaselineEntity(org.hypertrace.gateway.service.v1.baseline.BaselineEntity) BaselineInterval(org.hypertrace.gateway.service.v1.baseline.BaselineInterval) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) Value(org.hypertrace.gateway.service.v1.common.Value) Row(org.hypertrace.core.query.service.api.Row) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ResultSetChunk(org.hypertrace.core.query.service.api.ResultSetChunk)

Aggregations

AttributeMetadata (org.hypertrace.core.attribute.service.v1.AttributeMetadata)7 ColumnMetadata (org.hypertrace.core.query.service.api.ColumnMetadata)7 ResultSetChunk (org.hypertrace.core.query.service.api.ResultSetChunk)7 Row (org.hypertrace.core.query.service.api.Row)7 QueryRequest (org.hypertrace.core.query.service.api.QueryRequest)5 ArrayList (java.util.ArrayList)4 EntityKey (org.hypertrace.gateway.service.entity.EntityKey)4 Value (org.hypertrace.gateway.service.v1.common.Value)4 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 AggregatedMetricValue (org.hypertrace.gateway.service.v1.common.AggregatedMetricValue)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ChronoUnit (java.time.temporal.ChronoUnit)2 List (java.util.List)2 Expression (org.hypertrace.core.query.service.api.Expression)2 ResultSetMetadata (org.hypertrace.core.query.service.api.ResultSetMetadata)2 QueryAndGatewayDtoConverter.convertToQueryExpression (org.hypertrace.gateway.service.common.converters.QueryAndGatewayDtoConverter.convertToQueryExpression)2 QueryRequestUtil.createStringNullLiteralExpression (org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createStringNullLiteralExpression)2 QueryRequestUtil.createTimeColumnGroupByExpression (org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createTimeColumnGroupByExpression)2 FunctionExpression (org.hypertrace.gateway.service.v1.common.FunctionExpression)2