From eaa30129cc30883375537acee527ef5d68e3d301 Mon Sep 17 00:00:00 2001 From: Vegard Smines Date: Mon, 4 Sep 2023 11:15:39 +0200 Subject: [PATCH] added support for fetching single task --- .../src/components/TaskAccordionButton.tsx | 2 +- .../src/components/TaskList.tsx | 39 +++----------- .../src/components/TaskRunButton.tsx | 14 +++-- .../src/components/TitleRow.tsx | 54 +++++++++++++++++++ .../src/services/getTasks.ts | 4 +- .../controller/TaskController.java | 9 ++++ .../model/GetTasksResponse.java | 9 ++-- .../model/TaskDetailsRequestParams.java | 34 ++++++++++++ .../model/TaskRequestParams.java | 1 - .../dbscheduleruiapi/service/TaskLogic.java | 27 +++++++--- .../dbscheduleruiapi/util/TaskPagination.java | 14 +++++ .../util/mapper/TaskMapper.java | 6 +++ 12 files changed, 164 insertions(+), 49 deletions(-) create mode 100644 db-scheduler-ui-frontend/src/components/TitleRow.tsx create mode 100644 db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskDetailsRequestParams.java create mode 100644 db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/TaskPagination.java diff --git a/db-scheduler-ui-frontend/src/components/TaskAccordionButton.tsx b/db-scheduler-ui-frontend/src/components/TaskAccordionButton.tsx index 80c62108..939a5e5a 100644 --- a/db-scheduler-ui-frontend/src/components/TaskAccordionButton.tsx +++ b/db-scheduler-ui-frontend/src/components/TaskAccordionButton.tsx @@ -78,7 +78,7 @@ export const TaskAccordionButton: React.FC = ({ {!picked && ( )} diff --git a/db-scheduler-ui-frontend/src/components/TaskList.tsx b/db-scheduler-ui-frontend/src/components/TaskList.tsx index 2d2975d2..addf1064 100644 --- a/db-scheduler-ui-frontend/src/components/TaskList.tsx +++ b/db-scheduler-ui-frontend/src/components/TaskList.tsx @@ -1,6 +1,6 @@ import React, { useEffect, useState } from 'react'; -import { Accordion, Box, HStack } from '@chakra-ui/react'; +import { Accordion, Box } from '@chakra-ui/react'; import TaskCard from './TaskCard'; import { FilterBy, @@ -13,7 +13,7 @@ import { import { useQuery } from '@tanstack/react-query'; import PaginationButtons from 'src/components/PaginationButtons'; import { FilterBox } from 'src/components/FilterBox'; -import { SortButton } from 'src/components/SortButton'; +import TitleRow from 'src/components/TitleRow'; const TaskList: React.FC = () => { const [currentFilter, setCurrentFilter] = useState(FilterBy.All); @@ -48,35 +48,12 @@ const TaskList: React.FC = () => { setCurrentFilter={setCurrentFilter} /> - - - Status - - - - Task-ID - - - + {data?.tasks?.map((task) => ( = ({ style={style} onClick={(event) => { event.stopPropagation(); - runTask(taskInstance, taskName).then(() => refetch()); + runTask(taskInstance[0], taskName).then(() => refetch()); }} iconSpacing={2} width={100} @@ -39,14 +39,20 @@ export const TaskRunButton: React.FC = ({ }} fontWeight="normal" leftIcon={ - consecutiveFailures > 0 ? ( + taskInstance.length > 1 ? ( + <> + ) : consecutiveFailures > 0 ? ( ) : ( ) } > - {consecutiveFailures > 0 ? 'Rerun' : 'Run'} + {taskInstance.length > 1 + ? 'See all' + : consecutiveFailures > 0 + ? 'Rerun' + : 'Run'} )} diff --git a/db-scheduler-ui-frontend/src/components/TitleRow.tsx b/db-scheduler-ui-frontend/src/components/TitleRow.tsx new file mode 100644 index 00000000..b868f95c --- /dev/null +++ b/db-scheduler-ui-frontend/src/components/TitleRow.tsx @@ -0,0 +1,54 @@ +import React from 'react'; + +import { Box, HStack } from '@chakra-ui/react'; +import { SortBy } from 'src/services/getTasks'; + +import { SortButton } from 'src/components/SortButton'; + +interface TitleRowProps { + currentSort: SortBy; + setCurrentSort: React.Dispatch>; + sortAsc: boolean; + setSortAsc: React.Dispatch>; + detailView?: boolean; +} + +const TitleRow: React.FC = ({ + currentSort, + setCurrentSort, + setSortAsc, + sortAsc, + detailView, +}) => ( + + + Status + + + + Task-ID + + + +); + +export default TitleRow; diff --git a/db-scheduler-ui-frontend/src/services/getTasks.ts b/db-scheduler-ui-frontend/src/services/getTasks.ts index b8fd9ea3..d867bc1f 100644 --- a/db-scheduler-ui-frontend/src/services/getTasks.ts +++ b/db-scheduler-ui-frontend/src/services/getTasks.ts @@ -25,7 +25,9 @@ export const getTasks = async ( filter = FilterBy.All, { pageNumber = 1, limit = 10 }: PaginationParams, sorting = SortBy.Default, - isAsc = true + isAsc = true, + taskName?: string, + taskId?: string, ): Promise => { const queryParams = new URLSearchParams(); diff --git a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/controller/TaskController.java b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/controller/TaskController.java index dab21fc3..f714ccf1 100644 --- a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/controller/TaskController.java +++ b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/controller/TaskController.java @@ -1,8 +1,12 @@ package com.github.bekk.dbscheduleruiapi.controller; import com.github.bekk.dbscheduleruiapi.model.GetTasksResponse; +import com.github.bekk.dbscheduleruiapi.model.TaskDetailsRequestParams; import com.github.bekk.dbscheduleruiapi.service.TaskLogic; import com.github.bekk.dbscheduleruiapi.model.TaskRequestParams; + +import java.util.Optional; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -22,6 +26,11 @@ public GetTasksResponse getTasks(TaskRequestParams params) { return taskLogic.getAllTasks(params); } + @GetMapping("/tasks/details") + public GetTasksResponse getTasks(TaskDetailsRequestParams params) { + return taskLogic.getTask(params); + } + @PostMapping("/rerun") public void runNow(@RequestParam String id, @RequestParam String name) { taskLogic.runTaskNow(id, name); diff --git a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/GetTasksResponse.java b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/GetTasksResponse.java index 714a0e79..9135407f 100644 --- a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/GetTasksResponse.java +++ b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/GetTasksResponse.java @@ -3,15 +3,16 @@ import java.util.List; + public class GetTasksResponse { private int numberOfTasks; private int numberOfPages; private List tasks; - public GetTasksResponse(int numberOfTasks, int numberOfPages, List tasks) { - this.numberOfTasks = numberOfTasks; - this.numberOfPages = numberOfPages; - this.tasks = tasks; + public GetTasksResponse(int totalTasks, List pagedTasks) { + this.numberOfTasks = totalTasks; + this.numberOfPages = (int) Math.ceil((double) totalTasks / pagedTasks.size()); + this.tasks = pagedTasks; } public int getNumberOfTasks() { diff --git a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskDetailsRequestParams.java b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskDetailsRequestParams.java new file mode 100644 index 00000000..b664880e --- /dev/null +++ b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskDetailsRequestParams.java @@ -0,0 +1,34 @@ +package com.github.bekk.dbscheduleruiapi.model; + +import java.util.Optional; + +public class TaskDetailsRequestParams extends TaskRequestParams { + + private Optional taskId; + private String taskName; + + public TaskDetailsRequestParams() { + } + + public TaskDetailsRequestParams(TaskFilter filter, int pageNumber, int size, TaskSort sorting, boolean asc, String taskName, Optional taskId) { + super(filter, pageNumber, size, sorting, asc); + this.taskId = taskId; + this.taskName = taskName; + } + + public Optional getTaskId() { + return taskId; + } + + public void setTaskId(Optional taskId) { + this.taskId = taskId; + } + + public String getTaskName() { + return taskName; + } + + public void setTaskName(String taskName) { + this.taskName = taskName; + } +} diff --git a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskRequestParams.java b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskRequestParams.java index 8f074713..84bfb6c2 100644 --- a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskRequestParams.java +++ b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/model/TaskRequestParams.java @@ -12,7 +12,6 @@ public TaskRequestParams() { } public TaskRequestParams(TaskFilter filter, int pageNumber, int size, TaskSort sorting, boolean asc) { - System.out.println(size); this.filter = filter; this.pageNumber = pageNumber; this.size = size; diff --git a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/service/TaskLogic.java b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/service/TaskLogic.java index c6a7e18b..d1c7dd3e 100644 --- a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/service/TaskLogic.java +++ b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/service/TaskLogic.java @@ -1,8 +1,10 @@ package com.github.bekk.dbscheduleruiapi.service; import com.github.bekk.dbscheduleruiapi.model.GetTasksResponse; +import com.github.bekk.dbscheduleruiapi.model.TaskDetailsRequestParams; import com.github.bekk.dbscheduleruiapi.model.TaskModel; import com.github.bekk.dbscheduleruiapi.model.TaskRequestParams; +import com.github.bekk.dbscheduleruiapi.util.TaskPagination; import com.github.bekk.dbscheduleruiapi.util.mapper.TaskMapper; import com.github.kagkarlsson.scheduler.ScheduledExecution; import com.github.kagkarlsson.scheduler.Scheduler; @@ -77,16 +79,27 @@ public GetTasksResponse getAllTasks(TaskRequestParams params) { return params.isAsc() ? comparisonResult : -comparisonResult; }); } + List pagedTasks = TaskPagination.paginate(tasks, params.getPageNumber(), params.getSize()); - int totalTasks = tasks.size(); - int numberOfPages = (int) Math.ceil((double) totalTasks / params.getSize()); + return new GetTasksResponse(tasks.size(), pagedTasks); - int startIndex = params.getPageNumber() * params.getSize(); - int endIndex = Math.min(startIndex + params.getSize(), totalTasks); + } - List pagedTasks = (startIndex < endIndex) ? tasks.subList(startIndex, endIndex) : new ArrayList<>(); + public List getTask(TaskDetailsRequestParams params) { + List tasks = params.taskId.isPresent() + ? TaskMapper.mapAllExecutionsToTaskModelUngrouped(scheduler.getScheduledExecutions(), scheduler.getCurrentlyExecuting()).stream().filter(task -> { + return task.getTaskName().equals(params.taskName) && task.getTaskInstance().get(0).equals(params.taskId.get()); + }).collect(Collectors.toList()) + : TaskMapper.mapAllExecutionsToTaskModel(scheduler.getScheduledExecutions(), scheduler.getCurrentlyExecuting()).stream().filter(task -> { + return task.getTaskName().equals(params.taskName); + }).collect(Collectors.toList()); - return new GetTasksResponse(totalTasks, numberOfPages, pagedTasks); - } + if (tasks.isEmpty()) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND,"No ScheduledExecution found for taskName: " + params.taskName + ", taskId: " + params.taskId); + } + + List pagedTasks = TaskPagination.paginate(tasks, params.getPageNumber(), params.getSize()); + return new GetTasksResponse(tasks.size(), pagedTasks); + } } diff --git a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/TaskPagination.java b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/TaskPagination.java new file mode 100644 index 00000000..b2e64caa --- /dev/null +++ b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/TaskPagination.java @@ -0,0 +1,14 @@ +package com.github.bekk.dbscheduleruiapi.util; + +import java.util.ArrayList; +import java.util.List; + +public class TaskPagination { + + public static List paginate(List allItems, int pageNumber, int pageSize) { + int startIndex = pageNumber * pageSize; + int endIndex = Math.min(startIndex + pageSize, allItems.size()); + + return (startIndex < endIndex) ? allItems.subList(startIndex, endIndex) : new ArrayList<>(); + } +} diff --git a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/mapper/TaskMapper.java b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/mapper/TaskMapper.java index 8df72c99..1412fc51 100644 --- a/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/mapper/TaskMapper.java +++ b/db-scheduler-ui/src/main/java/com/github/bekk/dbscheduleruiapi/util/mapper/TaskMapper.java @@ -71,4 +71,10 @@ public static List mapAllExecutionsToTaskModel(List mapAllExecutionsToTaskModelUngrouped(List> scheduledExecutions, List currentlyExecuting) { + List scheduled = mapScheduledExecutionsToTaskModel(scheduledExecutions); + scheduled.addAll(mapCurrentlyExecutingToTaskModel(currentlyExecuting)); + return scheduled; + } } \ No newline at end of file