Pipeline.utils.ts137 lines · main
| 1 | import { PipelineStatusName } from './Replication.constants' |
| 2 | import { ReplicationPipelineStatusData } from '@/data/replication/pipeline-status-query' |
| 3 | import { PipelineStatusRequestStatus } from '@/state/replication-pipeline-request-status' |
| 4 | |
| 5 | export const getStatusName = ( |
| 6 | status: ReplicationPipelineStatusData['status'] | undefined |
| 7 | ): PipelineStatusName | undefined => { |
| 8 | if (!status || typeof status !== 'object' || !('name' in status)) return undefined |
| 9 | return normalizePipelineStatusName(status.name) |
| 10 | } |
| 11 | |
| 12 | export const normalizePipelineStatusName = (statusName?: string): PipelineStatusName | undefined => |
| 13 | typeof statusName === 'string' && |
| 14 | (Object.values(PipelineStatusName) as string[]).includes(statusName) |
| 15 | ? (statusName as PipelineStatusName) |
| 16 | : undefined |
| 17 | |
| 18 | export const PIPELINE_ENABLE_ALLOWED_FROM: PipelineStatusName[] = [PipelineStatusName.STOPPED] |
| 19 | export const PIPELINE_DISABLE_ALLOWED_FROM: PipelineStatusName[] = [ |
| 20 | PipelineStatusName.STARTED, |
| 21 | PipelineStatusName.FAILED, |
| 22 | ] |
| 23 | export const PIPELINE_ACTIONABLE_STATES: PipelineStatusName[] = [ |
| 24 | PipelineStatusName.FAILED, |
| 25 | PipelineStatusName.STARTED, |
| 26 | PipelineStatusName.STOPPED, |
| 27 | ] |
| 28 | |
| 29 | export type PipelineDisplayStateKey = |
| 30 | | 'starting' |
| 31 | | 'stopping' |
| 32 | | 'restarting' |
| 33 | | 'failed' |
| 34 | | 'stopped' |
| 35 | | 'running' |
| 36 | | 'unknown' |
| 37 | |
| 38 | export type PipelineDisplayType = 'failure' | 'loading' | 'success' | 'idle' |
| 39 | |
| 40 | export interface PipelineDisplayState { |
| 41 | key: PipelineDisplayStateKey |
| 42 | label: string |
| 43 | title: string |
| 44 | message: string |
| 45 | badge: string |
| 46 | type: PipelineDisplayType |
| 47 | } |
| 48 | |
| 49 | const PIPELINE_DISPLAY_STATES: Record<PipelineDisplayStateKey, PipelineDisplayState> = { |
| 50 | starting: { |
| 51 | key: 'starting', |
| 52 | label: 'Starting', |
| 53 | title: 'Starting pipeline', |
| 54 | message: 'Starting the pipeline. Replication will resume once running.', |
| 55 | badge: 'Starting', |
| 56 | type: 'loading', |
| 57 | }, |
| 58 | stopping: { |
| 59 | key: 'stopping', |
| 60 | label: 'Stopping', |
| 61 | title: 'Stopping pipeline', |
| 62 | message: 'Stopping replication. Data transfer will be paused once stopped.', |
| 63 | badge: 'Stopping', |
| 64 | type: 'loading', |
| 65 | }, |
| 66 | restarting: { |
| 67 | key: 'restarting', |
| 68 | label: 'Restarting', |
| 69 | title: 'Restarting pipeline', |
| 70 | message: 'Applying settings and restarting the pipeline.', |
| 71 | badge: 'Restarting', |
| 72 | type: 'loading', |
| 73 | }, |
| 74 | failed: { |
| 75 | key: 'failed', |
| 76 | label: 'Failed', |
| 77 | title: 'Pipeline failed', |
| 78 | message: 'Replication has encountered an error.', |
| 79 | badge: 'Failed', |
| 80 | type: 'failure', |
| 81 | }, |
| 82 | stopped: { |
| 83 | key: 'stopped', |
| 84 | label: 'Stopped', |
| 85 | title: 'Pipeline stopped', |
| 86 | message: 'Replication is paused. Start the pipeline to resume data synchronization.', |
| 87 | badge: 'Stopped', |
| 88 | type: 'idle', |
| 89 | }, |
| 90 | running: { |
| 91 | key: 'running', |
| 92 | label: 'Running', |
| 93 | title: 'Pipeline running', |
| 94 | message: 'Replication is active and processing changes.', |
| 95 | badge: 'Running', |
| 96 | type: 'success', |
| 97 | }, |
| 98 | unknown: { |
| 99 | key: 'unknown', |
| 100 | label: 'Unknown', |
| 101 | title: 'Pipeline status unknown', |
| 102 | message: 'Unable to determine pipeline status.', |
| 103 | badge: 'Unknown', |
| 104 | type: 'idle', |
| 105 | }, |
| 106 | } |
| 107 | |
| 108 | export const getPipelineDisplayState = ( |
| 109 | requestStatus?: PipelineStatusRequestStatus, |
| 110 | statusName?: PipelineStatusName |
| 111 | ): PipelineDisplayState => { |
| 112 | if (requestStatus === PipelineStatusRequestStatus.RestartRequested) { |
| 113 | return PIPELINE_DISPLAY_STATES.restarting |
| 114 | } |
| 115 | if (requestStatus === PipelineStatusRequestStatus.StartRequested) { |
| 116 | return PIPELINE_DISPLAY_STATES.starting |
| 117 | } |
| 118 | if (requestStatus === PipelineStatusRequestStatus.StopRequested) { |
| 119 | return PIPELINE_DISPLAY_STATES.stopping |
| 120 | } |
| 121 | |
| 122 | switch (statusName) { |
| 123 | case PipelineStatusName.STARTING: |
| 124 | return PIPELINE_DISPLAY_STATES.starting |
| 125 | case PipelineStatusName.FAILED: |
| 126 | return PIPELINE_DISPLAY_STATES.failed |
| 127 | case PipelineStatusName.STOPPED: |
| 128 | return PIPELINE_DISPLAY_STATES.stopped |
| 129 | case PipelineStatusName.STARTED: |
| 130 | return PIPELINE_DISPLAY_STATES.running |
| 131 | case PipelineStatusName.STOPPING: |
| 132 | return PIPELINE_DISPLAY_STATES.stopping |
| 133 | case PipelineStatusName.UNKNOWN: |
| 134 | default: |
| 135 | return PIPELINE_DISPLAY_STATES.unknown |
| 136 | } |
| 137 | } |