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

[backend] Improve latency on atomicTestings #1733

Merged
merged 10 commits into from
Oct 29, 2024
Merged
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.atomic_testing;

import io.openbas.aop.LogExecutionTime;
import io.openbas.database.model.Inject;
import io.openbas.database.model.InjectExpectation;
import io.openbas.inject_expectation.InjectExpectationService;
Expand Down Expand Up @@ -30,13 +31,15 @@ public class AtomicTestingApi extends RestBehavior {
private final AtomicTestingService atomicTestingService;
private final InjectExpectationService injectExpectationService;

@LogExecutionTime
@PostMapping("/search")
@Transactional(readOnly = true)
public Page<AtomicTestingOutput> findAllAtomicTestings(
@RequestBody @Valid final SearchPaginationInput searchPaginationInput) {
return atomicTestingService.findAllAtomicTestings(searchPaginationInput);
}

@LogExecutionTime
@GetMapping("/{injectId}")
public InjectResultDTO findAtomicTesting(@PathVariable String injectId) {
return atomicTestingService.findById(injectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import static io.openbas.helper.StreamHelper.iterableToSet;
import static io.openbas.utils.AtomicTestingUtils.*;
import static io.openbas.utils.JpaUtils.createJoinArrayAggOnId;
import static io.openbas.utils.JpaUtils.createLeftJoin;
import static io.openbas.utils.StringUtils.duplicateString;
import static io.openbas.utils.pagination.PaginationUtils.buildPaginationCriteriaBuilder;
import static io.openbas.utils.pagination.SortUtilsCriteriaBuilder.toSortCriteriaBuilder;
Expand Down Expand Up @@ -299,6 +298,8 @@

public Page<AtomicTestingOutput> findAllAtomicTestings(
@NotNull final SearchPaginationInput searchPaginationInput) {
Map<String, Join<Base, Base>> joinMap = new HashMap<>();

Check warning on line 301 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L301

Added line #L301 was not covered by tests

Specification<Inject> customSpec =
Specification.where(
(root, query, cb) -> {
Expand All @@ -312,20 +313,25 @@
Specification<Inject> specificationCount,
Pageable pageable) ->
this.atomicTestings(
customSpec.and(specification), customSpec.and(specificationCount), pageable),
customSpec.and(specification),
customSpec.and(specificationCount),

Check warning on line 317 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L316-L317

Added lines #L316 - L317 were not covered by tests
pageable,
joinMap),
searchPaginationInput,
Inject.class);
Inject.class,
joinMap);
}

public Page<AtomicTestingOutput> atomicTestings(
Specification<Inject> specification,
Specification<Inject> specificationCount,
Pageable pageable) {
Pageable pageable,
Map<String, Join<Base, Base>> joinMap) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Root<Inject> injectRoot = cq.from(Inject.class);
selectForAtomicTesting(cb, cq, injectRoot);
selectForAtomicTesting(cb, cq, injectRoot, joinMap);

Check warning on line 334 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L334

Added line #L334 was not covered by tests

// -- Text Search and Filters --
if (specification != null) {
Expand Down Expand Up @@ -399,13 +405,30 @@
}

private void selectForAtomicTesting(
CriteriaBuilder cb, CriteriaQuery<Tuple> cq, Root<Inject> injectRoot) {
CriteriaBuilder cb,
CriteriaQuery<Tuple> cq,
Root<Inject> injectRoot,
Map<String, Join<Base, Base>> joinMap) {
// Joins
Join<Inject, InjectorContract> injectorContractJoin =
createLeftJoin(injectRoot, "injectorContract");
Join<InjectorContract, Injector> injectorJoin =
injectorContractJoin.join("injector", JoinType.LEFT);
Join<Inject, InjectStatus> injectStatusJoin = createLeftJoin(injectRoot, "status");
Join<Base, Base> injectorContractJoin = injectRoot.join("injectorContract", JoinType.LEFT);
joinMap.put("injectorContract", injectorContractJoin);

Check warning on line 414 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L413-L414

Added lines #L413 - L414 were not covered by tests

Join<Base, Base> injectorJoin = injectorContractJoin.join("injector", JoinType.LEFT);
joinMap.put("injector", injectorJoin);

Check warning on line 417 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L416-L417

Added lines #L416 - L417 were not covered by tests

// Subquery for InjectStatus
Subquery<Tuple> statusSubquery = cq.subquery(Tuple.class);
Root<InjectStatus> statusRoot = statusSubquery.from(InjectStatus.class);
Subquery<Tuple> dateSubquery = cq.subquery(Tuple.class);
Root<InjectStatus> dateRoot = dateSubquery.from(InjectStatus.class);

Check warning on line 423 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L420-L423

Added lines #L420 - L423 were not covered by tests

statusSubquery
.select(statusRoot.get("name"))
.where(cb.equal(statusRoot.get("inject").get("id"), injectRoot.get("id")));
dateSubquery
.select(dateRoot.get("trackingSentDate"))
.where(cb.equal(dateRoot.get("inject").get("id"), injectRoot.get("id")));

Check warning on line 430 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L425-L430

Added lines #L425 - L430 were not covered by tests

// Array aggregations
Expression<String[]> injectExpectationIdsExpression =
createJoinArrayAggOnId(cb, injectRoot, EXPECTATIONS);
Expand All @@ -421,7 +444,15 @@
injectRoot.get("updatedAt").alias("inject_updated_at"),
injectorJoin.get("type").alias("inject_type"),
injectorContractJoin.alias("inject_injector_contract"),
injectStatusJoin.alias("inject_status"),
cb.selectCase()
.when(cb.exists(statusSubquery), statusSubquery.select(statusRoot.get("name")))
.otherwise(cb.nullLiteral(ExecutionStatus.class))
.alias("inject_status"),
cb.selectCase()
.when(
cb.exists(dateSubquery), dateSubquery.select(dateRoot.get("trackingSentDate")))
.otherwise(cb.nullLiteral(Instant.class))
.alias("tracking_sent_date"),

Check warning on line 455 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L447-L455

Added lines #L447 - L455 were not covered by tests
injectExpectationIdsExpression.alias("inject_expectations"),
teamIdsExpression.alias("inject_teams"),
assetIdsExpression.alias("inject_assets"),
Expand All @@ -431,27 +462,32 @@
// GROUP BY
cq.groupBy(
Arrays.asList(
injectRoot.get("id"),
injectorContractJoin.get("id"),
injectorJoin.get("id"),
injectStatusJoin.get("id")));
injectRoot.get("id"), injectorContractJoin.get("id"), injectorJoin.get("id")));

