use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class EntityRequestHandler method getEntityIds.
private Set<String> getEntityIds(ExploreRequestContext requestContext, ExploreRequest exploreRequest) {
EntitiesRequestContext entitiesRequestContext = convert(attributeMetadataProvider, requestContext);
EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(exploreRequest.getContext()).setStartTimeMillis(exploreRequest.getStartTimeMillis()).setEndTimeMillis(exploreRequest.getEndTimeMillis()).build();
EntityFetcherResponse response = queryServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequest);
return response.getEntityKeyBuilderMap().values().stream().map(Builder::getId).collect(Collectors.toUnmodifiableSet());
}
use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class EntityServiceAndGatewayServiceConverterTest method testAddBetweenFilter.
@Test
public void testAddBetweenFilter() {
int startTimeMillis = 1;
int endTimeMillis = 2;
String timestamp = "lastActivity";
String timestampAttributeName = BACKEND.name() + "." + timestamp;
Expression.Builder expectedStartTimeConstant = Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.LONG).setLong(startTimeMillis)));
Expression.Builder expectedEndTimeConstant = Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.LONG).setLong(endTimeMillis)));
Filter.Builder expectedStartTimeFilterBuilder = Filter.newBuilder();
expectedStartTimeFilterBuilder.setOperator(Operator.GE);
expectedStartTimeFilterBuilder.setLhs(EntityServiceAndGatewayServiceConverter.createColumnExpression(timestampAttributeName));
expectedStartTimeFilterBuilder.setRhs(expectedStartTimeConstant);
Filter.Builder expectedEndTimeFilterBuilder = Filter.newBuilder();
expectedEndTimeFilterBuilder.setOperator(Operator.LT);
expectedEndTimeFilterBuilder.setLhs(EntityServiceAndGatewayServiceConverter.createColumnExpression(timestampAttributeName));
expectedEndTimeFilterBuilder.setRhs(expectedEndTimeConstant);
Filter.Builder expectedFilterBuilder = Filter.newBuilder();
expectedFilterBuilder.setOperator(Operator.AND);
expectedFilterBuilder.addChildFilter(expectedStartTimeFilterBuilder);
expectedFilterBuilder.addChildFilter(expectedEndTimeFilterBuilder);
AttributeMetadataProvider mockProvider = mock(AttributeMetadataProvider.class);
when(mockProvider.getAttributeMetadata(any(EntitiesRequestContext.class), eq(BACKEND.name()), eq(timestamp))).thenReturn(Optional.of(AttributeMetadata.newBuilder().setId(timestampAttributeName).build()));
EntityQueryRequest.Builder builder = EntityQueryRequest.newBuilder();
EntitiesRequest request = EntitiesRequest.newBuilder().setEntityType(API.name()).build();
EntityServiceAndGatewayServiceConverter.addBetweenTimeFilter(startTimeMillis, endTimeMillis, mockProvider, request, builder, mock(EntitiesRequestContext.class));
// no filter added for unsupported entity types
assertEquals(Filter.getDefaultInstance(), builder.getFilter());
// if it's an backend, filters will be added
request = EntitiesRequest.newBuilder().setEntityType(BACKEND.name()).build();
EntityServiceAndGatewayServiceConverter.addBetweenTimeFilter(startTimeMillis, endTimeMillis, mockProvider, request, builder, mock(EntitiesRequestContext.class));
assertEquals(expectedFilterBuilder.build(), builder.getFilter());
// if there's an existing filter in EntitiesRequest, it will be preserved
Expression.Builder scoreConstant = Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.INT).setInt(10)));
Filter.Builder existingScoreFilter = Filter.newBuilder();
existingScoreFilter.setOperator(Operator.GT);
existingScoreFilter.setLhs(EntityServiceAndGatewayServiceConverter.createColumnExpression("TEST.score"));
existingScoreFilter.setRhs(scoreConstant);
expectedFilterBuilder.addChildFilter(existingScoreFilter);
builder.setFilter(existingScoreFilter.build());
EntityServiceAndGatewayServiceConverter.addBetweenTimeFilter(startTimeMillis, endTimeMillis, mockProvider, request, builder, mock(EntitiesRequestContext.class));
assertEquals(expectedFilterBuilder.build(), builder.getFilter());
}
use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class ExpressionContext method getDataSourceToTimeAggregation.
private ImmutableMap<String, List<TimeAggregation>> getDataSourceToTimeAggregation(List<TimeAggregation> timeAggregations) {
Map<String, List<TimeAggregation>> result = new HashMap<>();
for (TimeAggregation timeAggregation : timeAggregations) {
Map<String, List<Expression>> map = getDataSourceToExpressionMap(Collections.singletonList(timeAggregation.getAggregation()));
// There should only be one element in the map.
result.computeIfAbsent(map.keySet().iterator().next(), k -> new ArrayList<>()).add(timeAggregation);
}
return ImmutableMap.<String, List<TimeAggregation>>builder().putAll(result).build();
}
use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class ExpressionContext method getDataSourceToExpressionMap.
private ImmutableMap<String, List<Expression>> getDataSourceToExpressionMap(List<Expression> expressions) {
if (expressions == null || expressions.isEmpty()) {
return ImmutableMap.of();
}
Map<String, List<Expression>> sourceToExpressionMap = new HashMap<>();
for (Expression expression : expressions) {
Set<String> attributeIds = ExpressionReader.extractAttributeIds(expression);
Set<AttributeSource> sources = Arrays.stream(AttributeSource.values()).collect(Collectors.toSet());
for (String attributeId : attributeIds) {
List<AttributeSource> sourcesList = attributeMetadataMap.get(attributeId).getSourcesList();
sources.retainAll(sourcesList);
allAttributesToSourcesMap.computeIfAbsent(attributeId, v -> new HashSet<>()).addAll(sourcesList.stream().map(Enum::name).collect(Collectors.toList()));
}
if (sources.isEmpty()) {
LOG.error("Skipping Expression: {}. No source found", expression);
continue;
}
for (AttributeSource source : sources) {
sourceToExpressionMap.computeIfAbsent(source.name(), v -> new ArrayList<>()).add(expression);
}
}
return ImmutableMap.<String, List<Expression>>builder().putAll(sourceToExpressionMap).build();
}
use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class EntityServiceAndGatewayServiceConverter method addBetweenTimeFilter.
/**
* Add between time filter for queries on EDS where a time notion (based on the latest entity
* state) is supported.
*
* @param startTimeMillis
* @param endTimeMillis
* @param attributeMetadataProvider
* @param entitiesRequest
* @param builder
* @return
*/
public static void addBetweenTimeFilter(long startTimeMillis, long endTimeMillis, AttributeMetadataProvider attributeMetadataProvider, EntitiesRequest entitiesRequest, EntityQueryRequest.Builder builder, EntitiesRequestContext entitiesRequestContext) {
// only scope has timestamp is supported
if (TimestampConfigs.getTimestampColumn(entitiesRequest.getEntityType()) == null) {
return;
}
String timestampAttributeName = AttributeMetadataUtil.getTimestampAttributeId(attributeMetadataProvider, entitiesRequestContext, entitiesRequest.getEntityType());
Expression.Builder startTimeConstant = Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.LONG).setLong(startTimeMillis)));
Expression.Builder endTimeConstant = Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.LONG).setLong(endTimeMillis)));
Filter.Builder startTimeFilterBuilder = Filter.newBuilder();
startTimeFilterBuilder.setOperator(Operator.GE);
startTimeFilterBuilder.setLhs(createColumnExpression(timestampAttributeName));
startTimeFilterBuilder.setRhs(startTimeConstant);
Filter.Builder endTimeFilterBuilder = Filter.newBuilder();
endTimeFilterBuilder.setOperator(Operator.LT);
endTimeFilterBuilder.setLhs(createColumnExpression(timestampAttributeName));
endTimeFilterBuilder.setRhs(endTimeConstant);
Filter existingFilter = builder.getFilter();
Filter.Builder newFilterBuilder = Filter.newBuilder();
newFilterBuilder.setOperator(Operator.AND);
newFilterBuilder.addChildFilter(startTimeFilterBuilder).addChildFilter(endTimeFilterBuilder);
if (existingFilter != null && !existingFilter.equals(Filter.getDefaultInstance())) {
newFilterBuilder.addChildFilter(existingFilter);
}
builder.setFilter(newFilterBuilder);
}
Aggregations