Skip to content

Commit

Permalink
added support for fetching single task
Browse files Browse the repository at this point in the history
  • Loading branch information
vegarrsm committed Sep 4, 2023
1 parent 5d3c608 commit eaa3012
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const TaskAccordionButton: React.FC<TaskAccordionButtonProps> = ({
{!picked && (
<DotButton // Remove or adjust this button based on number of instances
taskName={taskName}
taskInstance={taskInstance[0]}
taskInstance={taskInstance}
style={{ marginRight: 5 }}
/>
)}
Expand Down
39 changes: 8 additions & 31 deletions db-scheduler-ui-frontend/src/components/TaskList.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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>(FilterBy.All);
Expand Down Expand Up @@ -48,35 +48,12 @@ const TaskList: React.FC = () => {
setCurrentFilter={setCurrentFilter}
/>
</Box>
<HStack
display={'flex'}
p="8px 16px"
justifyContent={'space-around'}
spacing={5}
>
<Box flex="1" textAlign="left" textColor={'#484848'} fontSize={'sm'}>
Status
</Box>
<SortButton
currentSort={currentSort}
setCurrentSort={setCurrentSort}
sortAsc={sortAsc}
setSortAsc={setSortAsc}
title={'Task Name'}
name={SortBy.Name}
/>
<Box flex="2" textAlign="left" textColor={'#484848'} fontSize={'sm'}>
Task-ID
</Box>
<SortButton
currentSort={currentSort}
setCurrentSort={setCurrentSort}
sortAsc={sortAsc}
setSortAsc={setSortAsc}
title={'Next Execution Time'}
name={SortBy.Default}
/>
</HStack>
<TitleRow
currentSort={currentSort}
setCurrentSort={setCurrentSort}
sortAsc={sortAsc}
setSortAsc={setSortAsc}
/>
<Accordion allowMultiple>
{data?.tasks?.map((task) => (
<TaskCard
Expand Down
14 changes: 10 additions & 4 deletions db-scheduler-ui-frontend/src/components/TaskRunButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PlayIcon, RepeatIcon } from '../assets/icons';
import React from 'react';

interface TaskRunButtonProps {
taskInstance: string;
taskInstance: string[];
taskName: string;
picked: boolean;
consecutiveFailures: number;
Expand All @@ -25,7 +25,7 @@ export const TaskRunButton: React.FC<TaskRunButtonProps> = ({
style={style}
onClick={(event) => {
event.stopPropagation();
runTask(taskInstance, taskName).then(() => refetch());
runTask(taskInstance[0], taskName).then(() => refetch());
}}
iconSpacing={2}
width={100}
Expand All @@ -39,14 +39,20 @@ export const TaskRunButton: React.FC<TaskRunButtonProps> = ({
}}
fontWeight="normal"
leftIcon={
consecutiveFailures > 0 ? (
taskInstance.length > 1 ? (
<></>
) : consecutiveFailures > 0 ? (
<RepeatIcon boxSize={6} />
) : (
<PlayIcon boxSize={4} />
)
}
>
{consecutiveFailures > 0 ? 'Rerun' : 'Run'}
{taskInstance.length > 1
? 'See all'
: consecutiveFailures > 0
? 'Rerun'
: 'Run'}
</Button>
)}
</>
Expand Down
54 changes: 54 additions & 0 deletions db-scheduler-ui-frontend/src/components/TitleRow.tsx
Original file line number Diff line number Diff line change
@@ -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<React.SetStateAction<SortBy>>;
sortAsc: boolean;
setSortAsc: React.Dispatch<React.SetStateAction<boolean>>;
detailView?: boolean;
}

const TitleRow: React.FC<TitleRowProps> = ({
currentSort,
setCurrentSort,
setSortAsc,
sortAsc,
detailView,
}) => (
<HStack
display={'flex'}
p="8px 16px"
justifyContent={'space-around'}
spacing={5}
>
<Box flex="1" textAlign="left" textColor={'#484848'} fontSize={'sm'}>
Status
</Box>
<SortButton
currentSort={currentSort}
setCurrentSort={setCurrentSort}
sortAsc={sortAsc}
setSortAsc={setSortAsc}
title={'Task Name'}
name={SortBy.Name}
/>
<Box flex="2" textAlign="left" textColor={'#484848'} fontSize={'sm'}>
Task-ID
</Box>
<SortButton
currentSort={currentSort}
setCurrentSort={setCurrentSort}
sortAsc={sortAsc}
setSortAsc={setSortAsc}
title={'Next Execution Time'}
name={SortBy.Default}
/>
</HStack>
);

export default TitleRow;
4 changes: 3 additions & 1 deletion db-scheduler-ui-frontend/src/services/getTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TasksResponse> => {
const queryParams = new URLSearchParams();

Expand Down
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@

import java.util.List;


public class GetTasksResponse {
private int numberOfTasks;
private int numberOfPages;
private List<TaskModel> tasks;

public GetTasksResponse(int numberOfTasks, int numberOfPages, List<TaskModel> tasks) {
this.numberOfTasks = numberOfTasks;
this.numberOfPages = numberOfPages;
this.tasks = tasks;
public GetTasksResponse(int totalTasks, List<TaskModel> pagedTasks) {
this.numberOfTasks = totalTasks;
this.numberOfPages = (int) Math.ceil((double) totalTasks / pagedTasks.size());
this.tasks = pagedTasks;
}

public int getNumberOfTasks() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.bekk.dbscheduleruiapi.model;

import java.util.Optional;

public class TaskDetailsRequestParams extends TaskRequestParams {

private Optional<String> taskId;
private String taskName;

public TaskDetailsRequestParams() {
}

public TaskDetailsRequestParams(TaskFilter filter, int pageNumber, int size, TaskSort sorting, boolean asc, String taskName, Optional<String> taskId) {
super(filter, pageNumber, size, sorting, asc);
this.taskId = taskId;
this.taskName = taskName;
}

public Optional<String> getTaskId() {
return taskId;
}

public void setTaskId(Optional<String> taskId) {
this.taskId = taskId;
}

public String getTaskName() {
return taskName;
}

public void setTaskName(String taskName) {
this.taskName = taskName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -77,16 +79,27 @@ public GetTasksResponse getAllTasks(TaskRequestParams params) {
return params.isAsc() ? comparisonResult : -comparisonResult;
});
}
List<TaskModel> 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<TaskModel> pagedTasks = (startIndex < endIndex) ? tasks.subList(startIndex, endIndex) : new ArrayList<>();
public List<TaskModel> getTask(TaskDetailsRequestParams params) {
List<TaskModel> 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<TaskModel> pagedTasks = TaskPagination.paginate(tasks, params.getPageNumber(), params.getSize());

return new GetTasksResponse(tasks.size(), pagedTasks);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.bekk.dbscheduleruiapi.util;

import java.util.ArrayList;
import java.util.List;

public class TaskPagination {

public static <T> List<T> paginate(List<T> 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<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ public static List<TaskModel> mapAllExecutionsToTaskModel(List<ScheduledExecutio
scheduled.addAll(groupTasks(mapCurrentlyExecutingToTaskModel(currentlyExecuting)));
return scheduled;
}

public static List<TaskModel> mapAllExecutionsToTaskModelUngrouped(List<ScheduledExecution<Object>> scheduledExecutions, List<CurrentlyExecuting> currentlyExecuting) {
List<TaskModel> scheduled = mapScheduledExecutionsToTaskModel(scheduledExecutions);
scheduled.addAll(mapCurrentlyExecutingToTaskModel(currentlyExecuting));
return scheduled;
}
}

0 comments on commit eaa3012

Please sign in to comment.