From 712587e8883960d0a91e6b70460d15609d8e9327 Mon Sep 17 00:00:00 2001 From: Jacek Date: Tue, 7 Apr 2020 15:34:48 +0200 Subject: [PATCH] SONAR-13188 update telemetry project count query to ignore applications --- .../measure/index/ProjectMeasuresIndex.java | 4 +- .../index/ProjectMeasuresIndexTest.java | 47 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java b/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java index 029b9ffbca3..820b2c8033a 100644 --- a/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java +++ b/server/sonar-webserver-es/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java @@ -242,7 +242,9 @@ public class ProjectMeasuresIndex { .setFetchSource(false) .setSize(0); - BoolQueryBuilder esFilter = boolQuery(); + BoolQueryBuilder esFilter = boolQuery() + .filter(termQuery(FIELD_INDEX_TYPE, TYPE_PROJECT_MEASURES.getName())) + .filter(termQuery(FIELD_QUALIFIER, Qualifiers.PROJECT)); request.setQuery(esFilter); request.addAggregation(AggregationBuilders.terms(FIELD_LANGUAGES) .field(FIELD_LANGUAGES) diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java index f8ce765abf6..90887064a9d 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java @@ -497,7 +497,7 @@ public class ProjectMeasuresIndexTest { @Test public void filter_on_qualifier() { - index(newDoc(PROJECT1), newDoc(PROJECT2), newDoc(PROJECT3), + index(newDoc(PROJECT1), newDoc(PROJECT2), newDoc(PROJECT3), newDoc(APP1), newDoc(APP2), newDoc(APP3)); assertResults(new ProjectMeasuresQuery(), @@ -1612,6 +1612,51 @@ public class ProjectMeasuresIndexTest { entry("java", 500L), entry("cs", 250L), entry("js", 50L), entry("python", 100L), entry("kotlin", 404L)); } + @Test + public void search_statistics_should_ignore_applications() { + es.putDocuments(TYPE_PROJECT_MEASURES, + // insert projects + newDoc(ComponentTesting.newPrivateProjectDto(ORG), "lines", 10, "coverage", 80) + .setLanguages(Arrays.asList("java", "cs", "js")) + .setNclocLanguageDistributionFromMap(ImmutableMap.of("java", 200, "cs", 250, "js", 50)), + newDoc(ComponentTesting.newPrivateProjectDto(ORG), "lines", 20, "coverage", 80) + .setLanguages(Arrays.asList("java", "python", "kotlin")) + .setNclocLanguageDistributionFromMap(ImmutableMap.of("java", 300, "python", 100, "kotlin", 404)), + + // insert applications + newDoc(ComponentTesting.newApplication(ORG), "lines", 1000, "coverage", 70) + .setLanguages(Arrays.asList("java", "python", "kotlin")) + .setNclocLanguageDistributionFromMap(ImmutableMap.of("java", 300, "python", 100, "kotlin", 404)), + newDoc(ComponentTesting.newApplication(ORG), "lines", 20, "coverage", 80) + .setLanguages(Arrays.asList("java", "python", "kotlin")) + .setNclocLanguageDistributionFromMap(ImmutableMap.of("java", 300, "python", 100, "kotlin", 404))); + + ProjectMeasuresStatistics result = underTest.searchTelemetryStatistics(); + + assertThat(result.getProjectCount()).isEqualTo(2); + assertThat(result.getProjectCountByLanguage()).containsOnly( + entry("java", 2L), entry("cs", 1L), entry("js", 1L), entry("python", 1L), entry("kotlin", 1L)); + assertThat(result.getNclocByLanguage()).containsOnly( + entry("java", 500L), entry("cs", 250L), entry("js", 50L), entry("python", 100L), entry("kotlin", 404L)); + } + + @Test + public void search_statistics_should_count_0_if_no_projects() { + es.putDocuments(TYPE_PROJECT_MEASURES, + // insert applications + newDoc(ComponentTesting.newApplication(ORG), "lines", 1000, "coverage", 70) + .setLanguages(Arrays.asList("java", "python", "kotlin")) + .setNclocLanguageDistributionFromMap(ImmutableMap.of("java", 300, "python", 100, "kotlin", 404)), + newDoc(ComponentTesting.newApplication(ORG), "lines", 20, "coverage", 80) + .setLanguages(Arrays.asList("java", "python", "kotlin")) + .setNclocLanguageDistributionFromMap(ImmutableMap.of("java", 300, "python", 100, "kotlin", 404))); + + ProjectMeasuresStatistics result = underTest.searchTelemetryStatistics(); + + assertThat(result.getProjectCount()).isEqualTo(0); + assertThat(result.getProjectCountByLanguage()).isEmpty(); + } + @Test public void fail_if_page_size_greater_than_500() { expectedException.expect(IllegalArgumentException.class); -- 2.39.5