import { isReadTopicAction , isWriteTopicAction } from '@/services/data/tuples/pipeline-stage-unit-action/pipeline-stage-unit-action-utils' ; import { Pipeline } from '@/services/data/tuples/pipeline-types' ; import { Topic } from '@/services/data/tuples/topic-types' ; import { getPipelineName } from '../../utils' ; export interface FlowTreePipelineNode { pipeline : Pipeline ; readTopics : Array < FlowTreeTopicNode > ; writeTopics : Array < FlowTreeTopicNode > ; checked : boolean ; parent : FlowTreeTopicNode ; } export interface FlowTreeTopicNode { topic : Topic ; loop : boolean ; pipelines : Array < FlowTreePipelineNode > ; parent : FlowTreePipelineNode | null ; } const buildPipelineNodes = ( availablePipelines , pipelines , topicNode , topics , ancestors ) => { const topic = topicNode . topic ; return availablePipelines . filter ( pipeline => pipeline . topicId == topic . topicId ) . sort ( ( p1 , p2 ) => { return getPipelineName ( p1 ) . toLowerCase ( ) . localeCompare ( getPipelineName ( p2 ) . toLowerCase ( ) ) ; } ) . map ( pipeline => { const readTopics = [ ] ; const writeTopics = [ ] ; const pipelineNode = { pipeline , readTopics , writeTopics , checked : false , parent : topicNode } as FlowTreePipelineNode ; pipeline . stages . forEach ( stage => { stage . units . forEach ( unit => { unit . do . forEach ( action => { if ( isReadTopicAction ( action ) ) { const readTopic = topics . find ( t => t . topicId == action . topicId ) ! ; readTopics . push ( buildTopicNode ( readTopic , pipelineNode , [ ] , [ ... new Set ( [ topic , ... ancestors ] ) ] , topics , pipelines , false ) ) ; } else if ( isWriteTopicAction ( action ) ) { const writeTopic = topics . find ( t => t . topicId == action . topicId ) ! ; writeTopics . push ( buildTopicNode ( writeTopic , pipelineNode , [ ] , [ ... new Set ( [ topic , ... ancestors ] ) ] , topics , pipelines , true ) ) ; } } ) ; } ) ; } ) ; return pipelineNode ; } ) ; } ; export const buildTopicNode = ( topic , parent , availablePipelines , ancestors , topics , pipelines , deep ) : FlowTreeTopicNode => { const loop = ancestors . includes ( topic ) ; const topicNode = { topic , loop , parent } as FlowTreeTopicNode ; topicNode . pipelines = ( loop || ! deep ) ? [ ] : buildPipelineNodes ( availablePipelines . length === 0 ? pipelines : availablePipelines , pipelines , topicNode , topics , ancestors ) ; return topicNode ; } ;