From 5d4f5510bd4ed81069d7040524dd03ec254a88e7 Mon Sep 17 00:00:00 2001 From: kfilippopolitis Date: Tue, 22 Nov 2022 17:20:43 +0200 Subject: [PATCH] AlgorithmsAPI's functionality is now moved to a new Service layer. Experiments now will run to their proper engines according to the get algorithms. --- config/application.tmpl | 1 + .../eu/hbp/mip/controllers/AlgorithmsAPI.java | 227 +-------------- .../eu/hbp/mip/services/AlgorithmService.java | 269 ++++++++++++++++++ .../hbp/mip/services/ExperimentService.java | 49 ++-- src/main/resources/application.yml | 1 + 5 files changed, 299 insertions(+), 248 deletions(-) create mode 100644 src/main/java/eu/hbp/mip/services/AlgorithmService.java diff --git a/config/application.tmpl b/config/application.tmpl index e42153df..0a309c1a 100644 --- a/config/application.tmpl +++ b/config/application.tmpl @@ -34,6 +34,7 @@ spring: ### EXTERNAL SERVICES ### services: + algorithmsUpdateInterval: {{ .Env.ALGORITHM_UPDATE_INTERVAL}} mipengine: algorithmsUrl: {{ .Env.MIPENGINE_URL}}/algorithms attributesUrl: {{ .Env.MIPENGINE_URL}}/data_models_attributes diff --git a/src/main/java/eu/hbp/mip/controllers/AlgorithmsAPI.java b/src/main/java/eu/hbp/mip/controllers/AlgorithmsAPI.java index 44e40651..8addb863 100644 --- a/src/main/java/eu/hbp/mip/controllers/AlgorithmsAPI.java +++ b/src/main/java/eu/hbp/mip/controllers/AlgorithmsAPI.java @@ -1,38 +1,18 @@ package eu.hbp.mip.controllers; -import com.github.jmchilton.blend4j.galaxy.GalaxyInstance; -import com.github.jmchilton.blend4j.galaxy.GalaxyInstanceFactory; -import com.github.jmchilton.blend4j.galaxy.WorkflowsClient; -import com.github.jmchilton.blend4j.galaxy.beans.Workflow; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import eu.hbp.mip.controllers.galaxy.retrofit.RetroFitGalaxyClients; -import eu.hbp.mip.controllers.galaxy.retrofit.RetrofitClientInstance; import eu.hbp.mip.models.DTOs.ExaremeAlgorithmDTO; -import eu.hbp.mip.models.DTOs.MIPEngineAlgorithmDTO; -import eu.hbp.mip.models.galaxy.WorkflowDTO; import eu.hbp.mip.services.ActiveUserService; -import eu.hbp.mip.utils.CustomResourceLoader; -import eu.hbp.mip.utils.HTTPUtil; +import eu.hbp.mip.services.AlgorithmService; import eu.hbp.mip.utils.Logger; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import retrofit2.Call; -import retrofit2.Response; -import java.io.IOException; -import java.net.ConnectException; -import java.util.ArrayList; import java.util.List; -import java.util.Objects; -import static eu.hbp.mip.utils.InputStreamConverter.convertInputStreamToString; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; @RestController @@ -40,218 +20,21 @@ @Api(value = "/algorithms") public class AlgorithmsAPI { - private static final Gson gson = new Gson(); - + private final AlgorithmService algorithmService; private final ActiveUserService activeUserService; - - @Value("#{'${services.mipengine.algorithmsUrl}'}") - private String mipengineAlgorithmsUrl; - - @Value("#{'${services.exareme.algorithmsUrl}'}") - private String exaremeAlgorithmsUrl; - - @Value("#{'${services.galaxy.galaxyUrl}'}") - private String galaxyUrl; - - @Value("#{'${services.galaxy.galaxyApiKey}'}") - private String galaxyApiKey; - - @Value("#{'${files.disabledAlgorithms_json}'}") - private String disabledAlgorithmsFilePath; - - public AlgorithmsAPI(ActiveUserService activeUserService, CustomResourceLoader resourceLoader) { + public AlgorithmsAPI(ActiveUserService activeUserService, AlgorithmService algorithmService) { this.activeUserService = activeUserService; - this.resourceLoader = resourceLoader; + this.algorithmService = algorithmService; } @ApiOperation(value = "List all algorithms", response = String.class) @RequestMapping(method = RequestMethod.GET) public ResponseEntity> getAlgorithms() { Logger logger = new Logger(activeUserService.getActiveUser().getUsername(), "(GET) /algorithms"); - logger.LogUserAction("Executing..."); - ArrayList mipengineAlgorithms = getMIPEngineAlgorithms(logger); - ArrayList exaremeAlgorithms = getExaremeAlgorithms(logger); - ArrayList galaxyAlgorithms = getGalaxyWorkflows(logger); - - ArrayList algorithms = new ArrayList<>(); - - // Remove Exareme algorithms that exist in the Exareme2 - if (mipengineAlgorithms != null && exaremeAlgorithms != null){ - int old_exareme_algorithm_size = exaremeAlgorithms.size(); - - for (ExaremeAlgorithmDTO algorithm : mipengineAlgorithms) { - exaremeAlgorithms.removeIf(obj -> Objects.equals(obj.getName(), algorithm.getName())); - } - logger.LogUserAction("Removed "+ (old_exareme_algorithm_size - exaremeAlgorithms.size()) +" deprecated exareme algorithms"); - } - - if (exaremeAlgorithms != null) { - algorithms.addAll(exaremeAlgorithms); - logger.LogUserAction("Loaded " + exaremeAlgorithms.size() + " exareme algorithms"); - } else { - logger.LogUserAction("Fetching exareme algorithms failed"); - } - if (mipengineAlgorithms != null) { - algorithms.addAll(mipengineAlgorithms); - logger.LogUserAction("Loaded " + mipengineAlgorithms.size() + " mipengine algorithms"); - } else { - logger.LogUserAction("Fetching mipengine algorithms failed"); - } - if (galaxyAlgorithms != null) { - algorithms.addAll(galaxyAlgorithms); - logger.LogUserAction("Loaded " + galaxyAlgorithms.size() + " galaxy algorithms"); - } else { - logger.LogUserAction("Fetching galaxy workflows failed"); - } - - List disabledAlgorithms = new ArrayList<>(); - try { - disabledAlgorithms = getDisabledAlgorithms(); - } catch (IOException e) { - logger.LogUserAction("The disabled algorithms could not be loaded."); - } - - // Remove any disabled algorithm - ArrayList allowedAlgorithms = new ArrayList<>(); - for (ExaremeAlgorithmDTO algorithm : algorithms) { - if (!disabledAlgorithms.contains(algorithm.getName())) { - allowedAlgorithms.add(algorithm); - } - } - - logger.LogUserAction("Removed "+ (algorithms.size() - allowedAlgorithms.size()) +" disabled algorithms"); + List allowedAlgorithms = algorithmService.getAlgorithms(logger); logger.LogUserAction("Successfully listed " + allowedAlgorithms.size() + " algorithms"); return ResponseEntity.ok(allowedAlgorithms); } - - /** - * This method gets all the available exareme algorithms and - * - * @return a list of AlgorithmDTOs or null if something fails - */ - public ArrayList getExaremeAlgorithms(Logger logger) { - ArrayList algorithms; - // Get exareme algorithms - try { - StringBuilder response = new StringBuilder(); - HTTPUtil.sendGet(exaremeAlgorithmsUrl, response); - algorithms = gson.fromJson( - response.toString(), - new TypeToken>() { - }.getType() - ); - } catch (Exception e) { - logger.LogUserAction("An exception occurred: " + e.getMessage()); - return null; - } - - logger.LogUserAction("Completed, returned " + algorithms.size() + " Exareme algorithms."); - return algorithms; - } - - /** - * This method gets all the available mipengine algorithms and - * - * @return a list of AlgorithmDTOs or null if something fails - */ - public ArrayList getMIPEngineAlgorithms(Logger logger) { - ArrayList mipEngineAlgorithms; - // Get MIPEngine algorithms - try { - StringBuilder response = new StringBuilder(); - HTTPUtil.sendGet(mipengineAlgorithmsUrl, response); - mipEngineAlgorithms = gson.fromJson( - response.toString(), - new TypeToken>() { - }.getType() - ); - } catch (Exception e) { - logger.LogUserAction("An exception occurred: " + e.getMessage()); - return null; - } - - ArrayList algorithms = new ArrayList<>(); - mipEngineAlgorithms.forEach(mipEngineAlgorithm -> algorithms.add(new ExaremeAlgorithmDTO(mipEngineAlgorithm))); - - logger.LogUserAction("Completed, returned " + algorithms.size() + " Exareme2 algorithms."); - return algorithms; - } - - /** - * This method gets all the available galaxy workflows, converts them into algorithms and - * - * @return a list of AlgorithmDTOs or null if something fails - */ - public ArrayList getGalaxyWorkflows(Logger logger) { - List workflowList; - try { - // Get all the workflows with the galaxy client - final GalaxyInstance instance = GalaxyInstanceFactory.get(galaxyUrl, galaxyApiKey); - final WorkflowsClient workflowsClient = instance.getWorkflowsClient(); - - workflowList = new ArrayList<>(workflowsClient.getWorkflows()); - } catch (Exception e) { - logger.LogUserAction("Error when calling list galaxy workflows: " + e.getMessage()); - return null; - } - - // Get the workflow details with the custom client to receive them as a WorkflowDTO - List workflows = new ArrayList<>(); - // Create the request client - RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); - for (Workflow workflow : workflowList) { - // Call Galaxy to run the workflow - Call call = service.getWorkflowFromGalaxy(workflow.getId(), galaxyApiKey); - try { - Response response = call.execute(); - - if (response.code() == 200) { // Call succeeded - workflows.add(response.body()); - - } else { // Something unexpected happened - String msgErr = gson.toJson(response.errorBody()); - logger.LogUserAction("Error Response: " + msgErr); - return null; - } - } catch (Exception e) { - logger.LogUserAction("An exception occurred: " + e.getMessage()); - return null; - } - } - logger.LogUserAction("Workflows fetched: " + workflows.size()); - - // Convert the workflows to algorithms - ArrayList algorithms = new ArrayList<>(); - for (WorkflowDTO workflow : workflows) { - logger.LogUserAction("Converting workflow: " + workflow); - - algorithms.add(workflow.convertToAlgorithmDTO()); - - logger.LogUserAction("Converted algorithm: " + algorithms.get(algorithms.size() - 1)); - } - - logger.LogUserAction("Completed!"); - return algorithms; - } - - private final CustomResourceLoader resourceLoader; - - /** - * Fetches the disabled algorithms from a .json file - * - * @return a list with their names - * @throws IOException when the file could not be loaded - */ - List getDisabledAlgorithms() throws IOException { - - Resource resource = resourceLoader.getResource(disabledAlgorithmsFilePath); - - return gson.fromJson(convertInputStreamToString( - resource.getInputStream()), - new TypeToken>() { - }.getType() - ); - } } diff --git a/src/main/java/eu/hbp/mip/services/AlgorithmService.java b/src/main/java/eu/hbp/mip/services/AlgorithmService.java new file mode 100644 index 00000000..5388b8d5 --- /dev/null +++ b/src/main/java/eu/hbp/mip/services/AlgorithmService.java @@ -0,0 +1,269 @@ +package eu.hbp.mip.services; + +import com.github.jmchilton.blend4j.galaxy.GalaxyInstance; +import com.github.jmchilton.blend4j.galaxy.GalaxyInstanceFactory; +import com.github.jmchilton.blend4j.galaxy.WorkflowsClient; +import com.github.jmchilton.blend4j.galaxy.beans.Workflow; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import eu.hbp.mip.controllers.galaxy.retrofit.RetroFitGalaxyClients; +import eu.hbp.mip.controllers.galaxy.retrofit.RetrofitClientInstance; +import eu.hbp.mip.models.DTOs.ExaremeAlgorithmDTO; +import eu.hbp.mip.models.DTOs.MIPEngineAlgorithmDTO; +import eu.hbp.mip.models.galaxy.WorkflowDTO; +import eu.hbp.mip.utils.CustomResourceLoader; +import eu.hbp.mip.utils.Exceptions.BadRequestException; +import eu.hbp.mip.utils.HTTPUtil; +import eu.hbp.mip.utils.Logger; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Service; +import retrofit2.Call; +import retrofit2.Response; + +import java.io.IOException; +import java.util.*; + +import static eu.hbp.mip.utils.InputStreamConverter.convertInputStreamToString; + +@Service +public class AlgorithmService { + + private static final Gson gson = new Gson(); + + private long algorithmsUpdated = 0; + private List algorithmDTOS = new ArrayList<>(); + + @Value("#{'${services.algorithmsUpdateInterval}'}") + private int algorithmsUpdateInterval; + + @Value("#{'${services.mipengine.algorithmsUrl}'}") + private String mipengineAlgorithmsUrl; + + @Value("#{'${services.exareme.algorithmsUrl}'}") + private String exaremeAlgorithmsUrl; + + @Value("#{'${services.galaxy.galaxyUrl}'}") + private String galaxyUrl; + + @Value("#{'${services.galaxy.galaxyApiKey}'}") + private String galaxyApiKey; + + @Value("#{'${files.disabledAlgorithms_json}'}") + private String disabledAlgorithmsFilePath; + + public AlgorithmService(CustomResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + + public ArrayList getAlgorithms(Logger logger) { + ArrayList mipengineAlgorithms = getMIPEngineAlgorithms(logger); + ArrayList exaremeAlgorithms = getExaremeAlgorithms(logger); + ArrayList galaxyAlgorithms = getGalaxyWorkflows(logger); + + ArrayList algorithms = new ArrayList<>(); + + // Remove Exareme algorithms that exist in the Exareme2 + if (mipengineAlgorithms != null && exaremeAlgorithms != null){ + int old_exareme_algorithm_size = exaremeAlgorithms.size(); + + for (ExaremeAlgorithmDTO algorithm : mipengineAlgorithms) { + exaremeAlgorithms.removeIf(obj -> Objects.equals(obj.getName(), algorithm.getName())); + } + logger.LogUserAction("Removed "+ (old_exareme_algorithm_size - exaremeAlgorithms.size()) +" deprecated exareme algorithms"); + } + + if (exaremeAlgorithms != null) { + algorithms.addAll(exaremeAlgorithms); + logger.LogUserAction("Loaded " + exaremeAlgorithms.size() + " exareme algorithms"); + } else { + logger.LogUserAction("Fetching exareme algorithms failed"); + } + if (mipengineAlgorithms != null) { + algorithms.addAll(mipengineAlgorithms); + logger.LogUserAction("Loaded " + mipengineAlgorithms.size() + " mipengine algorithms"); + } else { + logger.LogUserAction("Fetching mipengine algorithms failed"); + } + if (galaxyAlgorithms != null) { + algorithms.addAll(galaxyAlgorithms); + logger.LogUserAction("Loaded " + galaxyAlgorithms.size() + " galaxy algorithms"); + } else { + logger.LogUserAction("Fetching galaxy workflows failed"); + } + + List disabledAlgorithms = new ArrayList<>(); + try { + disabledAlgorithms = getDisabledAlgorithms(); + } catch (IOException e) { + logger.LogUserAction("The disabled algorithms could not be loaded."); + } + + // Remove any disabled algorithm + ArrayList allowedAlgorithms = new ArrayList<>(); + for (ExaremeAlgorithmDTO algorithm : algorithms) { + if (!disabledAlgorithms.contains(algorithm.getName())) { + allowedAlgorithms.add(algorithm); + } + } + + logger.LogUserAction("Removed "+ (algorithms.size() - allowedAlgorithms.size()) +" disabled algorithms"); + + this.algorithmsUpdated = System.currentTimeMillis(); + this.algorithmDTOS = allowedAlgorithms; + + return allowedAlgorithms; + } + + private boolean areAlgorithmsOutDated (){ + if (this.algorithmsUpdated == 0) return true; + return (int)((System.currentTimeMillis() - this.algorithmsUpdated) / 1000) > algorithmsUpdateInterval; + } + + public String getEngineName(Logger logger, String algorithmName){ + if(areAlgorithmsOutDated()) getAlgorithms(logger); + Optional exaremeAlgorithmDTO = this.algorithmDTOS.stream().filter(algorithmDTO -> algorithmDTO.getName().equals(algorithmName)).findAny(); + if (exaremeAlgorithmDTO.isPresent()) return getEngineNameForSpecificAlgorithm(exaremeAlgorithmDTO.get()); + else throw new BadRequestException("Algorithm: " + algorithmName + " does not exist."); + } + + private String getEngineNameForSpecificAlgorithm(ExaremeAlgorithmDTO algorithmDTO){ + switch (algorithmDTO.getType()) { + case "mipengine": + return "MIP-Engine"; + case "workflow": + return "Galaxy"; + default: + return "Exareme"; + } + } + + /** + * This method gets all the available exareme algorithms and + * + * @return a list of AlgorithmDTOs or null if something fails + */ + public ArrayList getExaremeAlgorithms(Logger logger) { + ArrayList algorithms; + // Get exareme algorithms + try { + StringBuilder response = new StringBuilder(); + HTTPUtil.sendGet(exaremeAlgorithmsUrl, response); + algorithms = gson.fromJson( + response.toString(), + new TypeToken>() { + }.getType() + ); + } catch (Exception e) { + logger.LogUserAction("An exception occurred: " + e.getMessage()); + return null; + } + + logger.LogUserAction("Completed, returned " + algorithms.size() + " Exareme algorithms."); + return algorithms; + } + + /** + * This method gets all the available mipengine algorithms and + * + * @return a list of AlgorithmDTOs or null if something fails + */ + public ArrayList getMIPEngineAlgorithms(Logger logger) { + ArrayList mipEngineAlgorithms; + // Get MIPEngine algorithms + try { + StringBuilder response = new StringBuilder(); + HTTPUtil.sendGet(mipengineAlgorithmsUrl, response); + mipEngineAlgorithms = gson.fromJson( + response.toString(), + new TypeToken>() { + }.getType() + ); + } catch (Exception e) { + logger.LogUserAction("An exception occurred: " + e.getMessage()); + return null; + } + + ArrayList algorithms = new ArrayList<>(); + mipEngineAlgorithms.forEach(mipEngineAlgorithm -> algorithms.add(new ExaremeAlgorithmDTO(mipEngineAlgorithm))); + + logger.LogUserAction("Completed, returned " + algorithms.size() + " Exareme2 algorithms."); + return algorithms; + } + + /** + * This method gets all the available galaxy workflows, converts them into algorithms and + * + * @return a list of AlgorithmDTOs or null if something fails + */ + public ArrayList getGalaxyWorkflows(Logger logger) { + List workflowList; + try { + // Get all the workflows with the galaxy client + final GalaxyInstance instance = GalaxyInstanceFactory.get(galaxyUrl, galaxyApiKey); + final WorkflowsClient workflowsClient = instance.getWorkflowsClient(); + + workflowList = new ArrayList<>(workflowsClient.getWorkflows()); + } catch (Exception e) { + logger.LogUserAction("Error when calling list galaxy workflows: " + e.getMessage()); + return null; + } + + // Get the workflow details with the custom client to receive them as a WorkflowDTO + List workflows = new ArrayList<>(); + // Create the request client + RetroFitGalaxyClients service = RetrofitClientInstance.getRetrofitInstance().create(RetroFitGalaxyClients.class); + for (Workflow workflow : workflowList) { + // Call Galaxy to run the workflow + Call call = service.getWorkflowFromGalaxy(workflow.getId(), galaxyApiKey); + try { + Response response = call.execute(); + + if (response.code() == 200) { // Call succeeded + workflows.add(response.body()); + + } else { // Something unexpected happened + String msgErr = gson.toJson(response.errorBody()); + logger.LogUserAction("Error Response: " + msgErr); + return null; + } + } catch (Exception e) { + logger.LogUserAction("An exception occurred: " + e.getMessage()); + return null; + } + } + logger.LogUserAction("Workflows fetched: " + workflows.size()); + + // Convert the workflows to algorithms + ArrayList algorithms = new ArrayList<>(); + for (WorkflowDTO workflow : workflows) { + logger.LogUserAction("Converting workflow: " + workflow); + + algorithms.add(workflow.convertToAlgorithmDTO()); + + logger.LogUserAction("Converted algorithm: " + algorithms.get(algorithms.size() - 1)); + } + + logger.LogUserAction("Completed!"); + return algorithms; + } + + private final CustomResourceLoader resourceLoader; + + /** + * Fetches the disabled algorithms from a .json file + * + * @return a list with their names + * @throws IOException when the file could not be loaded + */ + List getDisabledAlgorithms() throws IOException { + + Resource resource = resourceLoader.getResource(disabledAlgorithmsFilePath); + + return gson.fromJson(convertInputStreamToString( + resource.getInputStream()), + new TypeToken>() { + }.getType() + ); + } +} diff --git a/src/main/java/eu/hbp/mip/services/ExperimentService.java b/src/main/java/eu/hbp/mip/services/ExperimentService.java index a1c16a54..5aa6b09c 100644 --- a/src/main/java/eu/hbp/mip/services/ExperimentService.java +++ b/src/main/java/eu/hbp/mip/services/ExperimentService.java @@ -41,10 +41,12 @@ public class ExperimentService { private static final Gson gson = new Gson(); private final ActiveUserService activeUserService; + private final AlgorithmService algorithmService; private final GalaxyService galaxyService; private final ExperimentRepository experimentRepository; - public ExperimentService(ActiveUserService activeUserService, GalaxyService galaxyService, ExperimentRepository experimentRepository) { + public ExperimentService(ActiveUserService activeUserService, AlgorithmService algorithmService, GalaxyService galaxyService, ExperimentRepository experimentRepository) { + this.algorithmService = algorithmService; this.activeUserService = activeUserService; this.galaxyService = galaxyService; this.experimentRepository = experimentRepository; @@ -58,7 +60,7 @@ public ExperimentService(ActiveUserService activeUserService, GalaxyService gala * @param shared is optional, in case it is required to filter the experiments by shared * @param viewed is optional, in case it is required to filter the experiments by viewed * @param includeShared is optional, in case it is required to retrieve the experiment that is shared - * @param page is the page that is required to be retrieve + * @param page is the page that is required to be retrieved * @param size is the size of each page * @param orderBy is the column that is required to ordered by * @param descending is a boolean to determine if the experiments will be order by descending or ascending @@ -152,13 +154,10 @@ public ExperimentDTO createExperiment(Authentication authentication, ExperimentD //Checking if check (POST) /experiments has proper input. checkPostExperimentProperInput(experimentDTO, logger); - // Get the type of algorithm - String algorithmType = experimentDTO.getAlgorithm().getType(); - if (algorithmType == null) { - logger.LogUserAction("Please provide algorithm type."); - throw new BadRequestException("Please provide algorithm type."); - } + // Get the engine name from algorithmService + String engineName = algorithmService.getEngineName(logger, experimentDTO.getAlgorithm().getName().toUpperCase()); + logger.LogUserAction("Algorithm runs on " + engineName + "."); algorithmParametersLogging(experimentDTO, logger); @@ -168,12 +167,10 @@ public ExperimentDTO createExperiment(Authentication authentication, ExperimentD } // Run with the appropriate engine - if (algorithmType.equals("workflow")) { - logger.LogUserAction("Algorithm runs on Galaxy."); + if (engineName.equals("Galaxy")) { return galaxyService.createGalaxyExperiment(experimentDTO, logger); } else { - logger.LogUserAction("Algorithm runs on Exareme."); - return createSynchronousExperiment(experimentDTO, logger); + return createSynchronousExperiment(experimentDTO, engineName, logger); } } @@ -190,12 +187,13 @@ public ExperimentDTO runTransientExperiment(Authentication authentication, Exper //Checking if check (POST) /experiments has proper input. checkPostExperimentProperInput(experimentDTO, logger); - // Get the type of algorithm - String algorithmType = experimentDTO.getAlgorithm().getType(); + // Get the engine name from algorithmService + String engineName = algorithmService.getEngineName(logger, experimentDTO.getAlgorithm().getName().toUpperCase()); + experimentDTO.setUuid(UUID.randomUUID()); - if (algorithmType.equals("workflow")) { + if (engineName.equals("Galaxy")) { logger.LogUserAction("You can not run workflow algorithms transiently."); throw new BadRequestException("You can not run workflow algorithms transiently."); } @@ -209,7 +207,7 @@ public ExperimentDTO runTransientExperiment(Authentication authentication, Exper logger.LogUserAction("Completed, returning: " + experimentDTO); - ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(experimentDTO, logger); + ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(experimentDTO, engineName, logger); logger.LogUserAction( "Experiment with uuid: " + experimentDTO.getUuid() @@ -258,7 +256,7 @@ public ExperimentDTO updateExperiment(String uuid, ExperimentDTO experimentDTO, try { experimentRepository.save(experimentDAO); } catch (Exception e) { - logger.LogUserAction("Attempted to save changes to database but an error ocurred : " + e.getMessage() + "."); + logger.LogUserAction("Attempted to save changes to database but an error occurred : " + e.getMessage() + "."); throw new InternalServerError(e.getMessage()); } @@ -287,7 +285,7 @@ public void deleteExperiment(String uuid, Logger logger) { try { experimentRepository.delete(experimentDAO); } catch (Exception e) { - logger.LogUserAction("Attempted to delete an experiment to database but an error ocurred : " + e.getMessage() + "."); + logger.LogUserAction("Attempted to delete an experiment to database but an error occurred : " + e.getMessage() + "."); throw new InternalServerError(e.getMessage()); } @@ -400,7 +398,7 @@ private String getDatasetFromExperimentParameters(ExperimentDTO experimentDTO, L * @param logger contains username and the endpoint. * @return the experiment information that was retrieved from exareme */ - private ExperimentDTO createSynchronousExperiment(ExperimentDTO experimentDTO, Logger logger) { + private ExperimentDTO createSynchronousExperiment(ExperimentDTO experimentDTO, String engineName, Logger logger) { logger.LogUserAction("Running the algorithm..."); @@ -413,7 +411,7 @@ private ExperimentDTO createSynchronousExperiment(ExperimentDTO experimentDTO, L new Thread(() -> { try { - ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(finalExperimentDTO, logger); + ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(finalExperimentDTO, engineName, logger); logger.LogUserAction( "Experiment with uuid: " + experimentDAO.getUuid() @@ -444,9 +442,8 @@ private ExperimentDTO createSynchronousExperiment(ExperimentDTO experimentDTO, L * @param experimentDTO is the request with the experiment information * @return the result of experiment as well as the http status that was retrieved */ - private ExaremeAlgorithmResultDTO runSynchronousExperiment(ExperimentDTO experimentDTO, Logger logger) { - String algorithmType = experimentDTO.getAlgorithm().getType(); - if (algorithmType.equals("mipengine")) { + private ExaremeAlgorithmResultDTO runSynchronousExperiment(ExperimentDTO experimentDTO, String engineName, Logger logger) { + if (engineName.equals("MIP-Engine")) { return runMIPEngineExperiment(experimentDTO, logger); } else { return runExaremeExperiment(experimentDTO, logger); @@ -485,9 +482,9 @@ private ExaremeAlgorithmResultDTO runExaremeExperiment(ExperimentDTO experimentD logger.LogUserAction("Exareme algorithm execution. Body: " + algorithmBody); StringBuilder requestResponseBody = new StringBuilder(); - int requestReponseCode; + int requestResponseCode; try { - requestReponseCode = HTTPUtil.sendPost(algorithmEndpoint, algorithmBody, requestResponseBody); + requestResponseCode = HTTPUtil.sendPost(algorithmEndpoint, algorithmBody, requestResponseBody); } catch (Exception e) { throw new InternalServerError("Error occurred : " + e.getMessage()); } @@ -496,7 +493,7 @@ private ExaremeAlgorithmResultDTO runExaremeExperiment(ExperimentDTO experimentD ExaremeAlgorithmResultDTO exaremeResult = JsonConverters.convertJsonStringToObject( String.valueOf(requestResponseBody), ExaremeAlgorithmResultDTO.class ); - exaremeResult.setCode(requestReponseCode); + exaremeResult.setCode(requestResponseCode); return exaremeResult; } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e301494d..07f86b5d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -34,6 +34,7 @@ spring: ### EXTERNAL SERVICES ### services: + algorithmsUpdateInterval: 10 mipengine: algorithmsUrl: "http://127.0.0.1:5000/algorithms" attributesUrl: "http://127.0.0.1:5000/data_models_attributes"