# HG changeset patch # User ymh # Date 1366998337 -7200 # Node ID aacebeedae7056c06956d4f74a4e145596936d50 # Parent fdc74162bef1f8ab5b39fb207a9ce9a8c0b990e1 project count by user and count by username implemented using mongodb aggregation framework diff -r fdc74162bef1 -r aacebeedae70 server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryImpl.java --- a/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryImpl.java Wed Apr 24 14:08:19 2013 +0200 +++ b/server/src/main/java/org/iri_research/renkan/repositories/ProjectsRepositoryImpl.java Fri Apr 26 19:45:37 2013 +0200 @@ -1,27 +1,24 @@ package org.iri_research.renkan.repositories; +import static org.springframework.data.mongodb.core.query.Criteria.where; +import static org.springframework.data.mongodb.core.query.Query.query; + import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; -import org.eclipse.jetty.util.component.AggregateLifeCycle; -import org.iri_research.renkan.RenkanProperties; import org.iri_research.renkan.models.Project; import org.iri_research.renkan.models.ProjectRevision; -import org.iri_research.renkan.models.RenkanUser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.stereotype.Component; -import static org.springframework.data.mongodb.core.query.Query.query; -import static org.springframework.data.mongodb.core.query.Criteria.where; - import org.springframework.data.mongodb.core.mapreduce.GroupBy; import org.springframework.data.mongodb.core.mapreduce.GroupByResults; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Update; +import org.springframework.stereotype.Component; import com.mongodb.AggregationOutput; import com.mongodb.BasicDBObject; @@ -30,6 +27,7 @@ @Component public class ProjectsRepositoryImpl implements ProjectsRepositoryCustom { + @SuppressWarnings("unused") private final Logger logger = LoggerFactory.getLogger(ProjectsRepositoryImpl.class); @Autowired @@ -49,11 +47,6 @@ public String space_id; public int count; } - - private class GroupUsernameResult { - public String username; - public int count; - } @Autowired private MongoTemplate mongoTemplate; @@ -155,14 +148,25 @@ if(userIds != null) { filter = Criteria.where("users.user_id").in(userIds); } - + DBObject projectOp = new BasicDBObject("$project", new BasicDBObject("users", 1)); DBObject unwindOp = new BasicDBObject("$unwind","$users"); DBObject groupOpFields = new BasicDBObject("_id", "$users.user_id"); groupOpFields.put("count", new BasicDBObject("$sum", 1)); DBObject groupOp = new BasicDBObject("$group",groupOpFields); + DBObject matchOp = null; + if(filter != null) { + matchOp = new BasicDBObject("$match", filter.getCriteriaObject()); + } - AggregationOutput output = this.projectsRepository.getCollection().aggregate(projectOp, unwindOp, groupOp); + AggregationOutput output = null; + if(filter!=null) { + output = this.projectsRepository.getCollection().aggregate(matchOp, projectOp, unwindOp, matchOp, groupOp); + } + else { + output = this.projectsRepository.getCollection().aggregate(projectOp, unwindOp, groupOp); + } + HashMap res = new HashMap<>(); for (DBObject groupRes : output.results()) { @@ -181,7 +185,38 @@ @Override public Map getCountByUsername(Collection usernames) { - // TODO Auto-generated method stub - return null; + + Criteria filter = null; + + if(usernames != null) { + filter = Criteria.where("users.username").in(usernames); + } + + DBObject projectOp = new BasicDBObject("$project", new BasicDBObject("users", 1)); + DBObject unwindOp = new BasicDBObject("$unwind","$users"); + DBObject groupOpFields = new BasicDBObject("_id", "$users.username"); + groupOpFields.put("count", new BasicDBObject("$sum", 1)); + DBObject groupOp = new BasicDBObject("$group",groupOpFields); + DBObject matchOp = null; + if(filter != null) { + matchOp = new BasicDBObject("$match", filter.getCriteriaObject()); + } + + AggregationOutput output = null; + if(filter!=null) { + output = this.projectsRepository.getCollection().aggregate(matchOp, projectOp, unwindOp, matchOp, groupOp); + } + else { + output = this.projectsRepository.getCollection().aggregate(projectOp, unwindOp, groupOp); + } + + HashMap res = new HashMap<>(); + + for (DBObject groupRes : output.results()) { + res.put((String)groupRes.get("_id"), (Integer)groupRes.get("count")); + } + + return res; + } } diff -r fdc74162bef1 -r aacebeedae70 server/src/test/java/org/iri_research/renkan/test/repositories/ProjectsRepositoryTest.java --- a/server/src/test/java/org/iri_research/renkan/test/repositories/ProjectsRepositoryTest.java Wed Apr 24 14:08:19 2013 +0200 +++ b/server/src/test/java/org/iri_research/renkan/test/repositories/ProjectsRepositoryTest.java Fri Apr 26 19:45:37 2013 +0200 @@ -273,7 +273,7 @@ Assert.assertNotNull("count not null", count); Assert.assertEquals("Nb of user/project", testProjects.size()-i, count.intValue()); } - Assert.assertNull("Last user id has no project i.e count is null", groupRes.get(this.testUsers.get(nu-1))); + Assert.assertNull("Last user id has no project i.e count is null", groupRes.get(this.testUsers.get(nu-1).getId())); Integer count = groupRes.get(null); Assert.assertNotNull("count not null", count); @@ -283,21 +283,62 @@ @Test public void testGetCountByUserFilter() { - List spacesIdsFilter = Arrays.asList(this.spaceIds.get(0)); + List userIdsFilter = Arrays.asList(this.testUsers.get(0).getId()); - Map groupRes = projectsRepository.getCountBySpace(spacesIdsFilter); + Map groupRes = projectsRepository.getCountByUser(userIdsFilter); Assert.assertNotNull("GroupRes not null", groupRes); Assert.assertEquals("Group res size", 1, groupRes.size()); - Integer count = groupRes.get(this.spaceIds.get(0)); + Integer count = groupRes.get(this.testUsers.get(0).getId()); Assert.assertNotNull("count not null", count); - Assert.assertEquals("Nb of project/space", 2, count.intValue()); + Assert.assertEquals("Nb of user/project", testProjects.size(), count.intValue()); + + } + + @Test + public void testGetCountByUsername() { + + Map groupRes = projectsRepository.getCountByUsername(); + int nu = this.testUsers.size() - 1; + int nb_username = this.testUsers.size() + (SPACE_NB-1)*SPACE_NB/2 -2; + + Assert.assertNotNull("GroupRes not null", groupRes); + Assert.assertEquals("Group res size", nb_username, groupRes.size()); + + for(int i=0;i usernamesFilter = Arrays.asList(this.testUsers.get(0).getUsername()); + + Map groupRes = projectsRepository.getCountByUsername(usernamesFilter); + + Assert.assertNotNull("GroupRes not null", groupRes); + Assert.assertEquals("Group res size", 1, groupRes.size()); + + Integer count = groupRes.get(this.testUsers.get(0).getUsername()); + Assert.assertNotNull("count not null", count); + Assert.assertEquals("Nb of user/project", testProjects.size(), count.intValue()); + + } + @Test