Skip to content

Commit

Permalink
server: add 3 new web services to handle events, #TASK-4178
Browse files Browse the repository at this point in the history
  • Loading branch information
pfurio committed Oct 11, 2024
1 parent ae1cde6 commit 8ccbe83
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public abstract class OpenCgaCompleter implements Completer {
.map(Candidate::new)
.collect(toList());

private List<Candidate> organizationsList = asList( "create","notes-create","notes-search","notes-delete","notes-update","update-status-user","user-update","configuration-update","info","update")
private List<Candidate> organizationsList = asList( "create","notes-create","notes-search","notes-delete","notes-update","update-status-user","user-update","configuration-update","events-query","events-archive","events-retry","info","update")
.stream()
.map(Candidate::new)
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,9 @@ public OpencgaCliOptionsParser() {
organizationsSubCommands.addCommand("update-status-user", organizationsCommandOptions.userUpdateStatusCommandOptions);
organizationsSubCommands.addCommand("user-update", organizationsCommandOptions.updateUserCommandOptions);
organizationsSubCommands.addCommand("configuration-update", organizationsCommandOptions.updateConfigurationCommandOptions);
organizationsSubCommands.addCommand("events-query", organizationsCommandOptions.queryEventsCommandOptions);
organizationsSubCommands.addCommand("events-archive", organizationsCommandOptions.archiveEventsCommandOptions);
organizationsSubCommands.addCommand("events-retry", organizationsCommandOptions.retryEventsCommandOptions);
organizationsSubCommands.addCommand("info", organizationsCommandOptions.infoCommandOptions);
organizationsSubCommands.addCommand("update", organizationsCommandOptions.updateCommandOptions);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.opencb.opencga.client.exceptions.ClientException;
import org.opencb.opencga.core.common.JacksonUtils;
import org.opencb.opencga.core.config.Optimizations;
import org.opencb.opencga.core.models.event.CatalogEvent;
import org.opencb.opencga.core.models.notes.Note;
import org.opencb.opencga.core.models.notes.NoteCreateParams;
import org.opencb.opencga.core.models.notes.NoteUpdateParams;
Expand Down Expand Up @@ -89,6 +90,15 @@ public void execute() throws Exception {
case "configuration-update":
queryResponse = updateConfiguration();
break;
case "events-query":
queryResponse = queryEvents();
break;
case "events-archive":
queryResponse = archiveEvents();
break;
case "events-retry":
queryResponse = retryEvents();
break;
case "info":
queryResponse = info();
break;
Expand Down Expand Up @@ -345,6 +355,37 @@ private RestResponse<OrganizationConfiguration> updateConfiguration() throws Exc
return openCGAClient.getOrganizationClient().updateConfiguration(commandOptions.organization, organizationConfiguration, queryParams);
}

private RestResponse<CatalogEvent> queryEvents() throws Exception {
logger.debug("Executing queryEvents in Organizations command line");

OrganizationsCommandOptions.QueryEventsCommandOptions commandOptions = organizationsCommandOptions.queryEventsCommandOptions;

ObjectMap queryParams = new ObjectMap();
queryParams.putIfNotEmpty("study", commandOptions.study);
queryParams.putIfNotEmpty("creationDate", commandOptions.creationDate);
queryParams.putIfNotEmpty("modificationDate", commandOptions.modificationDate);
queryParams.putIfNotNull("successful", commandOptions.successful);
if (queryParams.get("study") == null && OpencgaMain.isShellMode()) {
queryParams.putIfNotEmpty("study", sessionManager.getSession().getCurrentStudy());
}

return openCGAClient.getOrganizationClient().queryEvents(commandOptions.organization, queryParams);
}

private RestResponse<CatalogEvent> archiveEvents() throws Exception {
logger.debug("Executing archiveEvents in Organizations command line");

OrganizationsCommandOptions.ArchiveEventsCommandOptions commandOptions = organizationsCommandOptions.archiveEventsCommandOptions;
return openCGAClient.getOrganizationClient().archiveEvents(commandOptions.organization, commandOptions.eventId);
}

private RestResponse<CatalogEvent> retryEvents() throws Exception {
logger.debug("Executing retryEvents in Organizations command line");

OrganizationsCommandOptions.RetryEventsCommandOptions commandOptions = organizationsCommandOptions.retryEventsCommandOptions;
return openCGAClient.getOrganizationClient().retryEvents(commandOptions.organization, commandOptions.eventId);
}

private RestResponse<Organization> info() throws Exception {
logger.debug("Executing info in Organizations command line");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class OrganizationsCommandOptions {
public UserUpdateStatusCommandOptions userUpdateStatusCommandOptions;
public UpdateUserCommandOptions updateUserCommandOptions;
public UpdateConfigurationCommandOptions updateConfigurationCommandOptions;
public QueryEventsCommandOptions queryEventsCommandOptions;
public ArchiveEventsCommandOptions archiveEventsCommandOptions;
public RetryEventsCommandOptions retryEventsCommandOptions;
public InfoCommandOptions infoCommandOptions;
public UpdateCommandOptions updateCommandOptions;

Expand All @@ -57,6 +60,9 @@ public OrganizationsCommandOptions(CommonCommandOptions commonCommandOptions, JC
this.userUpdateStatusCommandOptions = new UserUpdateStatusCommandOptions();
this.updateUserCommandOptions = new UpdateUserCommandOptions();
this.updateConfigurationCommandOptions = new UpdateConfigurationCommandOptions();
this.queryEventsCommandOptions = new QueryEventsCommandOptions();
this.archiveEventsCommandOptions = new ArchiveEventsCommandOptions();
this.retryEventsCommandOptions = new RetryEventsCommandOptions();
this.infoCommandOptions = new InfoCommandOptions();
this.updateCommandOptions = new UpdateCommandOptions();

Expand Down Expand Up @@ -354,6 +360,69 @@ public class UpdateConfigurationCommandOptions {

}

@Parameters(commandNames = {"events-query"}, commandDescription ="Search events")
public class QueryEventsCommandOptions {

@ParametersDelegate
public CommonCommandOptions commonOptions = commonCommandOptions;

@Parameter(names = {"--organization"}, description = "Organization id", required = true, arity = 1)
public String organization;

@Parameter(names = {"--study", "-s"}, description = "Study [[organization@]project:]study where study and project can be either the ID or UUID", required = false, arity = 1)
public String study;

@Parameter(names = {"--creation-date", "--cd"}, description = "Creation date. Format: yyyyMMddHHmmss. Examples: >2018, 2017-2018, <201805", required = false, arity = 1)
public String creationDate;

@Parameter(names = {"--modification-date", "--md"}, description = "Modification date. Format: yyyyMMddHHmmss. Examples: >2018, 2017-2018, <201805", required = false, arity = 1)
public String modificationDate;

@Parameter(names = {"--successful"}, description = "Boolean indicating if the event was successful or not.", required = false, arity = 1)
public Boolean successful;

}

@Parameters(commandNames = {"events-archive"}, commandDescription ="Archive an event")
public class ArchiveEventsCommandOptions {

@ParametersDelegate
public CommonCommandOptions commonOptions = commonCommandOptions;

@Parameter(names = {"--json-file"}, description = "File with the body data in JSON format. Note, that using this parameter will ignore all the other parameters.", required = false, arity = 1)
public String jsonFile;

@Parameter(names = {"--json-data-model"}, description = "Show example of file structure for body data.", help = true, arity = 0)
public Boolean jsonDataModel = false;

@Parameter(names = {"--organization"}, description = "Organization id", required = true, arity = 1)
public String organization;

@Parameter(names = {"--event-id"}, description = "Event id.", required = true, arity = 1)
public String eventId;

}

@Parameters(commandNames = {"events-retry"}, commandDescription ="Retry unsuccessful event")
public class RetryEventsCommandOptions {

@ParametersDelegate
public CommonCommandOptions commonOptions = commonCommandOptions;

@Parameter(names = {"--json-file"}, description = "File with the body data in JSON format. Note, that using this parameter will ignore all the other parameters.", required = false, arity = 1)
public String jsonFile;

@Parameter(names = {"--json-data-model"}, description = "Show example of file structure for body data.", help = true, arity = 0)
public Boolean jsonDataModel = false;

@Parameter(names = {"--organization"}, description = "Organization id", required = true, arity = 1)
public String organization;

@Parameter(names = {"--event-id"}, description = "Event id.", required = true, arity = 1)
public String eventId;

}

@Parameters(commandNames = {"info"}, commandDescription ="Return the organization information")
public class InfoCommandOptions {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ enum QueryParams implements QueryParam {
SUBSCRIBERS("subscribers", OBJECT, ""),
SUCCESSFUL("successful", BOOLEAN, ""),
CREATION_DATE("creationDate", STRING, ""),
MODIFICATION_DATE("modificationDate", STRING, "");
MODIFICATION_DATE("modificationDate", STRING, ""),
EVENT_STUDY_FQN("event.studyFqn", STRING, "");

private static Map<String, QueryParams> map = new HashMap<>();
static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mongodb.client.ClientSession;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Updates;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
Expand Down Expand Up @@ -50,7 +49,10 @@ public EventMongoDBAdaptor(MongoDBCollection eventCollection, MongoDBCollection
@Override
public OpenCGAResult<CatalogEvent> get(Query query, QueryOptions options)
throws CatalogDBException, CatalogParameterException, CatalogAuthorizationException {
throw new NotImplementedException("Yet to implement");
long startTime = startQuery();
try (DBIterator<CatalogEvent> dbIterator = iterator(null, query, options)) {
return endQuery(startTime, dbIterator);
}
}

@Override
Expand Down Expand Up @@ -196,6 +198,7 @@ private Bson parseQuery(Query query) throws CatalogDBException {
case MODIFICATION_DATE:
addAutoOrQuery(PRIVATE_MODIFICATION_DATE, queryParam.key(), query, queryParam.type(), andBsonList);
break;
case EVENT_STUDY_FQN:
case SUCCESSFUL:
case ID:
case UUID:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opencb.opencga.catalog.managers;

import org.apache.commons.lang3.StringUtils;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.Query;
import org.opencb.commons.datastore.core.QueryOptions;
Expand All @@ -9,8 +10,10 @@
import org.opencb.opencga.catalog.exceptions.CatalogDBException;
import org.opencb.opencga.catalog.exceptions.CatalogException;
import org.opencb.opencga.catalog.exceptions.CatalogParameterException;
import org.opencb.opencga.catalog.utils.CatalogFqn;
import org.opencb.opencga.catalog.utils.ParamUtils;
import org.opencb.opencga.catalog.utils.UuidUtils;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.common.TimeUtils;
import org.opencb.opencga.core.events.EventFactory;
import org.opencb.opencga.core.events.IEventHandler;
Expand Down Expand Up @@ -177,34 +180,25 @@ public CatalogEvent notify(String eventId, String organizationId, Supplier<Study
return catalogEvent;
}

// public CatalogEvent notify(String eventId, String organizationId, Study study, @Nullable String id, String userId,
// QueryOptions queryOptions, ObjectMap params, JwtPayload payload, ExecuteOperation<?> executeOperation)
// throws CatalogException {
// CatalogEvent catalogEvent = new CatalogEvent(eventId, new OpencgaEvent(organizationId, eventId, params, userId, study.getFqn(),
// id, payload.getToken(), null));
// validateNewEvent(catalogEvent);
// try {
// logger.info("Executing '{}' event", eventId);
// OpenCGAResult<?> execute = executeOperation.execute(organizationId, study, userId, queryOptions, payload);
// catalogEvent.getEvent().setResult(execute);
// } catch (Exception e) {
// eventHandler.notify(catalogEvent, e);
// throw new CatalogException(e.getMessage(), e);
// }
//
// logger.info("Notifying of event '{}'.", eventId);
// eventHandler.notify(catalogEvent);
// return catalogEvent;
// }

public OpenCGAResult<CatalogEvent> search(String organizationId, Query query, String token) throws CatalogException {
JwtPayload tokenPayload = catalogManager.getUserManager().validateToken(token);
String userId = tokenPayload.getUserId(organizationId);
catalogManager.getAuthorizationManager().checkIsAtLeastOrganizationOwnerOrAdmin(organizationId, userId);
fixQueryObject(query, tokenPayload);

return dbAdaptorFactory.getEventDBAdaptor(organizationId).get(query, QueryOptions.empty());
}

void fixQueryObject(Query query, JwtPayload tokenPayload) throws CatalogException {
String studyParam = query.getString(ParamConstants.STUDY_PARAM);
if (StringUtils.isNotEmpty(studyParam)) {
CatalogFqn catalogFqn = CatalogFqn.extractFqnFromStudy(studyParam, tokenPayload);
Study study = catalogManager.getStudyManager().resolveId(catalogFqn, QueryOptions.empty(), tokenPayload);
query.put(EventDBAdaptor.QueryParams.EVENT_STUDY_FQN.key(), study.getFqn());
query.remove(ParamConstants.STUDY_PARAM);
}
}

public OpenCGAResult<CatalogEvent> archiveEvent(String organizationId, String eventId, String token) throws CatalogException {
JwtPayload tokenPayload = catalogManager.getUserManager().validateToken(token);
String userId = tokenPayload.getUserId(organizationId);
Expand Down
2 changes: 1 addition & 1 deletion opencga-client/src/main/R/R/AllGenerics.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ##############################################################################
## OrganizationClient
setGeneric("organizationClient", function(OpencgaR, id, organization, user, endpointName, params=NULL, ...)
setGeneric("organizationClient", function(OpencgaR, eventId, id, organization, user, endpointName, params=NULL, ...)
standardGeneric("organizationClient"))

# ##############################################################################
Expand Down
32 changes: 31 additions & 1 deletion opencga-client/src/main/R/R/Organization-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#' | userUpdateStatus | /{apiVersion}/organizations/user/{user}/status/update | include, exclude, user[*], organization, includeResult, body[*] |
#' | updateUser | /{apiVersion}/organizations/user/{user}/update | include, exclude, user[*], organization, includeResult, body[*] |
#' | updateConfiguration | /{apiVersion}/organizations/{organization}/configuration/update | include, exclude, organization[*], includeResult, authenticationOriginsAction, body[*] |
#' | queryEvents | /{apiVersion}/organizations/{organization}/events/query | organization[*], study, creationDate, modificationDate, successful |
#' | archiveEvents | /{apiVersion}/organizations/{organization}/events/{eventId}/archive | organization[*], eventId[*] |
#' | retryEvents | /{apiVersion}/organizations/{organization}/events/{eventId}/retry | organization[*], eventId[*] |
#' | info | /{apiVersion}/organizations/{organization}/info | include, exclude, organization[*] |
#' | update | /{apiVersion}/organizations/{organization}/update | include, exclude, organization[*], includeResult, adminsAction, body[*] |
#'
Expand All @@ -36,7 +39,7 @@
#' [*]: Required parameter
#' @export

setMethod("organizationClient", "OpencgaR", function(OpencgaR, id, organization, user, endpointName, params=NULL, ...) {
setMethod("organizationClient", "OpencgaR", function(OpencgaR, eventId, id, organization, user, endpointName, params=NULL, ...) {
switch(endpointName,

#' @section Endpoint /{apiVersion}/organizations/create:
Expand Down Expand Up @@ -125,6 +128,33 @@ setMethod("organizationClient", "OpencgaR", function(OpencgaR, id, organization,
subcategory="configuration", subcategoryId=NULL, action="update", params=params, httpMethod="POST",
as.queryParam=NULL, ...),

#' @section Endpoint /{apiVersion}/organizations/{organization}/events/query:
#' Search events.
#' @param organization Organization id.
#' @param study Study [[organization@]project:]study where study and project can be either the ID or UUID.
#' @param creationDate Creation date. Format: yyyyMMddHHmmss. Examples: >2018, 2017-2018, <201805.
#' @param modificationDate Modification date. Format: yyyyMMddHHmmss. Examples: >2018, 2017-2018, <201805.
#' @param successful Boolean indicating if the event was successful or not.
queryEvents=fetchOpenCGA(object=OpencgaR, category="organizations", categoryId=organization,
subcategory="events", subcategoryId=NULL, action="query", params=params, httpMethod="GET",
as.queryParam=NULL, ...),

#' @section Endpoint /{apiVersion}/organizations/{organization}/events/{eventId}/archive:
#' Archive an event.
#' @param organization Organization id.
#' @param eventId Event id.
archiveEvents=fetchOpenCGA(object=OpencgaR, category="organizations", categoryId=organization,
subcategory="events", subcategoryId=eventId, action="archive", params=params, httpMethod="POST",
as.queryParam=NULL, ...),

#' @section Endpoint /{apiVersion}/organizations/{organization}/events/{eventId}/retry:
#' Retry unsuccessful event.
#' @param organization Organization id.
#' @param eventId Event id.
retryEvents=fetchOpenCGA(object=OpencgaR, category="organizations", categoryId=organization,
subcategory="events", subcategoryId=eventId, action="retry", params=params, httpMethod="POST",
as.queryParam=NULL, ...),

#' @section Endpoint /{apiVersion}/organizations/{organization}/info:
#' Return the organization information.
#' @param include Fields included in the response, whole JSON path must be provided.
Expand Down
Loading

0 comments on commit 8ccbe83

Please sign in to comment.