Search in sources :

Example 1 with FunctionExpression

use of org.hypertrace.gateway.service.v1.common.FunctionExpression 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 FunctionExpression

use of org.hypertrace.gateway.service.v1.common.FunctionExpression in project gateway-service by hypertrace.

the class BaselineServiceImpl method getTimeAggregationsForAggregateExpr.

private List<TimeAggregation> getTimeAggregationsForAggregateExpr(BaselineEntitiesRequest originalRequest, long alignedStartTime, long alignedEndTime) {
    List<FunctionExpression> aggregateList = originalRequest.getBaselineAggregateRequestList();
    List<TimeAggregation> timeAggregationList = new ArrayList<>();
    for (FunctionExpression function : aggregateList) {
        TimeAggregation timeAggregation = getAggregationFunction(function, alignedStartTime, alignedEndTime);
        timeAggregationList.add(timeAggregation);
    }
    return timeAggregationList;
}
Also used : FunctionExpression(org.hypertrace.gateway.service.v1.common.FunctionExpression) ArrayList(java.util.ArrayList) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) BaselineTimeAggregation(org.hypertrace.gateway.service.v1.baseline.BaselineTimeAggregation)

Example 3 with FunctionExpression

use of org.hypertrace.gateway.service.v1.common.FunctionExpression in project gateway-service by hypertrace.

the class ExpressionReader method getSelectionResultName.

public static Optional<String> getSelectionResultName(Expression expression) {
    switch(expression.getValueCase()) {
        case COLUMNIDENTIFIER:
            return Optional.of(expression.getColumnIdentifier().getAlias().isEmpty() ? expression.getColumnIdentifier().getColumnName() : expression.getColumnIdentifier().getAlias());
        case ATTRIBUTE_EXPRESSION:
            return Optional.of(expression.getAttributeExpression().getAlias().isEmpty() ? expression.getAttributeExpression().getAttributeId() : expression.getAttributeExpression().getAlias());
        case FUNCTION:
            FunctionExpression functionExpression = expression.getFunction();
            if (!functionExpression.getAlias().isEmpty()) {
                return Optional.of(functionExpression.getAlias());
            }
            String argumentString = functionExpression.getArgumentsList().stream().map(ExpressionReader::getSelectionResultName).flatMap(Optional::stream).collect(Collectors.joining(","));
            return Optional.of(String.format("%s_%s", functionExpression.getFunction(), argumentString));
        default:
            return Optional.empty();
    }
}
Also used : FunctionExpression(org.hypertrace.gateway.service.v1.common.FunctionExpression)

Example 4 with FunctionExpression

use of org.hypertrace.gateway.service.v1.common.FunctionExpression in project gateway-service by hypertrace.

the class RequestHandler method handleQueryServiceResponseSingleColumn.

protected void handleQueryServiceResponseSingleColumn(Value queryServiceValue, ColumnMetadata metadata, org.hypertrace.gateway.service.v1.common.Row.Builder rowBuilder, ExploreRequestContext requestContext, AttributeMetadataProvider attributeMetadataProvider) {
    FunctionExpression function = requestContext.getFunctionExpressionByAlias(metadata.getColumnName());
    handleQueryServiceResponseSingleColumn(queryServiceValue, metadata, rowBuilder, requestContext, attributeMetadataProvider, function);
}
Also used : FunctionExpression(org.hypertrace.gateway.service.v1.common.FunctionExpression)

Example 5 with FunctionExpression

use of org.hypertrace.gateway.service.v1.common.FunctionExpression in project gateway-service by hypertrace.

the class GatewayValueToQueryValueConverterTest method getFunctionMap.

private Map<FunctionExpression, Function> getFunctionMap() {
    Expression gatewayExprArgument = QueryExpressionUtil.buildAttributeExpression("API.apiId").build();
    org.hypertrace.core.query.service.api.Expression queryExprArgument = QueryRequestUtil.createAttributeExpression("API.apiId");
    return Map.of(FunctionExpression.newBuilder().setFunction(FunctionType.SUM).addArguments(gatewayExprArgument).build(), Function.newBuilder().setFunctionName("SUM").addArguments(queryExprArgument).build(), FunctionExpression.newBuilder().setFunction(FunctionType.AVG).addArguments(gatewayExprArgument).build(), Function.newBuilder().setFunctionName("AVG").addArguments(queryExprArgument).build(), FunctionExpression.newBuilder().setFunction(FunctionType.COUNT).addArguments(gatewayExprArgument).build(), Function.newBuilder().setFunctionName("COUNT").addArguments(queryExprArgument).build(), FunctionExpression.newBuilder().setFunction(FunctionType.DISTINCTCOUNT).addArguments(gatewayExprArgument).build(), Function.newBuilder().setFunctionName("DISTINCTCOUNT").addArguments(queryExprArgument).build(), FunctionExpression.newBuilder().setFunction(FunctionType.MAX).addArguments(gatewayExprArgument).build(), Function.newBuilder().setFunctionName("MAX").addArguments(queryExprArgument).build(), FunctionExpression.newBuilder().setFunction(FunctionType.MIN).addArguments(gatewayExprArgument).build(), Function.newBuilder().setFunctionName("MIN").addArguments(queryExprArgument).build(), FunctionExpression.newBuilder().setFunction(FunctionType.LATEST).addArguments(gatewayExprArgument).build(), Function.newBuilder().setFunctionName("LATEST").addArguments(queryExprArgument).build());
}
Also used : Expression(org.hypertrace.gateway.service.v1.common.Expression) FunctionExpression(org.hypertrace.gateway.service.v1.common.FunctionExpression)

Aggregations

FunctionExpression (org.hypertrace.gateway.service.v1.common.FunctionExpression)27 Test (org.junit.jupiter.api.Test)18 AttributeMetadata (org.hypertrace.core.attribute.service.v1.AttributeMetadata)3 HashMap (java.util.HashMap)2 AttributeKind (org.hypertrace.core.attribute.service.v1.AttributeKind)2 AggregatedMetricValue (org.hypertrace.gateway.service.v1.common.AggregatedMetricValue)2 Value (org.hypertrace.gateway.service.v1.common.Value)2 ArrayList (java.util.ArrayList)1 ColumnMetadata (org.hypertrace.core.query.service.api.ColumnMetadata)1 Expression (org.hypertrace.core.query.service.api.Expression)1 QueryRequest (org.hypertrace.core.query.service.api.QueryRequest)1 ResultSetChunk (org.hypertrace.core.query.service.api.ResultSetChunk)1 Row (org.hypertrace.core.query.service.api.Row)1 RequestContext (org.hypertrace.gateway.service.common.RequestContext)1 QueryAndGatewayDtoConverter.convertToQueryExpression (org.hypertrace.gateway.service.common.converters.QueryAndGatewayDtoConverter.convertToQueryExpression)1 QueryRequestUtil.createStringNullLiteralExpression (org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createStringNullLiteralExpression)1 QueryRequestUtil.createTimeColumnGroupByExpression (org.hypertrace.gateway.service.common.converters.QueryRequestUtil.createTimeColumnGroupByExpression)1 EntityKey (org.hypertrace.gateway.service.entity.EntityKey)1 BaselineEntitiesRequest (org.hypertrace.gateway.service.v1.baseline.BaselineEntitiesRequest)1 BaselineEntitiesResponse (org.hypertrace.gateway.service.v1.baseline.BaselineEntitiesResponse)1