import { Pipeline } from '@/services/data/tuples/pipeline-types' ; import { AlertLabel } from '@/widgets/alert/widgets' ; import { useEventBus } from '@/widgets/events/event-bus' ; import { EventTypes } from '@/widgets/events/types' ; import React , { useEffect , useState } from 'react' ; import { TopicsData } from '../state/types' ; import { PipelineRun } from './pipeline' ; import { useRunsEventBus } from './runs-event-bus' ; import { RunsEventTypes } from './runs-event-bus-types' ; import { AllTopics , PipelineRunStatus , PipelineRuntimeContext } from './types' ; import { buildPipelineRuntimeContext } from './utils' ; interface State { runs : Array < PipelineRuntimeContext > ; currentIndex : number ; } export const Pipelines = ( props ) => { const { pipelineContexts , topics , allPipelines } = props ; const { fire : fireGlobal } = useEventBus ( ) ; const { fire } = useRunsEventBus ( ) ; const onRunNext = ( ) => { const nextPipeline = pipelineContexts . find ( c => c . status === PipelineRunStatus . WAIT ) ; if ( nextPipeline ) { fire ( RunsEventTypes . RUN_PIPELINE , nextPipeline ) ; } else { fireGlobal ( EventTypes . SHOW_ALERT , < AlertLabel > All pipelines are completed . < /AlertLabel>); } } ; const [ first , ... rest ] = pipelineContexts ; return < > < PipelineRun context = { first } topics = { topics } pipelines = { allPipelines } runNext = { onRunNext } / > { rest . map ( ( context , index ) => { return < PipelineRun context = { context } topics = { topics } pipelines = { allPipelines } runNext = { onRunNext } key = { index } / > ; } ) } < />; } ; export const Runs = ( props ) => { const { runPipelines , allPipelines , topics , data } = props ; const { on , off } = useRunsEventBus ( ) ; const [ state ] = useState < State > ( ( ) => { const triggerDataRows = data [ runPipelines [ 0 ] . topicId ] ; const runtimeData = Object . keys ( data ) . reduce ( ( runtimeData , topicId ) => { if ( topicId == runPipelines [ 0 ] . topicId ) { } else { runtimeData [ topicId ] = data [ topicId ] ; } return runtimeData ; } , { } as TopicsData ) ; return { runs : triggerDataRows . map ( triggerData => { return runPipelines . map ( pipeline => { return buildPipelineRuntimeContext ( { pipeline , topic : topics [ pipeline . topicId ] ! , triggerData , runtimeData , allTopics : topics } ) ; } ) ; } ) . flat ( ) , currentIndex : 0 } ; } ) ; useEffect ( ( ) => { const onAskRuntimeData = ( onData ) => { const first = state . runs [ 0 ] ; const last = state . runs [ state . runs . length - 1 ] ; if ( first . status === PipelineRunStatus . WAIT || first . status === PipelineRunStatus . READY ) { onData ( false , false , first . runtimeData ) ; } else if ( last . status !== PipelineRunStatus . DONE && last . status !== PipelineRunStatus . FAIL && last . status !== PipelineRunStatus . IGNORED ) { onData ( true , false , first . runtimeData ) ; } else { onData ( true , true , first . runtimeData ) ; } } ; on ( RunsEventTypes . ASK_RUNTIME_DATA , onAskRuntimeData ) ; return ( ) => { off ( RunsEventTypes . ASK_RUNTIME_DATA , onAskRuntimeData ) ; } ; } ) ; state . runs [ 0 ] . status = PipelineRunStatus . READY ; return < Pipelines pipelineContexts = { state . runs } topics = { topics } allPipelines = { allPipelines } / > ; } ;