aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src
diff options
context:
space:
mode:
authorPierre <pierre.guillot@sonarsource.com>2023-03-28 10:16:41 +0200
committersonartech <sonartech@sonarsource.com>2023-04-27 20:03:01 +0000
commit4114d4103e050e6213c2b514773e22d4e4a00bb2 (patch)
tree5fd582004ad0394e86a1032608a4a276144b911f /server/sonar-db-dao/src
parent3758946e528a5234c5ab9aede8a9206fa161da0a (diff)
downloadsonarqube-4114d4103e050e6213c2b514773e22d4e4a00bb2.tar.gz
sonarqube-4114d4103e050e6213c2b514773e22d4e4a00bb2.zip
SONAR-18872 improve total ncloc computation9.9.1.69595
Diffstat (limited to 'server/sonar-db-dao/src')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java10
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java8
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java13
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java7
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml11
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml15
-rw-r--r--server/sonar-db-dao/src/schema/schema-sq.ddl3
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java40
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java26
9 files changed, 75 insertions, 58 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java
index 17441f11236..41c7df092e1 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java
@@ -94,14 +94,8 @@ public class LiveMeasureDao implements Dao {
mapper(dbSession).selectTreeByQuery(query, baseComponent.uuid(), query.getUuidPath(baseComponent), resultHandler);
}
- /**
- * Example:
- * If Main Branch = 0 LOCs (provisioned but never analyzed) and the "largest branch" is 120 LOCs, I'm expecting to consider the value 120.
- * If Main Branch = 100 LOCs and the "largest branch" is 120 LOCs, I'm expecting to consider the value 120.
- * If Main Branch = 100 LOCs and the "largest branch" is 80 LOCs, I'm expecting to consider the value 100.
- */
- public long sumNclocOfBiggestBranch(DbSession dbSession, SumNclocDbQuery dbQuery) {
- Long ncloc = mapper(dbSession).sumNclocOfBiggestBranch(NCLOC_KEY, dbQuery.getOnlyPrivateProjects(), dbQuery.getProjectUuidToExclude());
+ public long sumNclocOfBiggestBranchForProject(DbSession dbSession, String projectUuid){
+ Long ncloc = mapper(dbSession).sumNclocOfBiggestBranchForProject(projectUuid, NCLOC_KEY);
return ncloc == null ? 0L : ncloc;
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java
index 5891b136fd7..50671d04aba 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java
@@ -21,7 +21,7 @@ package org.sonar.db.measure;
import java.util.Collection;
import java.util.List;
-import javax.annotation.Nullable;
+import javax.annotation.CheckForNull;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.ResultHandler;
@@ -57,10 +57,8 @@ public interface LiveMeasureMapper {
@Param("baseUuidPath") String baseUuidPath,
ResultHandler<LiveMeasureDto> resultHandler);
- Long sumNclocOfBiggestBranch(
- @Param("ncloc") String nclocKey,
- @Param("private") Boolean privateProject,
- @Nullable @Param("projectUuidToExclude") String projectUuidToExclude);
+ @CheckForNull
+ Long sumNclocOfBiggestBranchForProject(@Param("projectUuid") String projectUuid, @Param("ncloc") String nclocKey);
List<LargestBranchNclocDto> getLargestBranchNclocPerProject();
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java
index 30f3796acb8..11f5c17c37f 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java
@@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
+import javax.annotation.Nullable;
import org.sonar.api.utils.System2;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
@@ -126,4 +127,16 @@ public class ProjectDao implements Dao {
Set<String> languageFilters = Set.of(language + "=%", "%;" + language + "=%");
return mapper(session).selectProjectUuidsAssociatedToDefaultQualityProfileByLanguage(languageFilters);
}
+
+ public void updateNcloc(DbSession dbSession, String projectUuid, long ncloc) {
+ mapper(dbSession).updateNcloc(projectUuid, ncloc);
+ }
+
+ public long getNclocSum(DbSession dbSession) {
+ return getNclocSum(dbSession, null);
+ }
+
+ public long getNclocSum(DbSession dbSession, @Nullable String projectUuidToExclude) {
+ return Optional.ofNullable(mapper(dbSession).getNclocSum(projectUuidToExclude)).orElse(0L);
+ }
}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java
index 58e635cd016..3253c1578b3 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java
@@ -23,6 +23,7 @@ import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.apache.ibatis.annotations.Param;
public interface ProjectMapper {
@@ -62,4 +63,10 @@ public interface ProjectMapper {
List<String> selectAllProjectUuids();
Set<String> selectProjectUuidsAssociatedToDefaultQualityProfileByLanguage(@Param("languageFilters") Set<String> languageFilters);
+
+ void updateNcloc(@Param("projectUuid") String projectUuid, @Param("ncloc") long ncloc);
+
+ @CheckForNull
+ Long getNclocSum(@Nullable @Param("projectUuidToExclude") String projectUuidToExclude);
+
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml
index 00819f44716..98182aad5e2 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml
@@ -61,8 +61,8 @@
and lm.component_uuid = #{componentUuid, jdbcType=VARCHAR}
</select>
- <select id="sumNclocOfBiggestBranch" parameterType="map" resultType="long">
- select sum(sumncloc.maxncloc) from (
+ <select id="sumNclocOfBiggestBranchForProject" parameterType="map" resultType="long">
+ select sumncloc.maxncloc from (
select b.project_uuid as projectUuid, max(lm.value) as maxncloc
from live_measures lm
inner join metrics m on m.uuid = lm.metric_uuid
@@ -70,12 +70,7 @@
inner join projects p on p.uuid = b.project_uuid and p.qualifier = 'TRK'
<where>
m.name = #{ncloc, jdbcType=VARCHAR}
- <if test="private">
- and p.private=${_true}
- </if>
- <if test="projectUuidToExclude != null">
- and b.project_uuid &lt;&gt; #{projectUuidToExclude,jdbcType=VARCHAR}
- </if>
+ and b.project_uuid = #{projectUuid,jdbcType=VARCHAR}
</where>
group by b.project_uuid
) sumncloc
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml
index 6e295c489cd..4bb106150bd 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml
@@ -179,4 +179,19 @@
uuid = #{uuid,jdbcType=VARCHAR}
</update>
+
+ <update id="updateNcloc">
+ update projects set
+ ncloc = #{ncloc,jdbcType=BIGINT}
+ where
+ uuid = #{projectUuid,jdbcType=VARCHAR}
+ </update>
+
+ <select id="getNclocSum" parameterType="string" resultType="long">
+ select sum(ncloc) from projects where qualifier = 'TRK'
+ <if test="projectUuidToExclude != null">
+ and uuid &lt;&gt; #{projectUuidToExclude,jdbcType=VARCHAR}
+ </if>
+ </select>
+
</mapper>
diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl
index 4a387471810..e0aa9358c7f 100644
--- a/server/sonar-db-dao/src/schema/schema-sq.ddl
+++ b/server/sonar-db-dao/src/schema/schema-sq.ddl
@@ -723,7 +723,8 @@ CREATE TABLE "PROJECTS"(
"PRIVATE" BOOLEAN NOT NULL,
"TAGS" CHARACTER VARYING(500),
"CREATED_AT" BIGINT,
- "UPDATED_AT" BIGINT NOT NULL
+ "UPDATED_AT" BIGINT NOT NULL,
+ "NCLOC" BIGINT
);
ALTER TABLE "PROJECTS" ADD CONSTRAINT "PK_NEW_PROJECTS" PRIMARY KEY("UUID");
CREATE UNIQUE INDEX "UNIQ_PROJECTS_KEE" ON "PROJECTS"("KEE" NULLS FIRST);
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java
index 13cf0c87aff..727addd55bd 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/measure/LiveMeasureDaoTest.java
@@ -365,12 +365,9 @@ public class LiveMeasureDaoTest {
db.measures().insertLiveMeasure(projectWithLinesButNoLoc, lines, m -> m.setValue(365d));
db.measures().insertLiveMeasure(projectWithLinesButNoLoc, ncloc, m -> m.setValue(0d));
- SumNclocDbQuery query = SumNclocDbQuery.builder()
- .setOnlyPrivateProjects(false)
- .build();
- long result = underTest.sumNclocOfBiggestBranch(db.getSession(), query);
-
- assertThat(result).isEqualTo(10L + 200L);
+ assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), simpleProject.uuid())).isEqualTo(10L);
+ assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), projectWithBiggerBranch.uuid())).isEqualTo(200L);
+ assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), projectWithLinesButNoLoc.uuid())).isZero();
}
@Test
@@ -410,41 +407,12 @@ public class LiveMeasureDaoTest {
public void countNcloc_empty() {
db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString()));
db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString()));
- SumNclocDbQuery query = SumNclocDbQuery.builder()
- .setOnlyPrivateProjects(false)
- .build();
- long result = underTest.sumNclocOfBiggestBranch(db.getSession(), query);
+ long result = underTest.sumNclocOfBiggestBranchForProject(db.getSession(), "non-existing-project-uuid");
assertThat(result).isZero();
}
@Test
- public void countNcloc_and_exclude_project() {
- MetricDto ncloc = db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString()));
-
- ComponentDto simpleProject = db.components().insertPublicProject();
- db.measures().insertLiveMeasure(simpleProject, ncloc, m -> m.setValue(10d));
-
- ComponentDto projectWithBiggerBranch = db.components().insertPublicProject();
- ComponentDto bigBranch = db.components().insertProjectBranch(projectWithBiggerBranch, b -> b.setBranchType(BranchType.BRANCH));
- db.measures().insertLiveMeasure(projectWithBiggerBranch, ncloc, m -> m.setValue(100d));
- db.measures().insertLiveMeasure(bigBranch, ncloc, m -> m.setValue(200d));
-
- ComponentDto projectToExclude = db.components().insertPublicProject();
- ComponentDto projectToExcludeBranch = db.components().insertProjectBranch(projectToExclude, b -> b.setBranchType(BranchType.BRANCH));
- db.measures().insertLiveMeasure(projectToExclude, ncloc, m -> m.setValue(300d));
- db.measures().insertLiveMeasure(projectToExcludeBranch, ncloc, m -> m.setValue(400d));
-
- SumNclocDbQuery query = SumNclocDbQuery.builder()
- .setProjectUuidToExclude(projectToExclude.uuid())
- .setOnlyPrivateProjects(false)
- .build();
- long result = underTest.sumNclocOfBiggestBranch(db.getSession(), query);
-
- assertThat(result).isEqualTo(10L + 200L);
- }
-
- @Test
public void insert_data() {
byte[] data = "text_value".getBytes(StandardCharsets.UTF_8);
MetricDto metric = db.measures().insertMetric();
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java
index 51476f1449f..ec4defa6af7 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/project/ProjectDaoTest.java
@@ -31,6 +31,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
+import org.assertj.core.api.Assertions;
import org.assertj.core.groups.Tuple;
import org.junit.Rule;
import org.junit.Test;
@@ -280,6 +281,31 @@ public class ProjectDaoTest {
}
@Test
+ public void update_ncloc_should_update_project() {
+ ComponentDto project = db.components().insertPublicProject();
+
+ projectDao.updateNcloc(db.getSession(), project.uuid(), 10L);
+
+ Assertions.assertThat(projectDao.getNclocSum(db.getSession())).isEqualTo(10L);
+ }
+
+ @Test
+ public void getNcloc_sum_compute_correctly_sum_of_projects() {
+ projectDao.updateNcloc(db.getSession(), db.components().insertPublicProject().uuid(), 1L);
+ projectDao.updateNcloc(db.getSession(), db.components().insertPublicProject().uuid(), 20L);
+ projectDao.updateNcloc(db.getSession(), db.components().insertPublicProject().uuid(), 100L);
+ Assertions.assertThat(projectDao.getNclocSum(db.getSession())).isEqualTo(121L);
+ }
+
+ @Test
+ public void getNcloc_sum_compute_correctly_sum_of_projects_while_excluding_project() {
+ projectDao.updateNcloc(db.getSession(), db.components().insertPublicProject().uuid(), 1L);
+ projectDao.updateNcloc(db.getSession(), db.components().insertPublicProject().uuid(), 20L);
+ ComponentDto project3 = db.components().insertPublicProject();
+ projectDao.updateNcloc(db.getSession(), project3.uuid(), 100L);
+ Assertions.assertThat(projectDao.getNclocSum(db.getSession(), project3.uuid())).isEqualTo(21L);
+ }
+ @Test
public void selectAllProjectUuids_shouldOnlyReturnProjectWithTRKQualifier() {
ComponentDto application = db.components().insertPrivateApplication();
ComponentDto project = db.components().insertPrivateProject();