From f16ba7b4228e0908d7dd7495be1d3905b14b577d Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Mon, 8 May 2017 15:44:41 +0200 Subject: [PATCH] SONAR-8894 Compute number of members by organization --- .../db/organization/OrganizationCount.java | 44 +++++++++++++++++++ .../organization/OrganizationMemberDao.java | 12 +++++ .../OrganizationMemberMapper.java | 3 ++ .../organization/OrganizationMemberMapper.xml | 11 +++++ .../OrganizationMemberDaoTest.java | 23 ++++++++++ 5 files changed, 93 insertions(+) create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationCount.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationCount.java b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationCount.java new file mode 100644 index 00000000000..70c3dfc4a49 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationCount.java @@ -0,0 +1,44 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.db.organization; + +public class OrganizationCount { + private String organizationUuid; + private int memberCount; + + public String getOrganizationUuid() { + return organizationUuid; + } + + public OrganizationCount setOrganizationUuid(String organizationUuid) { + this.organizationUuid = organizationUuid; + return this; + } + + public int getMemberCount() { + return memberCount; + } + + public OrganizationCount setMemberCount(int memberCount) { + this.memberCount = memberCount; + return this; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberDao.java index abc23c06245..497003eca11 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberDao.java @@ -20,6 +20,8 @@ package org.sonar.db.organization; +import com.google.common.collect.ImmutableMultiset; +import com.google.common.collect.Multiset; import java.util.List; import java.util.Optional; import java.util.Set; @@ -66,6 +68,16 @@ public class OrganizationMemberDao implements Dao { return mapper(dbSession).selectOrganizationUuidsByUser(userId); } + public Multiset countByOrganizationUuids(DbSession dbSession, List organizationUuids) { + ImmutableMultiset.Builder counts = ImmutableMultiset.builder(); + executeLargeInputsWithoutOutput(organizationUuids, list -> mapper(dbSession).countByOrganizationUuids(list, result -> { + OrganizationCount organizationUuidCount = (OrganizationCount) result.getResultObject(); + counts.setCount(organizationUuidCount.getOrganizationUuid(), organizationUuidCount.getMemberCount()); + })); + + return counts.build(); + } + /** * * @param loginOrganizationConsumer {@link BiConsumer} (login, organization uuid) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberMapper.java index 9186f9aa732..26091e3ea36 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberMapper.java @@ -24,6 +24,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.session.ResultHandler; public interface OrganizationMemberMapper { OrganizationMemberDto select(@Param("organizationUuid") String organizationUuid, @Param("userId") int userId); @@ -45,4 +46,6 @@ public interface OrganizationMemberMapper { void deleteByOrganization(@Param("organizationUuid") String organizationUuid); void deleteByUserId(@Param("userId") int userId); + + void countByOrganizationUuids(@Param("organizationUuids") List organizationUuids, ResultHandler resultHandler); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMemberMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMemberMapper.xml index 618a3726d3c..bd26bdae740 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMemberMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMemberMapper.xml @@ -52,6 +52,17 @@ inner join users u on om.user_id=u.id + + insert into organization_members ( diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationMemberDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationMemberDaoTest.java index b05dabee814..9c382e0315b 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationMemberDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationMemberDaoTest.java @@ -20,6 +20,7 @@ package org.sonar.db.organization; +import com.google.common.collect.Multiset; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -35,6 +36,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.user.UserDto; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; @@ -139,6 +141,27 @@ public class OrganizationMemberDaoTest { assertThat(result).containsOnly(tuple("L_1", "ORG_1"), tuple("L_1", "ORG_2"), tuple("L_2", "ORG_1")); } + @Test + public void count_by_organization_uuid() { + UserDto georgeOrwell = db.users().insertUser(); + UserDto rayBradbury = db.users().insertUser(); + UserDto frankHerbert = db.users().insertUser(); + OrganizationDto org1 = db.organizations().insert(); + db.organizations().addMember(org1, georgeOrwell); + db.organizations().addMember(org1, rayBradbury); + db.organizations().addMember(org1, frankHerbert); + OrganizationDto org2 = db.organizations().insert(); + db.organizations().addMember(org2, georgeOrwell); + OrganizationDto org3 = db.organizations().insert(); + + Multiset result = underTest.countByOrganizationUuids(dbSession, asList(org1.getUuid(), org2.getUuid(), org3.getUuid())); + + assertThat(result.count(org1.getUuid())).isEqualTo(3); + assertThat(result.count(org2.getUuid())).isEqualTo(1); + assertThat(result.count(org3.getUuid())).isEqualTo(0); + assertThat(result.count("ORG_42")).isEqualTo(0); + } + @Test public void insert() { underTest.insert(dbSession, create("O_1", 256)); -- 2.39.5