]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9482 Count projects associated to quality profiles
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 29 Jun 2017 09:22:29 +0000 (11:22 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 4 Jul 2017 14:29:36 +0000 (16:29 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java

index 494d623402eae132b6cf9d31b07214163be0b9b3..600be7ac2789e781960c25efb05c2e1fabb57a4b 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Map;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import org.sonar.api.utils.System2;
+import org.sonar.core.util.stream.MoreCollectors;
 import org.sonar.db.Dao;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
@@ -167,8 +168,9 @@ public class QualityProfileDao implements Dao {
     return mapper(dbSession).selectByNameAndLanguages(organization.getUuid(), name, languages);
   }
 
-  public Map<String, Long> countProjectsByProfileUuid(DbSession dbSession, OrganizationDto organization) {
-    return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileUuid(organization.getUuid()));
+  public Map<String, Long> countProjectsByOrganizationAndProfiles(DbSession dbSession, OrganizationDto organization, List<QProfileDto> profiles) {
+    List<String> profileUuids = profiles.stream().map(QProfileDto::getKee).collect(MoreCollectors.toList());
+    return KeyLongValue.toMap(executeLargeInputs(profileUuids, partition -> mapper(dbSession).countProjectsByOrganizationAndProfiles(organization.getUuid(), partition)));
   }
 
   public void insertProjectProfileAssociation(DbSession dbSession, ComponentDto project, QProfileDto profile) {
index 758786dc15ca9e0cac825c6c87fe2f68b71c24ea..04def0160fdbbefc6b4eb51b3bc0250bd31bf0ad 100644 (file)
@@ -77,7 +77,7 @@ public interface QualityProfileMapper {
 
   // PROJECTS
 
-  List<KeyLongValue> countProjectsByProfileUuid(@Param("organizationUuid") String organizationUuid);
+  List<KeyLongValue> countProjectsByOrganizationAndProfiles(@Param("organizationUuid") String organizationUuid, @Param("profileUuids") List<String> profiles);
 
   @CheckForNull
   QProfileDto selectAssociatedToProjectUuidAndLanguage(
index 1f8add3639b1fd8ce18e26c56642121f3d50134e..3f85132f1d465da380648becb59977d87677e822 100644 (file)
     ORDER BY rp.name
   </select>
 
-  <select id="countProjectsByProfileUuid" resultType="KeyLongValue" parameterType="map">
+  <select id="countProjectsByOrganizationAndProfiles" resultType="KeyLongValue" parameterType="map">
     select pqp.profile_key as "key", count(pj.uuid) as "value"
     from projects pj
     inner join project_qprofiles pqp on pqp.project_uuid = pj.uuid
       pj.enabled = ${_true}
       and pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
       and oqp.organization_uuid = pj.organization_uuid
+      and <foreach collection="profileUuids" item="profileUuid" open="(" separator=" or " close=")">
+        oqp.uuid = #{profileUuid, jdbcType=VARCHAR}
+      </foreach>
     group by pqp.profile_key
   </select>
 
index 48ae7bd63efe6484d8062329155006c9f0a392ff..d254c2b412cdea0c68a5bd3f9c5d26ccac178172 100644 (file)
@@ -488,8 +488,10 @@ public class QualityProfileDaoTest {
     ComponentDto projectInOtherOrg = db.components().insertPrivateProject(otherOrg);
     db.qualityProfiles().associateWithProject(projectInOtherOrg, profileInOtherOrg);
 
-    assertThat(underTest.countProjectsByProfileUuid(dbSession, organization)).containsOnly(
+    assertThat(underTest.countProjectsByOrganizationAndProfiles(dbSession, organization, asList(profileWithoutProjects, profileWithProjects, profileInOtherOrg))).containsOnly(
       MapEntry.entry(profileWithProjects.getKee(), 2L));
+    assertThat(underTest.countProjectsByOrganizationAndProfiles(dbSession, otherOrg, singletonList(profileWithoutProjects))).isEmpty();
+    assertThat(underTest.countProjectsByOrganizationAndProfiles(dbSession, organization, Collections.emptyList())).isEmpty();
   }
 
   @Test
index 0600bd45062fbce9a39fdc85e2ff3c357020023a..e8b28c8ccad9e7ead03742d5d59152667844cb89 100644 (file)
@@ -162,7 +162,7 @@ public class SearchAction implements QProfileWsAction {
           dbClient.activeRuleDao().countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).build()))
         .setActiveDeprecatedRuleCountByProfileKey(
           dbClient.activeRuleDao().countActiveRulesByQuery(dbSession, builder.setProfiles(profiles).setRuleStatus(DEPRECATED).build()))
-        .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileUuid(dbSession, organization))
+        .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByOrganizationAndProfiles(dbSession, organization, profiles))
         .setDefaultProfileKeys(defaultProfiles);
     }
   }
index c5764d1fb0c1c62eb097cc861ee9228670cdd78f..ab8c20e38e74857b3055bf3a2b024c920a8512a3 100644 (file)
@@ -356,6 +356,13 @@ public class SearchActionTest {
       .containsExactlyInAnyOrder(tuple(true, 2L), tuple(false, 0L));
   }
 
+  @Test
+  public void no_profile() {
+    SearchWsResponse result = call(ws.newRequest());
+
+    assertThat(result.getProfilesList()).isEmpty();
+  }
+
   @Test
   public void map_dates() {
     long time = DateUtils.parseDateTime("2016-12-22T19:10:03+0100").getTime();