Search in sources :

Example 1 with ResultSetChunk

use of org.hypertrace.entity.query.service.v1.ResultSetChunk 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 ResultSetChunk

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

the class EdsEntityUpdaterTest method nonExistentEntity.

@Test
public void nonExistentEntity() {
    EntityQueryServiceClient mockEqsClient = mock(EntityQueryServiceClient.class);
    EdsEntityUpdater entityUpdater = new EdsEntityUpdater(mockEqsClient);
    // mock empty ResultSetChunk
    UpdateEntityRequest updateEntityRequest = createResolveTestRequest("non-existent-id");
    ResultSetChunk emptyResult = ResultSetChunk.newBuilder().build();
    when(mockEqsClient.update(any(), any())).thenReturn(List.of(emptyResult).iterator());
    UpdateExecutionContext updateExecutionContext = new UpdateExecutionContext(null, mockTestMetadata());
    UpdateEntityResponse response = entityUpdater.update(updateEntityRequest, updateExecutionContext).build();
    assertFalse(response.hasEntity());
}
Also used : UpdateEntityRequest(org.hypertrace.gateway.service.v1.entity.UpdateEntityRequest) UpdateEntityResponse(org.hypertrace.gateway.service.v1.entity.UpdateEntityResponse) EntityQueryServiceClient(org.hypertrace.entity.query.service.client.EntityQueryServiceClient) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 3 with ResultSetChunk

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

the class EntityDataServiceEntityFetcherTests method test_getEntities_WithoutPagination.

@Test
public void test_getEntities_WithoutPagination() {
    List<OrderByExpression> orderByExpressions = List.of(buildOrderByExpression(API_ID_ATTR));
    long startTime = 1L;
    long endTime = 10L;
    int limit = 0;
    int offset = 0;
    String tenantId = "TENANT_ID";
    Map<String, String> requestHeaders = Map.of("x-tenant-id", tenantId);
    AttributeScope entityType = AttributeScope.API;
    EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(entityType.name()).setStartTimeMillis(startTime).setEndTimeMillis(endTime).setFilter(Filter.newBuilder().setOperator(AND).addChildFilter(generateEQFilter(API_TYPE_ATTR, "HTTP")).addChildFilter(generateEQFilter(API_NAME_ATTR, "DISCOVERED"))).addAllOrderBy(orderByExpressions).setLimit(limit).setOffset(offset).build();
    EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
    EntityQueryRequest expectedQueryRequest = EntityQueryRequest.newBuilder().setEntityType("API").addSelection(convertToEntityServiceExpression(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_ID_ATTR)).build())).setFilter(convertToEntityServiceFilter(entitiesRequest.getFilter())).addAllOrderBy(EntityServiceAndGatewayServiceConverter.convertToOrderByExpressions(orderByExpressions)).build();
    List<ResultSetChunk> resultSetChunks = List.of(getResultSetChunk(List.of("API.apiId"), new String[][] { { "apiId1" }, { "apiId2" } }));
    when(entityQueryServiceClient.execute(eq(expectedQueryRequest), eq(requestHeaders))).thenReturn(resultSetChunks.iterator());
    assertEquals(2, entityDataServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequest).size());
}
Also used : TotalEntitiesRequest(org.hypertrace.entity.query.service.v1.TotalEntitiesRequest) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) AttributeScope(org.hypertrace.core.attribute.service.v1.AttributeScope) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) EntitiesRequestAndResponseUtils.buildOrderByExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 4 with ResultSetChunk

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

the class EdsEntityUpdater method update.

public UpdateEntityResponse.Builder update(UpdateEntityRequest updateRequest, UpdateExecutionContext updateExecutionContext) {
    UpdateEntityResponse.Builder responseBuilder = UpdateEntityResponse.newBuilder();
    EntityUpdateRequest eqsUpdateRequest = convertToEqsUpdateRequest(updateRequest);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Sending update request to EDS  ======== \n {}", eqsUpdateRequest);
    }
    Iterator<ResultSetChunk> resultSetChunkIterator = eqsClient.update(eqsUpdateRequest, updateExecutionContext.getRequestHeaders());
    if (!resultSetChunkIterator.hasNext()) {
        return responseBuilder;
    }
    ResultSetChunk chunk = resultSetChunkIterator.next();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Received chunk: " + chunk.toString());
    }
    if (chunk.getRowCount() == 0) {
        return responseBuilder;
    }
    if (chunk.getRowCount() > 1) {
        LOG.warn("Received more than 1 row back. Only the first out of {} rows will be returned in the response", chunk.getRowCount());
    }
    Map<String, AttributeMetadata> resultAttributeMetadata = AttributeMetadataUtil.remapAttributeMetadataByResultKey(updateRequest.getSelectionList(), updateExecutionContext.getAttributeMetadata());
    Row row = chunk.getRow(0);
    Entity.Builder entityBuilder = Entity.newBuilder().setEntityType(updateRequest.getEntityType());
    for (int i = 0; i < chunk.getResultSetMetadata().getColumnMetadataCount(); i++) {
        String resultName = chunk.getResultSetMetadata().getColumnMetadata(i).getColumnName();
        entityBuilder.putAttribute(resultName, EntityServiceAndGatewayServiceConverter.convertQueryValueToGatewayValue(row.getColumn(i), resultAttributeMetadata.get(resultName)));
    }
    responseBuilder.setEntity(entityBuilder);
    return responseBuilder;
}
Also used : Entity(org.hypertrace.gateway.service.v1.entity.Entity) UpdateEntityResponse(org.hypertrace.gateway.service.v1.entity.UpdateEntityResponse) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) Row(org.hypertrace.entity.query.service.v1.Row) EntityUpdateRequest(org.hypertrace.entity.query.service.v1.EntityUpdateRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk)

Example 5 with ResultSetChunk

use of org.hypertrace.entity.query.service.v1.ResultSetChunk 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)

Aggregations

ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)21 Test (org.junit.jupiter.api.Test)17 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)16 Entity (org.hypertrace.entity.data.service.v1.Entity)11 Row (org.hypertrace.entity.query.service.v1.Row)9 ArrayList (java.util.ArrayList)6 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)5 List (java.util.List)4 Collection (org.hypertrace.core.documentstore.Collection)4 Document (org.hypertrace.core.documentstore.Document)4 EntityQueryServiceClient (org.hypertrace.entity.query.service.client.EntityQueryServiceClient)4 ResultSetMetadata (org.hypertrace.entity.query.service.v1.ResultSetMetadata)4 TotalEntitiesRequest (org.hypertrace.entity.query.service.v1.TotalEntitiesRequest)4 Disabled (org.junit.jupiter.api.Disabled)4 ProtocolStringList (com.google.protobuf.ProtocolStringList)3 Iterator (java.util.Iterator)3 AttributeMetadata (org.hypertrace.core.attribute.service.v1.AttributeMetadata)3 AttributeScope (org.hypertrace.core.attribute.service.v1.AttributeScope)3 AttributeValueList (org.hypertrace.entity.data.service.v1.AttributeValueList)3 EntityUpdateInfo (org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo)3