TaskInstanceService.java 6.2 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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.
 */
Q
qiaozhanwei 已提交
17
package org.apache.dolphinscheduler.api.service;
L
ligang 已提交
18 19


Q
qiaozhanwei 已提交
20 21
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.utils.PageInfo;
22
import org.apache.dolphinscheduler.common.Constants;
Q
qiaozhanwei 已提交
23 24 25
import org.apache.dolphinscheduler.common.enums.ExecutionStatus;
import org.apache.dolphinscheduler.common.utils.CollectionUtils;
import org.apache.dolphinscheduler.common.utils.DateUtils;
T
Tboy 已提交
26
import org.apache.dolphinscheduler.common.utils.StringUtils;
Q
qiaozhanwei 已提交
27 28 29 30 31
import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.entity.TaskInstance;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import org.apache.dolphinscheduler.dao.mapper.TaskInstanceMapper;
32
import org.apache.dolphinscheduler.service.process.ProcessService;
33 34 35 36 37 38 39 40 41

import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

L
ligang 已提交
42 43 44
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

45 46
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
L
ligang 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60

/**
 * task instance service
 */
@Service
public class TaskInstanceService extends BaseService {

    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    ProjectService projectService;

    @Autowired
61
    ProcessService processService;
L
ligang 已提交
62 63 64 65

    @Autowired
    TaskInstanceMapper taskInstanceMapper;

66 67 68 69 70 71
    @Autowired
    ProcessInstanceService processInstanceService;

    @Autowired
    UsersService usersService;

L
ligang 已提交
72 73 74 75

    /**
     * query task list by project, process instance, task name, task start time, task end time, task status, keyword paging
     *
B
bao liang 已提交
76 77 78 79 80 81 82 83 84 85 86 87
     * @param loginUser login user
     * @param projectName project name
     * @param processInstanceId process instance id
     * @param searchVal search value
     * @param taskName task name
     * @param stateType state type
     * @param host host
     * @param startDate start time
     * @param endDate end time
     * @param pageNo page number
     * @param pageSize page size
     * @return task list page
L
ligang 已提交
88
     */
89 90 91 92
    public Map<String, Object> queryTaskListPaging(User loginUser, String projectName,
                                                   Integer processInstanceId, String taskName, String executorName, String startDate,
                                                   String endDate, String searchVal, ExecutionStatus stateType, String host,
                                                   Integer pageNo, Integer pageSize) {
_和's avatar
_和 已提交
93
        Map<String, Object> result = new HashMap<>();
L
ligang 已提交
94 95 96 97 98 99 100 101 102
        Project project = projectMapper.queryByName(projectName);

        Map<String, Object> checkResult = projectService.checkProjectAndAuth(loginUser, project, projectName);
        Status status = (Status) checkResult.get(Constants.STATUS);
        if (status != Status.SUCCESS) {
            return checkResult;
        }

        int[] statusArray = null;
103
        if (stateType != null) {
L
ligang 已提交
104 105 106 107 108
            statusArray = new int[]{stateType.ordinal()};
        }

        Date start = null;
        Date end = null;
109 110 111 112
        if (StringUtils.isNotEmpty(startDate)) {
            start = DateUtils.getScheduleDate(startDate);
            if (start == null) {
                return generateInvalidParamRes(result, "startDate");
L
ligang 已提交
113
            }
114 115 116 117 118
        }
        if (StringUtils.isNotEmpty(endDate)) {
            end = DateUtils.getScheduleDate(endDate);
            if (end == null) {
                return generateInvalidParamRes(result, "endDate");
L
ligang 已提交
119 120 121
            }
        }

B
bao liang 已提交
122
        Page<TaskInstance> page = new Page(pageNo, pageSize);
123 124 125
        PageInfo pageInfo = new PageInfo<TaskInstance>(pageNo, pageSize);
        int executorId = usersService.getUserIdByName(executorName);

B
bao liang 已提交
126
        IPage<TaskInstance> taskInstanceIPage = taskInstanceMapper.queryTaskInstanceListPaging(
127
                page, project.getId(), processInstanceId, searchVal, taskName, executorId, statusArray, host, start, end
B
bao liang 已提交
128
        );
129 130 131
        Set<String> exclusionSet = new HashSet<>();
        exclusionSet.add(Constants.CLASS);
        exclusionSet.add("taskJson");
132
        List<TaskInstance> taskInstanceList = taskInstanceIPage.getRecords();
133

134
        for (TaskInstance taskInstance : taskInstanceList) {
135 136 137 138 139
            taskInstance.setDuration(DateUtils.differSec(taskInstance.getStartTime(), taskInstance.getEndTime()));
            User executor = usersService.queryUser(taskInstance.getExecutorId());
            if (null != executor) {
                taskInstance.setExecutorName(executor.getUserName());
            }
140
        }
141 142
        pageInfo.setTotalCount((int) taskInstanceIPage.getTotal());
        pageInfo.setLists(CollectionUtils.getListByExclusion(taskInstanceIPage.getRecords(), exclusionSet));
L
ligang 已提交
143 144 145 146 147
        result.put(Constants.DATA_LIST, pageInfo);
        putMsg(result, Status.SUCCESS);

        return result;
    }
148 149 150 151 152 153 154 155 156 157 158 159

    /***
     * generate {@link org.apache.dolphinscheduler.api.enums.Status#REQUEST_PARAMS_NOT_VALID_ERROR} res with  param name
     * @param result exist result map
     * @param params invalid params name
     * @return update result map
     */
    private Map<String, Object> generateInvalidParamRes(Map<String, Object> result, String params) {
        result.put(Constants.STATUS, Status.REQUEST_PARAMS_NOT_VALID_ERROR);
        result.put(Constants.MSG, MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), params));
        return result;
    }
L
ligang 已提交
160
}