Search in sources :

Example 16 with Builder

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());
}
Also used : EntityFetcherResponse(org.hypertrace.gateway.service.common.datafetcher.EntityFetcherResponse) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext)

Example 17 with Builder

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());
}
Also used : EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) Expression(org.hypertrace.entity.query.service.v1.Expression) Filter(org.hypertrace.entity.query.service.v1.Filter) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) AttributeMetadataProvider(org.hypertrace.gateway.service.common.AttributeMetadataProvider) Test(org.junit.jupiter.api.Test) AbstractGatewayServiceTest(org.hypertrace.gateway.service.AbstractGatewayServiceTest)

Example 18 with Builder

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();
}
Also used : Arrays(java.util.Arrays) Expression(org.hypertrace.gateway.service.v1.common.Expression) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) OrderByUtil(org.hypertrace.gateway.service.common.util.OrderByUtil) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ExpressionReader(org.hypertrace.gateway.service.common.util.ExpressionReader) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) Map(java.util.Map) AttributeSource(org.hypertrace.core.attribute.service.v1.AttributeSource) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Filter(org.hypertrace.gateway.service.v1.common.Filter) Set(java.util.Set) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Predicate.not(java.util.function.Predicate.not) Collections(java.util.Collections) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation)

Example 19 with Builder

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();
}
Also used : Arrays(java.util.Arrays) Expression(org.hypertrace.gateway.service.v1.common.Expression) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) OrderByUtil(org.hypertrace.gateway.service.common.util.OrderByUtil) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ExpressionReader(org.hypertrace.gateway.service.common.util.ExpressionReader) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) Map(java.util.Map) AttributeSource(org.hypertrace.core.attribute.service.v1.AttributeSource) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Filter(org.hypertrace.gateway.service.v1.common.Filter) Set(java.util.Set) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Predicate.not(java.util.function.Predicate.not) Collections(java.util.Collections) AttributeSource(org.hypertrace.core.attribute.service.v1.AttributeSource) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Expression(org.hypertrace.gateway.service.v1.common.Expression) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 20 with Builder

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);
}
Also used : Expression(org.hypertrace.entity.query.service.v1.Expression) AttributeExpression(org.hypertrace.gateway.service.v1.common.AttributeExpression) OrderByExpression(org.hypertrace.entity.query.service.v1.OrderByExpression) Filter(org.hypertrace.entity.query.service.v1.Filter)

Aggregations

EntityKey (org.hypertrace.gateway.service.entity.EntityKey)15 EntitiesRequest (org.hypertrace.gateway.service.v1.entity.EntitiesRequest)15 EntitiesRequestContext (org.hypertrace.gateway.service.entity.EntitiesRequestContext)14 Builder (org.hypertrace.gateway.service.v1.entity.Entity.Builder)14 OrderByExpression (org.hypertrace.gateway.service.v1.common.OrderByExpression)12 Test (org.junit.jupiter.api.Test)11 AttributeScope (org.hypertrace.core.attribute.service.v1.AttributeScope)10 Expression (org.hypertrace.gateway.service.v1.common.Expression)10 LinkedHashMap (java.util.LinkedHashMap)9 EntitiesRequestAndResponseUtils.buildOrderByExpression (org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression)9 EntityFetcherResponse (org.hypertrace.gateway.service.common.datafetcher.EntityFetcherResponse)9 QueryRequest (org.hypertrace.core.query.service.api.QueryRequest)8 ResultSetChunk (org.hypertrace.core.query.service.api.ResultSetChunk)8 EntityResponse (org.hypertrace.gateway.service.common.datafetcher.EntityResponse)8 TimeAggregation (org.hypertrace.gateway.service.v1.common.TimeAggregation)8 AttributeMetadata (org.hypertrace.core.attribute.service.v1.AttributeMetadata)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5