]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8894 Compute number of members by organization 2036/head
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 8 May 2017 13:44:41 +0000 (15:44 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 8 May 2017 13:44:56 +0000 (15:44 +0200)
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationCount.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/organization/OrganizationMemberMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/organization/OrganizationMemberMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/organization/OrganizationMemberDaoTest.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 (file)
index 0000000..70c3dfc
--- /dev/null
@@ -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;
+  }
+}
index abc23c06245598bf81e255cffc8ca3fb4beeb3b6..497003eca117abcb8a496506e019b6c0d5b60012 100644 (file)
@@ -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<String> countByOrganizationUuids(DbSession dbSession, List<String> organizationUuids) {
+    ImmutableMultiset.Builder<String> 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}<String,String> (login, organization uuid)
index 9186f9aa7329cc99ca36f876dd71e0236557c202..26091e3ea3690bcd408f507c30e7ad1b11dbe7e6 100644 (file)
@@ -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<String> organizationUuids, ResultHandler resultHandler);
 }
index 618a3726d3c0f1cbb1558ec424f1cdc77183d154..bd26bdae7407942be2cd4677e104d5c29e6e1757 100644 (file)
     inner join users u on om.user_id=u.id
   </select>
 
+  <select id="countByOrganizationUuids" resultType="org.sonar.db.organization.OrganizationCount">
+    select om.organization_uuid as "organizationUuid", count(om.user_id) as "memberCount"
+    from organization_members om
+    where
+      om.organization_uuid in
+      <foreach collection="organizationUuids" open="(" close=")" item="organizationUuid" separator=",">
+        #{organizationUuid, jdbcType=VARCHAR}
+      </foreach>
+    group by om.organization_uuid
+  </select>
+
   <insert id="insert" parameterType="OrganizationMember" useGeneratedKeys="false">
     insert into organization_members
     (
index b05dabee8145aa79d570e5b2587ad3661e605f1b..9c382e0315be05fd3eb71461128c9b04008f25c1 100644 (file)
@@ -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<String> 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));