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;
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();
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 {
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);
/**
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 <= 1000 then '1K'
+ when locs > 1000 and locs <= 5000 then '5K'
+ when locs > 5000 and locs <= 10000 then '10K'
+ when locs > 10000 and locs <= 20000 then '20K'
+ when locs > 20000 and locs <= 50000 then '50K'
+ when locs > 50000 and locs <= 100000 then '100K'
+ when locs > 100000 and locs <= 250000 then '250K'
+ when locs > 250000 and locs <= 500000 then '500K'
+ when locs > 500000 and locs <= 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"/>
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;
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();