Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend/backend] fix and improve dashboard statistics (#1697) #1698

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.openbas.rest.statistic;

import io.openbas.aop.LogExecutionTime;
import io.openbas.config.OpenBASPrincipal;
import io.openbas.database.model.AttackPattern;
import io.openbas.database.raw.RawGlobalInjectExpectation;
Expand All @@ -25,6 +26,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -34,6 +36,11 @@
import static io.openbas.utils.AtomicTestingUtils.getExpectationResultByTypesFromRaw;
import static java.util.stream.Collectors.groupingBy;

enum Type {
GLOBAL,
USER,

Check warning on line 41 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L39-L41

Added lines #L39 - L41 were not covered by tests
}

@RestController
@RequiredArgsConstructor
public class StatisticApi extends RestBehavior {
Expand All @@ -46,6 +53,7 @@
private final AssetGroupRepository assetGroupRepository;
private final InjectRepository injectRepository;

@LogExecutionTime
@GetMapping("/api/statistics")
@Transactional(rollbackOn = Exception.class)
@Operation(summary = "Retrieve platform statistics")
Expand All @@ -65,6 +73,9 @@
statistic.setInjectsCount(computeGlobalStat(now, injectRepository));
statistic.setResults(computeGlobalExpectationResults(now));
statistic.setInjectResults(computeGlobalInjectExpectationResults(now));
statistic.setExerciseCountByCategory(computeExerciseCountGroupByCategory(Type.GLOBAL, now));
statistic.setExercisesCountByWeek(computeExerciseCountGroupByWeek(Type.GLOBAL, now));
statistic.setInjectsCountByAttackPattern(computeInjectCountGroupByAttackPattern(Type.GLOBAL, now));

Check warning on line 78 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L76-L78

Added lines #L76 - L78 were not covered by tests
} else {
statistic.setScenariosCount(computeUserStat(now, scenarioRepository));
statistic.setExercisesCount(computeUserStat(now, exerciseRepository));
Expand All @@ -75,48 +86,61 @@
statistic.setInjectsCount(computeUserStat(now, injectRepository));
statistic.setResults(computeUserExpectationResults(now));
statistic.setInjectResults(computeUserInjectExpectationResults(now));
statistic.setExerciseCountByCategory(computeExerciseCountGroupByCategory(Type.USER, now));
statistic.setExercisesCountByWeek(computeExerciseCountGroupByWeek(Type.USER, now));
statistic.setInjectsCountByAttackPattern(computeInjectCountGroupByAttackPattern(Type.USER, now));

Check warning on line 91 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L89-L91

Added lines #L89 - L91 were not covered by tests
}
return statistic;
}

// -- GLOBAL STATISTIC --

private StatisticElement computeGlobalStat(Instant from, StatisticRepository repository) {
long global = repository.globalCount(from);
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
long global = repository.globalCount(minus3Months);

Check warning on line 100 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L99-L100

Added lines #L99 - L100 were not covered by tests
Instant minusMonth = from.minus(30, ChronoUnit.DAYS);
long progression = global - repository.globalCount(minusMonth);
long progression = repository.globalCount(minusMonth);

Check warning on line 102 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L102

Added line #L102 was not covered by tests
return new StatisticElement(global, progression);
}

private StatisticElement computeUserStat(Instant from, StatisticRepository repository) {
OpenBASPrincipal user = currentUser();
long global = repository.userCount(user.getId(), from);
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
long global = repository.userCount(user.getId(), minus3Months);

Check warning on line 109 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L108-L109

Added lines #L108 - L109 were not covered by tests
Instant minusMonth = from.minus(30, ChronoUnit.DAYS);
long progression = global - repository.userCount(user.getId(), minusMonth);
long progression = repository.userCount(user.getId(), minusMonth);

Check warning on line 111 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L111

Added line #L111 was not covered by tests
return new StatisticElement(global, progression);
}

private List<ExpectationResultsByType> computeGlobalExpectationResults(@NotNull final Instant from) {
List<RawInjectExpectation> rawInjectExpectations = fromIterable(this.exerciseRepository.allInjectExpectationsFromDate(from));
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
List<RawInjectExpectation> rawInjectExpectations = fromIterable(
this.exerciseRepository.allInjectExpectationsFromDate(minus3Months));

Check warning on line 118 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L116-L118

Added lines #L116 - L118 were not covered by tests
return getExpectationResultByTypesFromRaw(rawInjectExpectations);
}

