Skip to content

Commit c64b82b

Browse files
committed
catalog: implement facet in JobMongoDBAdaptor, #TASK-7152, #TASK-7134
1 parent 6dbb931 commit c64b82b

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

Diff for: opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/JobMongoDBAdaptor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,8 @@ public OpenCGAResult<?> distinct(long studyUid, List<String> fields, Query query
729729
@Override
730730
public OpenCGAResult<FacetField> facet(long studyUid, Query query, String facet, String userId)
731731
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
732-
return null;
732+
Bson bson = parseQuery(query, QueryOptions.empty(), userId);
733+
return facet(jobCollection, bson, facet);
733734
}
734735

735736
@Override

Diff for: opencga-catalog/src/main/java/org/opencb/opencga/catalog/db/mongodb/MongoDBAdaptor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,9 @@ protected OpenCGAResult<FacetField> facet(MongoDBCollection collection, Bson que
333333
}
334334
MongoDBDocumentToFacetFieldsConverter converter = new MongoDBDocumentToFacetFieldsConverter();
335335
List<Bson> facets = MongoDBQueryUtils.createFacet(query, facet);
336-
logger.debug("facet; input = {}", facets);
336+
logger.info("facet; input = {}", facets);
337337
DataResult<List<FacetField>> aggregate = collection.aggregate(facets, converter, null);
338-
logger.debug("facet; output = {}", aggregate.getResults());
338+
logger.info("facet; output = {}", aggregate.getResults());
339339

340340
// Replace "&#46;" by .
341341
List<FacetField> facetFields = aggregate.getResults().get(0);

Diff for: opencga-catalog/src/test/java/org/opencb/opencga/catalog/managers/CatalogManagerTest.java

+45-4
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
import org.apache.commons.lang3.RandomStringUtils;
2121
import org.apache.commons.lang3.StringUtils;
2222
import org.apache.commons.lang3.time.StopWatch;
23+
import org.junit.Assert;
2324
import org.junit.Test;
2425
import org.junit.experimental.categories.Category;
2526
import org.opencb.biodata.models.common.Status;
2627
import org.opencb.biodata.models.pedigree.IndividualProperty;
27-
import org.opencb.commons.datastore.core.DataResult;
28-
import org.opencb.commons.datastore.core.ObjectMap;
29-
import org.opencb.commons.datastore.core.Query;
30-
import org.opencb.commons.datastore.core.QueryOptions;
28+
import org.opencb.commons.datastore.core.*;
3129
import org.opencb.opencga.TestParamConstants;
3230
import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager;
3331
import org.opencb.opencga.catalog.db.api.*;
@@ -970,6 +968,49 @@ public void visitJob() throws CatalogException {
970968
assertEquals(1, catalogManager.getJobManager().count(studyFqn, query, ownerToken).getNumMatches());
971969
}
972970

971+
@Test
972+
public void testJobsFacet() throws CatalogException {
973+
Query query = new Query();
974+
String studyId = catalogManager.getStudyManager().searchInOrganization(organizationId, query, null, ownerToken).first().getId();
975+
976+
// catalogManager.getJobManager().create(studyId, new Job().setId("myErrorJob"), null, ownerToken);
977+
//
978+
// QueryOptions options = new QueryOptions(QueryOptions.COUNT, true);
979+
// DataResult<Job> allJobs = catalogManager.getJobManager().search(studyId, null, options, ownerToken);
980+
981+
int numJobs = 88;
982+
for (int i = numJobs; i > 0; i--) {
983+
ToolInfo toolInfo = new ToolInfo();
984+
toolInfo.setId("tool-" + (i % 5) + Integer.valueOf(RandomStringUtils.randomNumeric(1)));
985+
Job job = new Job().setId("myJob-" + i).setTool(toolInfo);
986+
catalogManager.getJobManager().create(studyId, job, null, ownerToken);
987+
}
988+
989+
Map<String, Integer> toolIdCounter = new HashMap<>();
990+
QueryOptions options = new QueryOptions(QueryOptions.COUNT, true);
991+
DataResult<Job> allJobs = catalogManager.getJobManager().search(studyId, null, options, ownerToken);
992+
for (Job job : allJobs.getResults()) {
993+
String toolId = job.getTool().getId();
994+
if (!toolIdCounter.containsKey(toolId)) {
995+
toolIdCounter.put(toolId, 0);
996+
}
997+
toolIdCounter.put(toolId, 1 + toolIdCounter.get(toolId));
998+
}
999+
1000+
for (Map.Entry<String, Integer> entry : toolIdCounter.entrySet()) {
1001+
System.out.println(entry.getKey() + " --> " + entry.getValue());
1002+
}
1003+
1004+
String field = "tool.id";
1005+
FacetField facetField = catalogManager.getJobManager().facet(studyId, new Query(), field, ownerToken).first();
1006+
Assert.assertEquals(field, facetField.getName());
1007+
Assert.assertEquals(numJobs, facetField.getCount(), 0.001);
1008+
for (FacetField.Bucket bucket : facetField.getBuckets()) {
1009+
Assert.assertTrue(toolIdCounter.containsKey(bucket.getValue()));
1010+
Assert.assertEquals(toolIdCounter.get(bucket.getValue()), bucket.getCount(), 0.001);
1011+
}
1012+
}
1013+
9731014
/**
9741015
* VariableSet methods ***************************
9751016
*/

0 commit comments

Comments
 (0)