use of com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.Entity in project gateway-service by hypertrace.
the class QueryServiceEntityFetcher method getTimeAggregatedMetrics.
@Override
public EntityFetcherResponse getTimeAggregatedMetrics(EntitiesRequestContext requestContext, EntitiesRequest entitiesRequest) {
// No need to make execute the rest of this if there are no TimeAggregations in the request.
if (entitiesRequest.getTimeAggregationCount() == 0) {
return new EntityFetcherResponse();
}
// Only supported filter is entityIds IN ["id1", "id2", "id3"]
List<String> idColumns = AttributeMetadataUtil.getIdAttributeIds(attributeMetadataProvider, entityIdColumnsConfigs, requestContext, entitiesRequest.getEntityType());
String timeColumn = AttributeMetadataUtil.getTimestampAttributeId(attributeMetadataProvider, requestContext, entitiesRequest.getEntityType());
Map<String, AttributeMetadata> attributeMetadataMap = attributeMetadataProvider.getAttributesMetadata(requestContext, entitiesRequest.getEntityType());
Map<String, AttributeMetadata> resultKeyToAttributeMetadataMap = this.remapAttributeMetadataByResultName(entitiesRequest, attributeMetadataMap);
entitiesRequestValidator.validate(entitiesRequest, attributeMetadataMap);
entitiesRequest.getTimeAggregationList().forEach(timeAggregation -> requestContext.mapAliasToTimeAggregation(timeAggregation.getAggregation().getFunction().getAlias(), timeAggregation));
// First group the Aggregations based on the period so that we can issue separate queries
// to QueryService for each different Period.
Collection<List<TimeAggregation>> result = entitiesRequest.getTimeAggregationList().stream().collect(Collectors.groupingBy(TimeAggregation::getPeriod)).values();
Map<EntityKey, Map<String, MetricSeries.Builder>> entityMetricSeriesMap = new LinkedHashMap<>();
for (List<TimeAggregation> batch : result) {
Period period = batch.get(0).getPeriod();
ChronoUnit unit = ChronoUnit.valueOf(period.getUnit());
long periodSecs = Duration.of(period.getValue(), unit).getSeconds();
QueryRequest request = buildTimeSeriesQueryRequest(entitiesRequest, requestContext, periodSecs, batch, idColumns, timeColumn);
if (LOG.isDebugEnabled()) {
LOG.debug("Sending time series queryRequest to query service: ======== \n {}", request.toString());
}
Iterator<ResultSetChunk> resultSetChunkIterator = queryServiceClient.executeQuery(request, requestContext.getHeaders(), requestTimeout);
while (resultSetChunkIterator.hasNext()) {
ResultSetChunk chunk = resultSetChunkIterator.next();
if (LOG.isDebugEnabled()) {
LOG.debug("Received chunk: " + chunk.toString());
}
if (chunk.getRowCount() < 1) {
break;
}
if (!chunk.hasResultSetMetadata()) {
LOG.warn("Chunk doesn't have result metadata so couldn't process the response.");
break;
}
for (Row row : chunk.getRowList()) {
// Construct the entity id from the entityIdAttributeIds columns
EntityKey entityKey = EntityKey.of(IntStream.range(0, idColumns.size()).mapToObj(value -> row.getColumn(value).getString()).toArray(String[]::new));
Map<String, MetricSeries.Builder> metricSeriesMap = entityMetricSeriesMap.computeIfAbsent(entityKey, k -> new LinkedHashMap<>());
Interval.Builder intervalBuilder = Interval.newBuilder();
// Second column is the time column
Value value = QueryAndGatewayDtoConverter.convertQueryValueToGatewayValue(row.getColumn(idColumns.size()));
if (value.getValueType() == ValueType.STRING) {
long startTime = Long.parseLong(value.getString());
long endTime = startTime + TimeUnit.SECONDS.toMillis(periodSecs);
intervalBuilder.setStartTimeMillis(startTime);
intervalBuilder.setEndTimeMillis(endTime);
for (int i = idColumns.size() + 1; i < chunk.getResultSetMetadata().getColumnMetadataCount(); i++) {
ColumnMetadata metadata = chunk.getResultSetMetadata().getColumnMetadata(i);
TimeAggregation timeAggregation = requestContext.getTimeAggregationByAlias(metadata.getColumnName());
if (timeAggregation == null) {
LOG.warn("Couldn't find an aggregate for column: {}", metadata.getColumnName());
continue;
}
FunctionType functionType = timeAggregation.getAggregation().getFunction().getFunction();
AttributeMetadata functionAttributeMetadata = resultKeyToAttributeMetadataMap.get(metadata.getColumnName());
Value convertedValue = QueryAndGatewayDtoConverter.convertToGatewayValueForMetricValue(MetricAggregationFunctionUtil.getValueTypeForFunctionType(functionType, functionAttributeMetadata), resultKeyToAttributeMetadataMap, metadata, row.getColumn(i));
List<org.hypertrace.gateway.service.v1.common.Expression> healthExpressions = timeAggregation.getAggregation().getFunction().getArgumentsList().stream().filter(org.hypertrace.gateway.service.v1.common.Expression::hasHealth).collect(Collectors.toList());
Preconditions.checkArgument(healthExpressions.size() <= 1);
Health health = Health.NOT_COMPUTED;
MetricSeries.Builder seriesBuilder = metricSeriesMap.computeIfAbsent(metadata.getColumnName(), k -> getMetricSeriesBuilder(timeAggregation));
seriesBuilder.addValue(Interval.newBuilder(intervalBuilder.build()).setValue(convertedValue).setHealth(health));
}
} else {
LOG.warn("Was expecting STRING values only but received valueType: {}", value.getValueType());
}
}
}
}
Map<EntityKey, Entity.Builder> resultMap = new LinkedHashMap<>();
for (Map.Entry<EntityKey, Map<String, MetricSeries.Builder>> entry : entityMetricSeriesMap.entrySet()) {
Entity.Builder entityBuilder = Entity.newBuilder().setEntityType(entitiesRequest.getEntityType()).setId(entry.getKey().toString()).putAllMetricSeries(entry.getValue().entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> getSortedMetricSeries(e.getValue()))));
for (int i = 0; i < idColumns.size(); i++) {
entityBuilder.putAttribute(idColumns.get(i), Value.newBuilder().setString(entry.getKey().getAttributes().get(i)).setValueType(ValueType.STRING).build());
}
resultMap.put(entry.getKey(), entityBuilder);
}
return new EntityFetcherResponse(resultMap);
}
use of com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.Entity in project gateway-service by hypertrace.
the class EntityServiceTest method testGetEntitiesOnlySelectFromSingleSourceWithTimeRangeShouldUseQueryService.
@Test
public void testGetEntitiesOnlySelectFromSingleSourceWithTimeRangeShouldUseQueryService() {
long endTime = System.currentTimeMillis();
long startTime = endTime - 1000;
EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType("API").setStartTimeMillis(startTime).setEndTimeMillis(endTime).addSelection(buildAttributeExpression("API.apiId", "API Id")).addSelection(buildAttributeExpression("API.apiName", "API Name")).setLimit(2).build();
// The filter sent down to query-service by QueryServiceEntityFetcher when there is no filter in
// EntitiesRequest
Filter queryServiceFilter = createQsDefaultRequestFilter("API.startTime", "API.apiId", startTime, endTime);
QueryRequest expectedQueryRequest = QueryRequest.newBuilder().addSelection(createAttributeExpression(// Added implicitly in the getEntitiesAndAggregatedMetrics() in
"API.apiId")).addSelection(createAttributeExpression("API.apiName", "API Name")).addSelection(createQsAggregationExpression("COUNT", "API.apiId")).setFilter(queryServiceFilter).addGroupBy(createAttributeExpression("API.apiId")).addGroupBy(createAttributeExpression("API.apiName", "API Name")).setLimit(QueryServiceClient.DEFAULT_QUERY_SERVICE_GROUP_BY_LIMIT).build();
when(queryServiceClient.executeQuery(eq(expectedQueryRequest), any(), Mockito.anyInt())).thenReturn(List.of(getResultSetChunk(List.of("API.apiId", "API.apiName"), new String[][] { { "apiId1", "/login" }, { "apiId2", "/checkout" } })).iterator());
// get total request.
expectedQueryRequest = QueryRequest.newBuilder().addSelection(createAttributeExpression(// Added implicitly in the getEntitiesAndAggregatedMetrics() in
"API.apiId")).addSelection(createQsAggregationExpression("COUNT", "API.apiId")).setFilter(queryServiceFilter).addGroupBy(createAttributeExpression("API.apiId")).setLimit(QueryServiceClient.DEFAULT_QUERY_SERVICE_GROUP_BY_LIMIT).build();
when(queryServiceClient.executeQuery(eq(expectedQueryRequest), any(), Mockito.anyInt())).thenReturn(List.of(getResultSetChunk(List.of("API.apiId"), new String[][] { { "apiId1" }, { "apiId2" } })).iterator());
ScopeFilterConfigs scopeFilterConfigs = new ScopeFilterConfigs(ConfigFactory.empty());
EntityService entityService = new EntityService(queryServiceClient, 500, entityQueryServiceClient, attributeMetadataProvider, entityIdColumnsConfigs, scopeFilterConfigs, logConfig);
EntitiesResponse response = entityService.getEntities(TENANT_ID, entitiesRequest, Map.of());
Assertions.assertNotNull(response);
Assertions.assertEquals(2, response.getTotal());
Entity entity1 = response.getEntity(0);
Assertions.assertEquals("apiId1", entity1.getAttributeMap().get("API.apiId").getString());
Assertions.assertEquals("/login", entity1.getAttributeMap().get("API.apiName").getString());
Entity entity2 = response.getEntity(1);
Assertions.assertEquals("apiId2", entity2.getAttributeMap().get("API.apiId").getString());
Assertions.assertEquals("/checkout", entity2.getAttributeMap().get("API.apiName").getString());
}
use of com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.Entity in project gateway-service by hypertrace.
the class EntityServiceTest method testGetEntitiesOnlySelectFromMultipleSources.
@Test
public void testGetEntitiesOnlySelectFromMultipleSources() {
when(entityQueryServiceClient.execute(any(), any())).thenReturn(List.of(org.hypertrace.entity.query.service.v1.ResultSetChunk.newBuilder().setResultSetMetadata(generateEntityServiceResultSetMetadataFor("API.apiId", "API Http method")).addRow(generateEntityServiceRowFor("apiId1", "GET")).addRow(generateEntityServiceRowFor("apiId2", "POST")).build()).iterator());
ScopeFilterConfigs scopeFilterConfigs = new ScopeFilterConfigs(ConfigFactory.empty());
EntityService entityService = new EntityService(queryServiceClient, 500, entityQueryServiceClient, attributeMetadataProvider, entityIdColumnsConfigs, scopeFilterConfigs, logConfig);
EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType("API").setStartTimeMillis(System.currentTimeMillis() - 1000).setEndTimeMillis(System.currentTimeMillis()).setFilter(org.hypertrace.gateway.service.v1.common.Filter.newBuilder().setOperator(Operator.IN).setLhs(buildAttributeExpression("API.httpMethod", "API Http method")).setRhs(getStringListLiteral(List.of("GET", "POST")))).addSelection(buildAttributeExpression("API.apiId", "API Id")).addSelection(buildAttributeExpression("API.apiName", "API Name")).addSelection(buildAttributeExpression("API.httpMethod", "API Http method")).addSelection(getAggregateFunctionExpression("API.duration", FunctionType.AVG, "duration")).addTimeAggregation(getTimeAggregationFor(getAggregateFunctionExpression("API.duration", FunctionType.AVG, "duration_ts").build())).build();
QueryRequest expectedQueryRequest = QueryRequest.newBuilder().setFilter(QueryServiceRequestAndResponseUtils.createQsDefaultRequestFilter("API.startTime", "API.apiId", entitiesRequest.getStartTimeMillis(), entitiesRequest.getEndTimeMillis())).addSelection(createAttributeExpression("API.apiId")).addSelection(createAttributeExpression("API.apiName", "API Name")).addSelection(QueryServiceRequestAndResponseUtils.createQsAggregationExpression("COUNT", "API.apiId")).addGroupBy(createAttributeExpression("API.apiId")).addGroupBy(createAttributeExpression("API.apiName", "API Name")).setLimit(10000).build();
when(queryServiceClient.executeQuery(expectedQueryRequest, Map.of(), 500)).thenReturn(List.of(ResultSetChunk.newBuilder().setResultSetMetadata(generateResultSetMetadataFor("API.apiId", "API Name")).addRow(generateRowFor("apiId1", "/login")).addRow(generateRowFor("apiId2", "/checkout")).build()).iterator());
QueryRequest secondQueryRequest = QueryRequest.newBuilder().setFilter(createCompositeFilter(AND, List.of(createFilter("API.apiId", NEQ, createStringNullLiteralExpression()), createBetweenTimesFilter("API.startTime", entitiesRequest.getStartTimeMillis(), entitiesRequest.getEndTimeMillis()), createFilter(createAttributeExpression("API.apiId", "entityId0"), IN, createStringArrayLiteralExpression(List.of("apiId2", "apiId1")))))).addSelection(createAttributeExpression("API.apiId")).addSelection(createQsAggregationExpression("AVG", "API.duration", "duration")).addGroupBy(createAttributeExpression("API.apiId")).setLimit(10000).build();
when(queryServiceClient.executeQuery(secondQueryRequest, Map.of(), 500)).thenReturn(List.of(ResultSetChunk.newBuilder().setResultSetMetadata(generateResultSetMetadataFor("API.apiId", "duration")).addRow(generateRowFor("apiId1", "10.0")).addRow(generateRowFor("apiId2", "20.0")).build()).iterator());
long alignedStartTime = alignToPeriodBoundary(entitiesRequest.getStartTimeMillis(), 60, true);
long alignedEndTime = alignToPeriodBoundary(entitiesRequest.getEndTimeMillis(), 60, false);
QueryRequest thirdQueryRequest = QueryRequest.newBuilder().setFilter(createCompositeFilter(AND, List.of(createFilter("API.apiId", NEQ, createStringNullLiteralExpression()), createBetweenTimesFilter("API.startTime", alignedStartTime, alignedEndTime), createFilter(createAttributeExpression("API.apiId", "entityId0"), IN, createStringArrayLiteralExpression(List.of("apiId2", "apiId1")))))).addSelection(createQsAggregationExpression("AVG", "API.duration", "duration_ts")).addGroupBy(createAttributeExpression("API.apiId")).addGroupBy(createTimeColumnGroupByExpression("API.startTime", 60)).setLimit(10000).build();
when(queryServiceClient.executeQuery(thirdQueryRequest, Map.of(), 500)).thenReturn(List.of(ResultSetChunk.newBuilder().setResultSetMetadata(generateResultSetMetadataFor("API.apiId", "timestamp", "duration_ts")).addRow(generateRowFor("apiId1", String.valueOf(alignedStartTime), "10.0")).addRow(generateRowFor("apiId2", String.valueOf(alignedStartTime), "20.0")).build()).iterator());
EntitiesResponse response = entityService.getEntities(TENANT_ID, entitiesRequest, Map.of());
Assertions.assertNotNull(response);
Assertions.assertEquals(2, response.getTotal());
for (Entity entity : response.getEntityList()) {
if ("apiId1".equals(entity.getAttributeMap().get("API Id").getString())) {
Assertions.assertEquals("/login", entity.getAttributeMap().get("API Name").getString());
Assertions.assertEquals("GET", entity.getAttributeMap().get("API Http method").getString());
} else if ("apiId2".equals(entity.getAttributeMap().get("API Id").getString())) {
Assertions.assertEquals("/checkout", entity.getAttributeMap().get("API Name").getString());
Assertions.assertEquals("POST", entity.getAttributeMap().get("API Http method").getString());
}
}
}
use of com.github.dirtpowered.dirtmv.network.versions.Release39To29.entity.Entity in project evocation-sunburst-RPG by aviphyr.
the class MON_Void method checkDrop.
public void checkDrop() {
dropItem(new OBJ_Heart(gp));
dropItem(new OBJ_Heart(gp));
int hasIt = 0;
Entity test = new OBJ_EchoingVoidStaff(gp);
for (Entity x : gp.player.inventory) {
if (x.name.equals(test.name)) {
dropItem(new OBJ_Key(gp));
hasIt++;
}
}
if (hasIt == 0) {
dropItem(new OBJ_EchoingVoidStaff(gp));
}
}
Aggregations