]> source.dussan.org Git - sonarqube.git/commitdiff
SONARCLOUD-78 add metric project_loc_ranges
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 26 Jun 2018 15:51:12 +0000 (17:51 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 29 Jun 2018 07:10:18 +0000 (09:10 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentMapper.java
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 37009c74415580581e5ad69f22a27c3e8a8d0149..ef7891ea62f7682fe7ea14bf8ea2cd5e741b2027 100644 (file)
@@ -38,6 +38,7 @@ import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Scopes;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
+import org.sonar.db.KeyLongValue;
 import org.sonar.db.RowNotFoundException;
 
 import static com.google.common.base.Preconditions.checkArgument;
@@ -136,6 +137,10 @@ public class ComponentDao implements Dao {
     return countByQueryImpl(session, organizationUuid, query);
   }
 
+  public List<KeyLongValue> countByNclocRanges(DbSession dbSession) {
+    return mapper(dbSession).countByNclocRanges();
+  }
+
   public List<ComponentDto> selectSubProjectsByComponentUuids(DbSession session, Collection<String> uuids) {
     if (uuids.isEmpty()) {
       return emptyList();
index 10e3b48241c0319a5391391c4f1285743643687f..c8247cd6393b9c952c8101754155ec0986a60c7f 100644 (file)
@@ -27,6 +27,7 @@ import javax.annotation.Nullable;
 import org.apache.ibatis.annotations.Param;
 import org.apache.ibatis.session.ResultHandler;
 import org.apache.ibatis.session.RowBounds;
+import org.sonar.db.KeyLongValue;
 
 public interface ComponentMapper {
 
@@ -75,6 +76,8 @@ public interface ComponentMapper {
 
   int countByQuery(@Nullable @Param("organizationUuid") String organizationUuid, @Param("query") ComponentQuery query);
 
+  List<KeyLongValue> countByNclocRanges();
+
   List<ComponentDto> selectDescendants(@Param("query") ComponentTreeQuery query, @Param("baseUuid") String baseUuid, @Param("baseUuidPath") String baseUuidPath);
 
   /**
index d67984456c39c23832f6a491d2b0fe98ba7ad1da..641e2301f2a024777e18b3727db35fb1327f89d3 100644 (file)
       and p.id = #{componentId,jdbcType=BIGINT}
   </select>
 
+  <select id="countByNclocRanges" resultType="org.sonar.db.KeyLongValue">
+    select range as "key", count(1) 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
+      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>
+
   <select id="selectByQuery" resultType="Component">
     select
       <include refid="componentColumns"/>
index 56075591440276c26cc7f90e911a679bdf679dbd..6d5422cefa826f4c4418bd6e3f9365a22f74f57f 100644 (file)
@@ -43,12 +43,15 @@ import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
+import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.resources.Scopes;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.KeyLongValue;
 import org.sonar.db.RowNotFoundException;
+import org.sonar.db.metric.MetricDto;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.source.FileSourceDto;
 
@@ -1161,6 +1164,34 @@ public class ComponentDaoTest {
     underTest.countByQuery(dbSession, query.build());
   }
 
+  @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);
+    ComponentDto project1Branch = db.components().insertProjectBranch(project1);
+    db.measures().insertLiveMeasure(project1, ncloc, m -> m.setValue(100.0));
+    db.measures().insertLiveMeasure(project1Branch, ncloc, m -> m.setValue(90_000.0));
+
+    // project with only main branch
+    ComponentDto project2 = db.components().insertMainBranch(org);
+    db.measures().insertLiveMeasure(project2, ncloc, m -> m.setValue(50.0));
+
+    // project with highest ncloc in main branch
+    ComponentDto project3 = db.components().insertMainBranch(org);
+    ComponentDto project3Branch = db.components().insertProjectBranch(project1);
+    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));
+  }
+
   @Test
   public void select_ghost_projects() {
     OrganizationDto organization = db.organizations().insert();