feat: 接口测试模块-首页开发Service方法
接口测试模块-首页开发,包含接口、用例、场景、定时任务、失败案例和运行中任务等功能的开发
This commit is contained in:
parent
e05657fe11
commit
7707f9902d
|
@ -1,153 +1,169 @@
|
|||
package io.metersphere.api.controller;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import io.metersphere.api.dto.*;
|
||||
import io.metersphere.api.dto.scenario.request.dubbo.RegistryCenter;
|
||||
import io.metersphere.api.service.APITestService;
|
||||
import io.metersphere.base.domain.ApiTest;
|
||||
import io.metersphere.base.domain.Schedule;
|
||||
import io.metersphere.commons.constants.RoleConstants;
|
||||
import io.metersphere.commons.utils.PageUtils;
|
||||
import io.metersphere.commons.utils.Pager;
|
||||
import io.metersphere.commons.utils.SessionUtils;
|
||||
import io.metersphere.controller.request.QueryScheduleRequest;
|
||||
import io.metersphere.dto.ScheduleDao;
|
||||
import io.metersphere.service.CheckOwnerService;
|
||||
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresRoles;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static io.metersphere.commons.utils.JsonPathUtils.getListJson;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api")
|
||||
@RequiresRoles(value = {RoleConstants.TEST_MANAGER, RoleConstants.TEST_USER, RoleConstants.TEST_VIEWER}, logical = Logical.OR)
|
||||
public class APITestController {
|
||||
@Resource
|
||||
private APITestService apiTestService;
|
||||
@Resource
|
||||
private CheckOwnerService checkownerService;
|
||||
|
||||
@GetMapping("recent/{count}")
|
||||
public List<APITestResult> recentTest(@PathVariable int count) {
|
||||
String currentWorkspaceId = SessionUtils.getCurrentWorkspaceId();
|
||||
QueryAPITestRequest request = new QueryAPITestRequest();
|
||||
request.setWorkspaceId(currentWorkspaceId);
|
||||
request.setUserId(SessionUtils.getUserId());
|
||||
PageHelper.startPage(1, count, true);
|
||||
return apiTestService.recentTest(request);
|
||||
}
|
||||
|
||||
@PostMapping("/list/{goPage}/{pageSize}")
|
||||
public Pager<List<APITestResult>> list(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryAPITestRequest request) {
|
||||
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
|
||||
request.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
|
||||
request.setProjectId(SessionUtils.getCurrentProjectId());
|
||||
return PageUtils.setPageInfo(page, apiTestService.list(request));
|
||||
}
|
||||
|
||||
@PostMapping("/list/ids")
|
||||
public List<ApiTest> listByIds(@RequestBody QueryAPITestRequest request) {
|
||||
return apiTestService.listByIds(request);
|
||||
}
|
||||
|
||||
@GetMapping("/list/{projectId}")
|
||||
public List<ApiTest> list(@PathVariable String projectId) {
|
||||
checkownerService.checkProjectOwner(projectId);
|
||||
return apiTestService.getApiTestByProjectId(projectId);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/schedule/update")
|
||||
public void updateSchedule(@RequestBody Schedule request) {
|
||||
apiTestService.updateSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/schedule/create")
|
||||
public void createSchedule(@RequestBody Schedule request) {
|
||||
apiTestService.createSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/create", consumes = {"multipart/form-data"})
|
||||
public void create(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
||||
apiTestService.create(request, file, bodyFiles);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/create/merge", consumes = {"multipart/form-data"})
|
||||
public void mergeCreate(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "selectIds") List<String> selectIds) {
|
||||
apiTestService.mergeCreate(request, file, selectIds);
|
||||
}
|
||||
@PostMapping(value = "/update", consumes = {"multipart/form-data"})
|
||||
public void update(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
||||
checkownerService.checkApiTestOwner(request.getId());
|
||||
apiTestService.update(request, file, bodyFiles);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/copy")
|
||||
public void copy(@RequestBody SaveAPITestRequest request) {
|
||||
apiTestService.copy(request);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{testId}")
|
||||
public APITestResult get(@PathVariable String testId) {
|
||||
checkownerService.checkApiTestOwner(testId);
|
||||
return apiTestService.get(testId);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/delete")
|
||||
public void delete(@RequestBody DeleteAPITestRequest request) {
|
||||
checkownerService.checkApiTestOwner(request.getId());
|
||||
apiTestService.delete(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/run")
|
||||
public String run(@RequestBody SaveAPITestRequest request) {
|
||||
return apiTestService.run(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/run/debug", consumes = {"multipart/form-data"})
|
||||
public String runDebug(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
||||
return apiTestService.runDebug(request, file, bodyFiles);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/checkName")
|
||||
public void checkName(@RequestBody SaveAPITestRequest request) {
|
||||
apiTestService.checkName(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/import", consumes = {"multipart/form-data"})
|
||||
@RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR)
|
||||
public ApiTest testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) {
|
||||
return apiTestService.apiTestImport(file, request);
|
||||
}
|
||||
|
||||
@PostMapping("/dubbo/providers")
|
||||
public List<DubboProvider> getProviders(@RequestBody RegistryCenter registry) {
|
||||
return apiTestService.getProviders(registry);
|
||||
}
|
||||
|
||||
@PostMapping("/list/schedule/{goPage}/{pageSize}")
|
||||
public List<ScheduleDao> listSchedule(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryScheduleRequest request) {
|
||||
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
|
||||
return apiTestService.listSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping("/list/schedule")
|
||||
public List<ScheduleDao> listSchedule(@RequestBody QueryScheduleRequest request) {
|
||||
return apiTestService.listSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping("/getJsonPaths")
|
||||
public List<HashMap> getJsonPaths(@RequestBody QueryJsonPathRequest request) {
|
||||
return getListJson(request.getJsonPath());
|
||||
}
|
||||
}
|
||||
package io.metersphere.api.controller;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import io.metersphere.api.dto.*;
|
||||
import io.metersphere.api.dto.scenario.request.dubbo.RegistryCenter;
|
||||
import io.metersphere.api.service.*;
|
||||
import io.metersphere.base.domain.ApiTest;
|
||||
import io.metersphere.base.domain.Schedule;
|
||||
import io.metersphere.commons.constants.RoleConstants;
|
||||
import io.metersphere.commons.constants.ScheduleGroup;
|
||||
import io.metersphere.commons.utils.*;
|
||||
import io.metersphere.controller.request.QueryScheduleRequest;
|
||||
import io.metersphere.dto.ScheduleDao;
|
||||
import io.metersphere.service.CheckOwnerService;
|
||||
|
||||
import io.metersphere.service.ScheduleService;
|
||||
import org.apache.shiro.authz.annotation.Logical;
|
||||
import org.apache.shiro.authz.annotation.RequiresRoles;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static io.metersphere.commons.utils.JsonPathUtils.getListJson;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api")
|
||||
@RequiresRoles(value = {RoleConstants.TEST_MANAGER, RoleConstants.TEST_USER, RoleConstants.TEST_VIEWER}, logical = Logical.OR)
|
||||
public class APITestController {
|
||||
@Resource
|
||||
private APITestService apiTestService;
|
||||
@Resource
|
||||
private ApiDefinitionService apiDefinitionService;
|
||||
@Resource
|
||||
private CheckOwnerService checkownerService;
|
||||
@Resource
|
||||
private ApiTestCaseService apiTestCaseService;
|
||||
@Resource
|
||||
private ApiDefinitionExecResultService apiDefinitionExecResultService;
|
||||
@Resource
|
||||
private ApiAutomationService apiAutomationService;
|
||||
@Resource
|
||||
private ApiScenarioReportService apiScenarioReportService;
|
||||
@Resource
|
||||
private ScheduleService scheduleService;
|
||||
@Resource
|
||||
private APIReportService apiReportService;
|
||||
|
||||
@GetMapping("recent/{count}")
|
||||
public List<APITestResult> recentTest(@PathVariable int count) {
|
||||
String currentWorkspaceId = SessionUtils.getCurrentWorkspaceId();
|
||||
QueryAPITestRequest request = new QueryAPITestRequest();
|
||||
request.setWorkspaceId(currentWorkspaceId);
|
||||
request.setUserId(SessionUtils.getUserId());
|
||||
PageHelper.startPage(1, count, true);
|
||||
return apiTestService.recentTest(request);
|
||||
}
|
||||
|
||||
@PostMapping("/list/{goPage}/{pageSize}")
|
||||
public Pager<List<APITestResult>> list(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryAPITestRequest request) {
|
||||
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
|
||||
request.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
|
||||
request.setProjectId(SessionUtils.getCurrentProjectId());
|
||||
return PageUtils.setPageInfo(page, apiTestService.list(request));
|
||||
}
|
||||
|
||||
@PostMapping("/list/ids")
|
||||
public List<ApiTest> listByIds(@RequestBody QueryAPITestRequest request) {
|
||||
return apiTestService.listByIds(request);
|
||||
}
|
||||
|
||||
@GetMapping("/list/{projectId}")
|
||||
public List<ApiTest> list(@PathVariable String projectId) {
|
||||
checkownerService.checkProjectOwner(projectId);
|
||||
return apiTestService.getApiTestByProjectId(projectId);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/schedule/update")
|
||||
public void updateSchedule(@RequestBody Schedule request) {
|
||||
apiTestService.updateSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/schedule/create")
|
||||
public void createSchedule(@RequestBody Schedule request) {
|
||||
apiTestService.createSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/create", consumes = {"multipart/form-data"})
|
||||
public void create(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
||||
apiTestService.create(request, file, bodyFiles);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/create/merge", consumes = {"multipart/form-data"})
|
||||
public void mergeCreate(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "selectIds") List<String> selectIds) {
|
||||
apiTestService.mergeCreate(request, file, selectIds);
|
||||
}
|
||||
@PostMapping(value = "/update", consumes = {"multipart/form-data"})
|
||||
public void update(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
||||
checkownerService.checkApiTestOwner(request.getId());
|
||||
apiTestService.update(request, file, bodyFiles);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/copy")
|
||||
public void copy(@RequestBody SaveAPITestRequest request) {
|
||||
apiTestService.copy(request);
|
||||
}
|
||||
|
||||
@GetMapping("/get/{testId}")
|
||||
public APITestResult get(@PathVariable String testId) {
|
||||
checkownerService.checkApiTestOwner(testId);
|
||||
return apiTestService.get(testId);
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/delete")
|
||||
public void delete(@RequestBody DeleteAPITestRequest request) {
|
||||
checkownerService.checkApiTestOwner(request.getId());
|
||||
apiTestService.delete(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/run")
|
||||
public String run(@RequestBody SaveAPITestRequest request) {
|
||||
return apiTestService.run(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/run/debug", consumes = {"multipart/form-data"})
|
||||
public String runDebug(@RequestPart("request") SaveAPITestRequest request, @RequestPart(value = "file") MultipartFile file, @RequestPart(value = "files") List<MultipartFile> bodyFiles) {
|
||||
return apiTestService.runDebug(request, file, bodyFiles);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/checkName")
|
||||
public void checkName(@RequestBody SaveAPITestRequest request) {
|
||||
apiTestService.checkName(request);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/import", consumes = {"multipart/form-data"})
|
||||
@RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR)
|
||||
public ApiTest testCaseImport(@RequestPart(value = "file", required = false) MultipartFile file, @RequestPart("request") ApiTestImportRequest request) {
|
||||
return apiTestService.apiTestImport(file, request);
|
||||
}
|
||||
|
||||
@PostMapping("/dubbo/providers")
|
||||
public List<DubboProvider> getProviders(@RequestBody RegistryCenter registry) {
|
||||
return apiTestService.getProviders(registry);
|
||||
}
|
||||
|
||||
@PostMapping("/list/schedule/{goPage}/{pageSize}")
|
||||
public List<ScheduleDao> listSchedule(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryScheduleRequest request) {
|
||||
Page<Object> page = PageHelper.startPage(goPage, pageSize, true);
|
||||
return apiTestService.listSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping("/list/schedule")
|
||||
public List<ScheduleDao> listSchedule(@RequestBody QueryScheduleRequest request) {
|
||||
return apiTestService.listSchedule(request);
|
||||
}
|
||||
|
||||
@PostMapping("/getJsonPaths")
|
||||
public List<HashMap> getJsonPaths(@RequestBody QueryJsonPathRequest request) {
|
||||
return getListJson(request.getJsonPath());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
package io.metersphere.api.dto.dataCount;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 已执行的案例
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class ExecutedCaseInfoResult {
|
||||
//案例名称
|
||||
private String caseName;
|
||||
//所属测试计划
|
||||
private String testPlan;
|
||||
//失败次数
|
||||
private Long failureTimes;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package io.metersphere.api.dto.dataCount.request;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* @author song.tianyang
|
||||
* @Date 2020/12/17 5:04 下午
|
||||
* @Description
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class ScheduleInfoRequest {
|
||||
private String taskID;
|
||||
private boolean enable;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package io.metersphere.api.dto.dataCount.response;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
/**
|
||||
* 已执行的案例
|
||||
*/
|
||||
@Getter
|
||||
@Setter
|
||||
public class ExecutedCaseInfoDTO {
|
||||
//排名
|
||||
private int sortIndex;
|
||||
//案例名称
|
||||
private String caseName;
|
||||
//所属测试计划
|
||||
private String testPlan;
|
||||
//失败次数
|
||||
private Long failureTimes;
|
||||
}
|
|
@ -15,6 +15,7 @@ import io.metersphere.base.mapper.ext.ExtApiTestReportMapper;
|
|||
import io.metersphere.commons.constants.APITestStatus;
|
||||
import io.metersphere.commons.constants.ReportTriggerMode;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.DateUtils;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.ServiceUtils;
|
||||
import io.metersphere.commons.utils.SessionUtils;
|
||||
|
@ -30,10 +31,7 @@ import java.nio.charset.StandardCharsets;
|
|||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
@Service
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
|
@ -212,4 +210,21 @@ public class APIReportService {
|
|||
apiTestReportExample.createCriteria().andIdIn(reportRequest.getIds());
|
||||
apiTestReportMapper.deleteByExample(apiTestReportExample);
|
||||
}
|
||||
|
||||
public long countByWorkspaceIdAndGroupAndCreateInThisWeek(String workspaceID, String group) {
|
||||
Map<String, Date> startAndEndDateInWeek = DateUtils.getWeedFirstTimeAndLastTime(new Date());
|
||||
|
||||
Date firstTime = startAndEndDateInWeek.get("firstTime");
|
||||
Date lastTime = startAndEndDateInWeek.get("lastTime");
|
||||
|
||||
if(firstTime==null || lastTime == null){
|
||||
return 0;
|
||||
}else {
|
||||
return apiTestReportMapper.countByProjectIDAndCreateInThisWeek(workspaceID,group,firstTime.getTime(),lastTime.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
public long countByWorkspaceIdAndGroup(String workspaceID, String group) {
|
||||
return apiTestReportMapper.countByWorkspaceIdAndGroup(workspaceID,group);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import com.alibaba.fastjson.JSON;
|
|||
import io.metersphere.api.jmeter.TestResult;
|
||||
import io.metersphere.base.domain.ApiDefinitionExecResult;
|
||||
import io.metersphere.base.mapper.ApiDefinitionExecResultMapper;
|
||||
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
|
||||
import io.metersphere.commons.utils.DateUtils;
|
||||
import io.metersphere.commons.utils.SessionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
@ -17,9 +19,14 @@ import java.util.UUID;
|
|||
public class ApiDefinitionExecResultService {
|
||||
@Resource
|
||||
private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper;
|
||||
@Resource
|
||||
private ExtApiDefinitionExecResultMapper extApiDefinitionExecResultMapper;
|
||||
|
||||
|
||||
public void saveApiResult(TestResult result) {
|
||||
result.getScenarios().get(0).getRequestResults().forEach(item -> {
|
||||
// 清理原始资源,每个执行 保留一条结果
|
||||
extApiDefinitionExecResultMapper.deleteByResourceId(item.getName());
|
||||
ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult();
|
||||
saveResult.setId(UUID.randomUUID().toString());
|
||||
saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
|
||||
|
|
|
@ -4,6 +4,7 @@ import io.metersphere.base.domain.ApiTestReport;
|
|||
import io.metersphere.base.domain.ApiTestReportExample;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
public interface ApiTestReportMapper {
|
||||
long countByExample(ApiTestReportExample example);
|
||||
|
@ -27,4 +28,21 @@ public interface ApiTestReportMapper {
|
|||
int updateByPrimaryKeySelective(ApiTestReport record);
|
||||
|
||||
int updateByPrimaryKey(ApiTestReport record);
|
||||
|
||||
@Select({
|
||||
"SELECT COUNT(testReportDetail.report_id) AS countNumber FROM api_test_report_detail testReportDetail ",
|
||||
"INNER JOIN `schedule` sch ON sch.resource_id = testReportDetail.test_id ",
|
||||
"INNER JOIN api_test_report testReport ON testReportDetail.report_id = testReport.id ",
|
||||
"WHERE workspace_id = #{workspaceID} AND `group` = #{group} ",
|
||||
})
|
||||
long countByWorkspaceIdAndGroup(@Param("workspaceID") String workspaceID, @Param("group")String group);
|
||||
|
||||
@Select({
|
||||
"SELECT COUNT(testReportDetail.report_id) AS countNumber FROM api_test_report_detail testReportDetail ",
|
||||
"INNER JOIN `schedule` sch ON sch.resource_id = testReportDetail.test_id ",
|
||||
"INNER JOIN api_test_report testReport ON testReportDetail.report_id = testReport.id ",
|
||||
"WHERE workspace_id = #{workspaceID} AND `group` = #{group} ",
|
||||
"AND testReport.create_time BETWEEN #{startTime} and #{endTime} ",
|
||||
})
|
||||
long countByProjectIDAndCreateInThisWeek(@Param("workspaceID") String workspaceID, @Param("group")String group, @Param("startTime") long startTime, @Param("endTime")long endTime);
|
||||
}
|
|
@ -1,9 +1,13 @@
|
|||
package io.metersphere.base.mapper;
|
||||
|
||||
import io.metersphere.api.dto.dataCount.response.TaskInfoResult;
|
||||
import io.metersphere.base.domain.Schedule;
|
||||
import io.metersphere.base.domain.ScheduleExample;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
import org.python.antlr.ast.Str;
|
||||
|
||||
public interface ScheduleMapper {
|
||||
long countByExample(ScheduleExample example);
|
||||
|
@ -33,4 +37,25 @@ public interface ScheduleMapper {
|
|||
int updateByPrimaryKeyWithBLOBs(Schedule record);
|
||||
|
||||
int updateByPrimaryKey(Schedule record);
|
||||
|
||||
@Select("SELECT COUNT(id) AS countNumber FROM `schedule` WHERE `workspace_id` = #{workspaceId} AND `group` = #{group} ")
|
||||
long countTaskByWorkspaceIdAndGroup(@Param("workspaceId") String workspaceId,@Param("group") String group);
|
||||
|
||||
@Select({
|
||||
"SELECT COUNT(id) AS countNumber FROM `schedule` ",
|
||||
"WHERE workspace_id = #{workspaceId} ",
|
||||
"AND `group` = #{group} ",
|
||||
"AND create_time BETWEEN #{startTime} and #{endTime}; "
|
||||
})
|
||||
long countTaskByWorkspaceIdAndGroupAndCreateTimeRange(@Param("workspaceId")String workspaceId,@Param("group") String group, @Param("startTime") long startTime, @Param("endTime") long endTime);
|
||||
|
||||
@Select({
|
||||
"SELECT apiTest.`name` AS scenario,sch.id AS taskID,sch.`value` AS rule,sch.`enable` AS `taskStatus`,u.`name` AS creator,sch.update_time AS updateTime ",
|
||||
"FROM api_test apiTest ",
|
||||
"INNER JOIN `schedule` sch ON apiTest.id = sch.resource_id ",
|
||||
"INNER JOIN `user` u ON u.id = sch.user_id ",
|
||||
"WHERE sch.`enable` = true AND sch.workspace_id = #{0,jdbcType=VARCHAR}"
|
||||
})
|
||||
List<TaskInfoResult> findRunningTaskInfoByWorkspaceID(String workspaceID);
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package io.metersphere.commons.utils;
|
||||
|
||||
import org.quartz.CronExpression;
|
||||
import org.quartz.CronScheduleBuilder;
|
||||
import org.quartz.CronTrigger;
|
||||
import org.quartz.TriggerBuilder;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author song.tianyang
|
||||
* @Date 2020/12/17 4:06 下午
|
||||
* @Description CRON解析类
|
||||
*/
|
||||
public class CronUtils {
|
||||
|
||||
/**
|
||||
* 解析表达式,获取CronTrigger
|
||||
* @param cron
|
||||
* @return
|
||||
*/
|
||||
public static CronTrigger getCronTrigger(String cron) {
|
||||
if (!CronExpression.isValidExpression(cron)) {
|
||||
throw new RuntimeException("cron :" + cron + "表达式解析错误");
|
||||
}
|
||||
return TriggerBuilder.newTrigger().withIdentity("Caclulate Date").withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取以指定时间为开始时间的下一次执行时间
|
||||
* @param cron
|
||||
* @param start
|
||||
* @return
|
||||
*/
|
||||
public static Date getNextTriggerTime(String cron, Date start) {
|
||||
if (start == null) {
|
||||
return getNextTriggerTime(cron);
|
||||
}else{
|
||||
CronTrigger trigger = getCronTrigger(cron);
|
||||
return trigger.getFireTimeAfter(start);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取以当前日期为准的下一次执行时间
|
||||
* @param cron
|
||||
* @return
|
||||
*/
|
||||
public static Date getNextTriggerTime(String cron) {
|
||||
Date date = null;
|
||||
try{
|
||||
CronTrigger trigger = getCronTrigger(cron);
|
||||
Date startDate = trigger.getStartTime();
|
||||
date = trigger.getFireTimeAfter(startDate);
|
||||
}catch (Exception e){
|
||||
|
||||
}
|
||||
return date;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
package io.metersphere.service;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.metersphere.api.dto.dataCount.response.TaskInfoResult;
|
||||
import io.metersphere.base.domain.Schedule;
|
||||
import io.metersphere.base.domain.ScheduleExample;
|
||||
import io.metersphere.base.domain.User;
|
||||
|
@ -9,6 +10,7 @@ import io.metersphere.base.mapper.ScheduleMapper;
|
|||
import io.metersphere.base.mapper.UserMapper;
|
||||
import io.metersphere.base.mapper.ext.ExtScheduleMapper;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.DateUtils;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.ServiceUtils;
|
||||
import io.metersphere.commons.utils.SessionUtils;
|
||||
|
@ -25,6 +27,7 @@ import org.springframework.stereotype.Service;
|
|||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
@ -162,4 +165,26 @@ public class ScheduleService {
|
|||
schedule.setUserName(userMap.get(schedule.getUserId()));
|
||||
});
|
||||
}
|
||||
|
||||
public long countTaskByWorkspaceIdAndGroup(String workspaceId,String group) {
|
||||
return scheduleMapper.countTaskByWorkspaceIdAndGroup(workspaceId,group);
|
||||
}
|
||||
|
||||
public long countTaskByWorkspaceIdAndGroupInThisWeek(String workspaceID, String group) {
|
||||
Map<String, Date> startAndEndDateInWeek = DateUtils.getWeedFirstTimeAndLastTime(new Date());
|
||||
|
||||
Date firstTime = startAndEndDateInWeek.get("firstTime");
|
||||
Date lastTime = startAndEndDateInWeek.get("lastTime");
|
||||
|
||||
if(firstTime==null || lastTime == null){
|
||||
return 0;
|
||||
}else {
|
||||
return scheduleMapper.countTaskByWorkspaceIdAndGroupAndCreateTimeRange(workspaceID,group,firstTime.getTime(),lastTime.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
public List<TaskInfoResult> findRunningTaskInfoByWorkspaceID(String workspaceID) {
|
||||
List<TaskInfoResult> runningTaskInfoList = scheduleMapper.findRunningTaskInfoByWorkspaceID(workspaceID);
|
||||
return runningTaskInfoList;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue