Search in sources :

Example 1 with EntityQueryRequest

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

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

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

the class EntityQueryServiceTest method testExecuteWithEqualsArrayFilter.

@Test
@Disabled("Disabled until we enable querying based on the new Query DTO")
void testExecuteWithEqualsArrayFilter() {
    String filterValue1 = generateRandomUUID();
    String filterValue2 = generateRandomUUID();
    String filterValue3 = generateRandomUUID();
    AttributeValueList.Builder attributeValueListBuilder = AttributeValueList.newBuilder();
    attributeValueListBuilder.clear();
    Stream.of(filterValue1, filterValue2).map(this::generateAttrValue).forEach(attributeValueListBuilder::addValues);
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 1").putAttributes("labels", AttributeValue.newBuilder().setValueList(attributeValueListBuilder).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1);
    assertFalse(createdEntity1.getEntityId().trim().isEmpty());
    attributeValueListBuilder.clear();
    Stream.of(filterValue2, filterValue3).map(this::generateAttrValue).forEach(attributeValueListBuilder::addValues);
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 2").putAttributes("labels", AttributeValue.newBuilder().setValueList(attributeValueListBuilder).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2);
    assertFalse(createdEntity2.getEntityId().trim().isEmpty());
    attributeValueListBuilder.clear();
    Stream.of(filterValue3, filterValue1, generateRandomUUID()).map(this::generateAttrValue).forEach(attributeValueListBuilder::addValues);
    Entity entity3 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 3").putAttributes("labels", AttributeValue.newBuilder().setValueList(attributeValueListBuilder).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity3 = entityDataServiceClient.upsert(entity3);
    assertNotNull(createdEntity3);
    assertFalse(createdEntity3.getEntityId().trim().isEmpty());
    Entity entity4 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 4").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity4 = entityDataServiceClient.upsert(entity4);
    assertNotNull(createdEntity4);
    assertFalse(createdEntity4.getEntityId().trim().isEmpty());
    attributeValueListBuilder.clear();
    Stream.of(generateRandomUUID(), generateRandomUUID()).map(this::generateAttrValue).forEach(attributeValueListBuilder::addValues);
    Entity entity5 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 5").putAttributes("labels", AttributeValue.newBuilder().setValueList(attributeValueListBuilder).build()).putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity5 = entityDataServiceClient.upsert(entity5);
    assertNotNull(createdEntity5);
    assertFalse(createdEntity5.getEntityId().trim().isEmpty());
    // Filtering for a single value in an array with EQUALS should fetch all rows having the VALUE
    // AS ONE OF THE ARRAY ELEMENTS
    {
        EntityQueryRequest queryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.SERVICE.name()).setFilter(Filter.newBuilder().setOperator(Operator.EQ).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_LABELS_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING).setString(filterValue1)))).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.id").build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.name").build()).build()).build();
        Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(queryRequest));
        List<ResultSetChunk> list = Lists.newArrayList(resultSetChunkIterator);
        assertEquals(1, list.size());
        assertEquals(2, list.get(0).getRowCount());
        assertEquals(0, list.get(0).getChunkId());
        assertTrue(list.get(0).getIsLastChunk());
        assertEquals(createdEntity1.getEntityId(), list.get(0).getRow(0).getColumn(0).getString());
        assertEquals(createdEntity1.getEntityName(), list.get(0).getRow(0).getColumn(1).getString());
        assertEquals(createdEntity3.getEntityId(), list.get(0).getRow(1).getColumn(0).getString());
        assertEquals(createdEntity3.getEntityName(), list.get(0).getRow(1).getColumn(1).getString());
        assertTrue(list.get(0).getResultSetMetadata().getColumnMetadataCount() > 0);
    }
    // Filtering for a list of values in an array with EQUALS should fetch only rows EXACTLY
    // MATCHING the array
    {
        EntityQueryRequest queryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.SERVICE.name()).setFilter(Filter.newBuilder().setOperator(Operator.EQ).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_LABELS_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING_ARRAY).addStringArray(filterValue1).addStringArray(filterValue2)))).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.id").build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.name").build()).build()).build();
        Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(queryRequest));
        List<ResultSetChunk> list = Lists.newArrayList(resultSetChunkIterator);
        assertEquals(1, list.size());
        assertEquals(1, list.get(0).getRowCount());
        assertEquals(0, list.get(0).getChunkId());
        assertTrue(list.get(0).getIsLastChunk());
        assertEquals(createdEntity1.getEntityId(), list.get(0).getRow(0).getColumn(0).getString());
        assertEquals(createdEntity1.getEntityName(), list.get(0).getRow(0).getColumn(1).getString());
        assertTrue(list.get(0).getResultSetMetadata().getColumnMetadataCount() > 0);
    }
    // Filtering for a list of (order-changed) values in an array with EQUALS should fetch EMPTY
    // RESULT-SET
    {
        EntityQueryRequest queryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.SERVICE.name()).setFilter(Filter.newBuilder().setOperator(Operator.EQ).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(API_LABELS_ATTR))).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setValueType(STRING_ARRAY).addStringArray(filterValue2).addStringArray(filterValue1)))).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.id").build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("API.name").build()).build()).build();
        Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(queryRequest));
        List<ResultSetChunk> list = Lists.newArrayList(resultSetChunkIterator);
        assertEquals(1, list.size());
        assertEquals(0, list.get(0).getRowCount());
        assertEquals(0, list.get(0).getChunkId());
        assertTrue(list.get(0).getIsLastChunk());
        assertTrue(list.get(0).getResultSetMetadata().getColumnMetadataCount() > 0);
    }
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) AttributeValueList(org.hypertrace.entity.data.service.v1.AttributeValueList) Iterator(java.util.Iterator) ProtocolStringList(com.google.protobuf.ProtocolStringList) List(java.util.List) ArrayList(java.util.ArrayList) AttributeValueList(org.hypertrace.entity.data.service.v1.AttributeValueList) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 4 with EntityQueryRequest

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

