Search in sources :

Example 1 with FIELD_NCLOC_DISTRIBUTION_NCLOC

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();
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) Arrays(java.util.Arrays) SECURITY_HOTSPOTS_REVIEWED_KEY(org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_KEY) RangeAggregationBuilder(org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder) EsClient(org.sonar.server.es.EsClient) DESC(org.elasticsearch.search.sort.SortOrder.DESC) SimpleFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.SimpleFieldFilterScope) SimpleFieldTopAggregationDefinition(org.sonar.server.es.searchrequest.SimpleFieldTopAggregationDefinition) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) Map(java.util.Map) BucketOrder(org.elasticsearch.search.aggregations.BucketOrder) FIELD_LANGUAGES(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_LANGUAGES) FILTER_LANGUAGES(org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_LANGUAGES) RELIABILITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY) NEW_COVERAGE_KEY(org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY) FIELD_QUALITY_GATE_STATUS(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_QUALITY_GATE_STATUS) SUB_FIELD_MEASURES_KEY(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.SUB_FIELD_MEASURES_KEY) FIELD_MEASURES_MEASURE_VALUE(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES_MEASURE_VALUE) QueryBuilders.boolQuery(org.elasticsearch.index.query.QueryBuilders.boolQuery) FIELD_NCLOC_DISTRIBUTION_LANGUAGE(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NCLOC_DISTRIBUTION_LANGUAGE) SORT_BY_NAME(org.sonar.server.measure.index.ProjectMeasuresQuery.SORT_BY_NAME) Set(java.util.Set) NEW_DUPLICATED_LINES_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY) FIELD_MEASURES_MEASURE_KEY(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES_MEASURE_KEY) FieldSortBuilder(org.elasticsearch.search.sort.FieldSortBuilder) ScoreMode(org.apache.lucene.search.join.ScoreMode) Sum(org.elasticsearch.search.aggregations.metrics.Sum) MAX_PAGE_SIZE(org.sonarqube.ws.client.project.ProjectsWsParameters.MAX_PAGE_SIZE) NEW_RELIABILITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING_KEY) ALERT_STATUS_KEY(org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY) Stream(java.util.stream.Stream) NestedFieldTopAggregationDefinition(org.sonar.server.es.searchrequest.NestedFieldTopAggregationDefinition) QueryBuilders.rangeQuery(org.elasticsearch.index.query.QueryBuilders.rangeQuery) EsUtils.termsToMap(org.sonar.server.es.EsUtils.termsToMap) AggregationBuilders.sum(org.elasticsearch.search.aggregations.AggregationBuilders.sum) AggregationBuilders.filters(org.elasticsearch.search.aggregations.AggregationBuilders.filters) ALERT_STATUS(org.sonar.server.measure.index.ProjectMeasuresIndex.Facet.ALERT_STATUS) WebAuthorizationTypeSupport(org.sonar.server.permission.index.WebAuthorizationTypeSupport) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) STICKY(org.sonar.server.es.searchrequest.TopAggregationDefinition.STICKY) FIELD_INDEX_TYPE(org.sonar.server.es.IndexType.FIELD_INDEX_TYPE) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) Nullable(javax.annotation.Nullable) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SECURITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY) NEW_LINES_KEY(org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY) FIELD_KEY(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_KEY) RequestFiltersComputer(org.sonar.server.es.searchrequest.RequestFiltersComputer) FIELD_NAME(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NAME) MetricCriterion(org.sonar.server.measure.index.ProjectMeasuresQuery.MetricCriterion) QUALITY_GATE_STATUS(org.sonar.server.measure.index.ProjectMeasuresDoc.QUALITY_GATE_STATUS) TotalHits(org.apache.lucene.search.TotalHits) QueryBuilders.termQuery(org.elasticsearch.index.query.QueryBuilders.termQuery) COVERAGE_KEY(org.sonar.api.measures.CoreMetrics.COVERAGE_KEY) Qualifiers(org.sonar.api.resources.Qualifiers) NCLOC_KEY(org.sonar.api.measures.CoreMetrics.NCLOC_KEY) FILTER_TAGS(org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_TAGS) KeyedFilter(org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) NestedFieldFilterScope(org.sonar.server.es.searchrequest.TopAggregationDefinition.NestedFieldFilterScope) QueryBuilders.nestedQuery(org.elasticsearch.index.query.QueryBuilders.nestedQuery) NEW_SECURITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SORT_BY_LAST_ANALYSIS_DATE(org.sonar.server.measure.index.ProjectMeasuresQuery.SORT_BY_LAST_ANALYSIS_DATE) SearchResponse(org.elasticsearch.action.search.SearchResponse) NEW_MAINTAINABILITY_RATING_KEY(org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY) TAGS(org.sonar.server.measure.index.ProjectMeasuresIndex.Facet.TAGS) FIELD_ANALYSED_AT(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_ANALYSED_AT) DefaultIndexSettingsElement(org.sonar.server.es.newindex.DefaultIndexSettingsElement) SubAggregationHelper(org.sonar.server.es.searchrequest.SubAggregationHelper) TYPE_PROJECT_MEASURES(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.TYPE_PROJECT_MEASURES) System2(org.sonar.api.utils.System2) SearchIdResult(org.sonar.server.es.SearchIdResult) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) NestedSortBuilder(org.elasticsearch.search.sort.NestedSortBuilder) TopAggregationDefinition(org.sonar.server.es.searchrequest.TopAggregationDefinition) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) FILTER_QUALIFIER(org.sonarqube.ws.client.project.ProjectsWsParameters.FILTER_QUALIFIER) Objects(java.util.Objects) TopAggregationHelper(org.sonar.server.es.searchrequest.TopAggregationHelper) List(java.util.List) SearchOptions(org.sonar.server.es.SearchOptions) SQALE_RATING_KEY(org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY) ServerSide(org.sonar.api.server.ServerSide) AllFilters(org.sonar.server.es.searchrequest.RequestFiltersComputer.AllFilters) MoreCollectors.uniqueIndex(org.sonar.core.util.stream.MoreCollectors.uniqueIndex) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) IntStream(java.util.stream.IntStream) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude) FIELD_NCLOC_DISTRIBUTION(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NCLOC_DISTRIBUTION) AbstractAggregationBuilder(org.elasticsearch.search.aggregations.AbstractAggregationBuilder) EsUtils.escapeSpecialRegexChars(org.sonar.server.es.EsUtils.escapeSpecialRegexChars) Multimap(com.google.common.collect.Multimap) FIELD_NCLOC_DISTRIBUTION_NCLOC(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_NCLOC_DISTRIBUTION_NCLOC) Function(java.util.function.Function) NEW_SECURITY_REVIEW_RATING_KEY(org.sonar.api.measures.CoreMetrics.NEW_SECURITY_REVIEW_RATING_KEY) MoreCollectors.toSet(org.sonar.core.util.stream.MoreCollectors.toSet) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SECURITY_REVIEW_RATING_KEY(org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) QueryBuilders.termsQuery(org.elasticsearch.index.query.QueryBuilders.termsQuery) Optional.ofNullable(java.util.Optional.ofNullable) DUPLICATED_LINES_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY) FIELD_QUALIFIER(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_QUALIFIER) AggregationBuilders(org.elasticsearch.search.aggregations.AggregationBuilders) LANGUAGES(org.sonar.server.measure.index.ProjectMeasuresIndex.Facet.LANGUAGES) Consumer(java.util.function.Consumer) FIELD_MEASURES(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_MEASURES) ASC(org.elasticsearch.search.sort.SortOrder.ASC) NO_EXTRA_FILTER(org.sonar.server.es.searchrequest.TopAggregationHelper.NO_EXTRA_FILTER) FIELD_TAGS(org.sonar.server.measure.index.ProjectMeasuresIndexDefinition.FIELD_TAGS) NEW_SECURITY_HOTSPOTS_REVIEWED_KEY(org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED_KEY) Metric(org.sonar.api.measures.Metric) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Bucket(org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Multimap (com.google.common.collect.Multimap)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1 Function (java.util.function.Function)1 IntStream (java.util.stream.IntStream)1 Stream (java.util.stream.Stream)1 Nullable (javax.annotation.Nullable)1 TotalHits (org.apache.lucene.search.TotalHits)1 ScoreMode (org.apache.lucene.search.join.ScoreMode)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1