diff --git a/src/main/java/hbp/mip/algorithm/AlgorithmService.java b/src/main/java/hbp/mip/algorithm/AlgorithmService.java index 0e5a570ba89fd8ac779fc5bba3b029e5b12ad9df..5aa713623df7ea8e4c9b9863ba558632530475c0 100644 --- a/src/main/java/hbp/mip/algorithm/AlgorithmService.java +++ b/src/main/java/hbp/mip/algorithm/AlgorithmService.java @@ -7,16 +7,22 @@ import hbp.mip.utils.HTTPUtil; import hbp.mip.utils.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import static hbp.mip.utils.InputStreamConverter.convertInputStreamToString; @Service +@EnableScheduling public class AlgorithmService { private static final Gson gson = new Gson(); @@ -76,11 +82,29 @@ public class AlgorithmService { return Collections.emptyList(); } + // Filter out algorithms with type "flower" + algorithms = algorithms.stream() + .filter(algorithm -> "exareme2".equals(algorithm.type())) + .collect(Collectors.toList()); logger.debug("Fetched " + algorithms.size() + " exareme2 algorithms."); exareme2AlgorithmsSpecs.setAlgorithms(algorithms); return algorithms; } + @EnableAsync + public static class AlgorithmAggregator { + + private final AlgorithmService algorithmService; + + public AlgorithmAggregator(AlgorithmService algorithmService){ + this.algorithmService = algorithmService; + } + @Async + @Scheduled(fixedDelayString = "${services.algorithmsUpdateInterval}000") + public void scheduleFixedRateTaskAsync() { + algorithmService.getExareme2Algorithms(new Logger("AlgorithmAggregator","(GET) /algorithms")); + } + } /** * Fetches the disabled algorithms from a .json file * diff --git a/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmRequestDTO.java b/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmRequestDTO.java index 72d4c537ec5d8402857e651d47807050777c9ac5..9da4a1f468865e9c00c648eaf4e5de56f8796ca2 100644 --- a/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmRequestDTO.java +++ b/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmRequestDTO.java @@ -15,15 +15,33 @@ public record Exareme2AlgorithmRequestDTO( String type ) { - public Exareme2AlgorithmRequestDTO( + public static Exareme2AlgorithmRequestDTO create( UUID experimentUUID, List<ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO> exaremeAlgorithmRequestParamDTOs, List<ExperimentExecutionDTO.AlgorithmExecutionDTO.TransformerExecutionDTO> exaremeTransformers, Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO) { - this( + + // List of inputDataFields + List<String> inputDataFields = Arrays.asList("pathology", "dataset", "x", "y", "filter"); + + // Create lists to hold the separated DTOs + List<ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO> inputDataDTOs = new ArrayList<>(); + List<ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO> parametersDTOs = new ArrayList<>(); + + // Split the DTOs into the respective lists + for (ExperimentExecutionDTO.AlgorithmExecutionDTO.AlgorithmParameterExecutionDTO dto : exaremeAlgorithmRequestParamDTOs) { + if (inputDataFields.contains(dto.name())) { + inputDataDTOs.add(dto); + } else { + parametersDTOs.add(dto); + } + } + + // Call the constructor with the separated lists + return new Exareme2AlgorithmRequestDTO( experimentUUID.toString(), - getInputData(exaremeAlgorithmRequestParamDTOs), - getParameters(exaremeAlgorithmRequestParamDTOs, exareme2AlgorithmSpecificationDTO), + getInputData(inputDataDTOs), + getParameters(parametersDTOs, exareme2AlgorithmSpecificationDTO), getPreprocessing(exaremeTransformers, exareme2AlgorithmSpecificationDTO), "exareme2" ); @@ -73,15 +91,13 @@ public record Exareme2AlgorithmRequestDTO( return null; } - // The input_data fields should be ignored and shouldn't be added in the parameters. - List<String> inputDataFields = Arrays.asList("pathology", "dataset", "x", "y", "filter"); - HashMap<String, Object> exareme2Parameters = new HashMap<>(); exaremeAlgorithmRequestParamDTOs.forEach(parameter -> { - if (!inputDataFields.contains(parameter.name())){ - Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto = exareme2AlgorithmSpecificationDTO.parameters().get(parameter.name()); - exareme2Parameters.put(parameter.name(), convertStringToProperExareme2ParameterTypeAccordingToSpecs(parameter.value(), paramSpecDto)); + Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto = exareme2AlgorithmSpecificationDTO.parameters().get(parameter.name()); + if (paramSpecDto == null){ + throw new InternalServerError("Parameter " + parameter.name() + " not found in algorithm:" + exareme2AlgorithmSpecificationDTO.name()); } + exareme2Parameters.put(parameter.name(), convertStringToProperExareme2ParameterTypeAccordingToSpecs(parameter.value(), paramSpecDto)); }); return exareme2Parameters; } @@ -103,6 +119,9 @@ public record Exareme2AlgorithmRequestDTO( if (transformerSpecificationDTO.isEmpty()) throw new InternalServerError("Missing the transformer: " + transformer_name); Exareme2AlgorithmSpecificationDTO.Exareme2AlgorithmParameterSpecificationDTO paramSpecDto = transformerSpecificationDTO.get().parameters().get(param_name); + if (paramSpecDto == null){ + throw new InternalServerError("Parameter " + parameter.name() + " not found in transformer:" + transformerSpecificationDTO.get().name()); + } transformerParameterDTOs.put(param_name, convertStringToProperExareme2ParameterTypeAccordingToSpecs(parameter.value(), paramSpecDto)); } exareme2Preprocessing.put(transformer_name, transformerParameterDTOs); diff --git a/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmSpecificationDTO.java b/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmSpecificationDTO.java index 93708e3affeeb675db432f6b55c046daa32f5389..b987a7741c3b234b2d53a15a5e4ee94158da5e88 100644 --- a/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmSpecificationDTO.java +++ b/src/main/java/hbp/mip/algorithm/Exareme2AlgorithmSpecificationDTO.java @@ -13,7 +13,8 @@ public record Exareme2AlgorithmSpecificationDTO( String desc, Exareme2AlgorithmInputdataSpecificationDTO inputdata, Map<String, Exareme2AlgorithmParameterSpecificationDTO> parameters, - List<Exareme2TransformerSpecificationDTO> preprocessing + List<Exareme2TransformerSpecificationDTO> preprocessing, + String type ) { @Override public Map<String, Exareme2AlgorithmParameterSpecificationDTO> parameters() { diff --git a/src/main/java/hbp/mip/experiment/ExperimentService.java b/src/main/java/hbp/mip/experiment/ExperimentService.java index 17cb7f3b92a7502d080084dd23122fd4b00b0e32..ee530ad43d1e50aa8ca55c232d11261cfa1b82b6 100644 --- a/src/main/java/hbp/mip/experiment/ExperimentService.java +++ b/src/main/java/hbp/mip/experiment/ExperimentService.java @@ -341,7 +341,7 @@ public class ExperimentService { String algorithmName = experimentExecutionDTO.algorithm().name(); String algorithmEndpoint = exareme2AlgorithmsUrl + "/" + algorithmName; Exareme2AlgorithmSpecificationDTO exareme2AlgorithmSpecificationDTO = getAlgorithmSpec(algorithmName); - var exareme2AlgorithmRequestDTO = new Exareme2AlgorithmRequestDTO(uuid, experimentExecutionDTO.algorithm().parameters(), experimentExecutionDTO.algorithm().preprocessing(), exareme2AlgorithmSpecificationDTO); + var exareme2AlgorithmRequestDTO = Exareme2AlgorithmRequestDTO.create(uuid, experimentExecutionDTO.algorithm().parameters(), experimentExecutionDTO.algorithm().preprocessing(), exareme2AlgorithmSpecificationDTO); String algorithmBody = convertObjectToJsonString(exareme2AlgorithmRequestDTO); logger.debug("Exareme2 algorithm request, endpoint: " + algorithmEndpoint); logger.debug("Exareme2 algorithm request, body: " + algorithmBody);