Search in sources :

Example 1 with Builder

use of org.hypertrace.entity.query.service.v1.LiteralConstant.Builder in project gateway-service by hypertrace.

the class EntityDataServiceEntityFetcher method getEntities.

@Override
public EntityFetcherResponse getEntities(EntitiesRequestContext requestContext, EntitiesRequest entitiesRequest) {
    List<String> entityIdAttributeIds = AttributeMetadataUtil.getIdAttributeIds(attributeMetadataProvider, entityIdColumnsConfigs, requestContext, entitiesRequest.getEntityType());
    Map<String, List<String>> requestedAliasesByEntityIdAttributeIds = getExpectedResultNamesForEachAttributeId(entitiesRequest.getSelectionList(), entityIdAttributeIds);
    EntityQueryRequest.Builder builder = EntityQueryRequest.newBuilder().setEntityType(entitiesRequest.getEntityType()).setFilter(EntityServiceAndGatewayServiceConverter.convertToEntityServiceFilter(entitiesRequest.getFilter())).addAllSelection(entityIdAttributeIds.stream().map(entityIdAttr -> EntityServiceAndGatewayServiceConverter.createColumnExpression(entityIdAttr).build()).collect(Collectors.toList()));
    // add time filter for supported scope
    if (!entitiesRequest.getIncludeNonLiveEntities()) {
        EntityServiceAndGatewayServiceConverter.addBetweenTimeFilter(entitiesRequest.getStartTimeMillis(), entitiesRequest.getEndTimeMillis(), attributeMetadataProvider, entitiesRequest, builder, requestContext);
    }
    // Add all expressions in the select that are already not part of the EntityID attributes
    entitiesRequest.getSelectionList().stream().filter(ExpressionReader::isAttributeSelection).filter(expression -> ExpressionReader.getAttributeIdFromAttributeSelection(expression).map(attributeId -> !entityIdAttributeIds.contains(attributeId)).orElse(true)).forEach(expression -> builder.addSelection(EntityServiceAndGatewayServiceConverter.convertToEntityServiceExpression(expression)));
    int limit = entitiesRequest.getLimit();
    if (limit > 0) {
        builder.setLimit(limit);
    }
    int offset = entitiesRequest.getOffset();
    if (offset > 0) {
        builder.setOffset(offset);
    }
    if (!entitiesRequest.getOrderByList().isEmpty()) {
        builder.addAllOrderBy(EntityServiceAndGatewayServiceConverter.convertToOrderByExpressions(entitiesRequest.getOrderByList()));
    }
    EntityQueryRequest entityQueryRequest = builder.build();
    LOG.debug("Sending Query to EDS  ======== \n {}", entityQueryRequest);
    Iterator<ResultSetChunk> resultSetChunkIterator = entityQueryServiceClient.execute(builder.build(), requestContext.getHeaders());
    Map<String, AttributeMetadata> resultMetadataMap = this.getAttributeMetadataByAlias(requestContext, entitiesRequest);
    // We want to retain the order as returned from the respective source. Hence using a
    // LinkedHashMap
    Map<EntityKey, Builder> entityBuilders = new LinkedHashMap<>();
    while (resultSetChunkIterator.hasNext()) {
        ResultSetChunk chunk = resultSetChunkIterator.next();
        LOG.debug("Received chunk: {}", chunk);
        if (chunk.getRowCount() < 1) {
            break;
        }
        for (Row row : chunk.getRowList()) {
            // Construct the entity id from the entityIdAttributes columns
            EntityKey entityKey = EntityKey.of(IntStream.range(0, entityIdAttributeIds.size()).mapToObj(value -> row.getColumn(value).getString()).toArray(String[]::new));
            Builder entityBuilder = entityBuilders.computeIfAbsent(entityKey, k -> Entity.newBuilder());
            entityBuilder.setEntityType(entitiesRequest.getEntityType());
            entityBuilder.setId(entityKey.toString());
            // as post processing.
            for (int i = 0; i < entityIdAttributeIds.size(); i++) {
                entityBuilder.putAttribute(entityIdAttributeIds.get(i), Value.newBuilder().setString(entityKey.getAttributes().get(i)).setValueType(ValueType.STRING).build());
            }
            requestedAliasesByEntityIdAttributeIds.forEach((attributeId, requestedAliasList) -> requestedAliasList.forEach(requestedAlias -> entityBuilder.putAttribute(requestedAlias, entityBuilder.getAttributeOrThrow(attributeId))));
            for (int i = entityIdAttributeIds.size(); i < chunk.getResultSetMetadata().getColumnMetadataCount(); i++) {
                String resultName = chunk.getResultSetMetadata().getColumnMetadata(i).getColumnName();
                AttributeMetadata attributeMetadata = resultMetadataMap.get(resultName);
                entityBuilder.putAttribute(resultName, EntityServiceAndGatewayServiceConverter.convertQueryValueToGatewayValue(row.getColumn(i), attributeMetadata));
            }
        }
    }
    return new EntityFetcherResponse(entityBuilders);
}
Also used : IntStream(java.util.stream.IntStream) Row(org.hypertrace.entity.query.service.v1.Row) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) LoggerFactory(org.slf4j.LoggerFactory) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) ExpressionReader.getExpectedResultNamesForEachAttributeId(org.hypertrace.gateway.service.common.util.ExpressionReader.getExpectedResultNamesForEachAttributeId) ExpressionReader(org.hypertrace.gateway.service.common.util.ExpressionReader) LinkedHashMap(java.util.LinkedHashMap) EntityIdColumnsConfigs(org.hypertrace.gateway.service.entity.config.EntityIdColumnsConfigs) Map(java.util.Map) TotalEntitiesResponse(org.hypertrace.entity.query.service.v1.TotalEntitiesResponse) EntityQueryServiceClient(org.hypertrace.entity.query.service.client.EntityQueryServiceClient) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) EntityServiceAndGatewayServiceConverter(org.hypertrace.gateway.service.common.converters.EntityServiceAndGatewayServiceConverter) Value(org.hypertrace.gateway.service.v1.common.Value) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) ValueType(org.hypertrace.gateway.service.v1.common.ValueType) Collectors(java.util.stream.Collectors) AttributeMetadataUtil(org.hypertrace.gateway.service.common.util.AttributeMetadataUtil) Entity(org.hypertrace.gateway.service.v1.entity.Entity) List(java.util.List) Entry(java.util.Map.Entry) TotalEntitiesRequest(org.hypertrace.entity.query.service.v1.TotalEntitiesRequest) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) AttributeMetadataProvider(org.hypertrace.gateway.service.common.AttributeMetadataProvider) Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) LinkedHashMap(java.util.LinkedHashMap) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) List(java.util.List) Row(org.hypertrace.entity.query.service.v1.Row) ExpressionReader(org.hypertrace.gateway.service.common.util.ExpressionReader) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Example 2 with Builder

use of org.hypertrace.entity.query.service.v1.LiteralConstant.Builder in project gateway-service by hypertrace.

the class EntityRequestHandler method handleRequest.

@Override
public ExploreResponse.Builder handleRequest(ExploreRequestContext requestContext, ExploreRequest exploreRequest) {
    // ourselves.
    if (!exploreRequest.getGroupByList().isEmpty()) {
        requestContext.setHasGroupBy(true);
    }
    Set<String> entityIds = getEntityIds(requestContext, exploreRequest);
    Iterator<ResultSetChunk> resultSetChunkIterator = entityServiceEntityFetcher.getResults(requestContext, exploreRequest, entityIds);
    ExploreResponse.Builder builder = ExploreResponse.newBuilder();
    while (resultSetChunkIterator.hasNext()) {
        org.hypertrace.entity.query.service.v1.ResultSetChunk chunk = resultSetChunkIterator.next();
        getLogger().debug("Received chunk: {}", chunk);
        if (chunk.getRowCount() < 1) {
            break;
        }
        if (!chunk.hasResultSetMetadata()) {
            getLogger().warn("Chunk doesn't have result metadata so couldn't process the response.");
            break;
        }
        chunk.getRowList().forEach(row -> handleRow(row, chunk.getResultSetMetadata(), builder, requestContext, attributeMetadataProvider));
    }
    // If there's a Group By in the request, we need to do the sorting and pagination ourselves.
    if (requestContext.hasGroupBy()) {
        sortAndPaginatePostProcess(builder, requestContext.getOrderByExpressions(), requestContext.getRowLimitBeforeRest(), requestContext.getOffset());
    }
    if (requestContext.hasGroupBy() && requestContext.getIncludeRestGroup()) {
        getTheRestGroupRequestHandler().getRowsForTheRestGroup(requestContext, exploreRequest, builder);
    }
    return builder;
}
Also used : ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) ExploreResponse(org.hypertrace.gateway.service.v1.explore.ExploreResponse) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Example 3 with Builder

use of org.hypertrace.entity.query.service.v1.LiteralConstant.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 4 with Builder

use of org.hypertrace.entity.query.service.v1.LiteralConstant.Builder in project entity-service by hypertrace.

