Skip to content

Commit

Permalink
Cancel scheduling run
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronPlave committed Mar 1, 2024
1 parent d79e920 commit 6535a10
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/routes/plans/[id]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,13 @@
Scheduling analysis not run
{/if}
</div>
{#if $schedulingAnalysisStatus === Status.Pending || $schedulingAnalysisStatus === Status.Incomplete}
<button
on:click={() => effects.cancelSchedulingRequest($latestSchedulingRequest.analysis_id, data.user)}
class="st-button cancel-button"
disabled={$planReadOnly}>Cancel</button
>
{/if}
</svelte:fragment>
</PlanNavButton>
<ExtensionMenu
Expand Down
4 changes: 2 additions & 2 deletions src/stores/scheduling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ export const schedulingAnalysisStatus = derived(
// No status if there are no requests
if (!$latestSchedulingRequest) {
return null;
} else if ($latestSchedulingRequest.canceled) {
return Status.Canceled;
} else if ($latestSchedulingRequest.status === 'incomplete') {
return Status.Incomplete;
} else if ($latestSchedulingRequest.status === 'pending' && !$latestSchedulingRequest.canceled) {
return Status.Pending;
} else if ($latestSchedulingRequest.canceled) {
return Status.Canceled;
} else {
let matchingSimDataset;
if (typeof $latestSchedulingRequest.dataset_id === 'number') {
Expand Down
29 changes: 27 additions & 2 deletions src/utilities/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,33 @@ const effects = {
}
},

async cancelSchedulingRequest(analysisId: number, user: User | null): Promise<void> {
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<SeqId>(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<void> {
try {
if (!queryPermissions.CANCEL_PENDING_SIMULATION(user)) {
throwPermissionError('update a simulation dataset');
throwPermissionError('cancel a simulation');
}
const { confirm } = await showConfirmModal(
'Cancel Simulation',
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/utilities/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
3 changes: 3 additions & 0 deletions src/utilities/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down

0 comments on commit 6535a10

Please sign in to comment.