From 0fa54338dd3f13a440cd2e10ef7f79d6c18bc2ed Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Mon, 14 Oct 2024 16:28:00 +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&=E7=BB=84=E7=BB=87&=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E4=B8=AD=E5=BF=83-=E6=89=A7=E8=A1=8C?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=81=9C=E6=AD=A2=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProjectTaskHubController.java | 12 +++ .../ProjectTaskHubControllerTests.java | 17 +++- .../OrganizationTaskHubController.java | 12 +++ .../controller/SystemTaskHubController.java | 16 +++- .../system/mapper/ExtExecTaskMapper.java | 3 + .../system/mapper/ExtExecTaskMapper.xml | 13 +++ .../system/service/BaseTaskHubLogService.java | 92 +++++++++++++++++++ .../system/service/BaseTaskHubService.java | 40 ++++++++ .../BaseTaskHubControllerTests.java | 31 +++++++ 9 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubLogService.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 936a64b1e0..13506c499a 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 @@ -8,6 +8,9 @@ import io.metersphere.system.dto.taskhub.TaskHubItemDTO; import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; +import io.metersphere.system.log.annotation.Log; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.service.BaseTaskHubLogService; import io.metersphere.system.service.BaseTaskHubService; import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.SessionUtils; @@ -68,4 +71,13 @@ public class ProjectTaskHubController { public List getUserProject() { return baseTaskHubService.getProjectResourcePoolOptions(SessionUtils.getCurrentProjectId()); } + + + @GetMapping("/exec-task/stop/{id}") + @Operation(summary = "项目-任务中心-用例执行任务-停止任务") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.projectStopLog(#id)", msClass = BaseTaskHubLogService.class) + @RequiresPermissions(PermissionConstants.PROJECT_CASE_TASK_CENTER_EXEC_STOP) + public void stopTask(@PathVariable String id) { + baseTaskHubService.stopTask(id, SessionUtils.getUserId(), null, SessionUtils.getCurrentProjectId()); + } } diff --git a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java index 5e2eee13ca..d9db682145 100644 --- a/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java +++ b/backend/services/project-management/src/test/java/io/metersphere/project/controller/ProjectTaskHubControllerTests.java @@ -24,6 +24,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { public static final String PROJECT_TASK_ITEM_PAGE = "/project/task-center/exec-task/item/page"; public static final String PROJECT_STATISTICS = "/project/task-center/exec-task/statistics"; public static final String PROJECT_RESOURCE_POOL_OPTIONS = "/project/task-center/resource-pool/options"; + public static final String PROJECT_TASK_STOP = "/project/task-center/exec-task/stop/"; @Test @Order(1) @@ -84,7 +85,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { @Test @Order(4) public void getProStatistics() throws Exception { - List ids = List.of("pro_1","pro_2"); + List ids = List.of("pro_1", "pro_2"); MvcResult mvcResult = this.requestPostWithOkAndReturn(PROJECT_STATISTICS, ids); // 获取返回值 String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); @@ -106,4 +107,18 @@ public class ProjectTaskHubControllerTests extends BaseTest { // 返回请求正常 Assertions.assertNotNull(resultHolder); } + + /** + * 项目执行任务停止 + */ + @Test + @Order(6) + public void projectTaskStop() throws Exception { + MvcResult mvcResult = this.requestGetWithOkAndReturn(PROJECT_TASK_STOP + "1"); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } } 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 8c581d1c80..4611b4a58f 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 @@ -9,7 +9,10 @@ import io.metersphere.system.dto.taskhub.TaskHubItemDTO; import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; +import io.metersphere.system.log.annotation.Log; +import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.mapper.BaseProjectMapper; +import io.metersphere.system.service.BaseTaskHubLogService; import io.metersphere.system.service.BaseTaskHubService; import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.SessionUtils; @@ -75,4 +78,13 @@ public class OrganizationTaskHubController { public List getUserProject() { return baseTaskHubService.getOrgResourcePoolOptions(SessionUtils.getCurrentOrganizationId()); } + + + @GetMapping("/exec-task/stop/{id}") + @Operation(summary = "组织-任务中心-用例执行任务-停止任务") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.orgStopLog(#id)", msClass = BaseTaskHubLogService.class) + @RequiresPermissions(PermissionConstants.ORGANIZATION_CASE_TASK_CENTER_EXEC_STOP) + public void stopTask(@PathVariable String id) { + baseTaskHubService.stopTask(id, SessionUtils.getUserId(), SessionUtils.getCurrentOrganizationId(), null); + } } 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 f6c9015216..43abffd03e 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 @@ -2,11 +2,17 @@ package io.metersphere.system.controller; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.system.dto.sdk.BasePageRequest; +import io.metersphere.system.dto.taskcenter.request.TaskCenterBatchRequest; import io.metersphere.system.dto.taskhub.*; import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest; import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; +import io.metersphere.system.log.annotation.Log; +import io.metersphere.system.log.constants.OperationLogType; +import io.metersphere.system.service.BaseTaskHubLogService; import io.metersphere.system.service.BaseTaskHubService; +import io.metersphere.system.service.SystemOrganizationLogService; import io.metersphere.system.utils.Pager; +import io.metersphere.system.utils.SessionUtils; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.media.Schema; @@ -73,7 +79,15 @@ public class SystemTaskHubController { public List resourcePoolStatus(@RequestBody List ids) { return baseTaskHubService.getResourcePoolStatus(ids); } - //TODO 检查节点状态 + + + @GetMapping("/exec-task/stop/{id}") + @Operation(summary = "系统-任务中心-用例执行任务-停止任务") + @Log(type = OperationLogType.UPDATE, expression = "#msClass.systemStopLog(#id)", msClass = BaseTaskHubLogService.class) + @RequiresPermissions(PermissionConstants.SYSTEM_CASE_TASK_CENTER_EXEC_STOP) + public void stopTask(@PathVariable String id) { + baseTaskHubService.stopTask(id, SessionUtils.getUserId(), null, null); + } //TODO 系统&组织&项目 任务按钮操作:删除 停止 失败重跑 查看报告 批量删除 批量停止 批量失败重跑 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 7f280191cd..2cc4f09b36 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 @@ -1,5 +1,6 @@ package io.metersphere.system.mapper; +import io.metersphere.system.domain.ExecTask; import io.metersphere.system.dto.sdk.BasePageRequest; import io.metersphere.system.dto.taskhub.TaskHubDTO; import org.apache.ibatis.annotations.Param; @@ -11,4 +12,6 @@ import java.util.List; */ public interface ExtExecTaskMapper { List selectList(@Param("request") BasePageRequest request, @Param("orgId") String orgId, @Param("projectId") String projectId); + + void updateTaskStatus(@Param("execTask") ExecTask execTask); } 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 47cb68e3b8..8c1384e6fa 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 @@ -20,4 +20,17 @@ + + UPDATE exec_task + SET `status` = #{execTask.status}, + create_user = #{execTask.createUser} + WHERE + id = #{execTask.id} + + and project_id = #{execTask.projectId} + + + and organization_id = #{execTask.organizationId} + + \ No newline at end of file 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 new file mode 100644 index 0000000000..cab2dacaea --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/BaseTaskHubLogService.java @@ -0,0 +1,92 @@ +package io.metersphere.system.service; + +import io.metersphere.sdk.constants.OperationLogConstants; +import io.metersphere.sdk.util.JSON; +import io.metersphere.system.domain.ExecTask; +import io.metersphere.system.domain.UserRole; +import io.metersphere.system.dto.sdk.request.UserRoleUpdateRequest; +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.mapper.ExecTaskMapper; +import jakarta.annotation.Resource; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * @author wx + */ +@Service +@Transactional(rollbackFor = Exception.class) +public class BaseTaskHubLogService { + @Resource + private ExecTaskMapper execTaskMapper; + + + /** + * 系统停止任务日志 + * @param id + * @return + */ + public LogDTO systemStopLog(String id) { + ExecTask execTask = execTaskMapper.selectByPrimaryKey(id); + LogDTO dto = null; + if (execTask != null) { + dto = new LogDTO( + OperationLogConstants.SYSTEM, + OperationLogConstants.SYSTEM, + execTask.getId(), + null, + OperationLogType.STOP.name(), + OperationLogModule.SETTING_SYSTEM_TASK_CENTER, + execTask.getTaskName()); + } + return dto; + } + + + /** + * 组织停止任务日志 + * @param id + * @return + */ + public LogDTO orgStopLog(String id) { + ExecTask execTask = execTaskMapper.selectByPrimaryKey(id); + LogDTO dto = null; + if (execTask != null) { + dto = new LogDTO( + OperationLogConstants.ORGANIZATION, + null, + execTask.getId(), + null, + OperationLogType.STOP.name(), + OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER, + execTask.getTaskName()); + } + return dto; + } + + + + /** + * 项目停止任务日志 + * @param id + * @return + */ + public LogDTO projectStopLog(String id) { + ExecTask execTask = execTaskMapper.selectByPrimaryKey(id); + LogDTO dto = null; + if (execTask != null) { + dto = new LogDTO( + null, + null, + execTask.getId(), + null, + OperationLogType.STOP.name(), + OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER, + execTask.getTaskName()); + } + return dto; + } + +} 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 299cc3d964..3791abc132 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 @@ -68,6 +68,10 @@ public class BaseTaskHubService { private ProjectTestResourcePoolMapper projectTestResourcePoolMapper; @Resource private NodeResourcePoolService nodeResourcePoolService; + @Resource + private ExecTaskMapper execTaskMapper; + @Resource + private ExecTaskItemMapper execTaskItemMapper; /** * 系统-获取执行任务列表 @@ -317,6 +321,12 @@ public class BaseTaskHubService { return null; } + /** + * 获取任务详情列表资源节点状态 + * + * @param ids + * @return + */ public List getResourcePoolStatus(List ids) { List statusDTOS = new ArrayList<>(); List itemList = extExecTaskItemMapper.selectPoolNodeByIds(ids); @@ -342,4 +352,34 @@ public class BaseTaskHubService { }); return statusDTOS; } + + + /** + * 停止任务 + * + * @param id + * @param userId + * @param orgId + * @param projectId + */ + public void stopTask(String id, String userId, String orgId, String projectId) { + //1.更新任务状态 + ExecTask execTask = new ExecTask(); + execTask.setId(id); + execTask.setStatus(ExecStatus.STOPPED.name()); + execTask.setCreateUser(userId); + execTask.setProjectId(projectId); + execTask.setOrganizationId(orgId); + extExecTaskMapper.updateTaskStatus(execTask); + //2.更新任务明细状态 + ExecTaskItemExample itemExample = new ExecTaskItemExample(); + itemExample.createCriteria().andTaskIdEqualTo(id); + ExecTaskItem execTaskItem = new ExecTaskItem(); + execTaskItem.setStatus(ExecStatus.STOPPED.name()); + execTaskItem.setExecutor(userId); + execTaskItemMapper.updateByExampleSelective(execTaskItem, itemExample); + //TODO 3.调用jmeter触发停止 + + + } } 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 549e2bd6f9..64f2c4b946 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 @@ -37,6 +37,7 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String SYSTEM_STATISTICS = "/system/task-center/exec-task/statistics"; public static final String SYSTEM_RESOURCE_POOL_OPTIONS = "/system/task-center/resource-pool/options"; public static final String SYSTEM_RESOURCE_POOL_STATUS = "/system/task-center/resource-pool/status"; + public static final String SYSTEM_TASK_STOP = "/system/task-center/exec-task/stop/"; @Test @Order(1) @@ -138,6 +139,21 @@ public class BaseTaskHubControllerTests extends BaseTest { } + /** + * 系统执行任务停止 + */ + @Test + @Order(4) + public void systemTaskStop() throws Exception { + MvcResult mvcResult = this.requestGetWithOkAndReturn(SYSTEM_TASK_STOP + "1"); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } + + /** * 组织任务中心测试用例 */ @@ -146,6 +162,7 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String ORG_TASK_ITEM_PAGE = "/organization/task-center/exec-task/item/page"; public static final String ORG_STATISTICS = "/organization/task-center/exec-task/statistics"; public static final String ORG_RESOURCE_POOL_OPTIONS = "/organization/task-center/resource-pool/options"; + public static final String ORG_TASK_STOP = "/organization/task-center/exec-task/stop/"; @Test @Order(20) @@ -229,6 +246,20 @@ public class BaseTaskHubControllerTests extends BaseTest { Assertions.assertNotNull(resultHolder); } + /** + * 组织执行任务停止 + */ + @Test + @Order(4) + public void orgTaskStop() throws Exception { + MvcResult mvcResult = this.requestGetWithOkAndReturn(ORG_TASK_STOP + "1"); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + } + @Test @Order(21)