Search in sources :

Example 1 with AttributeKind

use of org.hypertrace.core.attribute.service.v1.AttributeKind 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 AttributeKind

use of org.hypertrace.core.attribute.service.v1.AttributeKind in project gateway-service by hypertrace.

the class MetricAggregationFunctionUtil method getValueTypeForFunctionType.

public static AttributeKind getValueTypeForFunctionType(FunctionExpression functionExpression, Map<String, AttributeMetadata> attributeMetadataMap) {
    // assumes 1 level of aggregation for now, like the rest of the code
    // Also, for the type, it should follow the outer most aggregation type
    String attributeId = ExpressionReader.getAttributeIdFromAttributeSelection(functionExpression).orElseThrow();
    // AggregationValidator
    AttributeMetadata metadata = attributeMetadataMap.get(attributeId);
    Preconditions.checkArgument(metadata != null, "Failed to find value type for this function because it is unable to find the metadata for %s", attributeId);
    return getValueTypeForFunctionType(functionExpression.getFunction(), metadata);
}
Also used : AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata)

Example 3 with AttributeKind

use of org.hypertrace.core.attribute.service.v1.AttributeKind in project gateway-service by hypertrace.

the class QueryAndGatewayDtoConverter method convertToGatewayValueForMetricValue.

public static org.hypertrace.gateway.service.v1.common.Value convertToGatewayValueForMetricValue(AttributeKind attributeKind, Map<String, AttributeMetadata> resultKeyToAttributeMetadataMap, ColumnMetadata metadata, Value queryValue) {
    String resultKey = metadata.getColumnName();
    // if there's no override from the function, then this is regular attribute, then
    // use the attribute map to convert the value
    AttributeMetadata attributeMetadata;
    if (attributeKind == null) {
        attributeMetadata = resultKeyToAttributeMetadataMap.get(resultKey);
    } else {
        attributeMetadata = AttributeMetadata.newBuilder().setId(metadata.getColumnName()).setType(AttributeType.METRIC).setValueKind(attributeKind).build();
    }
    LOG.debug("Converting {} from type: {} to type: {}", resultKey, metadata.getValueType().name(), attributeMetadata.getValueKind().name());
    return QueryAndGatewayDtoConverter.convertQueryValueToGatewayValue(queryValue, attributeMetadata);
}
Also used : AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata)

Example 4 with AttributeKind

use of org.hypertrace.core.attribute.service.v1.AttributeKind in project gateway-service by hypertrace.

the class EntityServiceAndGatewayServiceConverter method convertToGatewayValueForMetricValue.

public static org.hypertrace.gateway.service.v1.common.Value convertToGatewayValueForMetricValue(AttributeKind attributeKind, Map<String, AttributeMetadata> resultKeyToAttributeMetadataMap, ColumnMetadata metadata, Value value) {
    String resultKey = metadata.getColumnName();
    // if there's no override from the function, then this is regular attribute, then
    // use the attribute map to convert the value
    AttributeMetadata attributeMetadata;
    if (attributeKind == null) {
        attributeMetadata = resultKeyToAttributeMetadataMap.get(resultKey);
    } else {
        attributeMetadata = AttributeMetadata.newBuilder().setId(metadata.getColumnName()).setType(AttributeType.METRIC).setValueKind(attributeKind).build();
    }
    LOG.debug("Converting {} from type: {} to type: {}", resultKey, metadata.getValueType().name(), attributeMetadata.getValueKind().name());
    return EntityServiceAndGatewayServiceConverter.convertQueryValueToGatewayValue(value, attributeMetadata);
}
Also used : AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata)

Example 5 with AttributeKind

use of org.hypertrace.core.attribute.service.v1.AttributeKind in project gateway-service by hypertrace.

the class QueryValueToGatewayValueConverterTest method whenMetricValueTypeAlreadyIsDoubleOrFloat_expectsSameType.

@Test
public void whenMetricValueTypeAlreadyIsDoubleOrFloat_expectsSameType() {
    Value value = Value.newBuilder().setValueType(org.hypertrace.core.query.service.api.ValueType.DOUBLE).setDouble(10.25f).build();
    Map<String, AttributeKind> aliasToAttributeKind = new HashMap<>();
    aliasToAttributeKind.put(WEIGHT_METRIC_NAME, AttributeKind.TYPE_DOUBLE);
    org.hypertrace.gateway.service.v1.common.Value actual = QueryAndGatewayDtoConverter.convertToGatewayValueForMetricValue(aliasToAttributeKind, attributeMetadataMap, weightColumnMetadata, value);
    org.hypertrace.gateway.service.v1.common.Value expected = QueryAndGatewayDtoConverter.convertQueryValueToGatewayValue(value);
    assertEquals(expected, actual);
}
Also used : HashMap(java.util.HashMap) Value(org.hypertrace.core.query.service.api.Value) AttributeKind(org.hypertrace.core.attribute.service.v1.AttributeKind) Test(org.junit.jupiter.api.Test)

Aggregations

AttributeKind (org.hypertrace.core.attribute.service.v1.AttributeKind)6 AttributeMetadata (org.hypertrace.core.attribute.service.v1.AttributeMetadata)5 HashMap (java.util.HashMap)3 Value (org.hypertrace.core.query.service.api.Value)2 Test (org.junit.jupiter.api.Test)2 ColumnMetadata (org.hypertrace.core.query.service.api.ColumnMetadata)1 ResultSetChunk (org.hypertrace.core.query.service.api.ResultSetChunk)1 Row (org.hypertrace.core.query.service.api.Row)1 EntityKey (org.hypertrace.gateway.service.entity.EntityKey)1 AggregatedMetricValue (org.hypertrace.gateway.service.v1.common.AggregatedMetricValue)1 Expression (org.hypertrace.gateway.service.v1.common.Expression)1 FunctionExpression (org.hypertrace.gateway.service.v1.common.FunctionExpression)1 Value (org.hypertrace.gateway.service.v1.common.Value)1 EntityInteraction (org.hypertrace.gateway.service.v1.entity.EntityInteraction)1