-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[backend/frontend] display latest simulations results on scenario ove…
…rview
- Loading branch information
1 parent
756dd1e
commit 7259a5d
Showing
12 changed files
with
286 additions
and
261 deletions.
There are no files selected for viewing
37 changes: 12 additions & 25 deletions
37
openbas-api/src/main/java/io/openbas/rest/scenario/ScenarioStatisticApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,33 @@ | ||
package io.openbas.rest.scenario; | ||
|
||
import static io.openbas.database.model.User.ROLE_USER; | ||
import static io.openbas.rest.scenario.ScenarioApi.SCENARIO_URI; | ||
import static org.springframework.util.StringUtils.hasText; | ||
|
||
import io.openbas.database.repository.ScenarioRepository; | ||
import io.openbas.rest.helper.RestBehavior; | ||
import io.openbas.rest.scenario.response.ScenarioStatistic; | ||
import io.openbas.rest.scenario.service.ScenarioStatisticService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import jakarta.transaction.Transactional; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.annotation.Secured; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Secured(ROLE_USER) | ||
@RequiredArgsConstructor | ||
public class ScenarioStatisticApi extends RestBehavior { | ||
|
||
private final ScenarioRepository scenarioRepository; | ||
private final ScenarioStatisticService scenarioStatisticService; | ||
|
||
@GetMapping(SCENARIO_URI + "/statistics") | ||
@GetMapping(SCENARIO_URI + "/{scenarioId}/statistics") | ||
@PreAuthorize("isScenarioObserver(#scenarioId)") | ||
@Transactional(rollbackOn = Exception.class) | ||
@Operation(summary = "Retrieve scenario statistics") | ||
public ScenarioStatistic scenarioStatistic() { | ||
ScenarioStatistic statistic = new ScenarioStatistic(); | ||
statistic.setScenariosGlobalCount(this.scenarioRepository.count()); | ||
statistic.setScenariosCategoriesCount(findTopCategories()); | ||
return statistic; | ||
} | ||
|
||
private Map<String, Long> findTopCategories() { | ||
List<Object[]> results = this.scenarioRepository.findTopCategories(3); | ||
Map<String, Long> categoryCount = new LinkedHashMap<>(); | ||
for (Object[] result : results) { | ||
String category = (String) result[0]; | ||
if (hasText(category)) { | ||
Long count = (Long) result[1]; | ||
categoryCount.put(category, count); | ||
} | ||
} | ||
return categoryCount; | ||
public ScenarioStatistic getScenarioStatistics(@PathVariable @NotBlank final String scenarioId) { | ||
return scenarioStatisticService.getStatistics(scenarioId); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...s-api/src/main/java/io/openbas/rest/scenario/response/GlobalScoreBySimulationEndDate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.openbas.rest.scenario.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.time.Instant; | ||
|
||
public record GlobalScoreBySimulationEndDate( | ||
@JsonProperty("simulation_end_date") @NotNull Instant simulationEndDate, | ||
@JsonProperty("global_score_success_percentage") @NotNull | ||
double globalScoreSuccessPercentage) {} |
15 changes: 4 additions & 11 deletions
15
openbas-api/src/main/java/io/openbas/rest/scenario/response/ScenarioStatistic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,8 @@ | ||
package io.openbas.rest.scenario.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import java.util.Map; | ||
import lombok.Data; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
@Data | ||
public class ScenarioStatistic { | ||
|
||
@JsonProperty("scenarios_global_count") | ||
private long scenariosGlobalCount; | ||
|
||
@JsonProperty("scenarios_attack_scenario_count") | ||
private Map<String, Long> scenariosCategoriesCount; | ||
} | ||
public record ScenarioStatistic( | ||
@JsonProperty("simulations_results_latest") @NotNull | ||
SimulationsResultsLatest simulationsResultsLatest) {} |
11 changes: 11 additions & 0 deletions
11
openbas-api/src/main/java/io/openbas/rest/scenario/response/SimulationsResultsLatest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.openbas.rest.scenario.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.openbas.expectation.ExpectationType; | ||
import jakarta.validation.constraints.NotNull; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public record SimulationsResultsLatest( | ||
@JsonProperty("global_scores_by_expectation_type") @NotNull | ||
Map<ExpectationType, List<GlobalScoreBySimulationEndDate>> globalScoresByExpectationType) {} |
136 changes: 136 additions & 0 deletions
136
openbas-api/src/main/java/io/openbas/rest/scenario/service/ScenarioStatisticService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package io.openbas.rest.scenario.service; | ||
|
||
import io.openbas.database.raw.RawFinishedExerciseWithInjects; | ||
import io.openbas.database.repository.ExerciseRepository; | ||
import io.openbas.expectation.ExpectationType; | ||
import io.openbas.rest.scenario.response.GlobalScoreBySimulationEndDate; | ||
import io.openbas.rest.scenario.response.ScenarioStatistic; | ||
import io.openbas.rest.scenario.response.SimulationsResultsLatest; | ||
import io.openbas.utils.AtomicTestingUtils.ExpectationResultsByType; | ||
import io.openbas.utils.AtomicTestingUtils.ResultDistribution; | ||
import io.openbas.utils.ResultUtils; | ||
import java.util.*; | ||
import java.util.function.BinaryOperator; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ScenarioStatisticService { | ||
|
||
private final ExerciseRepository exerciseRepository; | ||
|
||
private final ResultUtils resultUtils; | ||
|
||
public ScenarioStatistic getStatistics(String scenarioId) { | ||
return getLatestSimulationsStatistics(scenarioId); | ||
} | ||
|
||
private ScenarioStatistic getLatestSimulationsStatistics(String scenarioId) { | ||
List<RawFinishedExerciseWithInjects> rawFinishedExercises = | ||
exerciseRepository.rawLatestFinishedExercisesWithInjectsByScenarioId(scenarioId); | ||
|
||
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)); | ||
} | ||
|
||
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 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 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 static List<GlobalScoreBySimulationEndDate> getGlobalScoresBySimulationEndDates( | ||
RawFinishedExerciseWithInjects rawFinishedExercise, | ||
List<ExpectationResultsByType> expectationResultsByType, | ||
ExpectationType expectationType) { | ||
|
||
return expectationResultsByType.stream() | ||
.filter(expectationResultByType -> expectationResultByType.type() == expectationType) | ||
.map( | ||
expectationResultByType -> | ||
new GlobalScoreBySimulationEndDate( | ||
rawFinishedExercise.getExercise_end_date(), | ||
getPercentageOfInjectsOnSuccess(expectationResultByType))) | ||
.toList(); | ||
} | ||
|
||
private static float getPercentageOfInjectsOnSuccess( | ||
ExpectationResultsByType expectationResultByType) { | ||
if (expectationResultByType.distribution().isEmpty()) { | ||
return 0; | ||
} | ||
var totalNumberOfInjects = | ||
expectationResultByType.distribution().stream() | ||
.map(ResultDistribution::value) | ||
.reduce(0, Integer::sum); | ||
var 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; | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.