private List<ExpectationResultsByType> computeUserExpectationResults(@NotNull final Instant from) {
OpenBASPrincipal user = currentUser();
List<RawInjectExpectation> rawInjectExpectations = fromIterable(this.exerciseRepository.allGrantedInjectExpectationsFromDate(from, user.getId()));
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
List<RawInjectExpectation> rawInjectExpectations = fromIterable(
this.exerciseRepository.allGrantedInjectExpectationsFromDate(minus3Months, user.getId()));

Check warning on line 126 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L124-L126

Added lines #L124 - L126 were not covered by tests
return getExpectationResultByTypesFromRaw(rawInjectExpectations);
}

private List<InjectExpectationResultsByAttackPattern> computeGlobalInjectExpectationResults(
@NotNull final Instant from) {
List<RawGlobalInjectExpectation> rawGlobalInjectExpectations = fromIterable(this.exerciseRepository.rawGlobalInjectExpectationResultsFromDate(from));
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
List<RawGlobalInjectExpectation> rawGlobalInjectExpectations = fromIterable(
this.exerciseRepository.rawGlobalInjectExpectationResultsFromDate(minus3Months));

Check warning on line 134 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L132-L134

Added lines #L132 - L134 were not covered by tests
return injectExpectationResultsByAttackPatternFromRawGlobalInjectExpectation(rawGlobalInjectExpectations);
}

private List<InjectExpectationResultsByAttackPattern> computeUserInjectExpectationResults(
@NotNull final Instant from) {
OpenBASPrincipal user = currentUser();
List<RawGlobalInjectExpectation> rawGlobalInjectExpectations = fromIterable(this.exerciseRepository.rawGrantedInjectExpectationResultsFromDate(from, user.getId()));
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
List<RawGlobalInjectExpectation> rawGlobalInjectExpectations = fromIterable(
this.exerciseRepository.rawGrantedInjectExpectationResultsFromDate(minus3Months, user.getId()));

Check warning on line 143 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L141-L143

Added lines #L141 - L143 were not covered by tests
return injectExpectationResultsByAttackPatternFromRawGlobalInjectExpectation(rawGlobalInjectExpectations);
}

Expand Down Expand Up @@ -174,4 +198,58 @@
.collect(Collectors.toList());
}

private Map<String, Long> computeExerciseCountGroupByCategory(final Type type, @NotNull final Instant from) {
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
List<Object[]> result = new ArrayList<>();

Check warning on line 203 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L202-L203

Added lines #L202 - L203 were not covered by tests
if (type == Type.GLOBAL) {
result = exerciseRepository.globalCountGroupByCategory(minus3Months);

Check warning on line 205 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L205

Added line #L205 was not covered by tests
} else if (type == Type.USER) {
OpenBASPrincipal user = currentUser();
result = exerciseRepository.userCountGroupByCategory(user.getId(), minus3Months);

Check warning on line 208 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L207-L208

Added lines #L207 - L208 were not covered by tests
}
Map<String, Long> categoryCountMap = new HashMap<>();

Check warning on line 210 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L210

Added line #L210 was not covered by tests
for (Object[] row : result) {
String category = (String) row[0];
Long count = (Long) row[1];
categoryCountMap.put(category, count);
}
return categoryCountMap;

Check warning on line 216 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L212-L216

Added lines #L212 - L216 were not covered by tests
}

private Map<Instant, Long> computeExerciseCountGroupByWeek(final Type type, @NotNull final Instant from) {
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
List<Object[]> result = new ArrayList<>();

Check warning on line 221 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L220-L221

Added lines #L220 - L221 were not covered by tests
if (type == Type.GLOBAL) {
result = exerciseRepository.globalCountGroupByWeek(minus3Months);

Check warning on line 223 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L223

Added line #L223 was not covered by tests
} else if (type == Type.USER) {
OpenBASPrincipal user = currentUser();
result = exerciseRepository.userCountGroupByWeek(user.getId(), minus3Months);

Check warning on line 226 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L225-L226

Added lines #L225 - L226 were not covered by tests
}
Map<Instant, Long> weekCountMap = new HashMap<>();

Check warning on line 228 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L228

Added line #L228 was not covered by tests
for (Object[] row : result) {
Instant week = (Instant) row[0];
Long count = (Long) row[1];
weekCountMap.put(week, count);
}
return weekCountMap;

Check warning on line 234 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L230-L234

Added lines #L230 - L234 were not covered by tests
}

