From 6535a10228e4a9dc75d76d05a6ff5c43121b9935 Mon Sep 17 00:00:00 2001 From: Aaron Plave Date: Thu, 29 Feb 2024 16:58:45 -0800 Subject: [PATCH] Cancel scheduling run --- src/routes/plans/[id]/+page.svelte | 7 +++++++ src/stores/scheduling.ts | 4 ++-- src/utilities/effects.ts | 29 +++++++++++++++++++++++++++-- src/utilities/gql.ts | 10 ++++++++++ src/utilities/permissions.ts | 3 +++ 5 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/routes/plans/[id]/+page.svelte b/src/routes/plans/[id]/+page.svelte index 053fe997cc..40ee60c460 100644 --- a/src/routes/plans/[id]/+page.svelte +++ b/src/routes/plans/[id]/+page.svelte @@ -727,6 +727,13 @@ Scheduling analysis not run {/if} + {#if $schedulingAnalysisStatus === Status.Pending || $schedulingAnalysisStatus === Status.Incomplete} + + {/if} { + try { + if (!queryPermissions.CANCEL_PENDING_SCHEDULING_REQUEST(user)) { + throwPermissionError('cancel a scheduling request dataset'); + } + const { confirm } = await showConfirmModal( + 'Cancel Scheduling Request', + `This will cancel the scheduling request with Analysis ID: ${analysisId}.`, + 'Cancel Scheduling Request', + true, + 'Keep Scheduling', + ); + + if (confirm) { + await reqHasura(gql.CANCEL_SCHEDULING_REQUEST, { id: analysisId }, user); + showSuccessToast('Scheduling Request Successfully Canceled'); + } + } catch (e) { + catchError('Scheduling Request Unable To Be Canceled', e as Error); + showFailureToast('Scheduling Request Cancel Failed'); + } + }, + async cancelSimulation(simulationDatasetId: number, user: User | null): Promise { try { if (!queryPermissions.CANCEL_PENDING_SIMULATION(user)) { - throwPermissionError('update a simulation dataset'); + throwPermissionError('cancel a simulation'); } const { confirm } = await showConfirmModal( 'Cancel Simulation', @@ -3753,7 +3776,9 @@ const effects = { const unsubscribe = schedulingRequests.subscribe(async (requests: SchedulingRequest[]) => { const matchingRequest = requests.find(request => request.analysis_id === analysisId); if (matchingRequest) { - if (matchingRequest.status === 'success') { + if (matchingRequest.canceled) { + unsubscribe(); + } else if (matchingRequest.status === 'success') { // If a new simulation was run during scheduling, the response will include a datasetId // which will need to be cross referenced with a simulation_dataset.id so we // can load that new simulation. diff --git a/src/utilities/gql.ts b/src/utilities/gql.ts index da8d7c54f1..6435cc1cf4 100644 --- a/src/utilities/gql.ts +++ b/src/utilities/gql.ts @@ -14,6 +14,16 @@ const gql = { } `, + CANCEL_SCHEDULING_REQUEST: `#graphql + mutation CancelSchedulingRequest($id: Int!) { + update_scheduling_request(where: { analysis_id: { _eq: $id } }, _set: { + canceled: true + }) { + affected_rows + } + } + `, + CANCEL_SIMULATION: `#graphql mutation CancelSim($id: Int!) { update_simulation_dataset_by_pk(pk_columns: {id: $id}, _set: { diff --git a/src/utilities/permissions.ts b/src/utilities/permissions.ts index 30a2ab01d4..83ff189df5 100644 --- a/src/utilities/permissions.ts +++ b/src/utilities/permissions.ts @@ -292,6 +292,9 @@ const queryPermissions = { isUserAdmin(user) || (getPermission(queries, user) && getRolePlanPermission(queries, user, plan, model, preset)) ); }, + CANCEL_PENDING_SCHEDULING_REQUEST: (user: User | null): boolean => { + return isUserAdmin(user) || getPermission(['update_scheduling_request'], user); + }, CANCEL_PENDING_SIMULATION: (user: User | null): boolean => { return isUserAdmin(user) || getPermission(['update_simulation_dataset_by_pk'], user); },