From cbeb0e9ccc7d1a73da4f9fc9d641ab35a5b6c035 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Wed, 6 Nov 2024 13:52:45 +0100 Subject: [PATCH] [backend] wip --- .../exercise/service/ExerciseService.java | 70 +------------------ .../java/io/openbas/utils/ExerciseMapper.java | 70 +++++++++++++++++++ .../repository/ExerciseRepository.java | 2 +- 3 files changed, 74 insertions(+), 68 deletions(-) diff --git a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java index 6cdfc4a34f..cb55e4326b 100644 --- a/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java +++ b/openbas-api/src/main/java/io/openbas/rest/exercise/service/ExerciseService.java @@ -16,7 +16,6 @@ import io.openbas.database.model.*; import io.openbas.database.raw.*; import io.openbas.database.repository.*; -import io.openbas.rest.atomic_testing.form.TargetSimple; import io.openbas.rest.exception.ElementNotFoundException; import io.openbas.rest.exercise.form.ExerciseSimple; import io.openbas.rest.inject.service.InjectDuplicateService; @@ -36,7 +35,6 @@ import java.time.Instant; import java.util.*; import java.util.stream.Collectors; -import java.util.stream.Stream; import lombok.RequiredArgsConstructor; import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; @@ -90,58 +88,7 @@ public List exercises() { currentUser().isAdmin() ? exerciseRepository.rawAll() : exerciseRepository.rawAllGranted(currentUser().getId()); - - // -- MAP TO GENERATE TARGETSIMPLEs - List exerciseIds = - exercises.stream().map(exercise -> exercise.getExercise_id()).toList(); - - Map> teamMap = - teamRepository.teamsByExerciseIds(exerciseIds).stream() - .collect(Collectors.groupingBy(row -> (String) row[0])); - - Map> assetMap = - assetRepository.assetsByExerciseIds(exerciseIds).stream() - .collect(Collectors.groupingBy(row -> (String) row[0])); - - Map> assetGroupMap = - assetGroupRepository.assetGroupsByExerciseIds(exerciseIds).stream() - .collect(Collectors.groupingBy(row -> (String) row[0])); - - List exerciseSimples = new ArrayList<>(); - - for (RawExerciseSimple exercise : exercises) { - ExerciseSimple simple = exerciseMapper.fromRawExerciseSimple(exercise); - - if (exercise.getInject_ids() != null) { - // -- GLOBAL SCORE --- - simple.setExpectationResultByTypes(resultUtils.getResultsByTypes(exercise.getInject_ids())); - - // -- TARGETS -- - List allTargets = - Stream.concat( - injectMapper - .toTargetSimple( - teamMap.getOrDefault(simple.getId(), emptyList()), TargetType.TEAMS) - .stream(), - Stream.concat( - injectMapper - .toTargetSimple( - assetMap.getOrDefault(simple.getId(), emptyList()), - TargetType.ASSETS) - .stream(), - injectMapper - .toTargetSimple( - assetGroupMap.getOrDefault(simple.getId(), emptyList()), - TargetType.ASSETS_GROUPS) - .stream())) - .collect(Collectors.toList()); - - simple.getTargets().addAll(allTargets); - exerciseSimples.add(simple); - } - } - - return exerciseSimples; + return exerciseMapper.getExerciseSimples(exercises); } public Page exercises( @@ -523,19 +470,8 @@ private void getObjectives(Exercise duplicatedExercise, Exercise originalExercis // -- ScenarioExercise-- public Iterable scenarioExercises(@NotBlank String scenarioId) { - return exerciseRepository.rawAllByScenarioId(List.of(scenarioId)).stream() - .map( - exercise -> { - ExerciseSimple simple = exerciseMapper.fromRawExerciseSimple(exercise); - - // Processed parameters - simple.setExpectationResultByTypes( - resultUtils.getResultsByTypes(exercise.getInject_ids())); - simple.setTargets(emptyList()); // TODO - - return simple; - }) - .toList(); + List exercises = exerciseRepository.rawAllByScenarioId(List.of(scenarioId)); + return exerciseMapper.getExerciseSimples(exercises); } public List getGlobalResults(@NotBlank String exerciseId) { diff --git a/openbas-api/src/main/java/io/openbas/utils/ExerciseMapper.java b/openbas-api/src/main/java/io/openbas/utils/ExerciseMapper.java index 243d86e452..f5f20ab0a2 100644 --- a/openbas-api/src/main/java/io/openbas/utils/ExerciseMapper.java +++ b/openbas-api/src/main/java/io/openbas/utils/ExerciseMapper.java @@ -1,11 +1,20 @@ package io.openbas.utils; +import static java.util.Collections.emptyList; + import io.openbas.database.model.ExerciseStatus; import io.openbas.database.model.Tag; import io.openbas.database.raw.RawExerciseSimple; +import io.openbas.database.raw.TargetType; +import io.openbas.database.repository.*; +import io.openbas.rest.atomic_testing.form.TargetSimple; import io.openbas.rest.exercise.form.ExerciseSimple; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; @@ -13,6 +22,13 @@ @Component public class ExerciseMapper { + private final AssetRepository assetRepository; + private final AssetGroupRepository assetGroupRepository; + private final TeamRepository teamRepository; + + private final ResultUtils resultUtils; + private final InjectMapper injectMapper; + public ExerciseSimple fromRawExerciseSimple(RawExerciseSimple rawExercise) { ExerciseSimple simple = new ExerciseSimple(); simple.setId(rawExercise.getExercise_id()); @@ -38,4 +54,58 @@ public ExerciseSimple fromRawExerciseSimple(RawExerciseSimple rawExercise) { return simple; } + + public List getExerciseSimples(List exercises) { + // -- MAP TO GENERATE TARGETSIMPLEs + List exerciseIds = + exercises.stream().map(exercise -> exercise.getExercise_id()).toList(); + + Map> teamMap = + teamRepository.teamsByExerciseIds(exerciseIds).stream() + .collect(Collectors.groupingBy(row -> (String) row[0])); + + Map> assetMap = + assetRepository.assetsByExerciseIds(exerciseIds).stream() + .collect(Collectors.groupingBy(row -> (String) row[0])); + + Map> assetGroupMap = + assetGroupRepository.assetGroupsByExerciseIds(exerciseIds).stream() + .collect(Collectors.groupingBy(row -> (String) row[0])); + + List exerciseSimples = new ArrayList<>(); + + for (RawExerciseSimple exercise : exercises) { + ExerciseSimple simple = fromRawExerciseSimple(exercise); + + if (exercise.getInject_ids() != null) { + // -- GLOBAL SCORE --- + simple.setExpectationResultByTypes(resultUtils.getResultsByTypes(exercise.getInject_ids())); + + // -- TARGETS -- + List allTargets = + Stream.concat( + injectMapper + .toTargetSimple( + teamMap.getOrDefault(simple.getId(), emptyList()), TargetType.TEAMS) + .stream(), + Stream.concat( + injectMapper + .toTargetSimple( + assetMap.getOrDefault(simple.getId(), emptyList()), + TargetType.ASSETS) + .stream(), + injectMapper + .toTargetSimple( + assetGroupMap.getOrDefault(simple.getId(), emptyList()), + TargetType.ASSETS_GROUPS) + .stream())) + .collect(Collectors.toList()); + + simple.getTargets().addAll(allTargets); + exerciseSimples.add(simple); + } + } + + return exerciseSimples; + } } diff --git a/openbas-model/src/main/java/io/openbas/database/repository/ExerciseRepository.java b/openbas-model/src/main/java/io/openbas/database/repository/ExerciseRepository.java index 9fab5c78fd..15805f7f70 100644 --- a/openbas-model/src/main/java/io/openbas/database/repository/ExerciseRepository.java +++ b/openbas-model/src/main/java/io/openbas/database/repository/ExerciseRepository.java @@ -301,7 +301,7 @@ Iterable rawGrantedInjectExpectationResultsFromDate( + "WHERE s.scenario_id IN (:scenarioIds) " + "GROUP BY ex.exercise_id ;", nativeQuery = true) - Set rawAllByScenarioId(@Param("scenarioIds") List scenarioIds); + List rawAllByScenarioId(@Param("scenarioIds") List scenarioIds); // -- TEAM --