]> source.dussan.org Git - sonarqube.git/commitdiff
SONARCLOUD-78 complete buckets of project histograms
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 26 Jun 2018 22:10:55 +0000 (00:10 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 29 Jun 2018 07:10:18 +0000 (09:10 +0200)
server/sonar-db-dao/src/main/resources/org/sonar/db/component/ComponentMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java

index 641e2301f2a024777e18b3727db35fb1327f89d3..74689541611c7c2c5a186ac9787c86f1c7a7c025 100644 (file)
   </select>
 
   <select id="countByNclocRanges" resultType="org.sonar.db.KeyLongValue">
-    select range as "key", count(1) as "value"
+    select kee as "key", sum(val) as "value"
     from (
-      select case
-        when locs &lt;= 1000 then '1K'
-        when locs &gt; 1000 and locs &lt;= 5000 then '5K'
-        when locs &gt; 5000 and locs &lt;= 10000 then '10K'
-        when locs &gt; 10000 and locs &lt;= 20000 then '20K'
-        when locs &gt; 20000 and locs &lt;= 50000 then '50K'
-        when locs &gt; 50000 and locs &lt;= 100000 then '100K'
-        when locs &gt; 100000 and locs &lt;= 250000 then '250K'
-        when locs &gt; 250000 and locs &lt;= 500000 then '500K'
-        when locs &gt; 500000 and locs &lt;= 1000000 then '1M'
-        else 'More'
-        end as range
+      select '1K' as kee, 0 as val ${_from_dual}
+      union
+      select '5K' as kee, 0 as val ${_from_dual}
+      union
+      select '10K' as kee, 0 as val ${_from_dual}
+      union
+      select '20K' as kee, 0 as val ${_from_dual}
+      union
+      select '50K' as kee, 0 as val ${_from_dual}
+      union
+      select '100K' as kee, 0 as val ${_from_dual}
+      union
+      select '250K' as kee, 0 as val ${_from_dual}
+      union
+      select '500K' as kee, 0 as val ${_from_dual}
+      union
+      select '1M' as kee, 0 as val ${_from_dual}
+      union
+      select '+1M' as kee, 0 as val ${_from_dual}
+      union
+      select kee, count(1) as val
       from (
-        select b.project_uuid as projectUuid, max(lm.value) as locs
-        from live_measures lm
-        inner join metrics m on m.id = lm.metric_id
-        inner join projects p on p.uuid = lm.component_uuid
-        inner join project_branches b on b.uuid = p.uuid
-        where m.name = 'ncloc'
-          and p.enabled = ${_true}
-          and p.scope = 'PRJ'
-          and p.qualifier = 'TRK'
-          and p.copy_component_uuid is null
-          and b.branch_type = 'LONG'
-          and b.key_type = 'BRANCH'
-          group by b.project_uuid
-      ) alias1
-    ) alias2
-    group by range
+        select case
+          when locs &lt;= 1000 then '1K'
+          when locs &gt; 1000 and locs &lt;= 5000 then '5K'
+          when locs &gt; 5000 and locs &lt;= 10000 then '10K'
+          when locs &gt; 10000 and locs &lt;= 20000 then '20K'
+          when locs &gt; 20000 and locs &lt;= 50000 then '50K'
+          when locs &gt; 50000 and locs &lt;= 100000 then '100K'
+          when locs &gt; 100000 and locs &lt;= 250000 then '250K'
+          when locs &gt; 250000 and locs &lt;= 500000 then '500K'
+          when locs &gt; 500000 and locs &lt;= 1000000 then '1M'
+          else '+1M'
+          end as kee
+        from (
+          select b.project_uuid as projectUuid, max(lm.value) as locs
+          from live_measures lm
+          inner join metrics m on m.id = lm.metric_id
+          inner join projects p on p.uuid = lm.component_uuid
+          inner join project_branches b on b.uuid = p.uuid
+          where m.name = 'ncloc'
+            and p.enabled = ${_true}
+            and p.scope = 'PRJ'
+            and p.qualifier = 'TRK'
+            and p.copy_component_uuid is null
+            and b.branch_type = 'LONG'
+            and b.key_type = 'BRANCH'
+            group by b.project_uuid
+        ) alias1
+      ) alias2
+      group by kee
+    ) alias3
+    group by kee
   </select>
 
   <select id="selectByQuery" resultType="Component">
index 6d5422cefa826f4c4418bd6e3f9365a22f74f57f..859f280c75aabc933ad5ff6325c98a1380026aad 100644 (file)
@@ -1164,12 +1164,29 @@ public class ComponentDaoTest {
     underTest.countByQuery(dbSession, query.build());
   }
 
+
   @Test
+  public void countByNclocRanges_on_zero_projects() {
+    db.measures().insertMetric(m -> m.setKey(CoreMetrics.NCLOC_KEY));
+
+    assertThat(underTest.countByNclocRanges(dbSession))
+      .extracting(KeyLongValue::getKey, KeyLongValue::getValue)
+      .containsExactlyInAnyOrder(
+        tuple("1K", 0L),
+        tuple("5K", 0L),
+        tuple("10K", 0L),
+        tuple("20K", 0L),
+        tuple("50K", 0L),
+        tuple("100K", 0L),
+        tuple("250K", 0L),
+        tuple("500K", 0L),
+        tuple("1M", 0L),
+        tuple("+1M", 0L));
+  }
+    @Test
   public void countByNclocRanges() {
     MetricDto ncloc = db.measures().insertMetric(m -> m.setKey(CoreMetrics.NCLOC_KEY));
 
-    assertThat(underTest.countByNclocRanges(dbSession)).isEmpty();
-
     // project with highest ncloc in non-main branch
     OrganizationDto org = db.organizations().insert();
     ComponentDto project1 = db.components().insertMainBranch(org);
@@ -1183,13 +1200,23 @@ public class ComponentDaoTest {
 
     // project with highest ncloc in main branch
     ComponentDto project3 = db.components().insertMainBranch(org);
-    ComponentDto project3Branch = db.components().insertProjectBranch(project1);
+    ComponentDto project3Branch = db.components().insertProjectBranch(project3);
     db.measures().insertLiveMeasure(project3, ncloc, m -> m.setValue(80_000.0));
     db.measures().insertLiveMeasure(project3Branch, ncloc, m -> m.setValue(25_000.0));
 
     assertThat(underTest.countByNclocRanges(dbSession))
       .extracting(KeyLongValue::getKey, KeyLongValue::getValue)
-      .containsExactlyInAnyOrder(tuple("100K", 2L), tuple("1K", 1L));
+      .containsExactlyInAnyOrder(
+        tuple("1K", 1L),
+        tuple("5K", 0L),
+        tuple("10K", 0L),
+        tuple("20K", 0L),
+        tuple("50K", 0L),
+        tuple("100K", 2L),
+        tuple("250K", 0L),
+        tuple("500K", 0L),
+        tuple("1M", 0L),
+        tuple("+1M", 0L));
   }
 
   @Test