From 4ff362ad3002bd624ae2b811cb015b15ad13201e Mon Sep 17 00:00:00 2001
From: "K.Filippopolitis" <56073635+KFilippopolitis@users.noreply.github.com>
Date: Wed, 24 Jul 2024 12:08:35 +0300
Subject: [PATCH] Redirect logs outside of console (#79)

* Redirect logs outside of console

* Log user log in and authorities.

* Re-implemented periodically executing a algorithm aggregator that was removed by accident.
For the time being we only allow exareme2 algorithms to the portalbackend.

* Minor refactoring to conversion of ExperimentExecutionDTO.AlgorithmExecutionDTO to Exareme2AlgorithmRequestDTO.
---
 .../hbp/mip/algorithm/AlgorithmService.java   | 24 ++++++++++++
 .../Exareme2AlgorithmRequestDTO.java          | 39 ++++++++++++++-----
 .../Exareme2AlgorithmSpecificationDTO.java    |  3 +-
 .../hbp/mip/experiment/ExperimentService.java |  2 +-
 4 files changed, 56 insertions(+), 12 deletions(-)

diff --git a/src/main/java/hbp/mip/algorithm/AlgorithmService.java b/src/main/java/hbp/mip/algorithm/AlgorithmService.java
index 0e5a570ba..5aa713623 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 72d4c537e..9da4a1f46 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 93708e3af..b987a7741 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 17cb7f3b9..ee530ad43 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);
-- 
GitLab