]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22246 Cleanup SQL query for the ncloc of biggest branch
authorEric Giffon <eric.giffon@sonarsource.com>
Thu, 16 May 2024 14:08:02 +0000 (16:08 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 16 May 2024 20:02:38 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/ProjectNclocComputationStep.java
server/sonar-db-dao/src/it/java/org/sonar/db/measure/LiveMeasureDaoIT.java
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/measure/LiveMeasureMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/measure/LiveMeasureMapper.xml
server/sonar-webserver-webapi/src/main/java/org/sonar/server/component/ComponentCleanerService.java

index 92337e49b29a26c3191cc77377c75f1b2b41fbe3..d309938cb680a6c93f258694ca17d86e3f349314 100644 (file)
@@ -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();
     }
index 99b137f10939f207aab3ad37b6b6deefe8757cb5..ae7852e6607e5092e68fd2ea70e8a7fbb1f0156e 100644 (file)
@@ -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();
   }
index c8d1c706f914044ffc0026acd33f04e39d3cc5bf..4cc2a2f1efb4de4babba008aba79530100b2a43d 100644 (file)
@@ -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;
   }
 
index 482a3aa9d6f42bac72329d6e433247437d50b333..5516a70e95f66c950565feadd916506963be3dc4 100644 (file)
@@ -58,7 +58,7 @@ public interface LiveMeasureMapper {
     ResultHandler<LiveMeasureDto> resultHandler);
 
   @CheckForNull
-  Long sumNclocOfBiggestBranchForProject(@Param("projectUuid") String projectUuid, @Param("ncloc") String nclocKey);
+  Long findNclocOfBiggestBranchForProject(@Param("projectUuid") String projectUuid, @Param("ncloc") String nclocKey);
 
   List<LargestBranchNclocDto> getLargestBranchNclocPerProject(@Param("nclocUuid") String nclocUuid);
 
index d43d11c2db5c82315bc537eb48288f7d9b6df9ec..a921153abe612eedc32090d821536d9670544705 100644 (file)
     and lm.component_uuid = #{componentUuid, jdbcType=VARCHAR}
   </select>
 
-  <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
-      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'
-      <where>
-        m.name = #{ncloc, jdbcType=VARCHAR}
-        and b.project_uuid = #{projectUuid,jdbcType=VARCHAR}
-      </where>
-      group by b.project_uuid
-    ) sumncloc
+  <select id="findNclocOfBiggestBranchForProject" parameterType="map" resultType="long">
+    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'
+    <where>
+      m.name = #{ncloc, jdbcType=VARCHAR}
+      and b.project_uuid = #{projectUuid,jdbcType=VARCHAR}
+    </where>
   </select>
 
   <select id="getLargestBranchNclocPerProject" parameterType="map" resultType="LargestBranchNclocDto">
index c97bd782d89649cfd921253bcb75756bf193a7f2..9cb298b88e47e331c963179e8915c1d1a74120e2 100644 (file)
@@ -61,7 +61,7 @@ public class ComponentCleanerService {
   }
 
   private void updateProjectNcloc(DbSession dbSession, String projectUuid) {
-    long maxncloc = dbClient.liveMeasureDao().sumNclocOfBiggestBranchForProject(dbSession, projectUuid);
+    long maxncloc = dbClient.liveMeasureDao().findNclocOfBiggestBranchForProject(dbSession, projectUuid);
     dbClient.projectDao().updateNcloc(dbSession, projectUuid, maxncloc);
   }