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 04a1472d79..8aa72f7761 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 @@ -25,6 +25,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; @Tag(name = "项目任务中心") @RestController @@ -94,6 +95,12 @@ public class ProjectTaskHubController { baseTaskHubLogService.projectBatchStopLog(ids); } + @PostMapping("/exec-task/item/order") + @Operation(summary = "系统-任务中心-用例执行任务-获取任务项的排队信息") + @RequiresPermissions(PermissionConstants.PROJECT_CASE_TASK_CENTER_READ) + public Map getTaskItemOrder(@RequestBody List taskIdItemIds) { + return baseTaskHubService.getTaskItemOrder(taskIdItemIds); + } @GetMapping("/exec-task/delete/{id}") @Operation(summary = "项目-任务中心-用例执行任务-删除任务") 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 622a3520ab..d86a79fe0c 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 @@ -14,6 +14,7 @@ import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MvcResult; import java.nio.charset.StandardCharsets; +import java.util.HashMap; import java.util.List; public class ProjectTaskHubControllerTests extends BaseTest { @@ -28,6 +29,7 @@ public class ProjectTaskHubControllerTests extends BaseTest { public static final String PROJECT_TASK_STOP = "/project/task-center/exec-task/stop/"; public static final String PROJECT_TASK_DELETE = "/project/task-center/exec-task/delete/"; public static final String PROJECT_TASK_BATCH_STOP = "/project/task-center/exec-task/batch-stop/"; + public static final String PROJECT_TASK_ITEM_ORDER = "/project/task-center/exec-task/item/order"; public static final String PROJECT_TASK_BATCH_DELETE = "/organization/task-center/exec-task/batch-delete/"; @Test @@ -141,6 +143,17 @@ public class ProjectTaskHubControllerTests extends BaseTest { this.requestPost(PROJECT_TASK_BATCH_STOP, request); } + /** + * 获取任务项的排队信息 + */ + @Test + @Order(23) + public void projectGetTaskItemOrder() throws Exception { + MvcResult mvcResult = this.requestPostWithOkAndReturn(PROJECT_TASK_ITEM_ORDER, List.of("1")); + HashMap resultData = getResultData(mvcResult, HashMap.class); + // 返回请求正常 + Assertions.assertNotNull(resultData); + } /** * 项目执行任务删除 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 ce982f7f98..86093dfcf9 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 @@ -27,6 +27,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; @Tag(name = "组织任务中心") @RestController @@ -102,7 +103,12 @@ public class OrganizationTaskHubController { baseTaskHubLogService.orgBatchStopLog(ids); } - + @PostMapping("/exec-task/item/order") + @Operation(summary = "系统-任务中心-用例执行任务-获取任务项的排队信息") + @RequiresPermissions(PermissionConstants.ORGANIZATION_CASE_TASK_CENTER_READ) + public Map getTaskItemOrder(@RequestBody List taskIdItemIds) { + return baseTaskHubService.getTaskItemOrder(taskIdItemIds); + } @GetMapping("/exec-task/delete/{id}") @Operation(summary = "组织-任务中心-用例执行任务-删除任务") 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 5fb136b81d..5600b49248 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 @@ -24,6 +24,7 @@ import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; @Tag(name = "系统任务中心") @RestController @@ -102,6 +103,12 @@ public class SystemTaskHubController { baseTaskHubLogService.systemBatchStopLog(ids); } + @PostMapping("/exec-task/item/order") + @Operation(summary = "系统-任务中心-用例执行任务-获取任务项的排队信息") + @RequiresPermissions(PermissionConstants.SYSTEM_CASE_TASK_CENTER_READ) + public Map getTaskItemOrder(@RequestBody List taskIdItemIds) { + return baseTaskHubService.getTaskItemOrder(taskIdItemIds); + } @GetMapping("/exec-task/delete/{id}") @Operation(summary = "系统-任务中心-用例执行任务-删除任务") 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 4fc5282014..a449e36fe3 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 @@ -19,6 +19,7 @@ import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.SubListUtils; +import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.domain.*; import io.metersphere.system.dto.pool.TestResourceDTO; import io.metersphere.system.dto.pool.TestResourceNodeDTO; @@ -33,6 +34,7 @@ import io.metersphere.system.dto.taskhub.response.TaskStatisticsResponse; import io.metersphere.system.mapper.*; import io.metersphere.system.utils.PageUtils; import io.metersphere.system.utils.Pager; +import io.metersphere.system.utils.TaskRunnerClient; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -44,10 +46,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -97,6 +96,8 @@ public class BaseTaskHubService { @Resource private ApiReportRelateTaskMapper apiReportRelateTaskMapper; + private final static String GET_TASK_ITEM_ORDER_URL = "http://%s/api/task/item/order"; + /** * 系统-获取执行任务列表 * @@ -590,7 +591,6 @@ public class BaseTaskHubService { } } - /** * 停止任务项 * @@ -659,4 +659,46 @@ public class BaseTaskHubService { return request.getSelectIds(); } } + + public Map getTaskItemOrder(List taskIdItemIds) { + List taskItemIds = getTaskItemByIds(taskIdItemIds); + Map> nodeResourceMap = taskItemIds.stream() + .collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode)); + + Map taskItemOrderMap = new HashMap<>(); + + List threads = new ArrayList<>(); + nodeResourceMap.forEach((node, items) -> { + if (StringUtils.isNotBlank(node)) { + Thread thread = Thread.startVirtualThread(() -> taskItemOrderMap.putAll(getTaskItemOrder(node, taskIdItemIds))); + threads.add(thread); + } + }); + + for (Thread thread : threads) { + try { + thread.join(); + } catch (InterruptedException e) { + LogUtils.error(e); + } + } + + return taskItemOrderMap; + } + + public Map getTaskItemOrder(String node, List taskIdItemIds) { + try { + ResultHolder body = TaskRunnerClient.post(String.format(GET_TASK_ITEM_ORDER_URL, node), taskIdItemIds); + return JSON.parseMap(JSON.toJSONString(body.getData())); + } catch (Exception e) { + LogUtils.error(e.getMessage(), e); + } + return Map.of(); + } + + private List getTaskItemByIds(List taskIdItemIds) { + ExecTaskItemExample itemExample = new ExecTaskItemExample(); + itemExample.createCriteria().andIdIn(taskIdItemIds); + return execTaskItemMapper.selectByExample(itemExample); + } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TaskRunnerClient.java b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TaskRunnerClient.java index 3071481a05..92c9ef36a0 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TaskRunnerClient.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/utils/TaskRunnerClient.java @@ -57,6 +57,22 @@ public class TaskRunnerClient { return retry(url, null, action); } + public static ResultHolder post(String url, Object param, Object... uriVariables) throws Exception { + // 定义action + Action action = (u, body) -> { + String token = totpGenerator.now(); + HttpHeaders headers = new HttpHeaders(); + headers.add(MsHttpHeaders.OTP_TOKEN, token); + headers.add(HttpHeaders.CONTENT_TYPE, "application/json"); + headers.add(HttpHeaders.ACCEPT, "application/json"); + HttpEntity httpEntity = new HttpEntity<>(param, headers); + ResponseEntity entity = restTemplateWithTimeOut.exchange(u, HttpMethod.POST, httpEntity, ResultHolder.class, uriVariables); + return entity.getBody(); + }; + + return retry(url, null, action); + } + private static ResultHolder retry(String url, Object requestBody, Action action) throws Exception { ResultHolder body; try { 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 12d001844a..cdc153d027 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 @@ -20,6 +20,7 @@ import org.springframework.test.web.servlet.MvcResult; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @@ -47,6 +48,7 @@ public class BaseTaskHubControllerTests extends BaseTest { 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"; + public static final String SYSTEM_TASK_ITEM_ORDER = "/system/task-center/exec-task/item/order"; @Test @Order(1) @@ -178,6 +180,14 @@ public class BaseTaskHubControllerTests extends BaseTest { Assertions.assertNotNull(resultHolder); } + @Test + @Order(4) + public void systemGetTaskItemOrder() throws Exception { + MvcResult mvcResult = this.requestPostWithOkAndReturn(SYSTEM_TASK_ITEM_ORDER, List.of("1")); + HashMap resultData = getResultData(mvcResult, HashMap.class); + // 返回请求正常 + Assertions.assertNotNull(resultData); + } /** * 系统执行任务删除 @@ -244,6 +254,7 @@ public class BaseTaskHubControllerTests extends BaseTest { public static final String ORG_TASK_DELETE = "/organization/task-center/exec-task/delete/"; public static final String ORG_TASK_BATCH_STOP = "/organization/task-center/exec-task/batch-stop/"; public static final String ORG_TASK_BATCH_DELETE = "/organization/task-center/exec-task/batch-delete/"; + public static final String ORG_TASK_ITEM_ORDER = "/organization/task-center/exec-task/item/order"; @Test @Order(20) @@ -358,6 +369,17 @@ public class BaseTaskHubControllerTests extends BaseTest { this.requestPost(ORG_TASK_BATCH_STOP, request); } + /** + * 获取任务项的排队信息 + */ + @Test + @Order(23) + public void orgGetTaskItemOrder() throws Exception { + MvcResult mvcResult = this.requestPostWithOkAndReturn(ORG_TASK_ITEM_ORDER, List.of("1")); + HashMap resultData = getResultData(mvcResult, HashMap.class); + // 返回请求正常 + Assertions.assertNotNull(resultData); + } @Test @Order(23)