Skip to content

Commit

Permalink
Implement bulk analysis cancellation
Browse files Browse the repository at this point in the history
Signed-off-by: MiriSafra <[email protected]>
  • Loading branch information
MiriSafra committed Dec 8, 2024
1 parent 9e1b026 commit 2f3e8c5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions client/src/app/api/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ export const deleteTask = (id: number) => axios.delete<void>(`${TASKS}/${id}`);
export const cancelTask = (id: number) =>
axios.put<void>(`${TASKS}/${id}/cancel`);

export const cancelTasks = (ids: number[]) =>
axios.put<void>(`${TASKS}/cancel/list`, ids);

export const getTaskQueue = (addon?: string): Promise<TaskQueue> =>
axios
.get<TaskQueue>(`${TASKS}/report/queue`, { params: { addon } })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import {
import {
TaskStates,
useCancelTaskMutation,
useCancelTasksMutation,
useFetchTaskDashboard,
} from "@app/queries/tasks";
import { useDeleteAssessmentMutation } from "@app/queries/assessments";
Expand Down Expand Up @@ -202,15 +203,42 @@ export const ApplicationsTable: React.FC = () => {
variant: "danger",
});
};
const completedCancelTasks = () => {
pushNotification({
title: "Tasks",
message: "Canceled",
variant: "info",
});
};
const failedCancelTasks = () => {
pushNotification({
title: "Tasks",
message: "Cancelation failed.",
variant: "danger",
});
};

const { mutate: cancelTask } = useCancelTaskMutation(
completedCancelTask,
failedCancelTask
);
const { mutate: cancelTasks } = useCancelTasksMutation(
completedCancelTasks,
failedCancelTasks
);

const cancelAnalysis = (application: DecoratedApplication) => {
const task = application.tasks.currentAnalyzer;
if (task?.id) cancelTask(task.id);
const cancelAnalysis = (
application: DecoratedApplication | DecoratedApplication[]
) => {
if (!Array.isArray(application)) {
const task = application.tasks.currentAnalyzer;
if (task?.id) cancelTask(task.id);
} else {
const tasks = application
.map((app) => app.tasks.currentAnalyzer?.id)
.filter((id): id is number => id !== undefined);
cancelTasks(tasks);
}
};

const isTaskCancellable = (application: DecoratedApplication) => {
Expand Down Expand Up @@ -1196,9 +1224,7 @@ export const ApplicationsTable: React.FC = () => {
onCancel={() => setTasksToCancel([])}
onClose={() => setTasksToCancel([])}
onConfirm={() => {
tasksToCancel.forEach((application) => {
cancelAnalysis(application);
});
cancelAnalysis(tasksToCancel);
setTasksToCancel([]);
}}
/>
Expand Down
18 changes: 18 additions & 0 deletions client/src/app/queries/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {

import {
cancelTask,
cancelTasks,
deleteTask,
getServerTasks,
getTaskById,
Expand Down Expand Up @@ -192,6 +193,23 @@ export const useCancelTaskMutation = (
});
};

export const useCancelTasksMutation = (
onSuccess: (statusCode: number) => void,
onError: (err: Error | null) => void
) => {
const queryClient = useQueryClient();
return useMutation({
mutationFn: cancelTasks,
onSuccess: (response) => {
queryClient.invalidateQueries([TasksQueryKey]);
onSuccess && onSuccess(response.status);
},
onError: (err: Error) => {
onError && onError(err);
},
});
};

export const useUpdateTaskMutation = (
onSuccess: (statusCode: number) => void,
onError: (err: Error | null) => void
Expand Down

0 comments on commit 2f3e8c5

Please sign in to comment.