refactor(任务中心): 任务中心删除&批量删除任务逻辑优化

This commit is contained in:
WangXu10 2024-10-18 11:03:55 +08:00 committed by Craftsman
parent d6c94f3986
commit 83fb392680
11 changed files with 303 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.project.controller;
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.ResourcePoolOptionsDTO;
import io.metersphere.system.dto.taskhub.TaskHubDTO;
import io.metersphere.system.dto.taskhub.TaskHubItemDTO;
@ -82,12 +83,28 @@ 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());
}
@GetMapping("/exec-task/delete/{id}")
@Operation(summary = "项目-任务中心-用例执行任务-删除任务")
@Log(type = OperationLogType.DELETE, expression = "#msClass.projectDeleteLog(#id)", msClass = BaseTaskHubLogService.class)
@RequiresPermissions(PermissionConstants.PROJECT_CASE_TASK_CENTER_DELETE)
public void deleteTask(@PathVariable String id) {
baseTaskHubService.deleteTask(id, null, SessionUtils.getCurrentProjectId());
}
@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());
}
}

View File

@ -4,6 +4,7 @@ import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.metersphere.system.dto.taskhub.request.TaskHubItemRequest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Order;
@ -26,6 +27,8 @@ public class ProjectTaskHubControllerTests extends BaseTest {
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/";
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_BATCH_DELETE = "/organization/task-center/exec-task/batch-delete/";
@Test
@Order(1)
@ -124,6 +127,20 @@ public class ProjectTaskHubControllerTests extends BaseTest {
Assertions.assertNotNull(resultHolder);
}
/**
* 系统执行任务停止
*/
@Test
@Order(23)
public void projectTaskBatchStop() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(PROJECT_TASK_BATCH_STOP, request);
request.setSelectAll(false);
request.setSelectIds(List.of("pro_1", "pro_2"));
this.requestPost(PROJECT_TASK_BATCH_STOP, request);
}
/**
* 项目执行任务删除
@ -138,4 +155,13 @@ public class ProjectTaskHubControllerTests extends BaseTest {
// 返回请求正常
Assertions.assertNotNull(resultHolder);
}
@Test
@Order(6)
public void projectBatchTaskDelete() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(false);
request.setSelectIds(List.of("1"));
this.requestPost(PROJECT_TASK_BATCH_DELETE, request);
}
}

View File

@ -3,6 +3,7 @@ package io.metersphere.system.controller;
import io.metersphere.sdk.constants.PermissionConstants;
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.ResourcePoolOptionsDTO;
import io.metersphere.system.dto.taskhub.TaskHubDTO;
import io.metersphere.system.dto.taskhub.TaskHubItemDTO;
@ -89,6 +90,16 @@ 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);
}
@GetMapping("/exec-task/delete/{id}")
@Operation(summary = "组织-任务中心-用例执行任务-删除任务")
@Log(type = OperationLogType.DELETE, expression = "#msClass.orgDeleteLog(#id)", msClass = BaseTaskHubLogService.class)
@ -96,4 +107,13 @@ public class OrganizationTaskHubController {
public void deleteTask(@PathVariable String id) {
baseTaskHubService.deleteTask(id, SessionUtils.getCurrentOrganizationId(), null);
}
@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);
}
}

View File

@ -106,7 +106,16 @@ public class SystemTaskHubController {
baseTaskHubService.deleteTask(id, null, null);
}
//TODO 系统&组织&项目 任务按钮操作删除 停止 失败重跑 查看报告 批量删除 批量停止 批量失败重跑
@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);
}
//TODO 系统&组织&项目 任务按钮操作失败重跑 查看报告 批量失败重跑
//TODO 系统&组织&项目 任务详情按钮操作查看 停止 批量停止

View File

@ -36,4 +36,6 @@ public interface ExtExecTaskItemMapper {
Boolean hasErrorItem(@Param("taskId") String taskId);
Boolean hasFakeErrorItem(@Param("taskId") String taskId);
List<String> getItemIdByTaskIds(@Param("taskIds") List<String> taskIds);
}

View File

@ -153,4 +153,11 @@
WHERE task_id = #{taskId} AND `result` = 'FAKE_ERROR'
limit 1
</select>
<select id="getItemIdByTaskIds" resultType="java.lang.String">
select id from exec_task_item where task_id in
<foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
#{taskId}
</foreach>
</select>
</mapper>

View File

@ -13,7 +13,7 @@ import java.util.List;
public interface ExtExecTaskMapper {
List<TaskHubDTO> selectList(@Param("request") BasePageRequest request, @Param("orgId") String orgId, @Param("projectId") String projectId);
void deleteTaskById(@Param("id") String id, @Param("orgId") String orgId, @Param("projectId") String projectId);
void deleteTaskByIds(@Param("ids") List<String> ids, @Param("orgId") String orgId, @Param("projectId") String projectId);
List<String> getIds(@Param("request") TableBatchProcessDTO request);

View File

@ -27,10 +27,13 @@
</sql>
<delete id="deleteTaskById">
<delete id="deleteTaskByIds">
DELETE
FROM exec_task
WHERE id = #{id}
WHERE id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
<if test="projectId != null and projectId != ''">
and project_id = #{projectId}
</if>

View File

@ -1,11 +1,8 @@
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.ExecTaskExample;
import io.metersphere.system.domain.UserRole;
import io.metersphere.system.dto.sdk.request.UserRoleUpdateRequest;
import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
@ -32,6 +29,7 @@ public class BaseTaskHubLogService {
/**
* 系统停止任务日志
*
* @param id
* @return
*/
@ -82,6 +80,7 @@ public class BaseTaskHubLogService {
/**
* 组织停止任务日志
*
* @param id
* @return
*/
@ -102,9 +101,38 @@ public class BaseTaskHubLogService {
}
/**
* 组织批量停止任务日志
*
* @param request
* @return
*/
public List<LogDTO> orgBatchStopLog(TableBatchProcessDTO request) {
List<String> ids = baseTaskHubService.getTaskIds(request);
ExecTaskExample example = new ExecTaskExample();
example.createCriteria().andIdIn(ids);
List<ExecTask> execTasks = execTaskMapper.selectByExample(example);
List<LogDTO> logDTOList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(execTasks)) {
execTasks.forEach(item -> {
LogDTO dto = new LogDTO(
OperationLogConstants.ORGANIZATION,
null,
item.getId(),
null,
OperationLogType.STOP.name(),
OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER,
item.getTaskName());
logDTOList.add(dto);
});
}
return logDTOList;
}
/**
* 项目停止任务日志
*
* @param id
* @return
*/
@ -125,8 +153,38 @@ public class BaseTaskHubLogService {
}
/**
* 项目批量停止任务日志
*
* @param request
* @return
*/
public List<LogDTO> projectBatchStopLog(TableBatchProcessDTO request) {
List<String> ids = baseTaskHubService.getTaskIds(request);
ExecTaskExample example = new ExecTaskExample();
example.createCriteria().andIdIn(ids);
List<ExecTask> execTasks = execTaskMapper.selectByExample(example);
List<LogDTO> logDTOList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(execTasks)) {
execTasks.forEach(item -> {
LogDTO dto = new LogDTO(
null,
null,
item.getId(),
null,
OperationLogType.STOP.name(),
OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER,
item.getTaskName());
logDTOList.add(dto);
});
}
return logDTOList;
}
/**
* 系统删除任务日志
*
* @param id
* @return
*/
@ -146,8 +204,37 @@ public class BaseTaskHubLogService {
return dto;
}
/**
* 系统批量删除任务日志
*
* @param request
* @return
*/
public List<LogDTO> systemBatchDeleteLog(TableBatchProcessDTO request) {
List<String> ids = baseTaskHubService.getTaskIds(request);
ExecTaskExample example = new ExecTaskExample();
example.createCriteria().andIdIn(ids);
List<ExecTask> execTasks = execTaskMapper.selectByExample(example);
List<LogDTO> logDTOList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(execTasks)) {
execTasks.forEach(item -> {
LogDTO dto = new LogDTO(
OperationLogConstants.SYSTEM,
OperationLogConstants.SYSTEM,
item.getId(),
null,
OperationLogType.DELETE.name(),
OperationLogModule.SETTING_SYSTEM_TASK_CENTER,
item.getTaskName());
logDTOList.add(dto);
});
}
return logDTOList;
}
/**
* 组织删除任务日志
*
* @param id
* @return
*/
@ -167,9 +254,37 @@ public class BaseTaskHubLogService {
return dto;
}
/**
* 组织批量删除任务日志
*
* @param request
* @return
*/
public List<LogDTO> orgBatchDeleteLog(TableBatchProcessDTO request) {
List<String> ids = baseTaskHubService.getTaskIds(request);
ExecTaskExample example = new ExecTaskExample();
example.createCriteria().andIdIn(ids);
List<ExecTask> execTasks = execTaskMapper.selectByExample(example);
List<LogDTO> logDTOList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(execTasks)) {
execTasks.forEach(item -> {
LogDTO dto = new LogDTO(
OperationLogConstants.ORGANIZATION,
null,
item.getId(),
null,
OperationLogType.DELETE.name(),
OperationLogModule.SETTING_ORGANIZATION_TASK_CENTER,
item.getTaskName());
logDTOList.add(dto);
});
}
return logDTOList;
}
/**
* 项目删除任务日志
*
* @param id
* @return
*/
@ -189,4 +304,32 @@ public class BaseTaskHubLogService {
return dto;
}
/**
* 项目批量删除任务日志
*
* @param request
* @return
*/
public List<LogDTO> projectBatchDeleteLog(TableBatchProcessDTO request) {
List<String> ids = baseTaskHubService.getTaskIds(request);
ExecTaskExample example = new ExecTaskExample();
example.createCriteria().andIdIn(ids);
List<ExecTask> execTasks = execTaskMapper.selectByExample(example);
List<LogDTO> logDTOList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(execTasks)) {
execTasks.forEach(item -> {
LogDTO dto = new LogDTO(
null,
null,
item.getId(),
null,
OperationLogType.DELETE.name(),
OperationLogModule.PROJECT_MANAGEMENT_TASK_CENTER,
item.getTaskName());
logDTOList.add(dto);
});
}
return logDTOList;
}
}

