Skip to content

Commit

Permalink
[backend] add intermediate record to sort as stream
Browse files Browse the repository at this point in the history
  • Loading branch information
isselparra committed Dec 10, 2024
1 parent 1828c9e commit 5fd5f84
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.openbas.rest.scenario;

import java.time.Instant;
import java.util.Set;

public record FinishedExerciseWithInjects(Instant endDate, Set<String> injectIds)
implements Comparable<FinishedExerciseWithInjects> {
@Override
public int compareTo(FinishedExerciseWithInjects exercise) {
if (this.endDate.isBefore(exercise.endDate)) return 1;
if (this.endDate.isAfter(exercise.endDate)) return -1;
return 0;

Check warning on line 12 in openbas-api/src/main/java/io/openbas/rest/scenario/FinishedExerciseWithInjects.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/scenario/FinishedExerciseWithInjects.java#L12

Added line #L12 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.openbas.database.raw.RawFinishedExerciseWithInjects;
import io.openbas.database.repository.ExerciseRepository;
import io.openbas.expectation.ExpectationType;
import io.openbas.rest.scenario.FinishedExerciseWithInjects;
import io.openbas.rest.scenario.response.GlobalScoreBySimulationEndDate;
import io.openbas.rest.scenario.response.ScenarioStatistic;
import io.openbas.rest.scenario.response.SimulationsResultsLatest;
Expand Down Expand Up @@ -32,7 +33,7 @@ public ScenarioStatistic getStatistics(String scenarioId) {
}

private SimulationsResultsLatest getSimulationsResultsLatest(String scenarioId) {
List<RawFinishedExerciseWithInjects> orderedRawFinishedExercises =
List<FinishedExerciseWithInjects> orderedRawFinishedExercises =
getOrderedRawFinishedExercises(scenarioId);

Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> globalScoresByExpectationTypes =
Expand All @@ -42,7 +43,7 @@ private SimulationsResultsLatest getSimulationsResultsLatest(String scenarioId)
}

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

List<GlobalScoreBySimulationEndDate> preventionGlobalScores =
Expand All @@ -60,25 +61,25 @@ private SimulationsResultsLatest getSimulationsResultsLatest(String scenarioId)
}

private List<ExpectationTypeAndGlobalScore> getAllGlobalScores(
List<RawFinishedExerciseWithInjects> rawFinishedExercises) {
List<FinishedExerciseWithInjects> rawFinishedExercises) {
return rawFinishedExercises.stream().flatMap(this::getExpectationTypeAndGlobalScores).toList();
}

private Stream<ExpectationTypeAndGlobalScore> getExpectationTypeAndGlobalScores(
RawFinishedExerciseWithInjects rawFinishedExercise) {
return resultUtils.getResultsByTypes(rawFinishedExercise.getInject_ids()).stream()
FinishedExerciseWithInjects rawFinishedExercise) {
return resultUtils.getResultsByTypes(rawFinishedExercise.injectIds()).stream()
.map(
expectationResultByType ->
getExpectationTypeAndGlobalScore(rawFinishedExercise, expectationResultByType));
}

private static ExpectationTypeAndGlobalScore getExpectationTypeAndGlobalScore(
RawFinishedExerciseWithInjects rawFinishedExercise,
FinishedExerciseWithInjects rawFinishedExercise,
ExpectationResultsByType expectationResultByType) {
return new ExpectationTypeAndGlobalScore(
expectationResultByType.type(),
new GlobalScoreBySimulationEndDate(
rawFinishedExercise.getExercise_end_date(),
rawFinishedExercise.endDate(),
getPercentageOfInjectsOnSuccess(expectationResultByType)));
}

Expand All @@ -90,10 +91,16 @@ private static List<GlobalScoreBySimulationEndDate> getGlobalScoresForExpectatio
.toList();
}

private List<RawFinishedExerciseWithInjects> getOrderedRawFinishedExercises(String scenarioId) {
private List<FinishedExerciseWithInjects> getOrderedRawFinishedExercises(String scenarioId) {
List<RawFinishedExerciseWithInjects> rawFinishedExercises =
exerciseRepository.rawLatestFinishedExercisesWithInjectsByScenarioId(scenarioId);
return rawFinishedExercises.stream().sorted(Collections.reverseOrder()).toList();
return rawFinishedExercises.stream()
.map(
exercise ->
new FinishedExerciseWithInjects(
exercise.getExercise_end_date(), exercise.getInject_ids()))
.sorted(Collections.reverseOrder())
.toList();
}

private record ExpectationTypeAndGlobalScore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
import io.openbas.database.raw.RawFinishedExerciseWithInjects;
import java.time.Instant;
import java.util.Set;
import org.jetbrains.annotations.NotNull;

public class RawFinishedExerciseWithInjectsFixture {

private record TestableRawFinishedExerciseWithInjects(Instant endDate, Set<String> injectIds)
implements RawFinishedExerciseWithInjects,
Comparable<TestableRawFinishedExerciseWithInjects> {
implements RawFinishedExerciseWithInjects {

@Override
public Instant getExercise_end_date() {
Expand All @@ -20,13 +18,6 @@ public Instant getExercise_end_date() {
public Set<String> getInject_ids() {
return injectIds;
}

@Override
public int compareTo(@NotNull TestableRawFinishedExerciseWithInjects exercise) {
if (this.endDate.isBefore(exercise.endDate)) return 1;
if (this.endDate.isAfter(exercise.endDate)) return -1;
return 0;
}
}

public static RawFinishedExerciseWithInjects createDefaultRawFinishedExerciseWithInjects(
Expand Down

0 comments on commit 5fd5f84

Please sign in to comment.