the class EntityQueryServiceImplTest method testUpdate_success.

@Test
public void testUpdate_success() throws Exception {
    Collection mockEntitiesCollection = mockEntitiesCollection();
    Builder newStatus = LiteralConstant.newBuilder().setValue(Value.newBuilder().setValueType(ValueType.STRING).setString("NEW_STATUS"));
    EntityUpdateRequest updateRequest = EntityUpdateRequest.newBuilder().setEntityType(TEST_ENTITY_TYPE).addEntityIds("entity-id-1").setOperation(UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID2)).setValue(newStatus))).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID1))).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID2))).build();
    StreamObserver<ResultSetChunk> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(mockEntitiesCollection, mockMappingForAttributes1And2(), 1, false);
        eqs.update(updateRequest, mockResponseObserver);
        return null;
    });
    verify(mockEntitiesCollection, times(1)).bulkUpdateSubDocs(eq(Map.of(new SingleValueKey("tenant1", "entity-id-1"), Map.of("attributes.status", new JSONDocument(DocStoreJsonFormat.printer().print(newStatus))))));
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Builder(org.hypertrace.entity.query.service.v1.LiteralConstant.Builder) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 5 with Builder

use of org.hypertrace.entity.query.service.v1.LiteralConstant.Builder in project entity-service by hypertrace.

the class EntityQueryConverter method convertToAttributeFilter.

private AttributeFilter convertToAttributeFilter(RequestContext requestContext, Query.Builder queryBuilder, Filter filter) {
    if (filter == null || filter.equals(Filter.getDefaultInstance())) {
        return null;
    }
    AttributeFilter.Builder builder = null;
    if (filter.getChildFilterCount() == 0) {
        // Copy the lhs and rhs from the filter.
        AttributeColumnInformation attributeData = getAttributeColumnInfo(requestContext, filter.getLhs());
        String edsColumnName = attributeData.getColumnName();
        org.hypertrace.entity.query.service.v1.Value rhsValue = filter.getRhs().getLiteral().getValue();
        if (edsColumnName.equals(EntityServiceConstants.ENTITY_ID)) {
            if (rhsValue.getValueType() == ValueType.STRING) {
                queryBuilder.addEntityId(rhsValue.getString());
            } else if (rhsValue.getValueType() == ValueType.STRING_ARRAY) {
                queryBuilder.addAllEntityId(rhsValue.getStringArrayList());
            }
        } else if (edsColumnName.equals(EntityServiceConstants.ENTITY_NAME)) {
            queryBuilder.setEntityName(rhsValue.getString());
        } else {
            builder = AttributeFilter.newBuilder();
            builder.setOperator(convertOperator(filter.getOperator()));
            builder.setName(edsColumnName);
            builder.setIsMultiValued(attributeData.isMultiValued());
            builder.setAttributeValue(convertToAttributeValue(filter.getRhs()));
        }
    } else {
        builder = AttributeFilter.newBuilder();
        builder.setOperator(convertOperator(filter.getOperator()));
        for (Filter child : filter.getChildFilterList()) {
            AttributeFilter attributeFilter = convertToAttributeFilter(requestContext, queryBuilder, child);
            if (null != attributeFilter) {
                builder.addChildFilter(attributeFilter);
            }
        }
    }
    return (null != builder) ? builder.build() : null;
}
Also used : Filter(org.hypertrace.entity.query.service.v1.Filter) AttributeFilter(org.hypertrace.entity.data.service.v1.AttributeFilter) AttributeFilter(org.hypertrace.entity.data.service.v1.AttributeFilter)

Aggregations

EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)3 Filter (org.hypertrace.entity.query.service.v1.Filter)3 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)3 Expression (org.hypertrace.entity.query.service.v1.Expression)2 Row (org.hypertrace.entity.query.service.v1.Row)2 TotalEntitiesRequest (org.hypertrace.entity.query.service.v1.TotalEntitiesRequest)2 TotalEntitiesResponse (org.hypertrace.entity.query.service.v1.TotalEntitiesResponse)2 AttributeMetadataProvider (org.hypertrace.gateway.service.common.AttributeMetadataProvider)2 EntitiesRequestContext (org.hypertrace.gateway.service.entity.EntitiesRequestContext)2 EntitiesRequest (org.hypertrace.gateway.service.v1.entity.EntitiesRequest)2 Test (org.junit.jupiter.api.Test)2 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 AttributeMetadata (org.hypertrace.core.attribute.service.v1.AttributeMetadata)1 Collection (org.hypertrace.core.documentstore.Collection)1