Skip to content

Commit

Permalink
[backend] Improv performance on atomic testing pagination list (#1084)
Browse files Browse the repository at this point in the history
* [backend] Improv performance on atomic testing pagination list

* [backend] Add contract to AtomicTestingOutput

* [backend] Rebase

---------

Co-authored-by: Gael Leblan <gael.leblan@filigran.io>
  • Loading branch information
RomuDeuxfois and Dimfacion authored Jun 26, 2024
1 parent 7d1181b commit 7682415
Show file tree
Hide file tree
Showing 13 changed files with 540 additions and 300 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import io.openbas.rest.atomic_testing.form.InjectResultDTO;
import io.openbas.rest.exception.ElementNotFoundException;
import io.openbas.rest.helper.RestBehavior;
import io.openbas.rest.inject.output.AtomicTestingOutput;
import io.openbas.service.AtomicTestingService;
import io.openbas.utils.AtomicTestingMapper;
import io.openbas.utils.pagination.SearchPaginationInput;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -33,16 +34,10 @@ public class AtomicTestingApi extends RestBehavior {
private final InjectExpectationService injectExpectationService;

@PostMapping("/search")
public Page<InjectResultDTO> findAllAtomicTestings(
@Transactional(readOnly = true)
public Page<AtomicTestingOutput> findAllAtomicTestings(
@RequestBody @Valid final SearchPaginationInput searchPaginationInput) {
return this.atomicTestingService.findAllAtomicTestings(searchPaginationInput)
.map(inject -> AtomicTestingMapper.toDto(
inject, getTargets(
inject.getTeams(),
inject.getAssets(),
inject.getAssetGroups()
)
));
return this.atomicTestingService.findAllAtomicTestings(searchPaginationInput);
}

@GetMapping("/{injectId}")
Expand All @@ -53,7 +48,7 @@ public InjectResultDTO findAtomicTesting(@PathVariable String injectId) {
}

@PostMapping()
@Transactional(rollbackOn = Exception.class)
@Transactional(rollbackFor = Exception.class)
public InjectResultDTO createAtomicTesting(@Valid @RequestBody AtomicTestingInput input) {
Inject inject = this.atomicTestingService.createOrUpdate(input, null);
return AtomicTestingMapper.toDto(
Expand All @@ -66,7 +61,7 @@ inject, getTargets(
}

@PutMapping("/{injectId}")
@Transactional(rollbackOn = Exception.class)
@Transactional(rollbackFor = Exception.class)
public InjectResultDTO updateAtomicTesting(
@PathVariable @NotBlank final String injectId,
@Valid @RequestBody final AtomicTestingInput input) {
Expand Down Expand Up @@ -100,7 +95,7 @@ public List<InjectExpectation> findTargetResult(
}

@PutMapping("/{injectId}/tags")
@Transactional(rollbackOn = Exception.class)
@Transactional(rollbackFor = Exception.class)
public InjectResultDTO updateAtomicTestingTags(
@PathVariable @NotBlank final String injectId,
@Valid @RequestBody final AtomicTestingUpdateTagsInput input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package io.openbas.rest.inject.output;

import com.fasterxml.jackson.annotation.JsonProperty;
import io.openbas.database.model.InjectStatus;
import io.openbas.database.model.InjectorContract;
import io.openbas.rest.atomic_testing.form.InjectTargetWithResult;
import io.openbas.utils.AtomicTestingMapper;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.Getter;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static io.openbas.database.model.InjectStatus.draftInjectStatus;
import static lombok.AccessLevel.NONE;

@Data
public class AtomicTestingOutput {

@JsonProperty("inject_id")
@NotBlank
private String id;

@JsonProperty("inject_title")
@NotBlank
private String title;

@JsonProperty("inject_updated_at")
@NotNull
private Instant updatedAt;

@JsonProperty("inject_type")
public String injectType;

@JsonProperty("inject_injector_contract")
private InjectorContract injectorContract;

@Getter(NONE)
@JsonProperty("inject_status")
private InjectStatus status;

public InjectStatus getStatus() {
if (status == null) {
return draftInjectStatus();
}
return status;
}

@JsonProperty("inject_teams")
@NotNull
private List<String> teams;

@JsonProperty("inject_assets")
@NotNull
private List<String> assets;

@JsonProperty("inject_asset_groups")
@NotNull
private List<String> assetGroups;

@JsonProperty("inject_expectations")
@NotNull
private List<String> expectations;

// Pre Calcul

@JsonProperty("inject_targets")
private List<InjectTargetWithResult> targets;

@JsonProperty("inject_expectation_results")
private List<AtomicTestingMapper.ExpectationResultsByType> expectationResultByTypes = new ArrayList<>();

public AtomicTestingOutput(
String id,
String title,
Instant updatedAt,
String injectType,
InjectorContract injectorContract,
InjectStatus injectStatus,
String[] injectExpectations,
String[] teams,
String[] assets,
String[] assetGroups) {
this.id = id;
this.title = title;
this.updatedAt = updatedAt;
this.injectType = injectType;
this.injectorContract = injectorContract;
this.status = injectStatus;
this.expectations = injectExpectations != null ? new ArrayList<>(Arrays.asList(injectExpectations)) : new ArrayList<>();

this.teams = teams != null ? new ArrayList<>(Arrays.asList(teams)) : new ArrayList<>();
this.assets = assets != null ? new ArrayList<>(Arrays.asList(assets)) : new ArrayList<>();
this.assetGroups = assetGroups != null ? new ArrayList<>(Arrays.asList(assetGroups)) : new ArrayList<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import jakarta.validation.constraints.NotNull;
import lombok.Data;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

@Data
public class InjectOutput {
Expand Down Expand Up @@ -50,7 +48,13 @@ public class InjectOutput {
public String injectType;

@JsonProperty("inject_teams")
private Set<String> teams;
private List<String> teams;

@JsonProperty("inject_assets")
private List<String> assets;

@JsonProperty("inject_asset_groups")
private List<String> assetGroups;

@JsonProperty("inject_content")
private ObjectNode content;
Expand Down Expand Up @@ -78,16 +82,21 @@ public InjectOutput(
this.dependsDuration = dependsDuration;
this.injectorContract = injectorContract;
this.tags = tags != null ? new HashSet<>(Arrays.asList(tags)) : new HashSet<>();

this.teams = teams != null ? new ArrayList<>(Arrays.asList(teams)) : new ArrayList<>();
this.assets = assets != null ? new ArrayList<>(Arrays.asList(assets)) : new ArrayList<>();
this.assetGroups = assetGroups != null ? new ArrayList<>(Arrays.asList(assetGroups)) : new ArrayList<>();

this.isReady = InjectModelHelper.isReady(
injectorContract,
content,
allTeams,
teams != null ? new HashSet<>(Arrays.asList(teams)) : new HashSet<>(),
assets != null ? new HashSet<>(Arrays.asList(assets)) : new HashSet<>(),
assetGroups != null ? new HashSet<>(Arrays.asList(assetGroups)) : new HashSet<>()
this.teams,
this.assets,
this.assetGroups
);
this.injectType = injectType;
this.teams = teams != null ? new HashSet<>(Arrays.asList(teams)) : new HashSet<>();
this.teams = teams != null ? new ArrayList<>(Arrays.asList(teams)) : new ArrayList<>();
this.content = content;
}
}
Loading

0 comments on commit 7682415

Please sign in to comment.