From af4fc147d5e434589ef5f0d19555b1c553a2250b Mon Sep 17 00:00:00 2001 From: "brian.mulier" Date: Tue, 10 Sep 2024 14:34:43 +0200 Subject: [PATCH] fix(webserver): scope parameter for flows & executions is now a list --- .../kestra/core/models/flows/FlowScope.java | 6 +-- .../ExecutionRepositoryInterface.java | 8 ++-- .../repositories/FlowRepositoryInterface.java | 4 +- .../AbstractExecutionRepositoryTest.java | 38 +++++++++++++++++-- .../AbstractJdbcExecutionRepository.java | 27 ++++++------- .../AbstractJdbcFlowRepository.java | 16 ++++---- .../controllers/api/ExecutionController.java | 16 ++++---- .../controllers/api/FlowController.java | 12 +++--- .../controllers/api/StatsController.java | 2 +- 9 files changed, 77 insertions(+), 52 deletions(-) diff --git a/core/src/main/java/io/kestra/core/models/flows/FlowScope.java b/core/src/main/java/io/kestra/core/models/flows/FlowScope.java index e8503bfc2a..44221c56fd 100644 --- a/core/src/main/java/io/kestra/core/models/flows/FlowScope.java +++ b/core/src/main/java/io/kestra/core/models/flows/FlowScope.java @@ -1,6 +1,6 @@ package io.kestra.core.models.flows; public enum FlowScope { - USER, - SYSTEM - } \ No newline at end of file + USER, + SYSTEM +} \ No newline at end of file diff --git a/core/src/main/java/io/kestra/core/repositories/ExecutionRepositoryInterface.java b/core/src/main/java/io/kestra/core/repositories/ExecutionRepositoryInterface.java index 617f00ee9a..7c6d29821c 100644 --- a/core/src/main/java/io/kestra/core/repositories/ExecutionRepositoryInterface.java +++ b/core/src/main/java/io/kestra/core/repositories/ExecutionRepositoryInterface.java @@ -47,7 +47,7 @@ ArrayListTotal find( Pageable pageable, @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, @@ -61,7 +61,7 @@ ArrayListTotal find( default Flux find( @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, @@ -77,7 +77,7 @@ default Flux find( Flux find( @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, @@ -122,7 +122,7 @@ List dailyStatisticsForAllTenants( List dailyStatistics( @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, diff --git a/core/src/main/java/io/kestra/core/repositories/FlowRepositoryInterface.java b/core/src/main/java/io/kestra/core/repositories/FlowRepositoryInterface.java index 59c402dfe5..95052e3162 100644 --- a/core/src/main/java/io/kestra/core/repositories/FlowRepositoryInterface.java +++ b/core/src/main/java/io/kestra/core/repositories/FlowRepositoryInterface.java @@ -114,7 +114,7 @@ ArrayListTotal find( Pageable pageable, @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable Map labels ); @@ -122,7 +122,7 @@ ArrayListTotal find( List findWithSource( @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable Map labels ); diff --git a/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java b/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java index f2bf035cc0..87915ee720 100644 --- a/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java +++ b/core/src/test/java/io/kestra/core/repositories/AbstractExecutionRepositoryTest.java @@ -449,7 +449,22 @@ protected void dailyStatistics() throws InterruptedException { result = executionRepository.dailyStatistics( null, null, - FlowScope.USER, + List.of(FlowScope.USER, FlowScope.SYSTEM), + null, + null, + ZonedDateTime.now().minusDays(10), + ZonedDateTime.now(), + null, + null, + false); + + assertThat(result.size(), is(11)); + assertThat(result.get(10).getExecutionCounts().get(State.Type.SUCCESS), is(21L)); + + result = executionRepository.dailyStatistics( + null, + null, + List.of(FlowScope.USER), null, null, ZonedDateTime.now().minusDays(10), @@ -463,7 +478,7 @@ protected void dailyStatistics() throws InterruptedException { result = executionRepository.dailyStatistics( null, null, - FlowScope.SYSTEM, + List.of(FlowScope.SYSTEM), null, null, ZonedDateTime.now().minusDays(10), @@ -512,7 +527,22 @@ protected void taskRunsDailyStatistics() { result = executionRepository.dailyStatistics( null, null, - FlowScope.USER, + List.of(FlowScope.USER, FlowScope.SYSTEM), + null, + null, + ZonedDateTime.now().minusDays(10), + ZonedDateTime.now(), + null, + null, + true); + + assertThat(result.size(), is(11)); + assertThat(result.get(10).getExecutionCounts().get(State.Type.SUCCESS), is(57L)); + + result = executionRepository.dailyStatistics( + null, + null, + List.of(FlowScope.USER), null, null, ZonedDateTime.now().minusDays(10), @@ -526,7 +556,7 @@ protected void taskRunsDailyStatistics() { result = executionRepository.dailyStatistics( null, null, - FlowScope.SYSTEM, + List.of(FlowScope.SYSTEM), null, null, ZonedDateTime.now().minusDays(10), diff --git a/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcExecutionRepository.java b/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcExecutionRepository.java index 6afe7ea6b0..c4790dbc41 100644 --- a/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcExecutionRepository.java +++ b/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcExecutionRepository.java @@ -53,12 +53,7 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; -import java.util.AbstractMap; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; @@ -159,7 +154,7 @@ public ArrayListTotal find( Pageable pageable, @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, @@ -198,7 +193,7 @@ public ArrayListTotal find( public Flux find( @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, @@ -247,7 +242,7 @@ private SelectConditionStep> findSelect( DSLContext context, @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, @@ -365,7 +360,7 @@ public List dailyStatisticsForAllTenants( public List dailyStatistics( @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable ZonedDateTime startDate, @@ -462,7 +457,7 @@ private Results dailyStatisticsQuery( List> fields, @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, List flows, @@ -490,7 +485,7 @@ private Results dailyStatisticsQuery( Condition defaultFilter, List> fields, @Nullable String query, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, List flows, @@ -543,7 +538,7 @@ private Results dailyStatisticsQuery( private SelectConditionStep filteringQuery( SelectConditionStep select, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable String flowId, @Nullable List flows, @@ -552,10 +547,10 @@ private SelectConditionStep filteringQuery( @Nullable String triggerExecutionId, @Nullable ChildFilter childFilter ) { - if (scope != null) { - if (scope.equals(FlowScope.USER)) { + if (scope != null && !scope.containsAll(Arrays.stream(FlowScope.values()).toList())) { + if (scope.contains(FlowScope.USER)) { select = select.and(field("namespace").ne(namespaceUtils.getSystemFlowNamespace())); - } else if (scope.equals(FlowScope.SYSTEM)) { + } else if (scope.contains(FlowScope.SYSTEM)) { select = select.and(field("namespace").eq(namespaceUtils.getSystemFlowNamespace())); } } diff --git a/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcFlowRepository.java b/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcFlowRepository.java index 22ad2d858d..374147a2ce 100644 --- a/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcFlowRepository.java +++ b/jdbc/src/main/java/io/kestra/jdbc/repository/AbstractJdbcFlowRepository.java @@ -392,7 +392,7 @@ public ArrayListTotal find( Pageable pageable, @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable Map labels ) { @@ -405,10 +405,10 @@ public ArrayListTotal find( select.and(this.findCondition(query, labels)); - if (scope != null) { - if (scope.equals(FlowScope.USER)) { + if (scope != null && !scope.containsAll(Arrays.stream(FlowScope.values()).toList())) { + if (scope.contains(FlowScope.USER)) { select = select.and(field("namespace").ne(namespaceUtils.getSystemFlowNamespace())); - } else if (scope.equals(FlowScope.SYSTEM)) { + } else if (scope.contains(FlowScope.SYSTEM)) { select = select.and(field("namespace").eq(namespaceUtils.getSystemFlowNamespace())); } } @@ -425,7 +425,7 @@ public ArrayListTotal find( public List findWithSource( @Nullable String query, @Nullable String tenantId, - @Nullable FlowScope scope, + @Nullable List scope, @Nullable String namespace, @Nullable Map labels ) { @@ -438,10 +438,10 @@ public List findWithSource( select.and(this.findCondition(query, labels)); - if (scope != null) { - if (scope.equals(FlowScope.USER)) { + if (scope != null && !new HashSet<>(scope).containsAll(Arrays.stream(FlowScope.values()).toList())) { + if (scope.contains(FlowScope.USER)) { select = select.and(field("namespace").ne(namespaceUtils.getSystemFlowNamespace())); - } else if (scope.equals(FlowScope.SYSTEM)) { + } else if (scope.contains(FlowScope.SYSTEM)) { select = select.and(field("namespace").eq(namespaceUtils.getSystemFlowNamespace())); } } diff --git a/webserver/src/main/java/io/kestra/webserver/controllers/api/ExecutionController.java b/webserver/src/main/java/io/kestra/webserver/controllers/api/ExecutionController.java index cc8189cae2..607f8b8559 100644 --- a/webserver/src/main/java/io/kestra/webserver/controllers/api/ExecutionController.java +++ b/webserver/src/main/java/io/kestra/webserver/controllers/api/ExecutionController.java @@ -153,7 +153,7 @@ public PagedResults find( @Parameter(description = "The current page size") @QueryValue(defaultValue = "10") int size, @Parameter(description = "The sort of current page") @Nullable @QueryValue List sort, @Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query, - @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") FlowScope scope, + @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") List scope, @Parameter(description = "A namespace filter prefix") @Nullable @QueryValue String namespace, @Parameter(description = "A flow id filter") @Nullable @QueryValue String flowId, @Parameter(description = "The start datetime") @Nullable @Format("yyyy-MM-dd'T'HH:mm[:ss][.SSS][XXX]") @QueryValue ZonedDateTime startDate, @@ -349,7 +349,7 @@ public MutableHttpResponse deleteByIds( @Operation(tags = {"Executions"}, summary = "Delete executions filter by query parameters") public HttpResponse deleteByQuery( @Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query, - @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") FlowScope scope, + @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") List scope, @Parameter(description = "A namespace filter prefix") @Nullable @QueryValue String namespace, @Parameter(description = "A flow id filter") @Nullable @QueryValue String flowId, @Parameter(description = "The start datetime") @Nullable @Format("yyyy-MM-dd'T'HH:mm[:ss][.SSS][XXX]") @QueryValue ZonedDateTime startDate, @@ -762,7 +762,7 @@ public MutableHttpResponse restartByIds( @Operation(tags = {"Executions"}, summary = "Restart executions filter by query parameters") public HttpResponse restartByQuery( @Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query, - @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") FlowScope scope, + @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") List scope, @Parameter(description = "A namespace filter prefix") @Nullable @QueryValue String namespace, @Parameter(description = "A flow id filter") @Nullable @QueryValue String flowId, @Parameter(description = "The start datetime") @Nullable @Format("yyyy-MM-dd'T'HH:mm[:ss][.SSS][XXX]") @QueryValue ZonedDateTime startDate, @@ -963,7 +963,7 @@ public HttpResponse changeStatusById( @ApiResponse(responseCode = "422", description = "Changed status with errors", content = {@Content(schema = @Schema(implementation = BulkErrorResponse.class))}) public HttpResponse changeStatusByQuery( @Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query, - @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") FlowScope scope, + @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") List scope, @Parameter(description = "A namespace filter prefix") @Nullable @QueryValue String namespace, @Parameter(description = "A flow id filter") @Nullable @QueryValue String flowId, @Parameter(description = "The start datetime") @Nullable @Format("yyyy-MM-dd'T'HH:mm[:ss][.SSS][XXX]") @QueryValue ZonedDateTime startDate, @@ -1186,7 +1186,7 @@ public MutableHttpResponse resumeByIds( @Operation(tags = {"Executions"}, summary = "Resume executions filter by query parameters") public HttpResponse resumeByQuery( @Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query, - @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") FlowScope scope, + @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") List scope, @Parameter(description = "A namespace filter prefix") @Nullable @QueryValue String namespace, @Parameter(description = "A flow id filter") @Nullable @QueryValue String flowId, @Parameter(description = "The start datetime") @Nullable @Format("yyyy-MM-dd'T'HH:mm[:ss][.SSS][XXX]") @QueryValue ZonedDateTime startDate, @@ -1228,7 +1228,7 @@ public HttpResponse resumeByQuery( @Operation(tags = {"Executions"}, summary = "Kill executions filter by query parameters") public HttpResponse killByQuery( @Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query, - @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") FlowScope scope, + @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") List scope, @Parameter(description = "A namespace filter prefix") @Nullable @QueryValue String namespace, @Parameter(description = "A flow id filter") @Nullable @QueryValue String flowId, @Parameter(description = "The start datetime") @Nullable @Format("yyyy-MM-dd'T'HH:mm[:ss][.SSS][XXX]") @QueryValue ZonedDateTime startDate, @@ -1270,7 +1270,7 @@ public HttpResponse killByQuery( @Operation(tags = {"Executions"}, summary = "Create new executions from old ones filter by query parameters. Keep the flow revision") public HttpResponse replayByQuery( @Parameter(description = "A string filter") @Nullable @QueryValue(value = "q") String query, - @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") FlowScope scope, + @Parameter(description = "The scope of the executions to include") @Nullable @QueryValue(value = "scope") List scope, @Parameter(description = "A namespace filter prefix") @Nullable @QueryValue String namespace, @Parameter(description = "A flow id filter") @Nullable @QueryValue String flowId, @Parameter(description = "The start datetime") @Nullable @Format("yyyy-MM-dd'T'HH:mm[:ss][.SSS][XXX]") @QueryValue ZonedDateTime startDate, @@ -1562,7 +1562,7 @@ public record SetLabelsByIdsRequest(List executionsId, List