Skip to content

Commit

Permalink
[backend] Implement expiration of all expectations (#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelHassine committed May 31, 2024
1 parent 3989bb1 commit 1358f28
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public class ExpectationsExpirationManagerConfig {
private boolean enable = true;
private String id = "96e476e0-b9c4-4660-869c-98585adf754d";
private int interval = 60;
private int expirationTime = 3600;
private int expirationTimeForAsset = 3600; // 1 hour
private int expirationTime = 21600; // 6 hours

public int getAssetExpirationTimeInMinute() {
return this.expirationTimeForAsset / 60;
}

public int getExpirationTimeInMinute() {
return this.expirationTime / 60;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,22 @@ public class ExpectationsExpirationManagerService {
private final ExpectationsExpirationManagerConfig config;
private final ObjectMapper objectMapper = new ObjectMapper();


@Transactional(rollbackFor = Exception.class)
public void computeExpectations() {
// Retrieve all expectations
List<InjectExpectation> preventionExpectations = this.injectExpectationService.preventionExpectationsNotFill();
log.info("Number of prevention expectations: " + preventionExpectations.size());

List<InjectExpectation> detectionExpectations = this.injectExpectationService.detectionExpectationsNotFill();
log.info("Number of detection expectations: " + detectionExpectations.size());

List<InjectExpectation> expectations = concat(preventionExpectations.stream(), detectionExpectations.stream()).toList();

List<InjectExpectation> expectations = this.injectExpectationService.expectationsNotFill();
if (!expectations.isEmpty()) {
this.computeExpectationForAssets(expectations);
this.computeExpectationForAssetGroups(expectations);
this.computeExpectationsForAssets(expectations);
this.computeExpectationsForAssetGroups(expectations);
this.computeExpectations(expectations);
}
}

// -- PRIVATE --

private void computeExpectationForAssets(@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssets = expectations.stream().filter(e -> e.getAsset() != null).toList();
private void computeExpectations(@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssets = expectations.stream().toList();
expectationAssets.forEach((expectation) -> {
Asset asset = expectation.getAsset();
// Maximum time for detection
if (isExpired(expectation, this.config.getExpirationTimeInMinute())) {
String result = computeFailedMessage(expectation.getType());
Expand All @@ -66,7 +59,24 @@ private void computeExpectationForAssets(@NotNull final List<InjectExpectation>
});
}

private void computeExpectationForAssetGroups(@NotNull final List<InjectExpectation> expectations) {
private void computeExpectationsForAssets(@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssets = expectations.stream().filter(e -> e.getAsset() != null).toList();
expectationAssets.forEach((expectation) -> {
// Maximum time for detection
if (isExpired(expectation, this.config.getAssetExpirationTimeInMinute())) {
String result = computeFailedMessage(expectation.getType());
this.injectExpectationService.computeExpectation(
expectation,
this.config.getId(),
PRODUCT_NAME,
result,
false
);
}
});
}

private void computeExpectationsForAssetGroups(@NotNull final List<InjectExpectation> expectations) {
List<InjectExpectation> expectationAssetGroups = expectations.stream().filter(e -> e.getAssetGroup() != null).toList();
expectationAssetGroups.forEach((expectationAssetGroup -> {
List<InjectExpectation> expectationAssets = this.injectExpectationService.expectationsForAssets(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.stream.Stream;

import static io.openbas.database.model.InjectExpectation.EXPECTATION_TYPE.*;
import static io.openbas.helper.StreamHelper.fromIterable;
import static io.openbas.inject_expectation.InjectExpectationUtils.computeResult;
import static java.time.Instant.now;

Expand All @@ -38,25 +39,16 @@ public Optional<InjectExpectation> findInjectExpectation(@NotBlank final String
return this.injectExpectationRepository.findById(injectExpectationId);
}

public void addResultExpectation(
@NotNull final InjectExpectation expectation,
@NotBlank final String sourceId,
@NotBlank final String sourceName,
@NotBlank final String result) {
computeResult(expectation, sourceId, sourceName, result);
this.update(expectation);
}

public InjectExpectation computeExpectation(
@NotNull final InjectExpectation expectation,
@NotBlank final String sourceId,
@NotBlank final String sourceName,
@NotBlank final String result,
@NotBlank final boolean success) {
computeResult(expectation, sourceId, sourceName, result);
if( success ) {
if (success) {
expectation.setScore(expectation.getExpectedScore());
} else if( expectation.getScore() == null ) {
} else if (expectation.getScore() == null) {
expectation.setScore(0);
}
return this.update(expectation);
Expand Down Expand Up @@ -86,33 +78,24 @@ public InjectExpectation update(@NotNull InjectExpectation injectExpectation) {
return this.injectExpectationRepository.save(injectExpectation);
}

// -- PREVENTION --
// -- ALL --

public InjectExpectation preventionExpectationForAsset(
@NotNull final Inject inject,
@NotBlank final String assetId) {
return this.injectExpectationRepository.findPreventionExpectationForAsset(
inject.getId(), assetId
);
public List<InjectExpectation> expectationsNotFill() {
return fromIterable(this.injectExpectationRepository.findAll())
.stream()
.filter(e -> e.getResults().stream().toList().isEmpty())
.toList();
}

public List<InjectExpectation> preventionExpectationForAssets(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
List<String> assetIds = assetGroup.getAssets().stream().map(Asset::getId).toList();
return this.injectExpectationRepository.findAll(
Specification.where(InjectExpectationSpecification.type(PREVENTION))
.and(InjectExpectationSpecification.fromAssets(inject.getId(), assetIds))
);
public List<InjectExpectation> expectationsNotFill(@NotBlank final String source) {
return fromIterable(this.injectExpectationRepository.findAll())
.stream()
.filter(e -> e.getResults().stream().noneMatch(r -> source.equals(r.getSourceId())))
.toList();
}

public InjectExpectation preventionExpectationForAssetGroup(
@NotNull final Inject inject,
@NotNull final AssetGroup assetGroup) {
return this.injectExpectationRepository.findPreventionExpectationForAssetGroup(
inject.getId(), assetGroup.getId()
);
}

// -- PREVENTION --

public List<InjectExpectation> preventionExpectationsNotFill(@NotBlank final String source) {
return this.injectExpectationRepository.findAll(
Expand Down

0 comments on commit 1358f28

Please sign in to comment.