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());
}
}
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations