Skip to content

Commit

Permalink
catalog: implement facet in JobMongoDBAdaptor, #TASK-7152, #TASK-7134
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Jan 20, 2025
1 parent 6dbb931 commit c64b82b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,8 @@ public OpenCGAResult<?> distinct(long studyUid, List<String> fields, Query query
@Override
public OpenCGAResult<FacetField> facet(long studyUid, Query query, String facet, String userId)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
return null;
Bson bson = parseQuery(query, QueryOptions.empty(), userId);
return facet(jobCollection, bson, facet);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,9 @@ protected OpenCGAResult<FacetField> facet(MongoDBCollection collection, Bson que
}
MongoDBDocumentToFacetFieldsConverter converter = new MongoDBDocumentToFacetFieldsConverter();
List<Bson> facets = MongoDBQueryUtils.createFacet(query, facet);
logger.debug("facet; input = {}", facets);
logger.info("facet; input = {}", facets);
DataResult<List<FacetField>> aggregate = collection.aggregate(facets, converter, null);
logger.debug("facet; output = {}", aggregate.getResults());
logger.info("facet; output = {}", aggregate.getResults());

// Replace "&#46;" by .
List<FacetField> facetFields = aggregate.getResults().get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.opencb.biodata.models.common.Status;
import org.opencb.biodata.models.pedigree.IndividualProperty;
import org.opencb.commons.datastore.core.DataResult;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.commons.datastore.core.*;
import org.opencb.opencga.TestParamConstants;
import org.opencb.opencga.catalog.auth.authorization.AuthorizationManager;
import org.opencb.opencga.catalog.db.api.*;
Expand Down Expand Up @@ -970,6 +968,49 @@ public void visitJob() throws CatalogException {
assertEquals(1, catalogManager.getJobManager().count(studyFqn, query, ownerToken).getNumMatches());
}

@Test
public void testJobsFacet() throws CatalogException {
Query query = new Query();
String studyId = catalogManager.getStudyManager().searchInOrganization(organizationId, query, null, ownerToken).first().getId();

// catalogManager.getJobManager().create(studyId, new Job().setId("myErrorJob"), null, ownerToken);
//
// QueryOptions options = new QueryOptions(QueryOptions.COUNT, true);
// DataResult<Job> allJobs = catalogManager.getJobManager().search(studyId, null, options, ownerToken);

int numJobs = 88;
for (int i = numJobs; i > 0; i--) {
ToolInfo toolInfo = new ToolInfo();
toolInfo.setId("tool-" + (i % 5) + Integer.valueOf(RandomStringUtils.randomNumeric(1)));
Job job = new Job().setId("myJob-" + i).setTool(toolInfo);
catalogManager.getJobManager().create(studyId, job, null, ownerToken);
}

Map<String, Integer> toolIdCounter = new HashMap<>();
QueryOptions options = new QueryOptions(QueryOptions.COUNT, true);
DataResult<Job> allJobs = catalogManager.getJobManager().search(studyId, null, options, ownerToken);
for (Job job : allJobs.getResults()) {
String toolId = job.getTool().getId();
if (!toolIdCounter.containsKey(toolId)) {
toolIdCounter.put(toolId, 0);
}
toolIdCounter.put(toolId, 1 + toolIdCounter.get(toolId));
}

for (Map.Entry<String, Integer> entry : toolIdCounter.entrySet()) {
System.out.println(entry.getKey() + " --> " + entry.getValue());
}

String field = "tool.id";
FacetField facetField = catalogManager.getJobManager().facet(studyId, new Query(), field, ownerToken).first();
Assert.assertEquals(field, facetField.getName());
Assert.assertEquals(numJobs, facetField.getCount(), 0.001);
for (FacetField.Bucket bucket : facetField.getBuckets()) {
Assert.assertTrue(toolIdCounter.containsKey(bucket.getValue()));
Assert.assertEquals(toolIdCounter.get(bucket.getValue()), bucket.getCount(), 0.001);
}
}

/**
* VariableSet methods ***************************
*/
Expand Down

0 comments on commit c64b82b

Please sign in to comment.