From 2f3e8c525f41780de7a518ebcf936a049d9b4300 Mon Sep 17 00:00:00 2001 From: MiriSafra Date: Sun, 8 Dec 2024 21:15:07 +0200 Subject: [PATCH] Implement bulk analysis cancellation Signed-off-by: MiriSafra --- client/src/app/api/rest.ts | 3 ++ .../applications-table/applications-table.tsx | 38 ++++++++++++++++--- client/src/app/queries/tasks.ts | 18 +++++++++ 3 files changed, 53 insertions(+), 6 deletions(-) diff --git a/client/src/app/api/rest.ts b/client/src/app/api/rest.ts index 542507b07b..a1a1ca6347 100644 --- a/client/src/app/api/rest.ts +++ b/client/src/app/api/rest.ts @@ -371,6 +371,9 @@ export const deleteTask = (id: number) => axios.delete(`${TASKS}/${id}`); export const cancelTask = (id: number) => axios.put(`${TASKS}/${id}/cancel`); +export const cancelTasks = (ids: number[]) => + axios.put(`${TASKS}/cancel/list`, ids); + export const getTaskQueue = (addon?: string): Promise => axios .get(`${TASKS}/report/queue`, { params: { addon } }) diff --git a/client/src/app/pages/applications/applications-table/applications-table.tsx b/client/src/app/pages/applications/applications-table/applications-table.tsx index a7542103c7..21c59f592d 100644 --- a/client/src/app/pages/applications/applications-table/applications-table.tsx +++ b/client/src/app/pages/applications/applications-table/applications-table.tsx @@ -80,6 +80,7 @@ import { import { TaskStates, useCancelTaskMutation, + useCancelTasksMutation, useFetchTaskDashboard, } from "@app/queries/tasks"; import { useDeleteAssessmentMutation } from "@app/queries/assessments"; @@ -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) => { @@ -1196,9 +1224,7 @@ export const ApplicationsTable: React.FC = () => { onCancel={() => setTasksToCancel([])} onClose={() => setTasksToCancel([])} onConfirm={() => { - tasksToCancel.forEach((application) => { - cancelAnalysis(application); - }); + cancelAnalysis(tasksToCancel); setTasksToCancel([]); }} /> diff --git a/client/src/app/queries/tasks.ts b/client/src/app/queries/tasks.ts index 81b70ef4d1..1a982934f5 100644 --- a/client/src/app/queries/tasks.ts +++ b/client/src/app/queries/tasks.ts @@ -7,6 +7,7 @@ import { import { cancelTask, + cancelTasks, deleteTask, getServerTasks, getTaskById, @@ -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