use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class QueryServiceEntityFetcherTests method test_getEntitiesBySpace.
@Test
public void test_getEntitiesBySpace() {
long startTime = 1L;
long endTime = 10L;
int limit = 10;
int offset = 0;
String tenantId = "TENANT_ID";
String space = "test-space";
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).addSelection(buildExpression(API_NAME_ATTR)).setSpaceId(space).setLimit(limit).setOffset(offset).build();
EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
QueryRequest expectedQueryRequest = QueryRequest.newBuilder().addSelection(createAttributeExpression(API_ID_ATTR)).addSelection(createAttributeExpression(API_NAME_ATTR)).addSelection(QueryRequestUtil.createCountByColumnSelection("API.id")).setFilter(createQsRequestFilter(API_START_TIME_ATTR, API_ID_ATTR, startTime, endTime, createStringFilter(SPACE_IDS_ATTR, Operator.EQ, "test-space"))).addGroupBy(createAttributeExpression(API_ID_ATTR)).addGroupBy(createAttributeExpression(API_NAME_ATTR)).setOffset(offset).setLimit(QueryServiceClient.DEFAULT_QUERY_SERVICE_GROUP_BY_LIMIT).build();
List<ResultSetChunk> resultSetChunks = List.of(getResultSetChunk(List.of(API_ID_ATTR, API_NAME_ATTR), new String[][] { { "api-id-0", "api-0" } }));
Map<EntityKey, Builder> expectedEntityKeyBuilderResponseMap = Map.of(EntityKey.of("api-id-0"), Entity.newBuilder().setEntityType(AttributeScope.API.name()).setId("api-id-0").putAttribute(API_NAME_ATTR, getStringValue("api-0")).putAttribute(API_ID_ATTR, getStringValue("api-id-0")));
EntityFetcherResponse expectedEntityFetcherResponse = new EntityFetcherResponse(expectedEntityKeyBuilderResponseMap);
when(queryServiceClient.executeQuery(eq(expectedQueryRequest), eq(requestHeaders), eq(500))).thenReturn(resultSetChunks.iterator());
compareEntityFetcherResponses(expectedEntityFetcherResponse, queryServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequest));
}
use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class ExecutionVisitor method visit.
@Override
public EntityResponse visit(SortAndPaginateNode sortAndPaginateNode) {
EntityResponse childNodeResponse = sortAndPaginateNode.getChildNode().acceptVisitor(this);
// Create a list from elements of HashMap
List<Map.Entry<EntityKey, Builder>> list = new LinkedList<>(childNodeResponse.getEntityFetcherResponse().getEntityKeyBuilderMap().entrySet());
// Sort the list
List<Map.Entry<EntityKey, Entity.Builder>> sortedList = DataCollectionUtil.limitAndSort(list.stream(), sortAndPaginateNode.getLimit(), sortAndPaginateNode.getOffset(), sortAndPaginateNode.getOrderByExpressionList().size(), new EntityKeyEntityBuilderEntryComparator(sortAndPaginateNode.getOrderByExpressionList()));
// put data from sorted list to a linked hashmap
Map<EntityKey, Builder> linkedHashMap = new LinkedHashMap<>();
sortedList.forEach(entry -> linkedHashMap.put(entry.getKey(), entry.getValue()));
return new EntityResponse(new EntityFetcherResponse(linkedHashMap), childNodeResponse.getTotal());
}
use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class ExecutionVisitor method visit.
@Override
public EntityResponse visit(PaginateOnlyNode paginateOnlyNode) {
EntityResponse childNodeResponse = paginateOnlyNode.getChildNode().acceptVisitor(this);
// Create a list from elements of HashMap
List<Map.Entry<EntityKey, Builder>> list = new LinkedList<>(childNodeResponse.getEntityFetcherResponse().getEntityKeyBuilderMap().entrySet());
// Sort the list
List<Map.Entry<EntityKey, Entity.Builder>> sortedList = DataCollectionUtil.paginateAndLimit(list.stream(), paginateOnlyNode.getLimit(), paginateOnlyNode.getOffset());
// put data from sorted list to a linked hashmap
Map<EntityKey, Builder> linkedHashMap = new LinkedHashMap<>();
sortedList.forEach(entry -> linkedHashMap.put(entry.getKey(), entry.getValue()));
return new EntityResponse(new EntityFetcherResponse(linkedHashMap), childNodeResponse.getTotal());
}
use of org.hypertrace.gateway.service.v1.entity.Entity.Builder in project gateway-service by hypertrace.
the class RequestHandler method handleQueryServiceResponse.
private ExploreResponse.Builder handleQueryServiceResponse(ExploreRequestContext context, Iterator<ResultSetChunk> resultSetChunkIterator, ExploreRequestContext requestContext, AttributeMetadataProvider attributeMetadataProvider) {
ExploreResponse.Builder builder = ExploreResponse.newBuilder();
while (resultSetChunkIterator.hasNext()) {
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 -> handleQueryServiceResponseSingleRow(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()) {
theRestGroupRequestHandler.getRowsForTheRestGroup(context, requestContext.getExploreRequest(), builder);
}
return builder;
}
use of org.hypertrace.gateway.service.v1.entity.Entity.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;
}
Aggregations