From 597f139942a1f27d5e6f519388851a8fe37b0881 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 29 Jun 2017 11:22:29 +0200 Subject: [PATCH] SONAR-9482 Count projects associated to quality profiles --- .../org/sonar/db/qualityprofile/QualityProfileDao.java | 6 ++++-- .../org/sonar/db/qualityprofile/QualityProfileMapper.java | 2 +- .../org/sonar/db/qualityprofile/QualityProfileMapper.xml | 5 ++++- .../org/sonar/db/qualityprofile/QualityProfileDaoTest.java | 4 +++- .../org/sonar/server/qualityprofile/ws/SearchAction.java | 2 +- .../sonar/server/qualityprofile/ws/SearchActionTest.java | 7 +++++++ 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java index 494d623402e..600be7ac278 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java @@ -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 countProjectsByProfileUuid(DbSession dbSession, OrganizationDto organization) { - return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileUuid(organization.getUuid())); + public Map countProjectsByOrganizationAndProfiles(DbSession dbSession, OrganizationDto organization, List profiles) { + List 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) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java index 758786dc15c..04def0160fd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java @@ -77,7 +77,7 @@ public interface QualityProfileMapper { // PROJECTS - List countProjectsByProfileUuid(@Param("organizationUuid") String organizationUuid); + List countProjectsByOrganizationAndProfiles(@Param("organizationUuid") String organizationUuid, @Param("profileUuids") List profiles); @CheckForNull QProfileDto selectAssociatedToProjectUuidAndLanguage( diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml index 1f8add3639b..3f85132f1d4 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml @@ -205,7 +205,7 @@ ORDER BY rp.name - 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 @@ -214,6 +214,9 @@ pj.enabled = ${_true} and pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR} and oqp.organization_uuid = pj.organization_uuid + and + oqp.uuid = #{profileUuid, jdbcType=VARCHAR} + group by pqp.profile_key diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java index 48ae7bd63ef..d254c2b412c 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java @@ -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 diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java index 0600bd45062..e8b28c8ccad 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java @@ -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); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java index c5764d1fb0c..ab8c20e38e7 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/SearchActionTest.java @@ -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(); -- 2.39.5