From: Eric Giffon Date: Thu, 16 May 2024 14:08:02 +0000 (+0200) Subject: SONAR-22246 Cleanup SQL query for the ncloc of biggest branch X-Git-Tag: 10.6.0.92116~104 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7f7f7897ac9376984014e31d113408603b2cb149;p=sonarqube.git SONAR-22246 Cleanup SQL query for the ncloc of biggest branch --- diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java index 92337e49b29..d309938cb68 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java @@ -38,7 +38,7 @@ public class ProjectNclocComputationStep implements ComputationStep { public void execute(Context context) { try (DbSession dbSession = dbClient.openSession(false)) { String projectUuid = analysisMetadataHolder.getProject().getUuid(); - long maxncloc = dbClient.liveMeasureDao().sumNclocOfBiggestBranchForProject(dbSession, projectUuid); + long maxncloc = dbClient.liveMeasureDao().findNclocOfBiggestBranchForProject(dbSession, projectUuid); dbClient.projectDao().updateNcloc(dbSession, projectUuid, maxncloc); dbSession.commit(); } diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/measure/LiveMeasureDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/measure/LiveMeasureDaoIT.java index 99b137f1093..ae7852e6607 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/measure/LiveMeasureDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/measure/LiveMeasureDaoIT.java @@ -410,9 +410,9 @@ class LiveMeasureDaoIT { db.measures().insertLiveMeasure(projectWithLinesButNoLoc, lines, m -> m.setValue(365d)); db.measures().insertLiveMeasure(projectWithLinesButNoLoc, ncloc, m -> m.setValue(0d)); - assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), simpleProject.projectUuid())).isEqualTo(10L); - assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), projectWithBiggerBranch.projectUuid())).isEqualTo(200L); - assertThat(underTest.sumNclocOfBiggestBranchForProject(db.getSession(), projectWithLinesButNoLoc.projectUuid())).isZero(); + assertThat(underTest.findNclocOfBiggestBranchForProject(db.getSession(), simpleProject.projectUuid())).isEqualTo(10L); + assertThat(underTest.findNclocOfBiggestBranchForProject(db.getSession(), projectWithBiggerBranch.projectUuid())).isEqualTo(200L); + assertThat(underTest.findNclocOfBiggestBranchForProject(db.getSession(), projectWithLinesButNoLoc.projectUuid())).isZero(); } @Test @@ -473,7 +473,7 @@ class LiveMeasureDaoIT { void countNcloc_empty() { db.measures().insertMetric(m -> m.setKey("ncloc").setValueType(INT.toString())); db.measures().insertMetric(m -> m.setKey("lines").setValueType(INT.toString())); - long result = underTest.sumNclocOfBiggestBranchForProject(db.getSession(), "non-existing-project-uuid"); + long result = underTest.findNclocOfBiggestBranchForProject(db.getSession(), "non-existing-project-uuid"); assertThat(result).isZero(); } 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 c8d1c706f91..4cc2a2f1efb 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,8 +94,8 @@ public class LiveMeasureDao implements Dao { mapper(dbSession).selectTreeByQuery(query, baseComponent.uuid(), query.getUuidPath(baseComponent), resultHandler); } - public long sumNclocOfBiggestBranchForProject(DbSession dbSession, String projectUuid){ - Long ncloc = mapper(dbSession).sumNclocOfBiggestBranchForProject(projectUuid, NCLOC_KEY); + public long findNclocOfBiggestBranchForProject(DbSession dbSession, String projectUuid){ + Long ncloc = mapper(dbSession).findNclocOfBiggestBranchForProject(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 482a3aa9d6f..5516a70e95f 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 @@ -58,7 +58,7 @@ public interface LiveMeasureMapper { ResultHandler resultHandler); @CheckForNull - Long sumNclocOfBiggestBranchForProject(@Param("projectUuid") String projectUuid, @Param("ncloc") String nclocKey); + Long findNclocOfBiggestBranchForProject(@Param("projectUuid") String projectUuid, @Param("ncloc") String nclocKey); List getLargestBranchNclocPerProject(@Param("nclocUuid") String nclocUuid); 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 d43d11c2db5..a921153abe6 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 @@ -62,19 +62,16 @@ and lm.component_uuid = #{componentUuid, jdbcType=VARCHAR} - + select max(lm.value) + from live_measures lm + inner join metrics m on m.uuid = lm.metric_uuid + inner join project_branches b on b.uuid = lm.component_uuid + inner join projects p on p.uuid = b.project_uuid and p.qualifier = 'TRK' + + m.name = #{ncloc, jdbcType=VARCHAR} + and b.project_uuid = #{projectUuid,jdbcType=VARCHAR} +