Skip to content

Commit

Permalink
[backend] wip
Browse files Browse the repository at this point in the history
  • Loading branch information
savacano28 committed Nov 6, 2024
1 parent 759b0a7 commit cbeb0e9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -90,58 +88,7 @@ public List<ExerciseSimple> exercises() {
currentUser().isAdmin()
? exerciseRepository.rawAll()
: exerciseRepository.rawAllGranted(currentUser().getId());

// -- MAP TO GENERATE TARGETSIMPLEs
List<String> exerciseIds =
exercises.stream().map(exercise -> exercise.getExercise_id()).toList();

Map<String, List<Object[]>> teamMap =
teamRepository.teamsByExerciseIds(exerciseIds).stream()
.collect(Collectors.groupingBy(row -> (String) row[0]));

Map<String, List<Object[]>> assetMap =
assetRepository.assetsByExerciseIds(exerciseIds).stream()
.collect(Collectors.groupingBy(row -> (String) row[0]));

Map<String, List<Object[]>> assetGroupMap =
assetGroupRepository.assetGroupsByExerciseIds(exerciseIds).stream()
.collect(Collectors.groupingBy(row -> (String) row[0]));

List<ExerciseSimple> 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<TargetSimple> 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<ExerciseSimple> exercises(
Expand Down Expand Up @@ -523,19 +470,8 @@ private void getObjectives(Exercise duplicatedExercise, Exercise originalExercis

// -- ScenarioExercise--
public Iterable<ExerciseSimple> 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<RawExerciseSimple> exercises = exerciseRepository.rawAllByScenarioId(List.of(scenarioId));
return exerciseMapper.getExerciseSimples(exercises);
}

public List<InjectMapper.ExpectationResultsByType> getGlobalResults(@NotBlank String exerciseId) {
Expand Down
70 changes: 70 additions & 0 deletions openbas-api/src/main/java/io/openbas/utils/ExerciseMapper.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
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;

@RequiredArgsConstructor
@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());
Expand All @@ -38,4 +54,58 @@ public ExerciseSimple fromRawExerciseSimple(RawExerciseSimple rawExercise) {

return simple;
}

public List<ExerciseSimple> getExerciseSimples(List<RawExerciseSimple> exercises) {
// -- MAP TO GENERATE TARGETSIMPLEs
List<String> exerciseIds =
exercises.stream().map(exercise -> exercise.getExercise_id()).toList();

Map<String, List<Object[]>> teamMap =
teamRepository.teamsByExerciseIds(exerciseIds).stream()
.collect(Collectors.groupingBy(row -> (String) row[0]));

Map<String, List<Object[]>> assetMap =
assetRepository.assetsByExerciseIds(exerciseIds).stream()
.collect(Collectors.groupingBy(row -> (String) row[0]));

Map<String, List<Object[]>> assetGroupMap =
assetGroupRepository.assetGroupsByExerciseIds(exerciseIds).stream()
.collect(Collectors.groupingBy(row -> (String) row[0]));

List<ExerciseSimple> 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<TargetSimple> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ Iterable<RawGlobalInjectExpectation> rawGrantedInjectExpectationResultsFromDate(
+ "WHERE s.scenario_id IN (:scenarioIds) "
+ "GROUP BY ex.exercise_id ;",
nativeQuery = true)
Set<RawExerciseSimple> rawAllByScenarioId(@Param("scenarioIds") List<String> scenarioIds);
List<RawExerciseSimple> rawAllByScenarioId(@Param("scenarioIds") List<String> scenarioIds);

// -- TEAM --

Expand Down

0 comments on commit cbeb0e9

Please sign in to comment.