From 30db273c743f85891db296e66694835a5d524b4d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 22 May 2017 09:26:51 +0200 Subject: [PATCH] SONAR-9245 facet new_lines in search_projects WS --- .../it/projectSearch/SearchProjectsTest.java | 8 +- .../measure/index/ProjectMeasuresIndex.java | 6 +- .../ws/SearchProjectsActionTest.java | 79 ++++++++++++++++++- .../index/ProjectMeasuresIndexTest.java | 36 +++++++++ 4 files changed, 126 insertions(+), 3 deletions(-) diff --git a/it/it-tests/src/test/java/it/projectSearch/SearchProjectsTest.java b/it/it-tests/src/test/java/it/projectSearch/SearchProjectsTest.java index a4743dedc7b..46d30a139b6 100644 --- a/it/it-tests/src/test/java/it/projectSearch/SearchProjectsTest.java +++ b/it/it-tests/src/test/java/it/projectSearch/SearchProjectsTest.java @@ -200,7 +200,7 @@ public class SearchProjectsTest { analyzeProject(projectKey, "shared/xoo-history-v2"); SearchProjectsWsResponse response = searchProjects(SearchProjectsRequest.builder().setOrganization(organizationKey).setFacets(asList( - "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density")).build()); + "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density", "new_lines")).build()); checkFacet(response, "new_reliability_rating", tuple("1", 1L), @@ -232,6 +232,12 @@ public class SearchProjectsTest { tuple("5.0-10.0", 0L), tuple("10.0-20.0", 0L), tuple("20.0-*", 0L)); + checkFacet(response, "new_lines", + tuple("*-1000.0", 0L), + tuple("1000.0-10000.0", 0L), + tuple("10000.0-100000.0", 0L), + tuple("100000.0-500000.0", 0L), + tuple("500000.0-*", 0L)); } private void checkFacet(SearchProjectsWsResponse response, String facetKey, Tuple... values) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java index a97448c0944..4fced457579 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/index/ProjectMeasuresIndex.java @@ -67,6 +67,7 @@ import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY; import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY; +import static org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY; @@ -91,6 +92,7 @@ public class ProjectMeasuresIndex { public static final List SUPPORTED_FACETS = ImmutableList.of( NCLOC_KEY, + NEW_LINES_KEY, DUPLICATED_LINES_DENSITY_KEY, NEW_DUPLICATED_LINES_DENSITY_KEY, COVERAGE_KEY, @@ -105,6 +107,7 @@ public class ProjectMeasuresIndex { FILTER_LANGUAGES, FILTER_TAGS); + private static final Double[] LINES_THRESHOLDS = new Double[]{1_000d, 10_000d, 100_000d, 500_000d}; private static final Double[] COVERAGE_THRESHOLDS = new Double[]{30d, 50d, 70d, 80d}; private static final Double[] DUPLICATIONS_THRESHOLDS = new Double[]{3d, 5d, 10d, 20d}; @@ -112,7 +115,8 @@ public class ProjectMeasuresIndex { private static final String FIELD_MEASURES_VALUE = FIELD_MEASURES + "." + ProjectMeasuresIndexDefinition.FIELD_MEASURES_VALUE; private static final Map FACET_FACTORIES = ImmutableMap.builder() - .put(NCLOC_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NCLOC_KEY, facetBuilder, 1_000d, 10_000d, 100_000d, 500_000d)) + .put(NCLOC_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NCLOC_KEY, facetBuilder, LINES_THRESHOLDS)) + .put(NEW_LINES_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NEW_LINES_KEY, facetBuilder, LINES_THRESHOLDS)) .put(DUPLICATED_LINES_DENSITY_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, DUPLICATED_LINES_DENSITY_KEY, facetBuilder, DUPLICATIONS_THRESHOLDS)) .put(NEW_DUPLICATED_LINES_DENSITY_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, NEW_DUPLICATED_LINES_DENSITY_KEY, facetBuilder, DUPLICATIONS_THRESHOLDS)) .put(COVERAGE_KEY, (esSearch, query, facetBuilder) -> addRangeFacet(esSearch, COVERAGE_KEY, facetBuilder, COVERAGE_THRESHOLDS)) diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java index 8b5424ba831..1f06d4d4dff 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchProjectsActionTest.java @@ -69,6 +69,7 @@ import static org.assertj.core.api.Assertions.tuple; import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES_DENSITY_KEY; import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY; +import static org.sonar.api.measures.CoreMetrics.NEW_LINES_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING_KEY; import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_RATING_KEY; @@ -171,7 +172,7 @@ public class SearchProjectsActionTest { Param facets = def.param("facets"); assertThat(facets.defaultValue()).isNull(); assertThat(facets.possibleValues()).containsOnly("ncloc", "duplicated_lines_density", "coverage", "sqale_rating", "reliability_rating", "security_rating", "alert_status", - "languages", "tags", "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density"); + "languages", "tags", "new_reliability_rating", "new_security_rating", "new_maintainability_rating", "new_coverage", "new_duplicated_lines_density", "new_lines"); } @Test @@ -414,6 +415,34 @@ public class SearchProjectsActionTest { assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project3.key()); } + @Test + public void filter_projects_by_ncloc() { + userSession.logIn(); + OrganizationDto organizationDto = db.organizations().insert(); + MetricDto ncloc = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name())); + ComponentDto project1 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(80d))); + ComponentDto project2 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(85d))); + ComponentDto project3 = insertProject(organizationDto, new Measure(ncloc, c -> c.setValue(10d))); + + SearchProjectsWsResponse result = call(request.setFilter("ncloc <= 80")); + + assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project3.key()); + } + + @Test + public void filter_projects_by_new_lines() { + userSession.logIn(); + OrganizationDto organizationDto = db.organizations().insert(); + MetricDto newLines = db.measureDbTester().insertMetric(c -> c.setKey(NEW_LINES_KEY).setValueType(INT.name())); + ComponentDto project1 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(80d))); + ComponentDto project2 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(85d))); + ComponentDto project3 = insertProject(organizationDto, new Measure(newLines, c -> c.setVariation(10d))); + + SearchProjectsWsResponse result = call(request.setFilter("new_lines <= 80")); + + assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(project1.getKey(), project3.key()); + } + @Test public void filter_projects_by_text_query() { userSession.logIn(); @@ -775,6 +804,54 @@ public class SearchProjectsActionTest { tuple("20.0-*", 0L)); } + @Test + public void return_ncloc_facet() { + userSession.logIn(); + OrganizationDto organizationDto = db.organizations().insert(); + MetricDto coverage = db.measureDbTester().insertMetric(c -> c.setKey(NCLOC).setValueType(INT.name())); + insertProject(organizationDto, new Measure(coverage, c -> c.setValue(100d))); + insertProject(organizationDto, new Measure(coverage, c -> c.setValue(15_000d))); + insertProject(organizationDto, new Measure(coverage, c -> c.setValue(50_000d))); + + SearchProjectsWsResponse result = call(request.setFacets(singletonList(NCLOC))); + + Common.Facet facet = result.getFacets().getFacetsList().stream() + .filter(oneFacet -> NCLOC.equals(oneFacet.getProperty())) + .findFirst().orElseThrow(IllegalStateException::new); + assertThat(facet.getValuesList()) + .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount) + .containsExactly( + tuple("*-1000.0", 1L), + tuple("1000.0-10000.0", 0L), + tuple("10000.0-100000.0", 2L), + tuple("100000.0-500000.0", 0L), + tuple("500000.0-*", 0L)); + } + + @Test + public void return_new_lines_facet() { + userSession.logIn(); + OrganizationDto organizationDto = db.organizations().insert(); + MetricDto coverage = db.measureDbTester().insertMetric(c -> c.setKey(NEW_LINES_KEY).setValueType(INT.name())); + insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(100d))); + insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(15_000d))); + insertProject(organizationDto, new Measure(coverage, c -> c.setVariation(50_000d))); + + SearchProjectsWsResponse result = call(request.setFacets(singletonList(NEW_LINES_KEY))); + + Common.Facet facet = result.getFacets().getFacetsList().stream() + .filter(oneFacet -> NEW_LINES_KEY.equals(oneFacet.getProperty())) + .findFirst().orElseThrow(IllegalStateException::new); + assertThat(facet.getValuesList()) + .extracting(Common.FacetValue::getVal, Common.FacetValue::getCount) + .containsExactly( + tuple("*-1000.0", 1L), + tuple("1000.0-10000.0", 0L), + tuple("10000.0-100000.0", 2L), + tuple("100000.0-500000.0", 0L), + tuple("500000.0-*", 0L)); + } + @Test public void default_sort_is_by_ascending_name() throws Exception { userSession.logIn(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java index cd670d5d709..0c8cc042d9d 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/index/ProjectMeasuresIndexTest.java @@ -83,6 +83,7 @@ public class ProjectMeasuresIndexTest { private static final String DUPLICATION = "duplicated_lines_density"; private static final String NEW_DUPLICATION = "new_duplicated_lines_density"; private static final String NCLOC = "ncloc"; + private static final String NEW_LINES = "new_lines"; private static final String LANGUAGES = "languages"; private static final OrganizationDto ORG = OrganizationTesting.newOrganizationDto(); @@ -530,6 +531,41 @@ public class ProjectMeasuresIndexTest { entry("500000.0-*", 0L)); } + @Test + public void facet_new_lines() { + index( + // 3 docs with ncloc<1K + newDoc(NEW_LINES, 0d), + newDoc(NEW_LINES, 0d), + newDoc(NEW_LINES, 999d), + // 2 docs with ncloc>=1K and ncloc<10K + newDoc(NEW_LINES, 1_000d), + newDoc(NEW_LINES, 9_999d), + // 4 docs with ncloc>=10K and ncloc<100K + newDoc(NEW_LINES, 10_000d), + newDoc(NEW_LINES, 10_000d), + newDoc(NEW_LINES, 11_000d), + newDoc(NEW_LINES, 99_000d), + // 2 docs with ncloc>=100K and ncloc<500K + newDoc(NEW_LINES, 100_000d), + newDoc(NEW_LINES, 499_000d), + // 5 docs with ncloc>= 500K + newDoc(NEW_LINES, 500_000d), + newDoc(NEW_LINES, 100_000_000d), + newDoc(NEW_LINES, 500_000d), + newDoc(NEW_LINES, 1_000_000d), + newDoc(NEW_LINES, 100_000_000_000d)); + + Facets facets = underTest.search(new ProjectMeasuresQuery(), new SearchOptions().addFacets(NEW_LINES)).getFacets(); + + assertThat(facets.get(NEW_LINES)).containsExactly( + entry("*-1000.0", 3L), + entry("1000.0-10000.0", 2L), + entry("10000.0-100000.0", 4L), + entry("100000.0-500000.0", 2L), + entry("500000.0-*", 5L)); + } + @Test public void facet_coverage() { index( -- 2.39.5