use of org.elasticsearch.search.aggregations.metrics.ParsedMax in project sonarqube by SonarSource.
the class IssueIndex method searchProjectStatistics.
public List<ProjectStatistics> searchProjectStatistics(List<String> projectUuids, List<Long> froms, @Nullable String assigneeUuid) {
checkState(projectUuids.size() == froms.size(), "Expected same size for projectUuids (had size %s) and froms (had size %s)", projectUuids.size(), froms.size());
if (projectUuids.isEmpty()) {
return Collections.emptyList();
}
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(boolQuery().mustNot(existsQuery(FIELD_ISSUE_RESOLUTION)).filter(termQuery(FIELD_ISSUE_ASSIGNEE_UUID, assigneeUuid)).mustNot(termQuery(FIELD_ISSUE_TYPE, SECURITY_HOTSPOT.name()))).size(0);
IntStream.range(0, projectUuids.size()).forEach(i -> {
String projectUuid = projectUuids.get(i);
long from = froms.get(i);
sourceBuilder.aggregation(AggregationBuilders.filter(projectUuid, boolQuery().filter(termQuery(FIELD_ISSUE_PROJECT_UUID, projectUuid)).filter(rangeQuery(FIELD_ISSUE_FUNC_CREATED_AT).gte(epochMillisToEpochSeconds(from)))).subAggregation(AggregationBuilders.terms("branchUuid").field(FIELD_ISSUE_BRANCH_UUID).subAggregation(AggregationBuilders.count(AGG_COUNT).field(FIELD_ISSUE_KEY)).subAggregation(AggregationBuilders.max("maxFuncCreatedAt").field(FIELD_ISSUE_FUNC_CREATED_AT))));
});
SearchRequest requestBuilder = EsClient.prepareSearch(TYPE_ISSUE.getMainType());
requestBuilder.source(sourceBuilder);
SearchResponse response = client.search(requestBuilder);
return response.getAggregations().asList().stream().map(x -> (ParsedFilter) x).flatMap(projectBucket -> ((ParsedStringTerms) projectBucket.getAggregations().get("branchUuid")).getBuckets().stream().flatMap(branchBucket -> {
long count = ((ParsedValueCount) branchBucket.getAggregations().get(AGG_COUNT)).getValue();
if (count < 1L) {
return Stream.empty();
}
long lastIssueDate = (long) ((ParsedMax) branchBucket.getAggregations().get("maxFuncCreatedAt")).getValue();
return Stream.of(new ProjectStatistics(branchBucket.getKeyAsString(), count, lastIssueDate));
})).collect(MoreCollectors.toList(projectUuids.size()));
}
Aggregations