use of org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NCLOC_DISTRIBUTION_NCLOC in project sonarqube by SonarSource.
the class ProjectMeasuresIndex method searchTelemetryStatistics.
public ProjectMeasuresStatistics searchTelemetryStatistics() {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().fetchSource(false).size(0);
BoolQueryBuilder esFilter = boolQuery().filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())).filter(termQuery(FIELD_QUALIFIER, Qualifiers.PROJECT));
searchSourceBuilder.query(esFilter);
searchSourceBuilder.aggregation(AggregationBuilders.terms(FIELD_LANGUAGES).field(FIELD_LANGUAGES).size(MAX_PAGE_SIZE).minDocCount(1).order(BucketOrder.count(false)));
searchSourceBuilder.aggregation(AggregationBuilders.nested(FIELD_NCLOC_DISTRIBUTION, FIELD_NCLOC_DISTRIBUTION).subAggregation(AggregationBuilders.terms(FIELD_NCLOC_DISTRIBUTION + "_terms").field(FIELD_NCLOC_DISTRIBUTION_LANGUAGE).size(MAX_PAGE_SIZE).minDocCount(1).order(BucketOrder.count(false)).subAggregation(sum(FIELD_NCLOC_DISTRIBUTION_NCLOC).field(FIELD_NCLOC_DISTRIBUTION_NCLOC))));
searchSourceBuilder.aggregation(AggregationBuilders.nested(NCLOC_KEY, FIELD_MEASURES).subAggregation(AggregationBuilders.filter(NCLOC_KEY + "_filter", termQuery(FIELD_MEASURES_MEASURE_KEY, NCLOC_KEY)).subAggregation(sum(NCLOC_KEY + "_filter_sum").field(FIELD_MEASURES_MEASURE_VALUE))));
ProjectMeasuresStatistics.Builder statistics = ProjectMeasuresStatistics.builder();
SearchResponse response = client.search(EsClient.prepareSearch(TYPE_PROJECT_MEASURES.getMainType()).source(searchSourceBuilder));
statistics.setProjectCount(getTotalHits(response.getHits().getTotalHits()).value);
statistics.setProjectCountByLanguage(termsToMap(response.getAggregations().get(FIELD_LANGUAGES)));
Function<Terms.Bucket, Long> bucketToNcloc = bucket -> Math.round(((Sum) bucket.getAggregations().get(FIELD_NCLOC_DISTRIBUTION_NCLOC)).getValue());
Map<String, Long> nclocByLanguage = Stream.of((Nested) response.getAggregations().get(FIELD_NCLOC_DISTRIBUTION)).map(nested -> (Terms) nested.getAggregations().get(nested.getName() + "_terms")).flatMap(terms -> terms.getBuckets().stream()).collect(MoreCollectors.uniqueIndex(Bucket::getKeyAsString, bucketToNcloc));
statistics.setNclocByLanguage(nclocByLanguage);
return statistics.build();
}
Aggregations