</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 <= 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
+ 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 <= 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 '+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">
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);
// 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