private Map<String, Long> computeInjectCountGroupByAttackPattern(final Type type, @NotNull final Instant from) {
Instant minus3Months = from.minus(180, ChronoUnit.DAYS);
List<Object[]> result = new ArrayList<>();

Check warning on line 239 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L238-L239

Added lines #L238 - L239 were not covered by tests
if (type == Type.GLOBAL) {
result = injectRepository.globalCountGroupByAttackPatternInExercise(minus3Months);

Check warning on line 241 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L241

Added line #L241 was not covered by tests
} else if (type == Type.USER) {
OpenBASPrincipal user = currentUser();
result = injectRepository.userCountGroupByAttackPatternInExercise(user.getId(), minus3Months);

Check warning on line 244 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L243-L244

Added lines #L243 - L244 were not covered by tests
}
Map<String, Long> attackPatternMap = new HashMap<>();

Check warning on line 246 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L246

Added line #L246 was not covered by tests
for (Object[] row : result) {
String attackPattern = (String) row[0];
Long count = (Long) row[1];
attackPatternMap.put(attackPattern, count);
}
return attackPatternMap;

Check warning on line 252 in openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/StatisticApi.java#L248-L252

Added lines #L248 - L252 were not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,54 @@
import lombok.Getter;
import lombok.Setter;

import java.time.Instant;
import java.util.List;
import java.util.Map;

@Setter
@Getter
public class PlatformStatistic {

@JsonProperty("platform_id")
private String platformId = "openbas";
@JsonProperty("platform_id")
private String platformId = "openbas";

Check warning on line 18 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L17-L18

Added lines #L17 - L18 were not covered by tests

@JsonProperty("scenarios_count")
private StatisticElement scenariosCount;
@JsonProperty("scenarios_count")
private StatisticElement scenariosCount;

Check warning on line 21 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L21

Added line #L21 was not covered by tests

@JsonProperty("exercises_count")
private StatisticElement exercisesCount;
@JsonProperty("exercises_count")
private StatisticElement exercisesCount;

Check warning on line 24 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L24

Added line #L24 was not covered by tests

@JsonProperty("users_count")
private StatisticElement usersCount;
@JsonProperty("users_count")
private StatisticElement usersCount;

Check warning on line 27 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L27

Added line #L27 was not covered by tests

@JsonProperty("teams_count")
private StatisticElement teamsCount;
@JsonProperty("teams_count")
private StatisticElement teamsCount;

Check warning on line 30 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L30

Added line #L30 was not covered by tests

@JsonProperty("assets_count")
private StatisticElement assetsCount;
@JsonProperty("assets_count")
private StatisticElement assetsCount;

Check warning on line 33 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L33

Added line #L33 was not covered by tests

@JsonProperty("asset_groups_count")
private StatisticElement assetGroupsCount;
@JsonProperty("asset_groups_count")
private StatisticElement assetGroupsCount;

Check warning on line 36 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L36

Added line #L36 was not covered by tests

@JsonProperty("injects_count")
private StatisticElement injectsCount;
@JsonProperty("injects_count")
private StatisticElement injectsCount;

Check warning on line 39 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L39

Added line #L39 was not covered by tests

@JsonProperty("expectation_results")
private List<ExpectationResultsByType> results;
@JsonProperty("expectation_results")
private List<ExpectationResultsByType> results;

Check warning on line 42 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L42

Added line #L42 was not covered by tests

@JsonProperty("inject_expectation_results")
private List<InjectExpectationResultsByAttackPattern> injectResults;
@JsonProperty("inject_expectation_results")
private List<InjectExpectationResultsByAttackPattern> injectResults;

Check warning on line 45 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L45

Added line #L45 was not covered by tests

public PlatformStatistic() {
// Default constructor
}
@JsonProperty("exercises_count_by_category")
private Map<String, Long> exerciseCountByCategory;

Check warning on line 48 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L48

Added line #L48 was not covered by tests

@JsonProperty("exercises_count_by_week")
private Map<Instant, Long> exercisesCountByWeek;

Check warning on line 51 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L51

Added line #L51 was not covered by tests

@JsonProperty("injects_count_by_attack_pattern")
private Map<String, Long> injectsCountByAttackPattern;

Check warning on line 54 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L54

Added line #L54 was not covered by tests

public PlatformStatistic() {

Check warning on line 56 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L56

Added line #L56 was not covered by tests
// Default constructor
}

Check warning on line 58 in openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/statistic/response/PlatformStatistic.java#L58

Added line #L58 was not covered by tests
}
1 change: 1 addition & 0 deletions openbas-front/src/actions/payloads/Payload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export type PayloadStore = Omit<Payload, 'payload_collector'> & {
dns_resolution_hostname?: string;
file_drop_file?: string;
executable_file?: string;
executable_arch?: string;
};
Loading