From 30c690bf46f27f20020f7bfbe3a7a5603a9f2bad Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Fri, 18 Oct 2024 14:23:44 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E4=BB=BB=E5=8A=A1=E4=B8=AD=E5=BF=83):=20?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E4=BB=BB=E5=8A=A1=E4=B8=AD=E5=BF=83=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E8=AF=A6=E6=83=85=E5=81=9C=E6=AD=A2&=E6=89=B9?= =?UTF-8?q?=E9=87=8F=E5=81=9C=E6=AD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProjectTaskHubController.java | 14 +- .../OrganizationTaskHubController.java | 14 +- .../controller/SystemTaskHubController.java | 35 ++++- .../request/TaskHubItemBatchRequest.java | 24 ++++ .../system/mapper/ExtExecTaskItemMapper.java | 8 ++ .../system/mapper/ExtExecTaskItemMapper.xml | 66 ++++++++++ .../system/mapper/ExtExecTaskMapper.java | 2 +- .../system/mapper/ExtExecTaskMapper.xml | 6 + .../system/service/BaseTaskHubLogService.java | 124 ++++++++++++++---- .../system/service/BaseTaskHubService.java | 81 +++++++++++- .../BaseTaskHubControllerTests.java | 31 +++++ 11 files changed, 358 insertions(+), 47 deletions(-) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/TaskHubItemBatchRequest.java diff --git a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java index b05c7a7a7b..04a1472d79 100644 --- a/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java +++ b/backend/services/project-management/src/main/java/io/metersphere/project/controller/ProjectTaskHubController.java @@ -33,6 +33,8 @@ public class ProjectTaskHubController { @Resource private BaseTaskHubService baseTaskHubService; + @Resource + private BaseTaskHubLogService baseTaskHubLogService; @PostMapping("/exec-task/page") @Operation(summary = "项目-任务中心-执行任务列表") @@ -85,10 +87,11 @@ public class ProjectTaskHubController { @PostMapping("/exec-task/batch-stop") @Operation(summary = "项目-任务中心-用例执行任务-批量停止任务") - @Log(type = OperationLogType.UPDATE, expression = "#msClass.projectBatchStopLog(#id)", msClass = BaseTaskHubLogService.class) @RequiresPermissions(PermissionConstants.PROJECT_CASE_TASK_CENTER_EXEC_STOP) public void batchStopTask(@Validated @RequestBody TableBatchProcessDTO request) { - baseTaskHubService.batchStopTask(request, SessionUtils.getUserId(), null, SessionUtils.getCurrentProjectId()); + List ids = baseTaskHubService.getTaskIds(request, null, SessionUtils.getCurrentProjectId()); + baseTaskHubService.batchStopTask(ids, SessionUtils.getUserId(), null, SessionUtils.getCurrentProjectId()); + baseTaskHubLogService.projectBatchStopLog(ids); } @@ -102,9 +105,10 @@ public class ProjectTaskHubController { @PostMapping("/exec-task/batch-delete") @Operation(summary = "项目-任务中心-用例执行任务-批量删除任务") - @Log(type = OperationLogType.DELETE, expression = "#msClass.projectBatchDeleteLog(#id)", msClass = BaseTaskHubLogService.class) @RequiresPermissions(PermissionConstants.PROJECT_CASE_TASK_CENTER_DELETE) - public void batchDeleteTask(@PathVariable String id) { - baseTaskHubService.deleteTask(id, null, SessionUtils.getCurrentProjectId()); + public void batchDeleteTask(@Validated @RequestBody TableBatchProcessDTO request) { + List ids = baseTaskHubService.getTaskIds(request, null, SessionUtils.getCurrentProjectId()); + baseTaskHubService.batchDeleteTask(ids, null, SessionUtils.getCurrentProjectId()); + baseTaskHubLogService.projectBatchDeleteLog(ids); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java index afeb3efaad..ce982f7f98 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationTaskHubController.java @@ -37,6 +37,8 @@ public class OrganizationTaskHubController { private BaseTaskHubService baseTaskHubService; @Resource BaseProjectMapper baseProjectMapper; + @Resource + private BaseTaskHubLogService baseTaskHubLogService; @PostMapping("/exec-task/page") @Operation(summary = "组织-任务中心-执行任务列表") @@ -92,10 +94,12 @@ public class OrganizationTaskHubController { @PostMapping("/exec-task/batch-stop") @Operation(summary = "组织-任务中心-用例执行任务-批量停止任务") - @Log(type = OperationLogType.UPDATE, expression = "#msClass.orgBatchStopLog(#id)", msClass = BaseTaskHubLogService.class) @RequiresPermissions(PermissionConstants.ORGANIZATION_CASE_TASK_CENTER_EXEC_STOP) public void batchStopTask(@Validated @RequestBody TableBatchProcessDTO request) { - baseTaskHubService.batchStopTask(request, SessionUtils.getUserId(), SessionUtils.getCurrentOrganizationId(), null); + List ids = baseTaskHubService.getTaskIds(request, SessionUtils.getCurrentOrganizationId(), null); + baseTaskHubService.batchStopTask(ids, SessionUtils.getUserId(), SessionUtils.getCurrentOrganizationId(), null); + //日志 + baseTaskHubLogService.orgBatchStopLog(ids); } @@ -111,9 +115,11 @@ public class OrganizationTaskHubController { @PostMapping("/exec-task/batch-delete") @Operation(summary = "组织-任务中心-用例执行任务-批量删除任务") - @Log(type = OperationLogType.DELETE, expression = "#msClass.orgBatchDeleteLog(#request)", msClass = BaseTaskHubLogService.class) @RequiresPermissions(PermissionConstants.ORGANIZATION_CASE_TASK_CENTER_DELETE) public void batchDeleteTask(@Validated @RequestBody TableBatchProcessDTO request) { - baseTaskHubService.batchDeleteTask(request, SessionUtils.getCurrentOrganizationId(), null); + List ids = baseTaskHubService.getTaskIds(request, SessionUtils.getCurrentOrganizationId(), null); + baseTaskHubService.batchDeleteTask(ids, SessionUtils.getCurrentOrganizationId(), null); + //日志 + baseTaskHubLogService.orgBatchDeleteLog(ids); } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java index 52e72f26d1..5fb136b81d 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemTaskHubController.java @@ -4,6 +4,7 @@ import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.dto.taskhub.*; +import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; import io.metersphere.system.log.annotation.Log; @@ -31,6 +32,8 @@ public class SystemTaskHubController { @Resource private BaseTaskHubService baseTaskHubService; + @Resource + private BaseTaskHubLogService baseTaskHubLogService; @PostMapping("/exec-task/page") @Operation(summary = "系统-任务中心-执行任务列表") @@ -91,10 +94,12 @@ public class SystemTaskHubController { @PostMapping("/exec-task/batch-stop") @Operation(summary = "系统-任务中心-用例执行任务-批量停止任务") - @Log(type = OperationLogType.STOP, expression = "#msClass.systemBatchStopLog(#request)", msClass = BaseTaskHubLogService.class) @RequiresPermissions(PermissionConstants.SYSTEM_CASE_TASK_CENTER_EXEC_STOP) public void batchStopTask(@Validated @RequestBody TableBatchProcessDTO request) { - baseTaskHubService.batchStopTask(request, SessionUtils.getUserId(), null, null); + List ids = baseTaskHubService.getTaskIds(request, null, null); + baseTaskHubService.batchStopTask(ids, SessionUtils.getUserId(), null, null); + //系統日志 + baseTaskHubLogService.systemBatchStopLog(ids); } @@ -109,16 +114,36 @@ public class SystemTaskHubController { @PostMapping("/exec-task/batch-delete") @Operation(summary = "系统-任务中心-用例执行任务-批量删除任务") - @Log(type = OperationLogType.DELETE, expression = "#msClass.systemBatchDeleteLog(#request)", msClass = BaseTaskHubLogService.class) @RequiresPermissions(PermissionConstants.SYSTEM_CASE_TASK_CENTER_DELETE) public void batchDeleteTask(@Validated @RequestBody TableBatchProcessDTO request) { - baseTaskHubService.batchDeleteTask(request, SessionUtils.getCurrentOrganizationId(), null); + List ids = baseTaskHubService.getTaskIds(request, null, null); + baseTaskHubService.batchDeleteTask(ids, SessionUtils.getCurrentOrganizationId(), null); + //系統日志 + baseTaskHubLogService.systemBatchDeleteLog(ids); } //TODO 系统&组织&项目 任务按钮操作:失败重跑 查看报告 批量失败重跑 - //TODO 系统&组织&项目 任务详情按钮操作:查看 停止 批量停止 + @GetMapping("/exec-task/item/stop/{id}") + @Operation(summary = "系统-任务中心-用例任务详情-停止任务") + @Log(type = OperationLogType.STOP, expression = "#msClass.systemStopItemLog(#id)", msClass = BaseTaskHubLogService.class) + @RequiresPermissions(PermissionConstants.SYSTEM_CASE_TASK_CENTER_EXEC_STOP) + public void stopTaskItem(@PathVariable String id) { + baseTaskHubService.stopTaskItem(id, SessionUtils.getUserId(), null, null); + } + + @PostMapping("/exec-task/item/batch-stop") + @Operation(summary = "系统-任务中心-用例任务详情-批量停止任务") + @Log(type = OperationLogType.STOP, expression = "#msClass.systemBatchStopItemLog(#request)", msClass = BaseTaskHubLogService.class) + @RequiresPermissions(PermissionConstants.SYSTEM_CASE_TASK_CENTER_EXEC_STOP) + public void batchStopTaskItem(@Validated @RequestBody TaskHubItemBatchRequest request) { + List itemIds = baseTaskHubService.getTaskItemIds(request, null, null); + baseTaskHubService.batchStopTaskItem(itemIds, SessionUtils.getUserId(), null, null); + baseTaskHubLogService.systemBatchStopItemLog(itemIds); + + } + //TODO 系统&组织&项目 任务详情按钮操作:查看 //TODO 系统&组织&项目 后台任务操作:删除 批量开启 批量关闭 diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/TaskHubItemBatchRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/TaskHubItemBatchRequest.java new file mode 100644 index 0000000000..dc134c6a8c --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/taskhub/request/TaskHubItemBatchRequest.java @@ -0,0 +1,24 @@ +package io.metersphere.system.dto.taskhub.request; + +import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.List; + +/** + * @author wx + */ +@Data +public class TaskHubItemBatchRequest extends TableBatchProcessDTO { + + @Schema(description = "任务id") + private String taskId; + + @Schema(description = "资源池id") + private List resourcePoolIds; + + @Schema(description = "资源池节点") + private List resourcePoolNodes; + +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java index 455d53313e..9492202401 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.java @@ -1,6 +1,7 @@ package io.metersphere.system.mapper; import io.metersphere.system.domain.ExecTaskItem; +import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.dto.taskhub.TaskHubItemDTO; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import org.apache.ibatis.annotations.Param; @@ -25,6 +26,7 @@ public interface ExtExecTaskItemMapper { /** * 查询时间范围内的任务项ID集合 + * * @param timeMills 时间戳 * @param projectId 项目ID * @return 任务项ID列表 @@ -38,4 +40,10 @@ public interface ExtExecTaskItemMapper { Boolean hasFakeErrorItem(@Param("taskId") String taskId); List getItemIdByTaskIds(@Param("taskIds") List taskIds); + + void batchUpdateTaskItemStatusByIds(@Param("ids") List ids, @Param("userId") String userId, @Param("organizationId") String organizationId, @Param("projectId") String projectId, @Param("status") String status); + + List getResourcePoolsByItemIds(@Param("ids") List ids); + + List getIds(@Param("request") TableBatchProcessDTO request, @Param("organizationId") String organizationId, @Param("projectId") String projectId); } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml index 12dcee52ed..c609631736 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskItemMapper.xml @@ -99,6 +99,11 @@ and exec_task_item.result in + + + and exec_task.trigger_mode in + + @@ -160,4 +165,65 @@ #{taskId} + + + + UPDATE exec_task_item + SET `status` = #{status}, + executor = #{userId} + WHERE id in + + #{id} + + + and project_id = #{projectId} + + + and organization_id = #{organizationId} + + and `status` = 'RUNNING' + + + + + + + + + + + and ( + exec_task.num like concat('%', #{request.keyword},'%') + or exec_task_item.resource_name like concat('%', #{request.keyword},'%') + ) + + + + + \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.java index bf720048ee..ff4227e1bf 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.java @@ -15,7 +15,7 @@ public interface ExtExecTaskMapper { void deleteTaskByIds(@Param("ids") List ids, @Param("orgId") String orgId, @Param("projectId") String projectId); - List getIds(@Param("request") TableBatchProcessDTO request); + List getIds(@Param("request") TableBatchProcessDTO request, @Param("organizationId") String organizationId, @Param("projectId") String projectId); void batchUpdateTaskStatus(@Param("ids") List ids, @Param("userId") String userId, @Param("organizationId") String organizationId, @Param("projectId") String projectId, @Param("status") String status); diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.xml index ed5f60d19e..3d6105eaf4 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtExecTaskMapper.xml @@ -47,6 +47,12 @@ diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubLogService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubLogService.java index 311e1b4825..5a8f62ced5 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubLogService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubLogService.java @@ -3,10 +3,13 @@ package io.metersphere.system.service; import io.metersphere.sdk.constants.OperationLogConstants; import io.metersphere.system.domain.ExecTask; import io.metersphere.system.domain.ExecTaskExample; -import io.metersphere.system.dto.table.TableBatchProcessDTO; +import io.metersphere.system.domain.ExecTaskItem; +import io.metersphere.system.domain.ExecTaskItemExample; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.log.dto.LogDTO; +import io.metersphere.system.log.service.OperationLogService; +import io.metersphere.system.mapper.ExecTaskItemMapper; import io.metersphere.system.mapper.ExecTaskMapper; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; @@ -25,7 +28,9 @@ public class BaseTaskHubLogService { @Resource private ExecTaskMapper execTaskMapper; @Resource - private BaseTaskHubService baseTaskHubService; + private OperationLogService operationLogService; + @Resource + private ExecTaskItemMapper execTaskItemMapper; /** * 系统停止任务日志 @@ -52,11 +57,13 @@ public class BaseTaskHubLogService { /** * 系统批量停止任务日志 * - * @param request + * @param ids * @return */ - public List systemBatchStopLog(TableBatchProcessDTO request) { - List ids = baseTaskHubService.getTaskIds(request); + public void systemBatchStopLog(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } ExecTaskExample example = new ExecTaskExample(); example.createCriteria().andIdIn(ids); List execTasks = execTaskMapper.selectByExample(example); @@ -74,7 +81,7 @@ public class BaseTaskHubLogService { logDTOList.add(dto); }); } - return logDTOList; + operationLogService.batchAdd(logDTOList); } @@ -104,11 +111,13 @@ public class BaseTaskHubLogService { /** * 组织批量停止任务日志 * - * @param request + * @param ids * @return */ - public List orgBatchStopLog(TableBatchProcessDTO request) { - List ids = baseTaskHubService.getTaskIds(request); + public void orgBatchStopLog(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } ExecTaskExample example = new ExecTaskExample(); example.createCriteria().andIdIn(ids); List execTasks = execTaskMapper.selectByExample(example); @@ -126,7 +135,7 @@ public class BaseTaskHubLogService { logDTOList.add(dto); }); } - return logDTOList; + operationLogService.batchAdd(logDTOList); } @@ -156,11 +165,13 @@ public class BaseTaskHubLogService { /** * 项目批量停止任务日志 * - * @param request + * @param ids * @return */ - public List projectBatchStopLog(TableBatchProcessDTO request) { - List ids = baseTaskHubService.getTaskIds(request); + public void projectBatchStopLog(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } ExecTaskExample example = new ExecTaskExample(); example.createCriteria().andIdIn(ids); List execTasks = execTaskMapper.selectByExample(example); @@ -178,7 +189,7 @@ public class BaseTaskHubLogService { logDTOList.add(dto); }); } - return logDTOList; + operationLogService.batchAdd(logDTOList); } @@ -207,11 +218,13 @@ public class BaseTaskHubLogService { /** * 系统批量删除任务日志 * - * @param request + * @param ids * @return */ - public List systemBatchDeleteLog(TableBatchProcessDTO request) { - List ids = baseTaskHubService.getTaskIds(request); + public void systemBatchDeleteLog(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } ExecTaskExample example = new ExecTaskExample(); example.createCriteria().andIdIn(ids); List execTasks = execTaskMapper.selectByExample(example); @@ -229,7 +242,7 @@ public class BaseTaskHubLogService { logDTOList.add(dto); }); } - return logDTOList; + operationLogService.batchAdd(logDTOList); } /** @@ -257,11 +270,13 @@ public class BaseTaskHubLogService { /** * 组织批量删除任务日志 * - * @param request + * @param ids * @return */ - public List orgBatchDeleteLog(TableBatchProcessDTO request) { - List ids = baseTaskHubService.getTaskIds(request); + public void orgBatchDeleteLog(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } ExecTaskExample example = new ExecTaskExample(); example.createCriteria().andIdIn(ids); List execTasks = execTaskMapper.selectByExample(example); @@ -279,7 +294,7 @@ public class BaseTaskHubLogService { logDTOList.add(dto); }); } - return logDTOList; + operationLogService.batchAdd(logDTOList); } /** @@ -308,11 +323,13 @@ public class BaseTaskHubLogService { /** * 项目批量删除任务日志 * - * @param request + * @param ids * @return */ - public List projectBatchDeleteLog(TableBatchProcessDTO request) { - List ids = baseTaskHubService.getTaskIds(request); + public void projectBatchDeleteLog(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } ExecTaskExample example = new ExecTaskExample(); example.createCriteria().andIdIn(ids); List execTasks = execTaskMapper.selectByExample(example); @@ -330,6 +347,61 @@ public class BaseTaskHubLogService { logDTOList.add(dto); }); } - return logDTOList; + operationLogService.batchAdd(logDTOList); } + + + /** + * 系统停止任务项日志 + * + * @param id + * @return + */ + public LogDTO systemStopItemLog(String id) { + ExecTaskItem execTaskItem = execTaskItemMapper.selectByPrimaryKey(id); + LogDTO dto = null; + if (execTaskItem != null) { + dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + execTaskItem.getId(), + null, + OperationLogType.STOP.name(), + OperationLogModule.SETTING_SYSTEM_TASK_CENTER, + execTaskItem.getResourceName()); + } + return dto; + } + + + /** + * 系统批量停止任务项日志 + * + * @param ids + * @return + */ + public void systemBatchStopItemLog(List ids) { + if (CollectionUtils.isEmpty(ids)) { + return; + } + ExecTaskItemExample example = new ExecTaskItemExample(); + example.createCriteria().andIdIn(ids); + List execTasks = execTaskItemMapper.selectByExample(example); + List logDTOList = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(execTasks)) { + execTasks.forEach(item -> { + LogDTO dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + item.getId(), + null, + OperationLogType.STOP.name(), + OperationLogModule.SETTING_SYSTEM_TASK_CENTER, + item.getResourceName()); + logDTOList.add(dto); + }); + } + operationLogService.batchAdd(logDTOList); + } + } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java index 6973bd82b8..4fc5282014 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubService.java @@ -27,6 +27,7 @@ import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.table.TableBatchProcessDTO; import io.metersphere.system.dto.taskhub.*; +import io.metersphere.system.dto.taskhub.request.TaskHubItemBatchRequest; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; import io.metersphere.system.mapper.*; @@ -552,8 +553,7 @@ public class BaseTaskHubService { apiReportRelateTaskMapper.deleteByExample(reportExample); } - public void batchStopTask(TableBatchProcessDTO request, String userId, String orgId, String projectId) { - List ids = getTaskIds(request); + public void batchStopTask(List ids, String userId, String orgId, String projectId) { if (CollectionUtils.isNotEmpty(ids)) { //1.更新任务状态 extExecTaskMapper.batchUpdateTaskStatus(ids, userId, orgId, projectId, ExecStatus.STOPPED.name()); @@ -564,9 +564,9 @@ public class BaseTaskHubService { } - public List getTaskIds(TableBatchProcessDTO request) { + public List getTaskIds(TableBatchProcessDTO request, String orgId, String projectId) { if (request.isSelectAll()) { - List ids = extExecTaskMapper.getIds(request); + List ids = extExecTaskMapper.getIds(request, orgId, projectId); if (CollectionUtils.isNotEmpty(request.getExcludeIds())) { ids.removeAll(request.getExcludeIds()); } @@ -576,8 +576,7 @@ public class BaseTaskHubService { } } - public void batchDeleteTask(TableBatchProcessDTO request, String orgId, String projectId) { - List ids = getTaskIds(request); + public void batchDeleteTask(List ids, String orgId, String projectId) { if (CollectionUtils.isNotEmpty(ids)) { //1.删除任务 extExecTaskMapper.deleteTaskByIds(ids, orgId, projectId); @@ -590,4 +589,74 @@ public class BaseTaskHubService { handleStopTask(ids); } } + + + /** + * 停止任务项 + * + * @param id + * @param userId + * @param orgId + * @param projectId + */ + public void stopTaskItem(String id, String userId, String orgId, String projectId) { + //1.更新任务明细状态 + extExecTaskItemMapper.batchUpdateTaskItemStatusByIds(List.of(id), userId, orgId, projectId, ExecStatus.STOPPED.name()); + //2.通过任务项id停止任务 + handleStopTaskItem(List.of(id)); + } + + + private void handleStopTaskItem(List ids) { + List execTaskItems = extExecTaskItemMapper.getResourcePoolsByItemIds(ids); + Map> resourcePoolMaps = execTaskItems.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolId)); + resourcePoolMaps.forEach((k, v) -> { + //判断资源池类型 + TestResourcePoolReturnDTO testResourcePoolDTO = testResourcePoolService.getTestResourcePoolDetail(k); + boolean isK8SResourcePool = StringUtils.equals(testResourcePoolDTO.getType(), ResourcePoolTypeEnum.K8S.getName()); + if (isK8SResourcePool) { + List itemIds = v.stream().map(ExecTaskItem::getId).toList(); + //K8S 通过任务项id停止任务项 + handleK8STaskItem(itemIds, testResourcePoolDTO); + } else { + Map> nodeItem = execTaskItems.stream().collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode)); + handleNodeTask(nodeItem); + } + }); + } + + private void handleK8STaskItem(List itemIds, TestResourcePoolReturnDTO testResourcePoolDTO) { + SubListUtils.dealForSubList(itemIds, 100, subList -> { + try { + TestResourceDTO testResourceDTO = new TestResourceDTO(); + BeanUtils.copyBean(testResourceDTO, testResourcePoolDTO.getTestResourceReturnDTO()); + EngineFactory.stopApiTaskItem(subList, testResourceDTO); + } catch (Exception e) { + LogUtils.error(e); + } + }); + } + + public void batchStopTaskItem(List itemIds, String userId, String orgId, String projectId) { + if (CollectionUtils.isNotEmpty(itemIds)) { + //1.更新任务明细状态 + extExecTaskItemMapper.batchUpdateTaskItemStatusByIds(itemIds, userId, orgId, projectId, ExecStatus.STOPPED.name()); + //2.通过任务项id停止任务 + handleStopTaskItem(itemIds); + } + + } + + + public List getTaskItemIds(TaskHubItemBatchRequest request, String orgId, String projectId) { + if (request.isSelectAll()) { + List ids = extExecTaskItemMapper.getIds(request, orgId, projectId); + if (CollectionUtils.isNotEmpty(request.getExcludeIds())) { + ids.removeAll(request.getExcludeIds()); + } + return ids; + } else { + return request.getSelectIds(); + } + } } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java index ed3c236e9f..12d001844a 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/BaseTaskHubControllerTests.java @@ -45,6 +45,8 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String SYSTEM_TASK_DELETE = "/system/task-center/exec-task/delete/"; public static final String SYSTEM_TASK_BATCH_STOP = "/system/task-center/exec-task/batch-stop/"; public static final String SYSTEM_TASK_BATCH_DELETE = "/system/task-center/exec-task/batch-delete/"; + public static final String SYSTEM_TASK_ITEM_STOP = "/system/task-center/exec-task/item/stop/"; + public static final String SYSTEM_TASK_ITEM_BATCH_STOP = "/system/task-center/exec-task/item/batch-stop"; @Test @Order(1) @@ -161,6 +163,21 @@ public class BaseTaskHubControllerTests extends BaseTest { Assertions.assertNotNull(resultHolder); } + /** + * 系统任务项停止 + */ + @Test + @Order(4) + public void systemTaskItemStop() throws Exception { + this.requestGet(SYSTEM_TASK_ITEM_STOP + "1"); + MvcResult mvcResult = this.requestGetWithOkAndReturn(SYSTEM_TASK_ITEM_STOP + "2"); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } + /** * 系统执行任务删除 @@ -200,6 +217,20 @@ public class BaseTaskHubControllerTests extends BaseTest { this.requestPost(SYSTEM_TASK_BATCH_STOP, request); } + /** + * 系统任务项停止 + */ + @Test + @Order(5) + public void systemTaskItemBatchStop() throws Exception { + TableBatchProcessDTO request = new TableBatchProcessDTO(); + request.setSelectAll(true); + this.requestPost(SYSTEM_TASK_ITEM_BATCH_STOP, request); + request.setSelectAll(false); + request.setSelectIds(List.of("1", "2")); + this.requestPost(SYSTEM_TASK_ITEM_BATCH_STOP, request); + } + /** * 组织任务中心测试用例