the class EntityQueryServiceTest method testBulkUpdate.

@Test
public void testBulkUpdate() throws InterruptedException {
    Entity.Builder apiEntityBuilder1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.API.name()).setEntityName("api1").putIdentifyingAttributes(EntityConstants.getValue(ServiceAttribute.SERVICE_ATTRIBUTE_ID), createAttribute(SERVICE_ID)).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_NAME), createAttribute("api1")).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_API_TYPE), createAttribute(API_TYPE));
    apiEntityBuilder1.putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute("DISCOVERED")).putAttributes(apiAttributesMap.get(API_HTTP_METHOD_ATTR), createAttribute("GET"));
    Entity entity1 = entityDataServiceClient.upsert(apiEntityBuilder1.build());
    Entity.Builder apiEntityBuilder2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.API.name()).setEntityName("api2").putIdentifyingAttributes(EntityConstants.getValue(ServiceAttribute.SERVICE_ATTRIBUTE_ID), createAttribute(SERVICE_ID)).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_NAME), createAttribute("api2")).putIdentifyingAttributes(EntityConstants.getValue(ApiAttribute.API_ATTRIBUTE_API_TYPE), createAttribute(API_TYPE));
    apiEntityBuilder2.putAttributes(apiAttributesMap.get(API_DISCOVERY_STATE_ATTR), createAttribute("UNDER_DISCOVERY")).putAttributes(apiAttributesMap.get(API_HTTP_METHOD_ATTR), createAttribute("GET"));
    Entity entity2 = entityDataServiceClient.upsert(apiEntityBuilder2.build());
    // create BulkUpdate request
    UpdateOperation update1 = UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(API_DISCOVERY_STATE_ATTR)).setValue(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setString("DISCOVERED")))).build();
    UpdateOperation update2 = UpdateOperation.newBuilder().setSetAttribute(SetAttribute.newBuilder().setAttribute(ColumnIdentifier.newBuilder().setColumnName(API_HTTP_METHOD_ATTR)).setValue(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setString("POST")))).build();
    EntityUpdateInfo updateInfo1 = EntityUpdateInfo.newBuilder().addUpdateOperation(update2).build();
    EntityUpdateInfo updateInfo2 = EntityUpdateInfo.newBuilder().addUpdateOperation(update1).addUpdateOperation(update2).build();
    BulkEntityUpdateRequest bulkUpdateRequest = BulkEntityUpdateRequest.newBuilder().setEntityType(EntityType.API.name()).putEntities(entity1.getEntityId(), updateInfo1).putEntities(entity2.getEntityId(), updateInfo2).build();
    Iterator<ResultSetChunk> iterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.bulkUpdate(bulkUpdateRequest));
    while (iterator.hasNext()) {
        continue;
    }
    // Add a small delay for the update to reflect
    Thread.sleep(500);
    EntityQueryRequest entityQueryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.API.name()).addSelection(createExpression(API_DISCOVERY_STATE_ATTR)).addSelection(createExpression(API_HTTP_METHOD_ATTR)).build();
    Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(entityQueryRequest));
    List<String> values = new ArrayList<>();
    while (resultSetChunkIterator.hasNext()) {
        ResultSetChunk chunk = resultSetChunkIterator.next();
        for (Row row : chunk.getRowList()) {
            for (int i = 0; i < row.getColumnCount(); i++) {
                String value = row.getColumnList().get(i).getString();
                values.add(value);
            }
        }
    }
    assertEquals(4, values.size());
    assertEquals("DISCOVERED", values.get(0));
    assertEquals("POST", values.get(1));
    assertEquals("DISCOVERED", values.get(2));
    assertEquals("POST", values.get(3));
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) ArrayList(java.util.ArrayList) EntityUpdateInfo(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest.EntityUpdateInfo) BulkEntityUpdateRequest(org.hypertrace.entity.query.service.v1.BulkEntityUpdateRequest) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) Row(org.hypertrace.entity.query.service.v1.Row) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 5 with EntityQueryRequest

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

