feat(任务中心): 项目后台任务&提供批量入库接口

This commit is contained in:
WangXu10 2024-10-09 11:52:56 +08:00 committed by Craftsman
parent 472d27f9ac
commit 4509f117e3
4 changed files with 130 additions and 4 deletions

View File

@ -1,13 +1,17 @@
package io.metersphere.project.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.taskhub.TaskHubDTO;
import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO;
import io.metersphere.system.service.BaseTaskHubService;
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.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@ -29,4 +33,11 @@ public class ProjectTaskHubController {
public Pager<List<TaskHubDTO>> projectList(@Validated @RequestBody BasePageRequest request) {
return baseTaskHubService.getTaskList(request, null, SessionUtils.getCurrentProjectId());
}
@PostMapping("/schedule/page")
@Operation(summary = "项目-任务中心-后台执行任务列表")
@RequiresPermissions(PermissionConstants.SYSTEM_SCHEDULE_TASK_CENTER_READ)
public Pager<List<TaskHubScheduleDTO>> scheduleList(@Validated @RequestBody BasePageRequest request) {
return baseTaskHubService.getScheduleTaskList(request, List.of(SessionUtils.getCurrentProjectId()));
}
}

View File

@ -18,6 +18,7 @@ public class ProjectTaskHubControllerTests extends BaseTest {
* 项目任务中心测试用例
*/
public static final String PROJECT_TASK_PAGE = "/project/task-center/exec-task/page";
public static final String PROJECT_SCHEDULE_TASK_PAGE = "/project/task-center/schedule/page";
@Test
@Order(1)
@ -34,4 +35,23 @@ public class ProjectTaskHubControllerTests extends BaseTest {
// 返回请求正常
Assertions.assertNotNull(resultHolder);
}
/**
* 项目后台任务
*/
@Test
@Order(2)
public void getProjectSchedulePage() throws Exception {
BasePageRequest request = new BasePageRequest();
this.requestPost(PROJECT_SCHEDULE_TASK_PAGE, request);
request.setCurrent(1);
request.setPageSize(10);
MvcResult mvcResult = this.requestPostWithOkAndReturn(PROJECT_SCHEDULE_TASK_PAGE, request);
// 获取返回值
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
// 返回请求正常
Assertions.assertNotNull(resultHolder);
}
}

View File

@ -2,18 +2,23 @@ package io.metersphere.system.service;
import com.github.pagehelper.Page;
import com.github.pagehelper.page.PageMethod;
import io.metersphere.sdk.util.SubListUtils;
import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.dto.sdk.OptionDTO;
import io.metersphere.system.dto.taskhub.TaskHubDTO;
import io.metersphere.system.dto.taskhub.TaskHubScheduleDTO;
import io.metersphere.system.mapper.ExtExecTaskMapper;
import io.metersphere.system.mapper.ExtOrganizationMapper;
import io.metersphere.system.mapper.ExtScheduleMapper;
import io.metersphere.system.mapper.*;
import io.metersphere.system.utils.PageUtils;
import io.metersphere.system.utils.Pager;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -39,6 +44,8 @@ public class BaseTaskHubService {
ExtOrganizationMapper extOrganizationMapper;
@Resource
UserLoginService userLoginService;
@Resource
private SqlSessionFactory sqlSessionFactory;
/**
* 系统-获取执行任务列表
@ -103,4 +110,47 @@ public class BaseTaskHubService {
return extOrganizationMapper.getOrgListByProjectIds(projectIds);
}
/**
* 单任务详情数据入库接口
*
* @param items
*/
public void insertExecTaskAndDetail(List<ExecTaskItem> items) {
if (CollectionUtils.isNotEmpty(items)) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ExecTaskItemMapper itemMapper = sqlSession.getMapper(ExecTaskItemMapper.class);
SubListUtils.dealForSubList(items, 1000, subList -> {
subList.forEach(itemMapper::insertSelective);
});
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}
/**
* 批量任务&任务详情入库接口
*
* @param tasks
* @param items
*/
public void insertExecTaskAndDetail(List<ExecTask> tasks, List<ExecTaskItem> items) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
if (CollectionUtils.isNotEmpty(tasks)) {
ExecTaskMapper execTaskMapper = sqlSession.getMapper(ExecTaskMapper.class);
SubListUtils.dealForSubList(tasks, 1000, subList -> {
subList.forEach(execTaskMapper::insertSelective);
});
}
if (CollectionUtils.isNotEmpty(items)) {
ExecTaskItemMapper itemMapper = sqlSession.getMapper(ExecTaskItemMapper.class);
SubListUtils.dealForSubList(items, 1000, subList -> {
subList.forEach(itemMapper::insertSelective);
});
}
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}
}

View File

@ -3,7 +3,11 @@ package io.metersphere.system.controller;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.domain.ExecTask;
import io.metersphere.system.domain.ExecTaskItem;
import io.metersphere.system.dto.sdk.BasePageRequest;
import io.metersphere.system.service.BaseTaskHubService;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@ -12,12 +16,17 @@ import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.web.servlet.MvcResult;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BaseTaskHubControllerTests extends BaseTest {
@Resource
private BaseTaskHubService baseTaskHubService;
/**
* 系统任务中心测试用例
*/
@ -86,7 +95,7 @@ public class BaseTaskHubControllerTests extends BaseTest {
* 组织后台任务
*/
@Test
@Order(2)
@Order(21)
public void getOrgSchedulePage() throws Exception {
BasePageRequest request = new BasePageRequest();
this.requestPost(ORG_SCHEDULE_TASK_PAGE, request);
@ -99,4 +108,40 @@ public class BaseTaskHubControllerTests extends BaseTest {
// 返回请求正常
Assertions.assertNotNull(resultHolder);
}
@Test
@Order(21)
public void testInsert() throws Exception {
baseTaskHubService.insertExecTaskAndDetail(new ArrayList<>());
ExecTaskItem execTaskItem = new ExecTaskItem();
execTaskItem.setId("1111");
execTaskItem.setTaskId("1");
execTaskItem.setResourceId("1");
execTaskItem.setStatus("SUCCESS");
execTaskItem.setResourcePoolId("1");
execTaskItem.setResourceType("FUNCTIONAL");
execTaskItem.setProjectId("1234");
execTaskItem.setOrganizationId("1234123");
execTaskItem.setExecutor("admin");
baseTaskHubService.insertExecTaskAndDetail(List.of(execTaskItem));
baseTaskHubService.insertExecTaskAndDetail(new ArrayList<>(), new ArrayList<>());
execTaskItem.setId("2333");
ExecTask execTask = new ExecTask();
execTask.setId("121321");
execTask.setNum(123L);
execTask.setTaskName("名称");
execTask.setStatus("SUCCESS");
execTask.setCaseCount(123L);
execTask.setTaskType("API_CASE");
execTask.setTriggerMode("API");
execTask.setProjectId("1234");
execTask.setOrganizationId("123432");
execTask.setCreateTime(System.currentTimeMillis());
execTask.setCreateUser("admin");
baseTaskHubService.insertExecTaskAndDetail(List.of(execTask), List.of(execTaskItem));
}
}