feat(接口测试): 场景列表增加定时任务展示

This commit is contained in:
wxg0103 2024-01-30 15:17:14 +08:00 committed by 刘瑞斌
parent 400c127061
commit 81ed91f4c0
3 changed files with 67 additions and 11 deletions

View File

@ -22,11 +22,13 @@ public class ApiScenarioDTO extends ApiScenario {
private String modulePath;
@Schema(description = "环境名称")
private String environmentName;
@Schema(description = "是否有定时任务")
private Boolean schedule;
@Schema(description = "定时任务状态")
private Boolean enable;
@Schema(description = "下一次执行时间")
@Schema(description = "定时任务ID")
private String scheduleId;
@Schema(description = "定时任务是否开启")
private Boolean scheduleEnable;
@Schema(description = "定时任务规则")
private String scheduleCorn;
@Schema(description = "定时任务下一次执行时间")
private Long scheduleExecuteTime;

View File

@ -37,10 +37,13 @@ import io.metersphere.sdk.file.MinioRepository;
import io.metersphere.sdk.mapper.EnvironmentGroupMapper;
import io.metersphere.sdk.mapper.EnvironmentMapper;
import io.metersphere.sdk.util.*;
import io.metersphere.system.domain.Schedule;
import io.metersphere.system.domain.ScheduleExample;
import io.metersphere.system.dto.LogInsertModule;
import io.metersphere.system.dto.request.ScheduleConfig;
import io.metersphere.system.log.constants.OperationLogModule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.mapper.ScheduleMapper;
import io.metersphere.system.schedule.ScheduleService;
import io.metersphere.system.service.UserLoginService;
import io.metersphere.system.uid.IDGenerator;
@ -57,6 +60,10 @@ 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.quartz.CronExpression;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.TriggerBuilder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@ -123,6 +130,8 @@ public class ApiScenarioService {
private ApiScenarioCsvStepMapper apiScenarioCsvStepMapper;
@Resource
private ScheduleService scheduleService;
@Resource
private ScheduleMapper scheduleMapper;
public static final String PRIORITY = "Priority";
public static final String STATUS = "Status";
@ -134,7 +143,7 @@ public class ApiScenarioService {
//CustomFieldUtils.setBaseQueryRequestCustomMultipleFields(request, userId);
//TODO 场景的自定义字段 等设计 不一定会有
List<ApiScenarioDTO> list = extApiScenarioMapper.list(request);
if (!CollectionUtils.isEmpty(list)) {
if (CollectionUtils.isNotEmpty(list)) {
processApiScenario(list);
}
return list;
@ -159,6 +168,12 @@ public class ApiScenarioService {
List<ApiScenarioModule> modules = apiScenarioModuleMapper.selectByExample(moduleExample);
//生成map key为id value为name
Map<String, String> moduleMap = modules.stream().collect(Collectors.toMap(ApiScenarioModule::getId, ApiScenarioModule::getName));
//查询定时任务
List<String> scenarioIds = scenarioLists.stream().map(ApiScenarioDTO::getId).toList();
ScheduleExample scheduleExample = new ScheduleExample();
scheduleExample.createCriteria().andResourceIdIn(scenarioIds).andResourceTypeEqualTo(ScheduleResourceType.API_SCENARIO.name());
List<Schedule> schedules = scheduleMapper.selectByExample(scheduleExample);
Map<String, Schedule> scheduleMap = schedules.stream().collect(Collectors.toMap(Schedule::getResourceId, t -> t));
scenarioLists.forEach(item -> {
item.setCreateUserName(userMap.get(item.getCreateUser()));
item.setDeleteUserName(userMap.get(item.getDeleteUser()));
@ -169,9 +184,32 @@ public class ApiScenarioService {
} else if (item.getGrouped() && groupMap.containsKey(item.getId())) {
item.setEnvironmentName(groupMap.get(item.getEnvironmentId()));
}
if (MapUtils.isNotEmpty(scheduleMap) && scheduleMap.containsKey(item.getId())) {
Schedule schedule = scheduleMap.get(item.getId());
item.setScheduleId(schedule.getId());
item.setScheduleEnable(schedule.getEnable());
item.setScheduleCorn(schedule.getValue());
item.setScheduleExecuteTime(getNextTriggerTime(schedule.getValue()));
}
});
}
/**
* 获取下次执行时间getFireTimeAfter也可以下下次...
*
* @param cron cron表达式
* @return 下次执行时间
*/
private static long getNextTriggerTime(String cron) {
if (!CronExpression.isValidExpression(cron)) {
return 0;
}
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("Calculate Date").withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
Date time0 = trigger.getStartTime();
Date time1 = trigger.getFireTimeAfter(time0);
return time1 == null ? 0 : time1.getTime();
}
private Set<String> extractUserIds(List<ApiScenarioDTO> list) {
return list.stream()
.flatMap(apiScenario -> Stream.of(apiScenario.getUpdateUser(), apiScenario.getDeleteUser(), apiScenario.getCreateUser()))

View File

@ -13,6 +13,7 @@ import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.response.ApiScenarioBatchOperationResponse;
import io.metersphere.api.dto.response.OperationDataInfo;
import io.metersphere.api.dto.scenario.*;
import io.metersphere.api.job.ApiScenarioScheduleJob;
import io.metersphere.api.mapper.*;
import io.metersphere.api.service.ApiScenarioBatchOperationTestService;
import io.metersphere.api.service.BaseResourcePoolTestService;
@ -23,10 +24,7 @@ import io.metersphere.api.utils.ApiDataUtils;
import io.metersphere.project.dto.filemanagement.request.FileUploadRequest;
import io.metersphere.project.mapper.ExtBaseProjectVersionMapper;
import io.metersphere.project.service.FileMetadataService;
import io.metersphere.sdk.constants.ApplicationNumScope;
import io.metersphere.sdk.constants.DefaultRepositoryDir;
import io.metersphere.sdk.constants.ModuleConstants;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.constants.*;
import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.domain.EnvironmentExample;
import io.metersphere.sdk.domain.EnvironmentGroup;
@ -39,7 +37,9 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.domain.Schedule;
import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.mapper.ScheduleMapper;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.uid.NumGenerator;
import io.metersphere.system.utils.CheckLogModel;
@ -118,6 +118,8 @@ public class ApiScenarioControllerTests extends BaseTest {
private ApiScenarioCsvMapper apiScenarioCsvMapper;
@Resource
private ApiScenarioService apiScenarioService;
@Resource
private ScheduleMapper scheduleMapper;
private static String fileMetadataId;
private static String localFileId;
private static ApiScenario addApiScenario;
@ -161,7 +163,6 @@ public class ApiScenarioControllerTests extends BaseTest {
EnvironmentExample environmentExample = new EnvironmentExample();
environmentExample.createCriteria().andProjectIdEqualTo(DEFAULT_PROJECT_ID).andMockEqualTo(true);
List<Environment> environments = environmentMapper.selectByExample(environmentExample);
for (int i = 0; i < 10; i++) {
ApiScenario apiScenario = new ApiScenario();
apiScenario.setId("api-scenario-id" + i);
@ -183,6 +184,21 @@ public class ApiScenarioControllerTests extends BaseTest {
apiScenario.setTags(new ArrayList<>(List.of("tag1", "tag2")));
apiScenario.setGrouped(true);
apiScenario.setEnvironmentId("scenario-environment-group-id");
Schedule schedule = new Schedule();
schedule.setId(IDGenerator.nextStr());
schedule.setKey(apiScenario.getId());
schedule.setProjectId(DEFAULT_PROJECT_ID);
schedule.setResourceId(apiScenario.getId());
schedule.setJob(ApiScenarioScheduleJob.class.getName());
schedule.setResourceType(ScheduleResourceType.API_SCENARIO.name());
schedule.setValue("0 0 0/1 * * ? ");
schedule.setEnable(true);
schedule.setName("定时任务");
schedule.setCreateUser("admin");
schedule.setUpdateTime(System.currentTimeMillis());
schedule.setCreateTime(System.currentTimeMillis());
schedule.setType(ScheduleType.CRON.name());
scheduleMapper.insertSelective(schedule);
} else {
apiScenario.setGrouped(false);
apiScenario.setEnvironmentId(environments.get(0).getId());