From 31c7f548c8abc4539e3aac8ec2c5fa56945fab2c Mon Sep 17 00:00:00 2001 From: Raminder Singh Date: Tue, 22 Oct 2024 16:55:51 +0530 Subject: [PATCH] get pipeline status wip --- .../interfaces/Replication/Pipelines.tsx | 82 ++++++++++++------- apps/studio/data/replication/keys.ts | 2 + .../data/replication/pipeline-status-query.ts | 41 ++++++++++ packages/api-types/types/api.d.ts | 26 +++++- 4 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 apps/studio/data/replication/pipeline-status-query.ts diff --git a/apps/studio/components/interfaces/Replication/Pipelines.tsx b/apps/studio/components/interfaces/Replication/Pipelines.tsx index ba74f6b11ad..7a495cd429f 100644 --- a/apps/studio/components/interfaces/Replication/Pipelines.tsx +++ b/apps/studio/components/interfaces/Replication/Pipelines.tsx @@ -19,14 +19,16 @@ export const ReplicationPipelines = () => { const [showCreatePipelineModal, setShowCreatePipelineModal] = useState(false) const [showDeletePipelineModal, setShowDeletePipelineModal] = useState(false) const [pipelineIdToDelete, setPipelineIdToDelete] = useState(-1) + const [pipelineIdToStatus, setPipelineIdToStatus] = useState({ idToStatus: new Map() }) const pipelines = pipelines_data ?? [] + return ( <>
- +
{ Id, Source Id, Sink Id, + Action, Edit, Delete, ]} @@ -60,34 +63,55 @@ export const ReplicationPipelines = () => { ) : ( - pipelines.map((pipeline) => ( - - {pipeline.id} - {pipeline.source_id} - {pipeline.sink_id} - - - - - - - - )) + pipelines.map((pipeline) => { + const status = pipelineIdToStatus.idToStatus.get(pipeline.id) + const actionButtonLoading = !status + const pipelineStopped = !status || status === 'Stopped' + const actionButtonLabel = actionButtonLoading + ? 'Getting Status' + : pipelineStopped + ? 'Start' + : 'Stop' + return ( + + {pipeline.id} + {pipeline.source_id} + {pipeline.sink_id} + + + + + {} + + + + + + + ) + }) ) } > diff --git a/apps/studio/data/replication/keys.ts b/apps/studio/data/replication/keys.ts index 2f07a76f1b7..80d1c15471b 100644 --- a/apps/studio/data/replication/keys.ts +++ b/apps/studio/data/replication/keys.ts @@ -6,4 +6,6 @@ export const replicationKeys = { tables: (projectRef: string | undefined, source_id: number) => ['projects', projectRef, 'sources', source_id, 'tables'] as const, pipelines: (projectRef: string | undefined) => ['projects', projectRef, 'pipelines'] as const, + pipelinesStatus: (projectRef: string | undefined, pipelineId: number) => + ['projects', projectRef, 'pipelines', pipelineId, 'status'] as const, } diff --git a/apps/studio/data/replication/pipeline-status-query.ts b/apps/studio/data/replication/pipeline-status-query.ts new file mode 100644 index 00000000000..120fcf71e2d --- /dev/null +++ b/apps/studio/data/replication/pipeline-status-query.ts @@ -0,0 +1,41 @@ +import { UseQueryOptions, useQuery } from '@tanstack/react-query' + +import { get, handleError } from 'data/fetchers' +import { ResponseError } from 'types' +import { replicationKeys } from './keys' + +type ReplicationPipelinesStatusParams = { projectRef?: string; pipelineId: number } + +async function fetchReplicationPipelineStatus( + { projectRef, pipelineId }: ReplicationPipelinesStatusParams, + signal?: AbortSignal +) { + if (!projectRef) throw new Error('projectRef is required') + + const { data, error } = await get('/platform/replication/{ref}/pipelines/{pipeline_id}/status', { + params: { path: { ref: projectRef, pipeline_id: pipelineId } }, + signal, + }) + if (error) { + handleError(error) + } + + return data +} + +export type ReplicationPipelineStatusData = Awaited< + ReturnType +> + +export const useReplicationPipelineStatusQuery = ( + { projectRef, pipelineId }: ReplicationPipelinesStatusParams, + { + enabled = true, + ...options + }: UseQueryOptions = {} +) => + useQuery( + replicationKeys.pipelinesStatus(projectRef, pipelineId), + ({ signal }) => fetchReplicationPipelineStatus({ projectRef, pipelineId }, signal), + { enabled: enabled && typeof projectRef !== 'undefined', ...options } + ) diff --git a/packages/api-types/types/api.d.ts b/packages/api-types/types/api.d.ts index dc48c21fef0..55adc712a3c 100644 --- a/packages/api-types/types/api.d.ts +++ b/packages/api-types/types/api.d.ts @@ -3331,6 +3331,10 @@ export interface paths { /** Deletes a pipeline */ delete: operations['ReplicationPipelinesController_deletePipeline'] } + '/platform/replication/{ref}/pipelines/{pipeline_id}/status': { + /** Gets status of a pipeline */ + get: operations['ReplicationPipelinesController_getPipelineStatus'] + } '/platform/replication/{ref}/sinks': { /** Gets replication sinks */ get: operations['ReplicationSinksController_getSinks'] @@ -21733,7 +21737,27 @@ export interface operations { 200: { content: never } - /** @description Failed to delete pipelin */ + /** @description Failed to delete pipeline */ + 500: { + content: never + } + } + } + /** Gets status of a pipeline */ + ReplicationPipelinesController_getPipelineStatus: { + parameters: { + path: { + /** @description Project ref */ + ref: string + /** @description Pipeline id */ + pipeline_id: number + } + } + responses: { + 200: { + content: never + } + /** @description Failed to get pipeline status */ 500: { content: never }