View File

@ -2,6 +2,8 @@ package io.metersphere.system.service;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import io.metersphere.api.domain.ApiReportRelateTaskExample;
import io.metersphere.api.mapper.ApiReportRelateTaskMapper;
import io.metersphere.engine.EngineFactory;
import io.metersphere.engine.MsHttpClient;
import io.metersphere.project.domain.Project;
@ -91,6 +93,8 @@ public class BaseTaskHubService {
private UserMapper userMapper;
@Resource
private TestResourcePoolService testResourcePoolService;
@Resource
private ApiReportRelateTaskMapper apiReportRelateTaskMapper;
/**
* 系统-获取执行任务列表
@ -530,12 +534,22 @@ public class BaseTaskHubService {
public void deleteTask(String id, String orgId, String projectId) {
//1.删除任务
extExecTaskMapper.deleteTaskById(id, orgId, projectId);
extExecTaskMapper.deleteTaskByIds(List.of(id), orgId, projectId);
//2.删除任务明细
ExecTaskItemExample itemExample = new ExecTaskItemExample();
itemExample.createCriteria().andTaskIdEqualTo(id);
execTaskItemMapper.deleteByExample(itemExample);
//TODO jmeter执行队列中移除
//3.删除任务与报告关联关系
deleteReportRelateTask(List.of(id));
handleStopTask(List.of(id));
}
private void deleteReportRelateTask(List<String> ids) {
List<String> itemIds = extExecTaskItemMapper.getItemIdByTaskIds(ids);
itemIds.addAll(ids);
ApiReportRelateTaskExample reportExample = new ApiReportRelateTaskExample();
reportExample.createCriteria().andTaskResourceIdIn(itemIds);
apiReportRelateTaskMapper.deleteByExample(reportExample);
}
public void batchStopTask(TableBatchProcessDTO request, String userId, String orgId, String projectId) {
@ -561,4 +575,19 @@ public class BaseTaskHubService {
return request.getSelectIds();
}
}
public void batchDeleteTask(TableBatchProcessDTO request, String orgId, String projectId) {
List<String> ids = getTaskIds(request);
if (CollectionUtils.isNotEmpty(ids)) {
//1.删除任务
extExecTaskMapper.deleteTaskByIds(ids, orgId, projectId);
//2.删除任务明细
ExecTaskItemExample itemExample = new ExecTaskItemExample();
itemExample.createCriteria().andTaskIdIn(ids);
execTaskItemMapper.deleteByExample(itemExample);
//3.删除任务与报告关联关系
deleteReportRelateTask(ids);
handleStopTask(ids);
}
}
}

View File

@ -44,6 +44,7 @@ public class BaseTaskHubControllerTests extends BaseTest {
public static final String SYSTEM_TASK_STOP = "/system/task-center/exec-task/stop/";
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/";
@Test
@Order(1)
@ -175,6 +176,15 @@ public class BaseTaskHubControllerTests extends BaseTest {
Assertions.assertNotNull(resultHolder);
}
@Test
@Order(7)
public void systemBatchTaskDelete() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(false);
request.setSelectIds(List.of("1"));
this.requestPost(SYSTEM_TASK_BATCH_DELETE, request);
}
/**
* 系统执行任务停止
@ -201,6 +211,8 @@ public class BaseTaskHubControllerTests extends BaseTest {
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/";
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/";
@Test
@Order(20)
@ -300,6 +312,32 @@ public class BaseTaskHubControllerTests extends BaseTest {
testResourcePoolMapper.deleteByPrimaryKey("2");
}
/**
* 系统执行任务停止
*/
@Test
@Order(23)
public void orgTaskBatchStop() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(true);
this.requestPost(ORG_TASK_BATCH_STOP, request);
request.setSelectAll(false);
request.setSelectIds(List.of("1", "2"));
this.requestPost(ORG_TASK_BATCH_STOP, request);
}
@Test
@Order(23)
public void orgBatchTaskDelete() throws Exception {
TableBatchProcessDTO request = new TableBatchProcessDTO();
request.setSelectAll(false);
request.setSelectIds(List.of("1"));
this.requestPost(ORG_TASK_BATCH_DELETE, request);
}
/**
* 组织执行任务删除
*/