Skip to content

Commit

Permalink
[feat][API] New restful API for workflow state (apache#13031)
Browse files Browse the repository at this point in the history
  • Loading branch information
insist777 authored and shangeyao committed Dec 3, 2022
1 parent 6e0ba01 commit 1cb963d
Show file tree
Hide file tree
Showing 20 changed files with 998 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* 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.controller;

import static org.apache.dolphinscheduler.api.enums.Status.COUNT_PROCESS_DEFINITION_USER_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_ALL_WORKFLOW_COUNT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_ONE_TASK_STATES_COUNT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_ONE_WORKFLOW_STATE_COUNT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_TASK_STATES_COUNT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_WORKFLOW_STATES_COUNT_ERROR;

import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.dto.project.StatisticsStateRequest;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.DataAnalysisService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.User;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
/**
* StatisticsV2 controller
*/
@Tag(name = "STATISTICS_V2")
@RestController
@RequestMapping("/v2/statistics")
public class StatisticsV2Controller extends BaseController {

@Autowired
private DataAnalysisService dataAnalysisService;

/**
* query all workflow count
* @param loginUser login user
* @return workflow count
*/
@Operation(summary = "queryAllWorkflowCount", description = "QUERY_ALL_WORKFLOW_COUNT")
@GetMapping(value = "/workflows/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_ALL_WORKFLOW_COUNT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryWorkflowInstanceCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Map<String, Object> result = dataAnalysisService.queryAllWorkflowCounts(loginUser);
return returnDataList(result);
}

/**
* query all workflow states count
* @param loginUser login user
* @param statisticsStateRequest statisticsStateRequest
* @return workflow states count
*/
@Operation(summary = "queryAllWorkflowStatesCount", description = "QUERY_ALL_WORKFLOW_STATES_COUNT")
@GetMapping(value = "/workflows/states/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_WORKFLOW_STATES_COUNT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryWorkflowStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) {
Map<String, Object> result =
dataAnalysisService.countWorkflowStates(loginUser, statisticsStateRequest);
return returnDataList(result);
}

/**
* query one workflow states count
* @param loginUser login user
* @param workflowCode workflowCode
* @return workflow states count
*/
@Operation(summary = "queryOneWorkflowStatesCount", description = "QUERY_One_WORKFLOW_STATES_COUNT")
@GetMapping(value = "/{workflowCode}/states/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_ONE_WORKFLOW_STATE_COUNT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryOneWorkflowStates(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("workflowCode") Long workflowCode) {
Map<String, Object> result =
dataAnalysisService.countOneWorkflowStates(loginUser, workflowCode);
return returnDataList(result);
}

/**
* query all task states count
* @param loginUser login user
* @param statisticsStateRequest statisticsStateRequest
* @return tasks states count
*/
@Operation(summary = "queryAllTaskStatesCount", description = "QUERY_ALL_TASK_STATES_COUNT")
@GetMapping(value = "/tasks/states/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_TASK_STATES_COUNT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryTaskStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) {
Map<String, Object> result =
dataAnalysisService.countTaskStates(loginUser, statisticsStateRequest);
return returnDataList(result);
}

/**
* query one task states count
* @param loginUser login user
* @param taskCode taskCode
* @return tasks states count
*/
@Operation(summary = "queryOneTaskStatesCount", description = "QUERY_ONE_TASK_STATES_COUNT")
@GetMapping(value = "/tasks/{taskCode}/states/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_ONE_TASK_STATES_COUNT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryOneTaskStatesCounts(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("taskCode") Long taskCode) {
Map<String, Object> result =
dataAnalysisService.countOneTaskStates(loginUser, taskCode);
return returnDataList(result);
}

/**
* statistics the workflow quantities of certain user
* @param loginUser login user
* @param statisticsStateRequest statisticsStateRequest
* @return workflow count in project code
*/
@Operation(summary = "countDefinitionV2ByUserId", description = "COUNT_PROCESS_DEFINITION_V2_BY_USERID_NOTES")
@GetMapping(value = "/workflows/users/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(COUNT_PROCESS_DEFINITION_USER_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result countDefinitionByUser(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestBody(required = false) StatisticsStateRequest statisticsStateRequest) {
String projectName = statisticsStateRequest.getProjectName();
Long projectCode = statisticsStateRequest.getProjectCode();
if (null == projectCode && !StringUtils.isBlank(projectName)) {
projectCode = dataAnalysisService.getProjectCodeByName(projectName);
}
Map<String, Object> result = dataAnalysisService.countDefinitionByUserV2(loginUser, projectCode, null, null);
return returnDataList(result);
}

/**
* statistics the workflow quantities of certain userId
* @param loginUser login user
* @param userId userId
* @return workflow count in project code
*/
@Operation(summary = "countDefinitionV2ByUser", description = "COUNT_PROCESS_DEFINITION_V2_BY_USER_NOTES")
@GetMapping(value = "/workflows/users/{userId}/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(COUNT_PROCESS_DEFINITION_USER_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result countDefinitionByUserId(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("userId") Integer userId) {
Map<String, Object> result = dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, null);
return returnDataList(result);
}
/**
* statistics the workflow quantities of certain userId and releaseState
* @param loginUser login user
* @param userId userId
* @param releaseState releaseState
* @return workflow count in project code
*/
@Operation(summary = "countDefinitionV2ByUser", description = "COUNT_PROCESS_DEFINITION_V2_BY_USER_NOTES")
@GetMapping(value = "/workflows/users/{userId}/{releaseState}/count")
@ResponseStatus(HttpStatus.OK)
@ApiException(COUNT_PROCESS_DEFINITION_USER_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result countDefinitionByUserState(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("userId") Integer userId,
@PathVariable("releaseState") Integer releaseState) {
Map<String, Object> result = dataAnalysisService.countDefinitionByUserV2(loginUser, null, userId, releaseState);
return returnDataList(result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.dto.project;

import java.util.Date;

import lombok.Data;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class StatisticsStateRequest {

@Schema(name = "isAll", example = "true")
boolean isAll;

@Schema(name = "projectName", example = "PROJECT-NAME")
String projectName;

@Schema(name = "projectCode", example = "1234567890")
Long projectCode;

@Schema(name = "workflowName", example = "WORKFLOW-NAME")
String workflowName;

@Schema(name = "workflowCode", example = "1234567890")
Long workflowCode;

@Schema(name = "taskName", example = "TASK-NAME")
String taskName;

@Schema(name = "taskCode", example = "1234567890")
Long taskCode;

@Schema(name = "startDate", example = "2022-01-01 10:01:02")
Date startTime;

@Schema(name = "endDate", example = "2022-01-02 10:01:02")
Date endTime;

}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ public enum Status {
SAVE_ERROR(10136, "save error", "保存错误"),
DELETE_PROJECT_ERROR_DEFINES_NOT_NULL(10137, "please delete the process definitions in project first!",
"请先删除全部工作流定义"),
QUERY_ALL_WORKFLOW_COUNT_ERROR(10138, "query all workflow count error", "查询所有工作流数量错误"),
QUERY_WORKFLOW_STATES_COUNT_ERROR(10139, "query all workflow states count error", "查询所有工作流状态数量错误"),
QUERY_ONE_WORKFLOW_STATE_COUNT_ERROR(10140, "query one workflow state count error", "查询工作流状态数量错误"),
QUERY_TASK_STATES_COUNT_ERROR(10141, "query all task states count error", "查询所有任务状态数量错误"),
QUERY_ONE_TASK_STATES_COUNT_ERROR(10142, "query one task states count error", "查询任务状态数量错误"),
BATCH_DELETE_PROCESS_INSTANCE_BY_IDS_ERROR(10117, "batch delete process instance by ids {0} error",
"批量删除工作流实例错误: {0}"),
PREVIEW_SCHEDULE_ERROR(10139, "preview schedule error", "预览调度配置错误"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.dolphinscheduler.api.service;

import org.apache.dolphinscheduler.api.dto.project.StatisticsStateRequest;
import org.apache.dolphinscheduler.dao.entity.ExecuteStatusCount;
import org.apache.dolphinscheduler.dao.entity.User;

Expand Down Expand Up @@ -61,9 +62,21 @@ Map<String, Object> countProcessInstanceStateByProject(User loginUser, long proj
*
* @param loginUser login user
* @param projectCode project code
* @return definition count data
* @return workflow count data
*/
Map<String, Object> countDefinitionByUser(User loginUser, long projectCode);
/**
* statistics the workflow quantities of certain user
* <p>
* We only need projects which users have permission to see to determine whether the definition belongs to the user or not.
*
* @param loginUser login user
* @param projectCode project code
* @param userId userId
* @param releaseState releaseState
* @return workflow count data
*/
Map<String, Object> countDefinitionByUserV2(User loginUser, Long projectCode, Integer userId, Integer releaseState);

/**
* statistical command status data
Expand Down Expand Up @@ -94,4 +107,46 @@ Map<String, Object> countProcessInstanceStateByProject(User loginUser, long proj
List<ExecuteStatusCount> countTaskInstanceAllStatesByProjectCodes(@Param("startTime") Date startTime,
@Param("endTime") Date endTime,
@Param("projectCodes") Long[] projectCodes);

/**
* query all workflow count
* @param loginUser login user
* @return workflow count
*/
Map<String, Object> queryAllWorkflowCounts(User loginUser);

/**
* query all workflow states count
* @param loginUser login user
* @param statisticsStateRequest statisticsStateRequest
* @return workflow states count
*/
Map<String, Object> countWorkflowStates(User loginUser,
StatisticsStateRequest statisticsStateRequest);

/**
* query one workflow states count
* @param loginUser login user
* @param workflowCode workflowCode
* @return workflow states count
*/
Map<String, Object> countOneWorkflowStates(User loginUser, Long workflowCode);

/**
* query all task states count
* @param loginUser login user
* @param statisticsStateRequest statisticsStateRequest
* @return tasks states count
*/
Map<String, Object> countTaskStates(User loginUser, StatisticsStateRequest statisticsStateRequest);

/**
* query one task states count
* @param loginUser login user
* @param taskCode taskCode
* @return tasks states count
*/
Map<String, Object> countOneTaskStates(User loginUser, Long taskCode);

Long getProjectCodeByName(String projectName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,4 @@ Result queryProjectWithAuthorizedLevelListPaging(Integer userId, User loginUser,
* @return project list
*/
Result queryAllProjectListForDependent();

}
Loading

0 comments on commit 1cb963d

Please sign in to comment.