From d18049d6a351eae55e76482b07a7b8ecd0024f3d Mon Sep 17 00:00:00 2001 From: Dylan Hughes Date: Fri, 1 Nov 2024 18:05:41 -0400 Subject: [PATCH] useWorker --- src/compositions/index.ts | 1 + src/compositions/useWorker.ts | 39 +++++++++++++++++++++ src/models/api/Filters.ts | 1 - src/services/WorkspaceWorkPoolWorkersApi.ts | 7 ++++ src/services/can.ts | 2 ++ 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/compositions/useWorker.ts diff --git a/src/compositions/index.ts b/src/compositions/index.ts index 838d87f40..3abe70876 100644 --- a/src/compositions/index.ts +++ b/src/compositions/index.ts @@ -54,6 +54,7 @@ export * from './useTaskRunsHistory' export * from './useThemeTokens' export * from './useVariable' export * from './useVariables' +export * from './useWorker' export * from './useWorkPool' export * from './useWorkPoolById' export * from './useWorkPoolLastPolled' diff --git a/src/compositions/useWorker.ts b/src/compositions/useWorker.ts new file mode 100644 index 000000000..2ee79a065 --- /dev/null +++ b/src/compositions/useWorker.ts @@ -0,0 +1,39 @@ +import { useSubscriptionWithDependencies } from '@prefecthq/vue-compositions' +import { computed, MaybeRefOrGetter, toRef, toValue } from 'vue' +import { useCan } from '@/compositions/useCan' +import { useWorkspaceApi } from '@/compositions/useWorkspaceApi' +import { WorkspaceWorkPoolWorkersApi } from '@/services' +import { Getter } from '@/types/reactivity' +import { UseEntitySubscription } from '@/types/useEntitySubscription' + +export type UseWorker = UseEntitySubscription + +export function useWorker(workPoolName: MaybeRefOrGetter, workerId: MaybeRefOrGetter): UseWorker { + const api = useWorkspaceApi() + const can = useCan() + + const getter: Getter<[string, string] | null> = () => { + if (!can.read.worker) { + return null + } + + const name = toValue(workPoolName) + const id = toValue(workerId) + + if (!name || !id) { + return null + } + + return [name, id] + } + + const parameters = toRef(getter) + + const subscription = useSubscriptionWithDependencies(api.workPoolWorkers.getWorker, parameters) + const worker = computed(() => subscription.response) + + return { + subscription, + worker, + } +} \ No newline at end of file diff --git a/src/models/api/Filters.ts b/src/models/api/Filters.ts index c7157a92b..c9378e480 100644 --- a/src/models/api/Filters.ts +++ b/src/models/api/Filters.ts @@ -1,4 +1,3 @@ -import { ServerWorkPoolWorkerStatus } from '@/models/WorkPoolWorkerStatus' import { ArtifactSortValues, DeploymentSortValues, FlowRunSortValues, FlowSortValues, LogSortValues, TaskRunSortValues, VariableSortValues, WorkPoolWorkerSortValues } from '@/types/SortOptionTypes' /** A list where results will be returned only if they match all the values in the list */ diff --git a/src/services/WorkspaceWorkPoolWorkersApi.ts b/src/services/WorkspaceWorkPoolWorkersApi.ts index 50a4e91ca..3568c39ae 100644 --- a/src/services/WorkspaceWorkPoolWorkersApi.ts +++ b/src/services/WorkspaceWorkPoolWorkersApi.ts @@ -26,6 +26,13 @@ export class WorkspaceWorkPoolWorkersApi extends WorkspaceApi { return mapper.map('WorkPoolWorkersPaginationResponse', data, 'PaginatedWorkPoolWorkers') } + public async getWorker(workPoolName: string, workerId: string): Promise { + const encodedWorkPoolName = encodeURI(workPoolName) + const { data } = await this.get(`/${encodedWorkPoolName}/workers/${workerId}`) + + return mapper.map('WorkPoolWorkerResponse', data, 'WorkPoolWorker') + } + public deleteWorker(arg: WorkerDeleteArg): Promise { const encodedWorkPoolName = encodeURI(arg.workPoolName) const encodedWorkerName = encodeURI(arg.workerName) diff --git a/src/services/can.ts b/src/services/can.ts index 7ea057fc1..48c8c4016 100644 --- a/src/services/can.ts +++ b/src/services/can.ts @@ -36,6 +36,7 @@ export const workspacePermissions = [ 'delete:work_queue', 'delete:work_pool_queue', 'delete:work_pool', + 'delete:worker', 'delete:workspace_bot_access', 'delete:workspace_user_access', 'delete:variable', @@ -54,6 +55,7 @@ export const workspacePermissions = [ 'read:work_queue', 'read:work_pool_queue', 'read:work_pool', + 'read:worker', 'read:workspace_bot_access', 'read:workspace_settings', 'read:workspace_user_access',