/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper; import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage; import org.apache.dolphinscheduler.dao.entity.WorkFlowRelation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service public class WorkFlowLineageService extends BaseService { @Autowired private WorkFlowLineageMapper workFlowLineageMapper; public Map queryWorkFlowLineageByName(String workFlowName, int projectId) { Map result = new HashMap<>(5); List workFlowLineageList = workFlowLineageMapper.queryByName(workFlowName, projectId); result.put(Constants.DATA_LIST, workFlowLineageList); putMsg(result, Status.SUCCESS); return result; } private List getWorkFlowRelationRecursion(Set ids, List workFlowRelations,Set sourceIds) { for(int id : ids) { sourceIds.addAll(ids); List workFlowRelationsTmp = workFlowLineageMapper.querySourceTarget(id); if(workFlowRelationsTmp != null && !workFlowRelationsTmp.isEmpty()) { Set idsTmp = new HashSet<>(); for(WorkFlowRelation workFlowRelation:workFlowRelationsTmp) { if(!sourceIds.contains(workFlowRelation.getTargetWorkFlowId())){ idsTmp.add(workFlowRelation.getTargetWorkFlowId()); } } workFlowRelations.addAll(workFlowRelationsTmp); getWorkFlowRelationRecursion(idsTmp, workFlowRelations,sourceIds); } } return workFlowRelations; } public Map queryWorkFlowLineageByIds(Set ids,int projectId) { Map result = new HashMap<>(5); List workFlowLineageList = workFlowLineageMapper.queryByIds(ids, projectId); Map workFlowLists = new HashMap<>(5); Set idsV = new HashSet<>(); if(ids == null || ids.isEmpty()){ for(WorkFlowLineage workFlowLineage:workFlowLineageList) { idsV.add(workFlowLineage.getWorkFlowId()); } } else { idsV = ids; } List workFlowRelations = new ArrayList<>(); Set sourceIds = new HashSet<>(); getWorkFlowRelationRecursion(idsV, workFlowRelations, sourceIds); Set idSet = new HashSet<>(); //If the incoming parameter is not empty, you need to add downstream workflow detail attributes if(ids != null && !ids.isEmpty()) { for(WorkFlowRelation workFlowRelation : workFlowRelations) { idSet.add(workFlowRelation.getTargetWorkFlowId()); } for(int id : ids){ idSet.remove(id); } if(!idSet.isEmpty()) { workFlowLineageList.addAll(workFlowLineageMapper.queryByIds(idSet, projectId)); } } workFlowLists.put("workFlowList",workFlowLineageList); workFlowLists.put("workFlowRelationList",workFlowRelations); result.put(Constants.DATA_LIST, workFlowLists); putMsg(result, Status.SUCCESS); return result; } }