feat(接口测试): 任务排队信息查询

--task=1016575 --user=陈建星 任务项排队信息查询 https://www.tapd.cn/55049933/s/1594107
This commit is contained in:
AgAngle 2024-10-18 16:34:57 +08:00 committed by Craftsman
parent 222606dbb5
commit 7aa2decb27
7 changed files with 119 additions and 6 deletions

View File

@ -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<String, Integer> getTaskItemOrder(@RequestBody List<String> taskIdItemIds) {
return baseTaskHubService.getTaskItemOrder(taskIdItemIds);
}
@GetMapping("/exec-task/delete/{id}")
@Operation(summary = "项目-任务中心-用例执行任务-删除任务")

View File

@ -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);
}
/**
* 项目执行任务删除

View File

@ -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<String, Integer> getTaskItemOrder(@RequestBody List<String> taskIdItemIds) {
return baseTaskHubService.getTaskItemOrder(taskIdItemIds);
}
@GetMapping("/exec-task/delete/{id}")
@Operation(summary = "组织-任务中心-用例执行任务-删除任务")

View File

@ -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<String, Integer> getTaskItemOrder(@RequestBody List<String> taskIdItemIds) {
return baseTaskHubService.getTaskItemOrder(taskIdItemIds);
}
@GetMapping("/exec-task/delete/{id}")
@Operation(summary = "系统-任务中心-用例执行任务-删除任务")

View File

@ -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<String, Integer> getTaskItemOrder(List<String> taskIdItemIds) {
List<ExecTaskItem> taskItemIds = getTaskItemByIds(taskIdItemIds);
Map<String, List<ExecTaskItem>> nodeResourceMap = taskItemIds.stream()
.collect(Collectors.groupingBy(ExecTaskItem::getResourcePoolNode));
Map<String, Integer> taskItemOrderMap = new HashMap<>();
List<Thread> 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<String, Integer> getTaskItemOrder(String node, List<String> 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<ExecTaskItem> getTaskItemByIds(List<String> taskIdItemIds) {
ExecTaskItemExample itemExample = new ExecTaskItemExample();
itemExample.createCriteria().andIdIn(taskIdItemIds);
return execTaskItemMapper.selectByExample(itemExample);
}
}

View File

@ -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<Object> httpEntity = new HttpEntity<>(param, headers);
ResponseEntity<ResultHolder> 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 {

View File

@ -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)