Search in sources :

Example 1 with TreeViewDto

use of org.apache.dolphinscheduler.api.dto.treeview.TreeViewDto in project dolphinscheduler by apache.

the class ProcessDefinitionService method viewTree.

/**
 * Encapsulates the TreeView structure
 *
 * @param processId process definition id
 * @param limit limit
 * @return tree view json data
 * @throws Exception exception
 */
public Map<String, Object> viewTree(Integer processId, Integer limit) throws Exception {
    Map<String, Object> result = new HashMap<>();
    ProcessDefinition processDefinition = processDefineMapper.selectById(processId);
    if (null == processDefinition) {
        logger.info("process define not exists");
        putMsg(result, Status.PROCESS_DEFINE_NOT_EXIST, processDefinition);
        return result;
    }
    DAG<String, TaskNode, TaskNodeRelation> dag = genDagGraph(processDefinition);
    /**
     * nodes that is running
     */
    Map<String, List<TreeViewDto>> runningNodeMap = new ConcurrentHashMap<>();
    /**
     * nodes that is waiting torun
     */
    Map<String, List<TreeViewDto>> waitingRunningNodeMap = new ConcurrentHashMap<>();
    /**
     * List of process instances
     */
    List<ProcessInstance> processInstanceList = processInstanceMapper.queryByProcessDefineId(processId, limit);
    for (ProcessInstance processInstance : processInstanceList) {
        processInstance.setDuration(DateUtils.differSec(processInstance.getStartTime(), processInstance.getEndTime()));
    }
    if (limit > processInstanceList.size()) {
        limit = processInstanceList.size();
    }
    TreeViewDto parentTreeViewDto = new TreeViewDto();
    parentTreeViewDto.setName("DAG");
    parentTreeViewDto.setType("");
    for (int i = limit - 1; i >= 0; i--) {
        ProcessInstance processInstance = processInstanceList.get(i);
        Date endTime = processInstance.getEndTime() == null ? new Date() : processInstance.getEndTime();
        parentTreeViewDto.getInstances().add(new Instance(processInstance.getId(), processInstance.getName(), "", processInstance.getState().toString(), processInstance.getStartTime(), endTime, processInstance.getHost(), DateUtils.format2Readable(endTime.getTime() - processInstance.getStartTime().getTime())));
    }
    List<TreeViewDto> parentTreeViewDtoList = new ArrayList<>();
    parentTreeViewDtoList.add(parentTreeViewDto);
    // Here is the encapsulation task instance
    for (String startNode : dag.getBeginNode()) {
        runningNodeMap.put(startNode, parentTreeViewDtoList);
    }
    while (Stopper.isRunning()) {
        Set<String> postNodeList = null;
        Iterator<Map.Entry<String, List<TreeViewDto>>> iter = runningNodeMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, List<TreeViewDto>> en = iter.next();
            String nodeName = en.getKey();
            parentTreeViewDtoList = en.getValue();
            TreeViewDto treeViewDto = new TreeViewDto();
            treeViewDto.setName(nodeName);
            TaskNode taskNode = dag.getNode(nodeName);
            treeViewDto.setType(taskNode.getType());
            // set treeViewDto instances
            for (int i = limit - 1; i >= 0; i--) {
                ProcessInstance processInstance = processInstanceList.get(i);
                TaskInstance taskInstance = taskInstanceMapper.queryByInstanceIdAndName(processInstance.getId(), nodeName);
                if (taskInstance == null) {
                    treeViewDto.getInstances().add(new Instance(-1, "not running", null));
                } else {
                    Date startTime = taskInstance.getStartTime() == null ? new Date() : taskInstance.getStartTime();
                    Date endTime = taskInstance.getEndTime() == null ? new Date() : taskInstance.getEndTime();
                    int subProcessId = 0;
                    /**
                     * if process is sub process, the return sub id, or sub id=0
                     */
                    if (taskInstance.getTaskType().equals(TaskType.SUB_PROCESS.name())) {
                        String taskJson = taskInstance.getTaskJson();
                        taskNode = JSON.parseObject(taskJson, TaskNode.class);
                        subProcessId = Integer.parseInt(JSON.parseObject(taskNode.getParams()).getString(CMDPARAM_SUB_PROCESS_DEFINE_ID));
                    }
                    treeViewDto.getInstances().add(new Instance(taskInstance.getId(), taskInstance.getName(), taskInstance.getTaskType(), taskInstance.getState().toString(), taskInstance.getStartTime(), taskInstance.getEndTime(), taskInstance.getHost(), DateUtils.format2Readable(endTime.getTime() - startTime.getTime()), subProcessId));
                }
            }
            for (TreeViewDto pTreeViewDto : parentTreeViewDtoList) {
                pTreeViewDto.getChildren().add(treeViewDto);
            }
            postNodeList = dag.getSubsequentNodes(nodeName);
            if (CollectionUtils.isNotEmpty(postNodeList)) {
                for (String nextNodeName : postNodeList) {
                    List<TreeViewDto> treeViewDtoList = waitingRunningNodeMap.get(nextNodeName);
                    if (CollectionUtils.isNotEmpty(treeViewDtoList)) {
                        treeViewDtoList.add(treeViewDto);
                        waitingRunningNodeMap.put(nextNodeName, treeViewDtoList);
                    } else {
                        treeViewDtoList = new ArrayList<>();
                        treeViewDtoList.add(treeViewDto);
                        waitingRunningNodeMap.put(nextNodeName, treeViewDtoList);
                    }
                }
            }
            runningNodeMap.remove(nodeName);
        }
        if (waitingRunningNodeMap == null || waitingRunningNodeMap.size() == 0) {
            break;
        } else {
            runningNodeMap.putAll(waitingRunningNodeMap);
            waitingRunningNodeMap.clear();
        }
    }
    result.put(Constants.DATA_LIST, parentTreeViewDto);
    result.put(Constants.STATUS, Status.SUCCESS);
    result.put(Constants.MSG, Status.SUCCESS.getMsg());
    return result;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) Instance(org.apache.dolphinscheduler.api.dto.treeview.Instance) TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) ArrayList(java.util.ArrayList) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) TaskNodeRelation(org.apache.dolphinscheduler.common.model.TaskNodeRelation) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) TaskNode(org.apache.dolphinscheduler.common.model.TaskNode) Date(java.util.Date) JSONObject(com.alibaba.fastjson.JSONObject) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) TreeViewDto(org.apache.dolphinscheduler.api.dto.treeview.TreeViewDto) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Instance (org.apache.dolphinscheduler.api.dto.treeview.Instance)1 TreeViewDto (org.apache.dolphinscheduler.api.dto.treeview.TreeViewDto)1 TaskNode (org.apache.dolphinscheduler.common.model.TaskNode)1 TaskNodeRelation (org.apache.dolphinscheduler.common.model.TaskNodeRelation)1 ProcessDefinition (org.apache.dolphinscheduler.dao.entity.ProcessDefinition)1 ProcessInstance (org.apache.dolphinscheduler.dao.entity.ProcessInstance)1 TaskInstance (org.apache.dolphinscheduler.dao.entity.TaskInstance)1