-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
control-service: Add Data Job Executions to the graphql
Testing: Local run of tests and manual testing, also added some new test cases in unit tests and some code change in Integration tests Signed-off-by: Plamen Kostov <[email protected]>
- Loading branch information
Plamen Kostov
committed
Sep 26, 2021
1 parent
e8f1772
commit fe23af0
Showing
25 changed files
with
547 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...control_service/src/main/java/com/vmware/taurus/service/graphql/ExecutionDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
* Copyright (c) 2021 VMware, Inc. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.vmware.taurus.service.graphql; | ||
|
||
import com.vmware.taurus.datajobs.ToApiModelConverter; | ||
import com.vmware.taurus.service.JobExecutionRepository; | ||
import com.vmware.taurus.service.graphql.model.V2DataJob; | ||
import com.vmware.taurus.service.graphql.strategy.datajob.JobFieldStrategyBy; | ||
import com.vmware.taurus.service.model.DataJobExecution; | ||
import com.vmware.taurus.service.graphql.model.Filter; | ||
import com.vmware.taurus.service.graphql.model.ExecutionQueryVariables; | ||
import graphql.GraphQLException; | ||
import graphql.schema.DataFetchingEnvironment; | ||
import graphql.schema.SelectedField; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Data fetcher class for Data Job Executions | ||
* | ||
* Data fetchers are classes that provides to graphql api the needed data while it modify the source data: | ||
* By providing list of data jobs it alter each job and attach its execution while reading requested | ||
* information from graphql query to specify how many executions, are they sorted by specific field, etc. | ||
* | ||
* Currently, execution data fetcher does not provide filtering by specifying fields due | ||
* to its post-pagination loading - the execution data is attached after slicing of the requested page. | ||
*/ | ||
@Component | ||
public class ExecutionDataFetcher { | ||
|
||
private final JobExecutionRepository jobsExecutionRepository; | ||
|
||
public ExecutionDataFetcher(JobExecutionRepository jobsExecutionRepository) { | ||
this.jobsExecutionRepository = jobsExecutionRepository; | ||
} | ||
|
||
List<V2DataJob> populateExecutions(List<V2DataJob> allDataJob, DataFetchingEnvironment dataFetchingEnvironment) { | ||
final ExecutionQueryVariables queryVariables = fetchQueryVariables(dataFetchingEnvironment); | ||
final Pageable pageable = constructPageable(queryVariables); | ||
allDataJob.forEach(dataJob -> { | ||
if (dataJob.getDeployments() != null) { | ||
List<DataJobExecution> executionsPerJob = jobsExecutionRepository.findDataJobExecutionsByDataJobName(dataJob.getJobName(), pageable); | ||
dataJob.getDeployments() | ||
.stream() | ||
.findFirst() | ||
.ifPresent(deployment -> deployment.setExecutions( | ||
executionsPerJob | ||
.stream() | ||
.map(ToApiModelConverter::jobExecutionToConvert) | ||
.collect(Collectors.toList()))); | ||
} | ||
}); | ||
return allDataJob; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private ExecutionQueryVariables fetchQueryVariables(DataFetchingEnvironment dataFetchingEnvironment) { | ||
ExecutionQueryVariables queryVariables = new ExecutionQueryVariables(); | ||
SelectedField executionFields = dataFetchingEnvironment | ||
.getSelectionSet().getField(JobFieldStrategyBy.DEPLOYMENT_EXECUTIONS.getPath()); | ||
|
||
Map<String, Object> execArgs = executionFields.getArguments(); | ||
if (execArgs.get("pageNumber") == null || execArgs.get("pageSize") == null) { | ||
throw new GraphQLException("Executions field must contain pageSize and pageNumber"); | ||
} | ||
queryVariables.setPageNumber((int) execArgs.get("pageNumber")); | ||
queryVariables.setPageSize((int) execArgs.get("pageSize")); | ||
GraphQLUtils.validatePageInput(queryVariables.getPageSize(), queryVariables.getPageNumber()); | ||
queryVariables.setFilters(GraphQLUtils.convertFilters((ArrayList<LinkedHashMap<String, String>>) execArgs.get("filter"))); | ||
validateFilterInputForExecutions(queryVariables.getFilters()); | ||
|
||
return queryVariables; | ||
} | ||
|
||
/** | ||
* As we receive filters as custom GraphQL object, this method translated it to Spring data Pageable element | ||
* By default if there isn't any fields specified we return only paginating details | ||
* If sorting is not provided we use the default (ASC), by design it take maximum 1 sorting | ||
* @param queryVar Query variables which holds multiple Filter object | ||
* @return Pageable element containing page and sort | ||
*/ | ||
private Pageable constructPageable(ExecutionQueryVariables queryVar) { | ||
Sort.Direction direction = queryVar.getFilters().stream() | ||
.map(Filter::getSort) | ||
.filter(Objects::nonNull) | ||
.findFirst() | ||
.orElse(Sort.Direction.ASC); | ||
|
||
List<Sort.Order> order = queryVar.getFilters().stream() | ||
.map(Filter::getProperty) | ||
.filter(Objects::nonNull) | ||
.map(s -> s.replace(JobFieldStrategyBy.DEPLOYMENT_EXECUTIONS.getField() + ".", "")) | ||
.map(s -> new Sort.Order(direction, s)) | ||
.collect(Collectors.toList()); | ||
|
||
PageRequest pageRequest = PageRequest.of(queryVar.getPageNumber() - 1, queryVar.getPageSize()); | ||
return order.isEmpty() ? pageRequest : pageRequest.withSort(Sort.by(order)); | ||
} | ||
|
||
void validateFilterInputForExecutions(List<Filter> executionsFilter) { | ||
final Optional<Filter> filterNotSupported = executionsFilter.stream() | ||
.filter(e -> e.getPattern() != null) | ||
.findAny(); | ||
if (filterNotSupported.isPresent()) { | ||
throw new GraphQLException("Using patterns for execution filtering is currently not supported"); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...pelines_control_service/src/main/java/com/vmware/taurus/service/graphql/GraphQLUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.vmware.taurus.service.graphql; | ||
|
||
import com.vmware.taurus.service.graphql.model.Filter; | ||
import graphql.GraphQLException; | ||
import lombok.experimental.UtilityClass; | ||
import org.springframework.data.domain.Sort; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
|
||
@UtilityClass | ||
public class GraphQLUtils { | ||
|
||
/** | ||
* GraphQL's library parses json query variables to List of LinkedHashMaps. In order to use later the | ||
* filtering and sorting by specific field easily we convert them to list of Filters | ||
* @see Filter | ||
* @see GraphQLDataFetchers | ||
* | ||
* @param rawFilters filters object in fetched from graphql environment to convert | ||
* @return List of converted filters | ||
*/ | ||
public static List<Filter> convertFilters(List<LinkedHashMap<String, String>> rawFilters) { | ||
List<Filter> filters = new ArrayList<>(); | ||
if (rawFilters != null && !rawFilters.isEmpty()) { | ||
rawFilters.forEach(map -> { | ||
if (map != null && !map.isEmpty()) { | ||
Sort.Direction direction = map.get("sort") == null ? null : Sort.Direction.valueOf(map.get("sort")); | ||
filters.add(new Filter(map.get("property"), map.get("pattern"), direction)); | ||
} | ||
}); | ||
} | ||
return filters; | ||
} | ||
|
||
public static void validatePageInput(int pageSize, int pageNumber) { | ||
if (pageSize < 1) { | ||
throw new GraphQLException("Page size cannot be less than 1"); | ||
} | ||
if (pageNumber < 1) { | ||
throw new GraphQLException("Page cannot be less than 1"); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.