@ -20,20 +20,32 @@ package org.apache.dolphinscheduler.api.service.impl;
import org.apache.dolphinscheduler.api.enums.Status ;
import org.apache.dolphinscheduler.api.enums.Status ;
import org.apache.dolphinscheduler.api.service.WorkFlowLineageService ;
import org.apache.dolphinscheduler.api.service.WorkFlowLineageService ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.Constants ;
import org.apache.dolphinscheduler.common.enums.TaskType ;
import org.apache.dolphinscheduler.common.model.DependentItem ;
import org.apache.dolphinscheduler.common.model.DependentTaskModel ;
import org.apache.dolphinscheduler.common.task.dependent.DependentParameters ;
import org.apache.dolphinscheduler.common.utils.JSONUtils ;
import org.apache.dolphinscheduler.dao.entity.ProcessLineage ;
import org.apache.dolphinscheduler.dao.entity.ProcessLineage ;
import org.apache.dolphinscheduler.dao.entity.Project ;
import org.apache.dolphinscheduler.dao.entity.Project ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinitionLog ;
import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage ;
import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage ;
import org.apache.dolphinscheduler.dao.entity.WorkFlowRelation ;
import org.apache.dolphinscheduler.dao.entity.WorkFlowRelation ;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper ;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper ;
import org.apache.dolphinscheduler.dao.mapper.TaskDefinitionLogMapper ;
import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper ;
import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper ;
import org.apache.dolphinscheduler.spi.utils.StringUtils ;
import org.apache.commons.lang.StringUtil s ;
import org.apache.curator.shaded.com.google.common.collect.Set s ;
import java.util.ArrayList ;
import java.util.HashMap ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.HashSet ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import java.util.Map.Entry ;
import java.util.Set ;
import java.util.Set ;
import java.util.stream.Collectors ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
@ -50,6 +62,9 @@ public class WorkFlowLineageServiceImpl extends BaseServiceImpl implements WorkF
@Autowired
@Autowired
private ProjectMapper projectMapper ;
private ProjectMapper projectMapper ;
@Autowired
private TaskDefinitionLogMapper taskDefinitionLogMapper ;
@Override
@Override
public Map < String , Object > queryWorkFlowLineageByName ( long projectCode , String workFlowName ) {
public Map < String , Object > queryWorkFlowLineageByName ( long projectCode , String workFlowName ) {
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
@ -72,12 +87,47 @@ public class WorkFlowLineageServiceImpl extends BaseServiceImpl implements WorkF
putMsg ( result , Status . PROJECT_NOT_FOUNT , projectCode ) ;
putMsg ( result , Status . PROJECT_NOT_FOUNT , projectCode ) ;
return result ;
return result ;
}
}
WorkFlowLineage workFlowLineage = workFlowLineageMapper . queryWorkFlowLineageByCode ( projectCode , workFlowCode ) ;
Map < Long , WorkFlowLineage > workFlowLineagesMap = new HashMap < > ( ) ;
result . put ( Constants . DATA_LIST , workFlowLineage ) ;
Set < WorkFlowRelation > workFlowRelations = new HashSet < > ( ) ;
Set < Long > sourceWorkFlowCodes = Sets . newHashSet ( workFlowCode ) ;
recursiveWorkFlow ( projectCode , workFlowLineagesMap , workFlowRelations , sourceWorkFlowCodes ) ;
Map < String , Object > workFlowLists = new HashMap < > ( ) ;
workFlowLists . put ( Constants . WORKFLOW_LIST , workFlowLineagesMap . values ( ) ) ;
workFlowLists . put ( Constants . WORKFLOW_RELATION_LIST , workFlowRelations ) ;
result . put ( Constants . DATA_LIST , workFlowLists ) ;
putMsg ( result , Status . SUCCESS ) ;
putMsg ( result , Status . SUCCESS ) ;
return result ;
return result ;
}
}
private void recursiveWorkFlow ( long projectCode ,
Map < Long , WorkFlowLineage > workFlowLineagesMap ,
Set < WorkFlowRelation > workFlowRelations ,
Set < Long > sourceWorkFlowCodes ) {
for ( Long workFlowCode : sourceWorkFlowCodes ) {
WorkFlowLineage workFlowLineage = workFlowLineageMapper . queryWorkFlowLineageByCode ( projectCode , workFlowCode ) ;
workFlowLineagesMap . put ( workFlowCode , workFlowLineage ) ;
List < ProcessLineage > processLineages = workFlowLineageMapper . queryProcessLineageByCode ( projectCode , workFlowCode ) ;
List < TaskDefinition > taskDefinitionList = new ArrayList < > ( ) ;
for ( ProcessLineage processLineage : processLineages ) {
if ( processLineage . getPreTaskCode ( ) > 0 ) {
taskDefinitionList . add ( new TaskDefinition ( processLineage . getPreTaskCode ( ) , processLineage . getPreTaskVersion ( ) ) ) ;
}
if ( processLineage . getPostTaskCode ( ) > 0 ) {
taskDefinitionList . add ( new TaskDefinition ( processLineage . getPostTaskCode ( ) , processLineage . getPostTaskVersion ( ) ) ) ;
}
}
sourceWorkFlowCodes = querySourceWorkFlowCodes ( projectCode , workFlowCode , taskDefinitionList ) ;
if ( sourceWorkFlowCodes . isEmpty ( ) ) {
workFlowRelations . add ( new WorkFlowRelation ( 0L , workFlowCode ) ) ;
return ;
} else {
workFlowLineagesMap . get ( workFlowCode ) . setSourceWorkFlowCode ( StringUtils . join ( sourceWorkFlowCodes , Constants . COMMA ) ) ;
sourceWorkFlowCodes . forEach ( code - > workFlowRelations . add ( new WorkFlowRelation ( code , workFlowCode ) ) ) ;
recursiveWorkFlow ( projectCode , workFlowLineagesMap , workFlowRelations , sourceWorkFlowCodes ) ;
}
}
}
@Override
@Override
public Map < String , Object > queryWorkFlowLineage ( long projectCode ) {
public Map < String , Object > queryWorkFlowLineage ( long projectCode ) {
Map < String , Object > result = new HashMap < > ( ) ;
Map < String , Object > result = new HashMap < > ( ) ;
@ -87,58 +137,65 @@ public class WorkFlowLineageServiceImpl extends BaseServiceImpl implements WorkF
return result ;
return result ;
}
}
List < ProcessLineage > processLineages = workFlowLineageMapper . queryProcessLineage ( projectCode ) ;
List < ProcessLineage > processLineages = workFlowLineageMapper . queryProcessLineage ( projectCode ) ;
Map < Long , WorkFlowLineage > workFlowLineagesMap = new HashMap < > ( ) ;
Map < Long , WorkFlowLineage > workFlowLineages = new HashMap < > ( ) ;
Set < WorkFlowRelation > workFlowRelations = new HashSet < > ( ) ;
Set < WorkFlowRelation > workFlowRelations = new HashSet < > ( ) ;
if ( ! processLineages . isEmpty ( ) ) {
for ( ProcessLineage processLineage : processLineages ) {
List < WorkFlowLineage > workFlowLineages = workFlowLineageMapper . queryWorkFlowLineageByLineage ( processLineages ) ;
getRelation ( workFlowLineages , workFlowRelations , processLineage ) ;
workFlowLineagesMap = workFlowLineages . stream ( ) . collect ( Collectors . toMap ( WorkFlowLineage : : getWorkFlowCode , workFlowLineage - > workFlowLineage ) ) ;
Map < Long , List < TaskDefinition > > workFlowMap = new HashMap < > ( ) ;
for ( ProcessLineage processLineage : processLineages ) {
workFlowMap . compute ( processLineage . getProcessDefinitionCode ( ) , ( k , v ) - > {
if ( v = = null ) {
v = new ArrayList < > ( ) ;
}
if ( processLineage . getPreTaskCode ( ) > 0 ) {
v . add ( new TaskDefinition ( processLineage . getPreTaskCode ( ) , processLineage . getPreTaskVersion ( ) ) ) ;
}
if ( processLineage . getPostTaskCode ( ) > 0 ) {
v . add ( new TaskDefinition ( processLineage . getPostTaskCode ( ) , processLineage . getPostTaskVersion ( ) ) ) ;
}
return v ;
} ) ;
}
for ( Entry < Long , List < TaskDefinition > > workFlow : workFlowMap . entrySet ( ) ) {
Set < Long > sourceWorkFlowCodes = querySourceWorkFlowCodes ( projectCode , workFlow . getKey ( ) , workFlow . getValue ( ) ) ;
if ( sourceWorkFlowCodes . isEmpty ( ) ) {
workFlowRelations . add ( new WorkFlowRelation ( 0L , workFlow . getKey ( ) ) ) ;
} else {
workFlowLineagesMap . get ( workFlow . getKey ( ) ) . setSourceWorkFlowCode ( StringUtils . join ( sourceWorkFlowCodes , Constants . COMMA ) ) ;
sourceWorkFlowCodes . forEach ( code - > workFlowRelations . add ( new WorkFlowRelation ( code , workFlow . getKey ( ) ) ) ) ;
}
}
}
}
Map < String , Object > workFlowLists = new HashMap < > ( ) ;
Map < String , Object > workFlowLists = new HashMap < > ( ) ;
workFlowLists . put ( Constants . WORKFLOW_LIST , workFlowLineages . values ( ) ) ;
workFlowLists . put ( Constants . WORKFLOW_LIST , workFlowLineagesMap . values ( ) ) ;
workFlowLists . put ( Constants . WORKFLOW_RELATION_LIST , workFlowRelations ) ;
workFlowLists . put ( Constants . WORKFLOW_RELATION_LIST , workFlowRelations ) ;
result . put ( Constants . DATA_LIST , workFlowLists ) ;
result . put ( Constants . DATA_LIST , workFlowLists ) ;
putMsg ( result , Status . SUCCESS ) ;
putMsg ( result , Status . SUCCESS ) ;
return result ;
return result ;
}
}
private void getRelation ( Map < Long , WorkFlowLineage > workFlowLineageMap ,
private Set < Long > querySourceWorkFlowCodes ( long projectCode , long workFlowCode , List < TaskDefinition > taskDefinitionList ) {
Set < WorkFlowRelation > workFlowRelations ,
Set < Long > sourceWorkFlowCodes = new HashSet < > ( ) ;
ProcessLineage processLineage ) {
List < TaskDefinitionLog > taskDefinitionLogs = taskDefinitionLogMapper . queryByTaskDefinitions ( taskDefinitionList ) ;
List < ProcessLineage > relations = workFlowLineageMapper . queryCodeRelation ( processLineage . getProjectCode ( ) ,
for ( TaskDefinitionLog taskDefinitionLog : taskDefinitionLogs ) {
processLineage . getProcessDefinitionCode ( ) , processLineage . getPostTaskCode ( ) , processLineage . getPostTaskVersion ( ) ) ;
if ( taskDefinitionLog . getProjectCode ( ) = = projectCode ) {
if ( ! relations . isEmpty ( ) ) {
if ( taskDefinitionLog . getTaskType ( ) . equals ( TaskType . DEPENDENT . getDesc ( ) ) ) {
Set < Long > preWorkFlowCodes = new HashSet < > ( ) ;
DependentParameters dependentParameters = JSONUtils . parseObject ( taskDefinitionLog . getDependence ( ) , DependentParameters . class ) ;
List < ProcessLineage > preRelations = workFlowLineageMapper . queryCodeRelation ( processLineage . getProjectCode ( ) ,
if ( dependentParameters ! = null ) {
processLineage . getProcessDefinitionCode ( ) , processLineage . getPreTaskCode ( ) , processLineage . getPreTaskVersion ( ) ) ;
List < DependentTaskModel > dependTaskList = dependentParameters . getDependTaskList ( ) ;
for ( ProcessLineage preRelation : preRelations ) {
for ( DependentTaskModel taskModel : dependTaskList ) {
preWorkFlowCodes . add ( preRelation . getProcessDefinitionCode ( ) ) ;
List < DependentItem > dependItemList = taskModel . getDependItemList ( ) ;
}
for ( DependentItem dependentItem : dependItemList ) {
ProcessLineage postRelation = relations . get ( 0 ) ;
if ( dependentItem . getProjectCode ( ) = = projectCode & & dependentItem . getDefinitionCode ( ) ! = workFlowCode ) {
WorkFlowLineage post = workFlowLineageMapper . queryWorkFlowLineageByCode ( postRelation . getProjectCode ( ) , postRelation . getProcessDefinitionCode ( ) ) ;
sourceWorkFlowCodes . add ( dependentItem . getDefinitionCode ( ) ) ;
preWorkFlowCodes . remove ( post . getWorkFlowCode ( ) ) ;
}
if ( ! workFlowLineageMap . containsKey ( post . getWorkFlowCode ( ) ) ) {
}
post . setSourceWorkFlowCode ( StringUtils . join ( preWorkFlowCodes , "," ) ) ;
}
workFlowLineageMap . put ( post . getWorkFlowCode ( ) , post ) ;
} else {
WorkFlowLineage workFlowLineage = workFlowLineageMap . get ( post . getWorkFlowCode ( ) ) ;
String sourceWorkFlowCode = workFlowLineage . getSourceWorkFlowCode ( ) ;
if ( StringUtils . isBlank ( sourceWorkFlowCode ) ) {
post . setSourceWorkFlowCode ( StringUtils . join ( preWorkFlowCodes , "," ) ) ;
} else {
if ( ! preWorkFlowCodes . isEmpty ( ) ) {
workFlowLineage . setSourceWorkFlowCode ( sourceWorkFlowCode + "," + StringUtils . join ( preWorkFlowCodes , "," ) ) ;
}
}
}
}
}
}
if ( preWorkFlowCodes . isEmpty ( ) ) {
workFlowRelations . add ( new WorkFlowRelation ( 0L , post . getWorkFlowCode ( ) ) ) ;
} else {
for ( long workFlowCode : preWorkFlowCodes ) {
workFlowRelations . add ( new WorkFlowRelation ( workFlowCode , post . getWorkFlowCode ( ) ) ) ;
}
}
}
}
return sourceWorkFlowCodes ;
}
}
}
}