Skip to content
Snippets Groups Projects
Commit 5d4f5510 authored by kfilippopolitis's avatar kfilippopolitis
Browse files

AlgorithmsAPI's functionality is now moved to a new Service layer.

Experiments now will run to their proper engines according to the get algorithms.
parent 7128f32f
No related branches found
No related tags found
1 merge request!52Experiments now will run to their proper engines according to the get algorithms.
...@@ -34,6 +34,7 @@ spring: ...@@ -34,6 +34,7 @@ spring:
### EXTERNAL SERVICES ### ### EXTERNAL SERVICES ###
services: services:
algorithmsUpdateInterval: {{ .Env.ALGORITHM_UPDATE_INTERVAL}}
mipengine: mipengine:
algorithmsUrl: {{ .Env.MIPENGINE_URL}}/algorithms algorithmsUrl: {{ .Env.MIPENGINE_URL}}/algorithms
attributesUrl: {{ .Env.MIPENGINE_URL}}/data_models_attributes attributesUrl: {{ .Env.MIPENGINE_URL}}/data_models_attributes
......
package eu.hbp.mip.controllers; 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.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.services.ActiveUserService;
import eu.hbp.mip.utils.CustomResourceLoader; import eu.hbp.mip.services.AlgorithmService;
import eu.hbp.mip.utils.HTTPUtil;
import eu.hbp.mip.utils.Logger; import eu.hbp.mip.utils.Logger;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; 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.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; 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.List;
import java.util.Objects;
import static eu.hbp.mip.utils.InputStreamConverter.convertInputStreamToString;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@RestController @RestController
...@@ -40,218 +20,21 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; ...@@ -40,218 +20,21 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
@Api(value = "/algorithms") @Api(value = "/algorithms")
public class AlgorithmsAPI { public class AlgorithmsAPI {
private static final Gson gson = new Gson(); private final AlgorithmService algorithmService;
private final ActiveUserService activeUserService; private final ActiveUserService activeUserService;
public AlgorithmsAPI(ActiveUserService activeUserService, AlgorithmService algorithmService) {
@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) {
this.activeUserService = activeUserService; this.activeUserService = activeUserService;
this.resourceLoader = resourceLoader; this.algorithmService = algorithmService;
} }
@ApiOperation(value = "List all algorithms", response = String.class) @ApiOperation(value = "List all algorithms", response = String.class)
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<ExaremeAlgorithmDTO>> getAlgorithms() { public ResponseEntity<List<ExaremeAlgorithmDTO>> getAlgorithms() {
Logger logger = new Logger(activeUserService.getActiveUser().getUsername(), "(GET) /algorithms"); Logger logger = new Logger(activeUserService.getActiveUser().getUsername(), "(GET) /algorithms");
logger.LogUserAction("Executing..."); logger.LogUserAction("Executing...");
ArrayList<ExaremeAlgorithmDTO> mipengineAlgorithms = getMIPEngineAlgorithms(logger); List<ExaremeAlgorithmDTO> allowedAlgorithms = algorithmService.getAlgorithms(logger);
ArrayList<ExaremeAlgorithmDTO> exaremeAlgorithms = getExaremeAlgorithms(logger);
ArrayList<ExaremeAlgorithmDTO> galaxyAlgorithms = getGalaxyWorkflows(logger);
ArrayList<ExaremeAlgorithmDTO> 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<String> disabledAlgorithms = new ArrayList<>();
try {
disabledAlgorithms = getDisabledAlgorithms();
} catch (IOException e) {
logger.LogUserAction("The disabled algorithms could not be loaded.");
}
// Remove any disabled algorithm
ArrayList<ExaremeAlgorithmDTO> allowedAlgorithms = new ArrayList<>();
for (ExaremeAlgorithmDTO algorithm : algorithms) {
if (!disabledAlgorithms.contains(algorithm.getName())) {
allowedAlgorithms.add(algorithm);
}
}
logger.LogUserAction("Removed "+ (algorithms.size() - allowedAlgorithms.size()) +" disabled algorithms");
logger.LogUserAction("Successfully listed " + allowedAlgorithms.size() + " algorithms"); logger.LogUserAction("Successfully listed " + allowedAlgorithms.size() + " algorithms");
return ResponseEntity.ok(allowedAlgorithms); 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<ExaremeAlgorithmDTO> getExaremeAlgorithms(Logger logger) {
ArrayList<ExaremeAlgorithmDTO> algorithms;
// Get exareme algorithms
try {
StringBuilder response = new StringBuilder();
HTTPUtil.sendGet(exaremeAlgorithmsUrl, response);
algorithms = gson.fromJson(
response.toString(),
new TypeToken<ArrayList<ExaremeAlgorithmDTO>>() {
}.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<ExaremeAlgorithmDTO> getMIPEngineAlgorithms(Logger logger) {
ArrayList<MIPEngineAlgorithmDTO> mipEngineAlgorithms;
// Get MIPEngine algorithms
try {
StringBuilder response = new StringBuilder();
HTTPUtil.sendGet(mipengineAlgorithmsUrl, response);
mipEngineAlgorithms = gson.fromJson(
response.toString(),
new TypeToken<ArrayList<MIPEngineAlgorithmDTO>>() {
}.getType()
);
} catch (Exception e) {
logger.LogUserAction("An exception occurred: " + e.getMessage());
return null;
}
ArrayList<ExaremeAlgorithmDTO> 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<ExaremeAlgorithmDTO> getGalaxyWorkflows(Logger logger) {
List<Workflow> 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<WorkflowDTO> 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<WorkflowDTO> call = service.getWorkflowFromGalaxy(workflow.getId(), galaxyApiKey);
try {
Response<WorkflowDTO> 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<ExaremeAlgorithmDTO> 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<String> getDisabledAlgorithms() throws IOException {
Resource resource = resourceLoader.getResource(disabledAlgorithmsFilePath);
return gson.fromJson(convertInputStreamToString(
resource.getInputStream()),
new TypeToken<List<String>>() {
}.getType()
);
}
} }
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<ExaremeAlgorithmDTO> 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<ExaremeAlgorithmDTO> getAlgorithms(Logger logger) {
ArrayList<ExaremeAlgorithmDTO> mipengineAlgorithms = getMIPEngineAlgorithms(logger);
ArrayList<ExaremeAlgorithmDTO> exaremeAlgorithms = getExaremeAlgorithms(logger);
ArrayList<ExaremeAlgorithmDTO> galaxyAlgorithms = getGalaxyWorkflows(logger);
ArrayList<ExaremeAlgorithmDTO> 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<String> disabledAlgorithms = new ArrayList<>();
try {
disabledAlgorithms = getDisabledAlgorithms();
} catch (IOException e) {
logger.LogUserAction("The disabled algorithms could not be loaded.");
}
// Remove any disabled algorithm
ArrayList<ExaremeAlgorithmDTO> 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> 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<ExaremeAlgorithmDTO> getExaremeAlgorithms(Logger logger) {
ArrayList<ExaremeAlgorithmDTO> algorithms;
// Get exareme algorithms
try {
StringBuilder response = new StringBuilder();
HTTPUtil.sendGet(exaremeAlgorithmsUrl, response);
algorithms = gson.fromJson(
response.toString(),
new TypeToken<ArrayList<ExaremeAlgorithmDTO>>() {
}.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<ExaremeAlgorithmDTO> getMIPEngineAlgorithms(Logger logger) {
ArrayList<MIPEngineAlgorithmDTO> mipEngineAlgorithms;
// Get MIPEngine algorithms
try {
StringBuilder response = new StringBuilder();
HTTPUtil.sendGet(mipengineAlgorithmsUrl, response);
mipEngineAlgorithms = gson.fromJson(
response.toString(),
new TypeToken<ArrayList<MIPEngineAlgorithmDTO>>() {
}.getType()
);
} catch (Exception e) {
logger.LogUserAction("An exception occurred: " + e.getMessage());
return null;
}
ArrayList<ExaremeAlgorithmDTO> 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<ExaremeAlgorithmDTO> getGalaxyWorkflows(Logger logger) {
List<Workflow> 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<WorkflowDTO> 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<WorkflowDTO> call = service.getWorkflowFromGalaxy(workflow.getId(), galaxyApiKey);
try {
Response<WorkflowDTO> 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<ExaremeAlgorithmDTO> 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<String> getDisabledAlgorithms() throws IOException {
Resource resource = resourceLoader.getResource(disabledAlgorithmsFilePath);
return gson.fromJson(convertInputStreamToString(
resource.getInputStream()),
new TypeToken<List<String>>() {
}.getType()
);
}
}
...@@ -41,10 +41,12 @@ public class ExperimentService { ...@@ -41,10 +41,12 @@ public class ExperimentService {
private static final Gson gson = new Gson(); private static final Gson gson = new Gson();
private final ActiveUserService activeUserService; private final ActiveUserService activeUserService;
private final AlgorithmService algorithmService;
private final GalaxyService galaxyService; private final GalaxyService galaxyService;
private final ExperimentRepository experimentRepository; 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.activeUserService = activeUserService;
this.galaxyService = galaxyService; this.galaxyService = galaxyService;
this.experimentRepository = experimentRepository; this.experimentRepository = experimentRepository;
...@@ -58,7 +60,7 @@ public class ExperimentService { ...@@ -58,7 +60,7 @@ public class ExperimentService {
* @param shared is optional, in case it is required to filter the experiments by shared * @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 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 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 size is the size of each page
* @param orderBy is the column that is required to ordered by * @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 * @param descending is a boolean to determine if the experiments will be order by descending or ascending
...@@ -152,13 +154,10 @@ public class ExperimentService { ...@@ -152,13 +154,10 @@ public class ExperimentService {
//Checking if check (POST) /experiments has proper input. //Checking if check (POST) /experiments has proper input.
checkPostExperimentProperInput(experimentDTO, logger); checkPostExperimentProperInput(experimentDTO, logger);
// Get the type of algorithm
String algorithmType = experimentDTO.getAlgorithm().getType();
if (algorithmType == null) { // Get the engine name from algorithmService
logger.LogUserAction("Please provide algorithm type."); String engineName = algorithmService.getEngineName(logger, experimentDTO.getAlgorithm().getName().toUpperCase());
throw new BadRequestException("Please provide algorithm type."); logger.LogUserAction("Algorithm runs on " + engineName + ".");
}
algorithmParametersLogging(experimentDTO, logger); algorithmParametersLogging(experimentDTO, logger);
...@@ -168,12 +167,10 @@ public class ExperimentService { ...@@ -168,12 +167,10 @@ public class ExperimentService {
} }
// Run with the appropriate engine // Run with the appropriate engine
if (algorithmType.equals("workflow")) { if (engineName.equals("Galaxy")) {
logger.LogUserAction("Algorithm runs on Galaxy.");
return galaxyService.createGalaxyExperiment(experimentDTO, logger); return galaxyService.createGalaxyExperiment(experimentDTO, logger);
} else { } else {
logger.LogUserAction("Algorithm runs on Exareme."); return createSynchronousExperiment(experimentDTO, engineName, logger);
return createSynchronousExperiment(experimentDTO, logger);
} }
} }
...@@ -190,12 +187,13 @@ public class ExperimentService { ...@@ -190,12 +187,13 @@ public class ExperimentService {
//Checking if check (POST) /experiments has proper input. //Checking if check (POST) /experiments has proper input.
checkPostExperimentProperInput(experimentDTO, logger); checkPostExperimentProperInput(experimentDTO, logger);
// Get the type of algorithm // Get the engine name from algorithmService
String algorithmType = experimentDTO.getAlgorithm().getType(); String engineName = algorithmService.getEngineName(logger, experimentDTO.getAlgorithm().getName().toUpperCase());
experimentDTO.setUuid(UUID.randomUUID()); experimentDTO.setUuid(UUID.randomUUID());
if (algorithmType.equals("workflow")) { if (engineName.equals("Galaxy")) {
logger.LogUserAction("You can not run workflow algorithms transiently."); logger.LogUserAction("You can not run workflow algorithms transiently.");
throw new BadRequestException("You can not run workflow algorithms transiently."); throw new BadRequestException("You can not run workflow algorithms transiently.");
} }
...@@ -209,7 +207,7 @@ public class ExperimentService { ...@@ -209,7 +207,7 @@ public class ExperimentService {
logger.LogUserAction("Completed, returning: " + experimentDTO); logger.LogUserAction("Completed, returning: " + experimentDTO);
ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(experimentDTO, logger); ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(experimentDTO, engineName, logger);
logger.LogUserAction( logger.LogUserAction(
"Experiment with uuid: " + experimentDTO.getUuid() "Experiment with uuid: " + experimentDTO.getUuid()
...@@ -258,7 +256,7 @@ public class ExperimentService { ...@@ -258,7 +256,7 @@ public class ExperimentService {
try { try {
experimentRepository.save(experimentDAO); experimentRepository.save(experimentDAO);
} catch (Exception e) { } 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()); throw new InternalServerError(e.getMessage());
} }
...@@ -287,7 +285,7 @@ public class ExperimentService { ...@@ -287,7 +285,7 @@ public class ExperimentService {
try { try {
experimentRepository.delete(experimentDAO); experimentRepository.delete(experimentDAO);
} catch (Exception e) { } 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()); throw new InternalServerError(e.getMessage());
} }
...@@ -400,7 +398,7 @@ public class ExperimentService { ...@@ -400,7 +398,7 @@ public class ExperimentService {
* @param logger contains username and the endpoint. * @param logger contains username and the endpoint.
* @return the experiment information that was retrieved from exareme * @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..."); logger.LogUserAction("Running the algorithm...");
...@@ -413,7 +411,7 @@ public class ExperimentService { ...@@ -413,7 +411,7 @@ public class ExperimentService {
new Thread(() -> { new Thread(() -> {
try { try {
ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(finalExperimentDTO, logger); ExaremeAlgorithmResultDTO exaremeAlgorithmResultDTO = runSynchronousExperiment(finalExperimentDTO, engineName, logger);
logger.LogUserAction( logger.LogUserAction(
"Experiment with uuid: " + experimentDAO.getUuid() "Experiment with uuid: " + experimentDAO.getUuid()
...@@ -444,9 +442,8 @@ public class ExperimentService { ...@@ -444,9 +442,8 @@ public class ExperimentService {
* @param experimentDTO is the request with the experiment information * @param experimentDTO is the request with the experiment information
* @return the result of experiment as well as the http status that was retrieved * @return the result of experiment as well as the http status that was retrieved
*/ */
private ExaremeAlgorithmResultDTO runSynchronousExperiment(ExperimentDTO experimentDTO, Logger logger) { private ExaremeAlgorithmResultDTO runSynchronousExperiment(ExperimentDTO experimentDTO, String engineName, Logger logger) {
String algorithmType = experimentDTO.getAlgorithm().getType(); if (engineName.equals("MIP-Engine")) {
if (algorithmType.equals("mipengine")) {
return runMIPEngineExperiment(experimentDTO, logger); return runMIPEngineExperiment(experimentDTO, logger);
} else { } else {
return runExaremeExperiment(experimentDTO, logger); return runExaremeExperiment(experimentDTO, logger);
...@@ -485,9 +482,9 @@ public class ExperimentService { ...@@ -485,9 +482,9 @@ public class ExperimentService {
logger.LogUserAction("Exareme algorithm execution. Body: " + algorithmBody); logger.LogUserAction("Exareme algorithm execution. Body: " + algorithmBody);
StringBuilder requestResponseBody = new StringBuilder(); StringBuilder requestResponseBody = new StringBuilder();
int requestReponseCode; int requestResponseCode;
try { try {
requestReponseCode = HTTPUtil.sendPost(algorithmEndpoint, algorithmBody, requestResponseBody); requestResponseCode = HTTPUtil.sendPost(algorithmEndpoint, algorithmBody, requestResponseBody);
} catch (Exception e) { } catch (Exception e) {
throw new InternalServerError("Error occurred : " + e.getMessage()); throw new InternalServerError("Error occurred : " + e.getMessage());
} }
...@@ -496,7 +493,7 @@ public class ExperimentService { ...@@ -496,7 +493,7 @@ public class ExperimentService {
ExaremeAlgorithmResultDTO exaremeResult = JsonConverters.convertJsonStringToObject( ExaremeAlgorithmResultDTO exaremeResult = JsonConverters.convertJsonStringToObject(
String.valueOf(requestResponseBody), ExaremeAlgorithmResultDTO.class String.valueOf(requestResponseBody), ExaremeAlgorithmResultDTO.class
); );
exaremeResult.setCode(requestReponseCode); exaremeResult.setCode(requestResponseCode);
return exaremeResult; return exaremeResult;
} }
......
...@@ -34,6 +34,7 @@ spring: ...@@ -34,6 +34,7 @@ spring:
### EXTERNAL SERVICES ### ### EXTERNAL SERVICES ###
services: services:
algorithmsUpdateInterval: 10
mipengine: mipengine:
algorithmsUrl: "http://127.0.0.1:5000/algorithms" algorithmsUrl: "http://127.0.0.1:5000/algorithms"
attributesUrl: "http://127.0.0.1:5000/data_models_attributes" attributesUrl: "http://127.0.0.1:5000/data_models_attributes"
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment