use of org.hypertrace.gateway.service.v1.common.ValueType 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);
}
use of org.hypertrace.gateway.service.v1.common.ValueType 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();
}
Aggregations