Check warning on line 465 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L465

Added line #L465 was not covered by tests
}

private List<AtomicTestingOutput> execAtomicTesting(TypedQuery<Tuple> query) {
return query.getResultList().stream()
.map(
tuple ->
new AtomicTestingOutput(
tuple.get("inject_id", String.class),
tuple.get("inject_title", String.class),
tuple.get("inject_updated_at", Instant.class),
tuple.get("inject_type", String.class),
tuple.get("inject_injector_contract", InjectorContract.class),
tuple.get("inject_status", InjectStatus.class),
tuple.get("inject_expectations", String[].class),
tuple.get("inject_teams", String[].class),
tuple.get("inject_assets", String[].class),
tuple.get("inject_asset_groups", String[].class)))
tuple -> {
InjectStatus injectStatus = null;
ExecutionStatus status = tuple.get("inject_status", ExecutionStatus.class);

Check warning on line 473 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L472-L473

Added lines #L472 - L473 were not covered by tests
if (status != null) {
injectStatus = new InjectStatus();
injectStatus.setName(status);
injectStatus.setTrackingSentDate(tuple.get("tracking_sent_date", Instant.class));

Check warning on line 477 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L475-L477

Added lines #L475 - L477 were not covered by tests
}
return new AtomicTestingOutput(
tuple.get("inject_id", String.class),
tuple.get("inject_title", String.class),
tuple.get("inject_updated_at", Instant.class),
tuple.get("inject_type", String.class),
tuple.get("inject_injector_contract", InjectorContract.class),

Check warning on line 484 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L479-L484

Added lines #L479 - L484 were not covered by tests
injectStatus,
tuple.get("inject_expectations", String[].class),
tuple.get("inject_teams", String[].class),
tuple.get("inject_assets", String[].class),
tuple.get("inject_asset_groups", String[].class));

Check warning on line 489 in openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java#L486-L489

Added lines #L486 - L489 were not covered by tests
})
.toList();
}

Expand Down