From 788acf84c6067c3ccab3a6a1c820dce36ecd8495 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Wed, 23 Oct 2024 17:16:16 +0200 Subject: [PATCH 01/10] [backend] wip --- .../java/io/openbas/rest/atomic_testing/AtomicTestingApi.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openbas-api/src/main/java/io/openbas/rest/atomic_testing/AtomicTestingApi.java b/openbas-api/src/main/java/io/openbas/rest/atomic_testing/AtomicTestingApi.java index 0ab17d6af9..99a79ba5ce 100644 --- a/openbas-api/src/main/java/io/openbas/rest/atomic_testing/AtomicTestingApi.java +++ b/openbas-api/src/main/java/io/openbas/rest/atomic_testing/AtomicTestingApi.java @@ -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; @@ -30,6 +31,7 @@ public class AtomicTestingApi extends RestBehavior { private final AtomicTestingService atomicTestingService; private final InjectExpectationService injectExpectationService; + @LogExecutionTime @PostMapping("/search") @Transactional(readOnly = true) public Page findAllAtomicTestings( @@ -37,6 +39,7 @@ public Page findAllAtomicTestings( return atomicTestingService.findAllAtomicTestings(searchPaginationInput); } + @LogExecutionTime @GetMapping("/{injectId}") public InjectResultDTO findAtomicTesting(@PathVariable String injectId) { return atomicTestingService.findById(injectId); From 65d4e41ce9541b06625874e1e28b513a7d08eaef Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Mon, 28 Oct 2024 10:36:40 +0100 Subject: [PATCH 02/10] [backend] Rebase --- .../openbas/service/AtomicTestingService.java | 73 +++++++++++++------ .../atomic_testings/InjectDtoList.tsx | 2 +- 2 files changed, 53 insertions(+), 22 deletions(-) diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java index 356a9c53f7..068f4c182d 100644 --- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java +++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java @@ -1,5 +1,6 @@ package io.openbas.service; +import static io.openbas.aop.LoggingAspect.logger; import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.database.criteria.GenericCriteria.countQuery; import static io.openbas.database.model.Command.COMMAND_TYPE; @@ -405,14 +406,28 @@ private void selectForAtomicTesting( createLeftJoin(injectRoot, "injectorContract"); Join injectorJoin = injectorContractJoin.join("injector", JoinType.LEFT); - Join injectStatusJoin = createLeftJoin(injectRoot, "status"); - // Array aggregations - Expression injectExpectationIdsExpression = - createJoinArrayAggOnId(cb, injectRoot, EXPECTATIONS); - Expression teamIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "teams"); - Expression assetIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assets"); - Expression assetGroupIdsExpression = - createJoinArrayAggOnId(cb, injectRoot, "assetGroups"); + //Join injectStatusJoin = createLeftJoin(injectRoot, "status"); + + // Subquery for InjectStatus + Subquery statusSubquery = cq.subquery(Tuple.class); + Root statusRoot = statusSubquery.from(InjectStatus.class); + Subquery dateSubquery = cq.subquery(Tuple.class); + Root dateRoot = dateSubquery.from(InjectStatus.class); + + // Define the subquery to select the necessary fields + 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"))); + + + // Array aggregations + Expression injectExpectationIdsExpression = createJoinArrayAggOnId(cb, injectRoot, EXPECTATIONS); + Expression teamIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "teams"); + Expression assetIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assets"); + Expression assetGroupIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assetGroups"); // SELECT cq.multiselect( @@ -421,11 +436,17 @@ private void selectForAtomicTesting( injectRoot.get("updatedAt").alias("inject_updated_at"), injectorJoin.get("type").alias("inject_type"), injectorContractJoin.alias("inject_injector_contract"), - injectStatusJoin.alias("inject_status"), - injectExpectationIdsExpression.alias("inject_expectations"), - teamIdsExpression.alias("inject_teams"), - assetIdsExpression.alias("inject_assets"), - assetGroupIdsExpression.alias("inject_asset_groups")) + //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"), + injectExpectationIdsExpression.alias("inject_expectations"), + teamIdsExpression.alias("inject_teams"), + assetIdsExpression.alias("inject_assets"), + assetGroupIdsExpression.alias("inject_asset_groups")) .distinct(true); // GROUP BY @@ -433,25 +454,35 @@ private void selectForAtomicTesting( Arrays.asList( injectRoot.get("id"), injectorContractJoin.get("id"), - injectorJoin.get("id"), - injectStatusJoin.get("id"))); + injectorJoin.get("id") + )); } private List execAtomicTesting(TypedQuery query) { - return query.getResultList().stream() - .map( - tuple -> - new AtomicTestingOutput( + long start = System.currentTimeMillis(); + List resultList = query.getResultList(); + long executionTime = System.currentTimeMillis() - start; + logger.info("Execution time of execution query true + raw query: " + executionTime + " ms"); + + return resultList + .stream() + .map(tuple -> { + InjectStatus injectStatus = new InjectStatus(); + injectStatus.setName(tuple.get("inject_status", ExecutionStatus.class)); + injectStatus.setTrackingSentDate(tuple.get("tracking_sent_date", Instant.class)); + 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), - tuple.get("inject_status", InjectStatus.class), + 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))) + tuple.get("inject_asset_groups", String[].class)); + } + ) .toList(); } diff --git a/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx b/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx index 48ba77899b..119e7630d6 100644 --- a/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx +++ b/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx @@ -121,7 +121,7 @@ const InjectDtoList: FunctionComponent = ({ label: 'Status', isSortable: false, value: (injectDto: InjectResultDTO) => { - return (); + return (); }, }, { From 7a7afe6996894c5867eff48e818a5ea691072db2 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Mon, 28 Oct 2024 09:25:12 +0100 Subject: [PATCH 03/10] [backend] Add subqueries for atomicTestings --- .../src/admin/components/atomic_testings/InjectDtoList.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx b/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx index 119e7630d6..f357aa3a9c 100644 --- a/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx +++ b/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx @@ -121,7 +121,7 @@ const InjectDtoList: FunctionComponent = ({ label: 'Status', isSortable: false, value: (injectDto: InjectResultDTO) => { - return (); + return (); }, }, { From 07a5af3f83fefb83d6b3f51f17f27f98e91e8f2d Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Mon, 28 Oct 2024 09:41:07 +0100 Subject: [PATCH 04/10] [backend] Add subqueries for atomicTestings --- .../openbas/service/AtomicTestingService.java | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java index 068f4c182d..574ee8c6dc 100644 --- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java +++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java @@ -399,14 +399,10 @@ public Page atomicTestings( return new PageImpl<>(injects, pageable, total); } - private void selectForAtomicTesting( - CriteriaBuilder cb, CriteriaQuery cq, Root injectRoot) { - // Joins - Join injectorContractJoin = - createLeftJoin(injectRoot, "injectorContract"); - Join injectorJoin = - injectorContractJoin.join("injector", JoinType.LEFT); - //Join injectStatusJoin = createLeftJoin(injectRoot, "status"); + private void selectForAtomicTesting(CriteriaBuilder cb, CriteriaQuery cq, Root injectRoot) { + // Joins + Join injectorContractJoin = createLeftJoin(injectRoot, "injectorContract"); + Join injectorJoin = injectorContractJoin.join("injector", JoinType.LEFT); // Subquery for InjectStatus Subquery statusSubquery = cq.subquery(Tuple.class); @@ -422,21 +418,19 @@ private void selectForAtomicTesting( ) .where(cb.equal(dateRoot.get("inject").get("id"), injectRoot.get("id"))); - // Array aggregations Expression injectExpectationIdsExpression = createJoinArrayAggOnId(cb, injectRoot, EXPECTATIONS); Expression teamIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "teams"); Expression assetIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assets"); Expression assetGroupIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assetGroups"); - // SELECT - cq.multiselect( - injectRoot.get("id").alias("inject_id"), - injectRoot.get("title").alias("inject_title"), - injectRoot.get("updatedAt").alias("inject_updated_at"), - injectorJoin.get("type").alias("inject_type"), - injectorContractJoin.alias("inject_injector_contract"), - //injectStatusJoin.alias("inject_status"), + // SELECT + cq.multiselect( + injectRoot.get("id").alias("inject_id"), + injectRoot.get("title").alias("inject_title"), + injectRoot.get("updatedAt").alias("inject_updated_at"), + injectorJoin.get("type").alias("inject_type"), + injectorContractJoin.alias("inject_injector_contract"), cb.selectCase() .when(cb.exists(statusSubquery), statusSubquery.select(statusRoot.get("name"))) .otherwise(cb.nullLiteral(ExecutionStatus.class)).alias("inject_status"), @@ -459,12 +453,7 @@ private void selectForAtomicTesting( } private List execAtomicTesting(TypedQuery query) { - long start = System.currentTimeMillis(); - List resultList = query.getResultList(); - long executionTime = System.currentTimeMillis() - start; - logger.info("Execution time of execution query true + raw query: " + executionTime + " ms"); - - return resultList + return query.getResultList() .stream() .map(tuple -> { InjectStatus injectStatus = new InjectStatus(); From 45cdc763761c9058934b0ed0f6e7575be11bed17 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Mon, 28 Oct 2024 10:28:14 +0100 Subject: [PATCH 05/10] [backend] Add subqueries for atomicTestings --- .../openbas/service/AtomicTestingService.java | 68 ++++++++++--------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java index 574ee8c6dc..935c8bdc03 100644 --- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java +++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java @@ -8,7 +8,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; @@ -298,35 +297,39 @@ public void deleteAtomicTesting(String injectId) { // -- PAGINATION -- - public Page findAllAtomicTestings( - @NotNull final SearchPaginationInput searchPaginationInput) { - Specification customSpec = - Specification.where( - (root, query, cb) -> { - Predicate predicate = cb.conjunction(); - predicate = cb.and(predicate, cb.isNull(root.get("scenario"))); - predicate = cb.and(predicate, cb.isNull(root.get("exercise"))); - return predicate; - }); - return buildPaginationCriteriaBuilder( - (Specification specification, - Specification specificationCount, - Pageable pageable) -> - this.atomicTestings( - customSpec.and(specification), customSpec.and(specificationCount), pageable), - searchPaginationInput, - Inject.class); - } + public Page findAllAtomicTestings(@NotNull final SearchPaginationInput searchPaginationInput) { + Map> joinMap = new HashMap<>(); + + Specification customSpec = Specification.where((root, query, cb) -> { + Predicate predicate = cb.conjunction(); + predicate = cb.and(predicate, cb.isNull(root.get("scenario"))); + predicate = cb.and(predicate, cb.isNull(root.get("exercise"))); + return predicate; + }); + return buildPaginationCriteriaBuilder( + (Specification specification, Specification specificationCount, Pageable pageable) -> this.atomicTestings( + customSpec.and(specification), + customSpec.and(specificationCount), + pageable, + joinMap + ), + searchPaginationInput, + Inject.class, + joinMap + ); + } - public Page atomicTestings( - Specification specification, - Specification specificationCount, - Pageable pageable) { - CriteriaBuilder cb = this.entityManager.getCriteriaBuilder(); - CriteriaQuery cq = cb.createTupleQuery(); - Root injectRoot = cq.from(Inject.class); - selectForAtomicTesting(cb, cq, injectRoot); + public Page atomicTestings( + Specification specification, + Specification specificationCount, + Pageable pageable, + Map> joinMap) { + CriteriaBuilder cb = this.entityManager.getCriteriaBuilder(); + + CriteriaQuery cq = cb.createTupleQuery(); + Root injectRoot = cq.from(Inject.class); + selectForAtomicTesting(cb, cq, injectRoot, joinMap); // -- Text Search and Filters -- if (specification != null) { @@ -399,10 +402,13 @@ public Page atomicTestings( return new PageImpl<>(injects, pageable, total); } - private void selectForAtomicTesting(CriteriaBuilder cb, CriteriaQuery cq, Root injectRoot) { + private void selectForAtomicTesting(CriteriaBuilder cb, CriteriaQuery cq, Root injectRoot, Map> joinMap) { // Joins - Join injectorContractJoin = createLeftJoin(injectRoot, "injectorContract"); - Join injectorJoin = injectorContractJoin.join("injector", JoinType.LEFT); + Join injectorContractJoin = injectRoot.join("injectorContract", JoinType.LEFT); + joinMap.put("injectorContract", injectorContractJoin); + + Join injectorJoin = injectorContractJoin.join("injector", JoinType.LEFT); + joinMap.put("injector", injectorJoin); // Subquery for InjectStatus Subquery statusSubquery = cq.subquery(Tuple.class); From b7bbf932438d3abe23c01a74d88af5d12d797b39 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Mon, 28 Oct 2024 10:40:36 +0100 Subject: [PATCH 06/10] [backend] Clean --- .../src/main/java/io/openbas/service/AtomicTestingService.java | 1 - 1 file changed, 1 deletion(-) diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java index 935c8bdc03..bf1c80c117 100644 --- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java +++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java @@ -416,7 +416,6 @@ private void selectForAtomicTesting(CriteriaBuilder cb, CriteriaQuery cq, Subquery dateSubquery = cq.subquery(Tuple.class); Root dateRoot = dateSubquery.from(InjectStatus.class); - // Define the subquery to select the necessary fields statusSubquery.select(statusRoot.get("name") ) .where(cb.equal(statusRoot.get("inject").get("id"), injectRoot.get("id"))); From bce95326a7fc65fafaaf48d153ea37aea5bf59a4 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Mon, 28 Oct 2024 10:42:29 +0100 Subject: [PATCH 07/10] [backend] Clean --- .../openbas/service/AtomicTestingService.java | 199 +++++++++--------- 1 file changed, 103 insertions(+), 96 deletions(-) diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java index bf1c80c117..76cf2c6327 100644 --- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java +++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java @@ -1,6 +1,5 @@ package io.openbas.service; -import static io.openbas.aop.LoggingAspect.logger; import static io.openbas.config.SessionHelper.currentUser; import static io.openbas.database.criteria.GenericCriteria.countQuery; import static io.openbas.database.model.Command.COMMAND_TYPE; @@ -297,39 +296,42 @@ public void deleteAtomicTesting(String injectId) { // -- PAGINATION -- - public Page findAllAtomicTestings(@NotNull final SearchPaginationInput searchPaginationInput) { - Map> joinMap = new HashMap<>(); - - Specification customSpec = Specification.where((root, query, cb) -> { - Predicate predicate = cb.conjunction(); - predicate = cb.and(predicate, cb.isNull(root.get("scenario"))); - predicate = cb.and(predicate, cb.isNull(root.get("exercise"))); - return predicate; - }); - return buildPaginationCriteriaBuilder( - (Specification specification, Specification specificationCount, Pageable pageable) -> this.atomicTestings( - customSpec.and(specification), - customSpec.and(specificationCount), - pageable, - joinMap - ), - searchPaginationInput, - Inject.class, - joinMap - ); - } - + public Page findAllAtomicTestings( + @NotNull final SearchPaginationInput searchPaginationInput) { + Map> joinMap = new HashMap<>(); + + Specification customSpec = + Specification.where( + (root, query, cb) -> { + Predicate predicate = cb.conjunction(); + predicate = cb.and(predicate, cb.isNull(root.get("scenario"))); + predicate = cb.and(predicate, cb.isNull(root.get("exercise"))); + return predicate; + }); + return buildPaginationCriteriaBuilder( + (Specification specification, + Specification specificationCount, + Pageable pageable) -> + this.atomicTestings( + customSpec.and(specification), + customSpec.and(specificationCount), + pageable, + joinMap), + searchPaginationInput, + Inject.class, + joinMap); + } - public Page atomicTestings( - Specification specification, - Specification specificationCount, - Pageable pageable, - Map> joinMap) { - CriteriaBuilder cb = this.entityManager.getCriteriaBuilder(); + public Page atomicTestings( + Specification specification, + Specification specificationCount, + Pageable pageable, + Map> joinMap) { + CriteriaBuilder cb = this.entityManager.getCriteriaBuilder(); - CriteriaQuery cq = cb.createTupleQuery(); - Root injectRoot = cq.from(Inject.class); - selectForAtomicTesting(cb, cq, injectRoot, joinMap); + CriteriaQuery cq = cb.createTupleQuery(); + Root injectRoot = cq.from(Inject.class); + selectForAtomicTesting(cb, cq, injectRoot, joinMap); // -- Text Search and Filters -- if (specification != null) { @@ -402,81 +404,86 @@ public Page atomicTestings( return new PageImpl<>(injects, pageable, total); } - private void selectForAtomicTesting(CriteriaBuilder cb, CriteriaQuery cq, Root injectRoot, Map> joinMap) { - // Joins - Join injectorContractJoin = injectRoot.join("injectorContract", JoinType.LEFT); - joinMap.put("injectorContract", injectorContractJoin); - - Join injectorJoin = injectorContractJoin.join("injector", JoinType.LEFT); - joinMap.put("injector", injectorJoin); - - // Subquery for InjectStatus - Subquery statusSubquery = cq.subquery(Tuple.class); - Root statusRoot = statusSubquery.from(InjectStatus.class); - Subquery dateSubquery = cq.subquery(Tuple.class); - Root dateRoot = dateSubquery.from(InjectStatus.class); - - 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"))); - - // Array aggregations - Expression injectExpectationIdsExpression = createJoinArrayAggOnId(cb, injectRoot, EXPECTATIONS); - Expression teamIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "teams"); - Expression assetIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assets"); - Expression assetGroupIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assetGroups"); - - // SELECT - cq.multiselect( - injectRoot.get("id").alias("inject_id"), - injectRoot.get("title").alias("inject_title"), - injectRoot.get("updatedAt").alias("inject_updated_at"), - injectorJoin.get("type").alias("inject_type"), - injectorContractJoin.alias("inject_injector_contract"), + private void selectForAtomicTesting( + CriteriaBuilder cb, + CriteriaQuery cq, + Root injectRoot, + Map> joinMap) { + // Joins + Join injectorContractJoin = injectRoot.join("injectorContract", JoinType.LEFT); + joinMap.put("injectorContract", injectorContractJoin); + + Join injectorJoin = injectorContractJoin.join("injector", JoinType.LEFT); + joinMap.put("injector", injectorJoin); + + // Subquery for InjectStatus + Subquery statusSubquery = cq.subquery(Tuple.class); + Root statusRoot = statusSubquery.from(InjectStatus.class); + Subquery dateSubquery = cq.subquery(Tuple.class); + Root dateRoot = dateSubquery.from(InjectStatus.class); + + 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"))); + + // Array aggregations + Expression injectExpectationIdsExpression = + createJoinArrayAggOnId(cb, injectRoot, EXPECTATIONS); + Expression teamIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "teams"); + Expression assetIdsExpression = createJoinArrayAggOnId(cb, injectRoot, "assets"); + Expression assetGroupIdsExpression = + createJoinArrayAggOnId(cb, injectRoot, "assetGroups"); + + // SELECT + cq.multiselect( + injectRoot.get("id").alias("inject_id"), + injectRoot.get("title").alias("inject_title"), + injectRoot.get("updatedAt").alias("inject_updated_at"), + injectorJoin.get("type").alias("inject_type"), + injectorContractJoin.alias("inject_injector_contract"), cb.selectCase() .when(cb.exists(statusSubquery), statusSubquery.select(statusRoot.get("name"))) - .otherwise(cb.nullLiteral(ExecutionStatus.class)).alias("inject_status"), + .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"), - injectExpectationIdsExpression.alias("inject_expectations"), - teamIdsExpression.alias("inject_teams"), - assetIdsExpression.alias("inject_assets"), - assetGroupIdsExpression.alias("inject_asset_groups")) + .when( + cb.exists(dateSubquery), dateSubquery.select(dateRoot.get("trackingSentDate"))) + .otherwise(cb.nullLiteral(Instant.class)) + .alias("tracking_sent_date"), + injectExpectationIdsExpression.alias("inject_expectations"), + teamIdsExpression.alias("inject_teams"), + assetIdsExpression.alias("inject_assets"), + assetGroupIdsExpression.alias("inject_asset_groups")) .distinct(true); // GROUP BY cq.groupBy( Arrays.asList( - injectRoot.get("id"), - injectorContractJoin.get("id"), - injectorJoin.get("id") - )); + injectRoot.get("id"), injectorContractJoin.get("id"), injectorJoin.get("id"))); } private List execAtomicTesting(TypedQuery query) { - return query.getResultList() - .stream() - .map(tuple -> { - InjectStatus injectStatus = new InjectStatus(); - injectStatus.setName(tuple.get("inject_status", ExecutionStatus.class)); - injectStatus.setTrackingSentDate(tuple.get("tracking_sent_date", Instant.class)); - 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), - 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)); - } - ) + return query.getResultList().stream() + .map( + tuple -> { + InjectStatus injectStatus = new InjectStatus(); + injectStatus.setName(tuple.get("inject_status", ExecutionStatus.class)); + injectStatus.setTrackingSentDate(tuple.get("tracking_sent_date", Instant.class)); + 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), + 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)); + }) .toList(); } From 82dd72dcaf154860e943dbed90d413ee9ef8f01f Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Tue, 29 Oct 2024 14:31:28 +0100 Subject: [PATCH 08/10] [backend] wip --- .../src/admin/components/atomic_testings/InjectDtoList.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx b/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx index f357aa3a9c..48ba77899b 100644 --- a/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx +++ b/openbas-front/src/admin/components/atomic_testings/InjectDtoList.tsx @@ -121,7 +121,7 @@ const InjectDtoList: FunctionComponent = ({ label: 'Status', isSortable: false, value: (injectDto: InjectResultDTO) => { - return (); + return (); }, }, { From 633e76017ba79285c37eef0cefc3f4845a9ba447 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Tue, 29 Oct 2024 14:43:46 +0100 Subject: [PATCH 09/10] [backend] clean --- .../src/main/java/io/openbas/service/AtomicTestingService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java index 76cf2c6327..486675ed7b 100644 --- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java +++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java @@ -470,7 +470,8 @@ private List execAtomicTesting(TypedQuery query) { .map( tuple -> { InjectStatus injectStatus = new InjectStatus(); - injectStatus.setName(tuple.get("inject_status", ExecutionStatus.class)); + ExecutionStatus status = tuple.get("inject_status", ExecutionStatus.class); + injectStatus.setName(status != null ? status : ExecutionStatus.DRAFT); injectStatus.setTrackingSentDate(tuple.get("tracking_sent_date", Instant.class)); return new AtomicTestingOutput( tuple.get("inject_id", String.class), From a08e44e0bf08aa34b9f6924e0b18896a2656c305 Mon Sep 17 00:00:00 2001 From: Stephanya Casanova Date: Tue, 29 Oct 2024 14:51:26 +0100 Subject: [PATCH 10/10] [backend] clean --- .../java/io/openbas/service/AtomicTestingService.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java index 486675ed7b..523bb90cc4 100644 --- a/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java +++ b/openbas-api/src/main/java/io/openbas/service/AtomicTestingService.java @@ -469,10 +469,13 @@ private List execAtomicTesting(TypedQuery query) { return query.getResultList().stream() .map( tuple -> { - InjectStatus injectStatus = new InjectStatus(); + InjectStatus injectStatus = null; ExecutionStatus status = tuple.get("inject_status", ExecutionStatus.class); - injectStatus.setName(status != null ? status : ExecutionStatus.DRAFT); - injectStatus.setTrackingSentDate(tuple.get("tracking_sent_date", Instant.class)); + if (status != null) { + injectStatus = new InjectStatus(); + injectStatus.setName(status); + injectStatus.setTrackingSentDate(tuple.get("tracking_sent_date", Instant.class)); + } return new AtomicTestingOutput( tuple.get("inject_id", String.class), tuple.get("inject_title", String.class),