the class EntityQueryServiceTest method testExecute.

@Test
public void testExecute() {
    // create and upsert some entities
    Entity entity1 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 1").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity1 = entityDataServiceClient.upsert(entity1);
    assertNotNull(createdEntity1);
    assertFalse(createdEntity1.getEntityId().trim().isEmpty());
    Entity entity2 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 2").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity2 = entityDataServiceClient.upsert(entity2);
    assertNotNull(createdEntity2);
    assertFalse(createdEntity2.getEntityId().trim().isEmpty());
    Entity entity3 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 3").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity3 = entityDataServiceClient.upsert(entity3);
    assertNotNull(createdEntity3);
    assertFalse(createdEntity3.getEntityId().trim().isEmpty());
    Entity entity4 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 4").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity4 = entityDataServiceClient.upsert(entity4);
    assertNotNull(createdEntity4);
    assertFalse(createdEntity4.getEntityId().trim().isEmpty());
    Entity entity5 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 5").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity5 = entityDataServiceClient.upsert(entity5);
    assertNotNull(createdEntity5);
    assertFalse(createdEntity5.getEntityId().trim().isEmpty());
    EntityQueryRequest queryRequest = EntityQueryRequest.newBuilder().setEntityType(EntityType.SERVICE.name()).setFilter(Filter.newBuilder().setOperatorValue(Operator.LT.getNumber()).setLhs(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("SERVICE.createdTime").build()).build()).setRhs(Expression.newBuilder().setLiteral(LiteralConstant.newBuilder().setValue(org.hypertrace.entity.query.service.v1.Value.newBuilder().setLong(Instant.now().toEpochMilli()).setValueType(ValueType.LONG).build()).build()).build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("SERVICE.id").build()).build()).addSelection(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName("SERVICE.name").build()).build()).build();
    // this entity will be filtered out
    Entity entity6 = Entity.newBuilder().setTenantId(TENANT_ID).setEntityType(EntityType.SERVICE.name()).setEntityName("Some Service 6").putIdentifyingAttributes(EntityConstants.getValue(CommonAttribute.COMMON_ATTRIBUTE_FQN), generateRandomUUIDAttrValue()).build();
    Entity createdEntity6 = entityDataServiceClient.upsert(entity6);
    assertNotNull(createdEntity6);
    assertFalse(createdEntity6.getEntityId().trim().isEmpty());
    Iterator<ResultSetChunk> resultSetChunkIterator = GrpcClientRequestContextUtil.executeWithHeadersContext(HEADERS, () -> entityQueryServiceClient.execute(queryRequest));
    List<ResultSetChunk> list = Lists.newArrayList(resultSetChunkIterator);
    assertEquals(3, list.size());
    assertEquals(2, list.get(0).getRowCount());
    assertEquals(0, list.get(0).getChunkId());
    assertFalse(list.get(0).getIsLastChunk());
    assertEquals(2, list.get(1).getRowCount());
    assertEquals(1, list.get(1).getChunkId());
    assertFalse(list.get(1).getIsLastChunk());
    assertEquals(1, list.get(2).getRowCount());
    assertEquals(2, list.get(2).getChunkId());
    assertTrue(list.get(2).getIsLastChunk());
    assertEquals(createdEntity1.getEntityId(), list.get(0).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity1.getEntityName(), list.get(0).getRow(0).getColumn(1).getString());
    assertEquals(createdEntity2.getEntityId(), list.get(0).getRow(1).getColumn(0).getString());
    assertEquals(createdEntity2.getEntityName(), list.get(0).getRow(1).getColumn(1).getString());
    assertEquals(createdEntity3.getEntityId(), list.get(1).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity3.getEntityName(), list.get(1).getRow(0).getColumn(1).getString());
    assertEquals(createdEntity4.getEntityId(), list.get(1).getRow(1).getColumn(0).getString());
    assertEquals(createdEntity4.getEntityName(), list.get(1).getRow(1).getColumn(1).getString());
    assertEquals(createdEntity5.getEntityId(), list.get(2).getRow(0).getColumn(0).getString());
    assertEquals(createdEntity5.getEntityName(), list.get(2).getRow(0).getColumn(1).getString());
    // metadata sent for each chunk
    assertTrue(list.get(0).getResultSetMetadata().getColumnMetadataCount() > 0);
    assertTrue(list.get(1).getResultSetMetadata().getColumnMetadataCount() > 0);
    assertTrue(list.get(2).getResultSetMetadata().getColumnMetadataCount() > 0);
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Aggregations

EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)24 Test (org.junit.jupiter.api.Test)19 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)15 Entity (org.hypertrace.entity.data.service.v1.Entity)11 ArrayList (java.util.ArrayList)7 Row (org.hypertrace.entity.query.service.v1.Row)7 Query (org.hypertrace.entity.data.service.v1.Query)6 List (java.util.List)5 TotalEntitiesRequest (org.hypertrace.entity.query.service.v1.TotalEntitiesRequest)5 Document (org.hypertrace.core.documentstore.Document)4 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)4 Disabled (org.junit.jupiter.api.Disabled)4 ProtocolStringList (com.google.protobuf.ProtocolStringList)3 Iterator (java.util.Iterator)3 AttributeScope (org.hypertrace.core.attribute.service.v1.AttributeScope)3 Collection (org.hypertrace.core.documentstore.Collection)3 AttributeValueList (org.hypertrace.entity.data.service.v1.AttributeValueList)3 ResultSetMetadata (org.hypertrace.entity.query.service.v1.ResultSetMetadata)3 TotalEntitiesResponse (org.hypertrace.entity.query.service.v1.TotalEntitiesResponse)3 ServiceException (com.google.protobuf.ServiceException)2