Skip to content

Commit

Permalink
[backend] simplify scenario statistics obtention
Browse files Browse the repository at this point in the history
  • Loading branch information
isselparra committed Dec 6, 2024
1 parent 45c9719 commit 8752c6c
Showing 1 changed file with 57 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import io.openbas.utils.AtomicTestingUtils.ResultDistribution;
import io.openbas.utils.ResultUtils;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.stream.Stream;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -23,115 +23,85 @@ public class ScenarioStatisticService {
private final ResultUtils resultUtils;

public ScenarioStatistic getStatistics(String scenarioId) {
return getLatestSimulationsStatistics(scenarioId);
}
List<RawFinishedExerciseWithInjects> orderedRawFinishedExercises =
getOrderedRawFinishedExercises(scenarioId);

private ScenarioStatistic getLatestSimulationsStatistics(String scenarioId) {
List<RawFinishedExerciseWithInjects> rawFinishedExercises =
exerciseRepository.rawLatestFinishedExercisesWithInjectsByScenarioId(scenarioId);
Collections.reverse(rawFinishedExercises);
Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> globalScoresByExpectationTypes =
getGlobalScoresByExpectationTypes(orderedRawFinishedExercises);

Map<ExpectationType, List<GlobalScoreBySimulationEndDate>>
initialGlobalScoresByExpectationType = new HashMap<>();
initialGlobalScoresByExpectationType.put(ExpectationType.PREVENTION, new ArrayList<>());
initialGlobalScoresByExpectationType.put(ExpectationType.DETECTION, new ArrayList<>());
initialGlobalScoresByExpectationType.put(ExpectationType.HUMAN_RESPONSE, new ArrayList<>());

Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> globalScoresByExpectationType =
rawFinishedExercises.stream()
.reduce(
initialGlobalScoresByExpectationType,
(scoresByType, rawFinishedExercise) ->
addGlobalScores(
scoresByType,
rawFinishedExercise,
resultUtils.getResultsByTypes(rawFinishedExercise.getInject_ids())),
getMapBinaryOperator());

return new ScenarioStatistic(new SimulationsResultsLatest(globalScoresByExpectationType));
return new ScenarioStatistic(new SimulationsResultsLatest(globalScoresByExpectationTypes));
}

private static Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> addGlobalScores(
Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> globalScoresByExpectationType,
RawFinishedExerciseWithInjects rawFinishedExercise,
List<ExpectationResultsByType> expectationResultsByType) {

updateGlobalScores(
globalScoresByExpectationType,
rawFinishedExercise,
expectationResultsByType,
ExpectationType.PREVENTION);

updateGlobalScores(
globalScoresByExpectationType,
rawFinishedExercise,
expectationResultsByType,
ExpectationType.DETECTION);

updateGlobalScores(
globalScoresByExpectationType,
rawFinishedExercise,
expectationResultsByType,
ExpectationType.HUMAN_RESPONSE);

return globalScoresByExpectationType;
private Map<ExpectationType, List<GlobalScoreBySimulationEndDate>>
getGlobalScoresByExpectationTypes(List<RawFinishedExerciseWithInjects> rawFinishedExercises) {
List<ExpectationTypeAndGlobalScore> allGlobalScores = getAllGlobalScores(rawFinishedExercises);

List<GlobalScoreBySimulationEndDate> preventionGlobalScores =
getGlobalScoresForExpectationType(allGlobalScores, ExpectationType.PREVENTION);
List<GlobalScoreBySimulationEndDate> detectionGlobalScores =
getGlobalScoresForExpectationType(allGlobalScores, ExpectationType.DETECTION);
List<GlobalScoreBySimulationEndDate> humanResponseGlobalScores =
getGlobalScoresForExpectationType(allGlobalScores, ExpectationType.HUMAN_RESPONSE);

return new HashMap<>(
Map.of(
ExpectationType.PREVENTION, preventionGlobalScores,
ExpectationType.DETECTION, detectionGlobalScores,
ExpectationType.HUMAN_RESPONSE, humanResponseGlobalScores));
}

private static void updateGlobalScores(
Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> globalScoresByExpectationType,
RawFinishedExerciseWithInjects rawFinishedExercise,
List<ExpectationResultsByType> expectationResultsByType,
ExpectationType expectationType) {
List<GlobalScoreBySimulationEndDate> globalScores =
getGlobalScoresBySimulationEndDates(
rawFinishedExercise, expectationResultsByType, expectationType);
updateGlobalScoresByExpectationType(
globalScoresByExpectationType, globalScores, expectationType);
private List<ExpectationTypeAndGlobalScore> getAllGlobalScores(
List<RawFinishedExerciseWithInjects> rawFinishedExercises) {
return rawFinishedExercises.stream().flatMap(this::getExpectationTypeAndGlobalScores).toList();
}

private static void updateGlobalScoresByExpectationType(
Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> globalScoresByType,
List<GlobalScoreBySimulationEndDate> globalScores,
ExpectationType expectationType) {
List<GlobalScoreBySimulationEndDate> previousGlobalScores =
globalScoresByType.getOrDefault(expectationType, new ArrayList<>());
previousGlobalScores.addAll(globalScores);
globalScoresByType.put(expectationType, previousGlobalScores);
private Stream<ExpectationTypeAndGlobalScore> getExpectationTypeAndGlobalScores(
RawFinishedExerciseWithInjects rawFinishedExercise) {
return resultUtils.getResultsByTypes(rawFinishedExercise.getInject_ids()).stream()
.map(
expectationResultByType ->
getExpectationTypeAndGlobalScore(rawFinishedExercise, expectationResultByType));
}

private static List<GlobalScoreBySimulationEndDate> getGlobalScoresBySimulationEndDates(
private static ExpectationTypeAndGlobalScore getExpectationTypeAndGlobalScore(
RawFinishedExerciseWithInjects rawFinishedExercise,
List<ExpectationResultsByType> expectationResultsByType,
ExpectationType expectationType) {
ExpectationResultsByType expectationResultByType) {
return new ExpectationTypeAndGlobalScore(
expectationResultByType.type(),
new GlobalScoreBySimulationEndDate(
rawFinishedExercise.getExercise_end_date(),
getPercentageOfInjectsOnSuccess(expectationResultByType)));
}

return expectationResultsByType.stream()
.filter(expectationResultByType -> expectationResultByType.type() == expectationType)
.map(
expectationResultByType ->
new GlobalScoreBySimulationEndDate(
rawFinishedExercise.getExercise_end_date(),
getPercentageOfInjectsOnSuccess(expectationResultByType)))
private static List<GlobalScoreBySimulationEndDate> getGlobalScoresForExpectationType(
List<ExpectationTypeAndGlobalScore> allGlobalScores, ExpectationType expectationType) {
return allGlobalScores.stream()
.filter(typeAndScore -> typeAndScore.expectationType == expectationType)
.map(typeAndScore -> typeAndScore.globalScoreBySimulationEndDate)
.toList();
}

private List<RawFinishedExerciseWithInjects> getOrderedRawFinishedExercises(String scenarioId) {
List<RawFinishedExerciseWithInjects> rawFinishedExercises =
exerciseRepository.rawLatestFinishedExercisesWithInjectsByScenarioId(scenarioId);
Collections.reverse(rawFinishedExercises);
return rawFinishedExercises;
}

private record ExpectationTypeAndGlobalScore(
ExpectationType expectationType,
GlobalScoreBySimulationEndDate globalScoreBySimulationEndDate) {}

private static float getPercentageOfInjectsOnSuccess(
ExpectationResultsByType expectationResultByType) {
if (expectationResultByType.distribution().isEmpty()) {
return 0;
}
var totalNumberOfInjects =
int totalNumberOfInjects =
expectationResultByType.distribution().stream()
.map(ResultDistribution::value)
.reduce(0, Integer::sum);
var numberOfInjectsOnSuccess = expectationResultByType.distribution().getFirst().value();
int numberOfInjectsOnSuccess = expectationResultByType.distribution().getFirst().value();
return (float) numberOfInjectsOnSuccess / totalNumberOfInjects;
}

private static BinaryOperator<Map<ExpectationType, List<GlobalScoreBySimulationEndDate>>>
getMapBinaryOperator() {
return (m1, m2) -> {
m1.putAll(m2);
return m1;
};
}
}

0 comments on commit 8752c6c

Please sign in to comment.