From 7707f9902d92604b425e695de93a93cdd2959ca0 Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Thu, 17 Dec 2020 17:35:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E6=A8=A1=E5=9D=97-=E9=A6=96=E9=A1=B5=E5=BC=80=E5=8F=91Service?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 接口测试模块-首页开发,包含接口、用例、场景、定时任务、失败案例和运行中任务等功能的开发 --- .../api/controller/APITestController.java | 322 +++++++++--------- .../dto/dataCount/ExecutedCaseInfoResult.java | 18 + .../request/ScheduleInfoRequest.java | 16 + .../response/ExecutedCaseInfoDTO.java | 20 ++ .../api/service/APIReportService.java | 23 +- .../ApiDefinitionExecResultService.java | 7 + .../base/mapper/ApiTestReportMapper.java | 18 + .../base/mapper/ScheduleMapper.java | 25 ++ .../metersphere/commons/utils/CronUtils.java | 62 ++++ .../metersphere/service/ScheduleService.java | 25 ++ 10 files changed, 379 insertions(+), 157 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/api/dto/dataCount/ExecutedCaseInfoResult.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/dataCount/request/ScheduleInfoRequest.java create mode 100644 backend/src/main/java/io/metersphere/api/dto/dataCount/response/ExecutedCaseInfoDTO.java create mode 100644 backend/src/main/java/io/metersphere/commons/utils/CronUtils.java diff --git a/backend/src/main/java/io/metersphere/api/controller/APITestController.java b/backend/src/main/java/io/metersphere/api/controller/APITestController.java index 5a58efb37e..229d0f7fdf 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -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 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(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryAPITestRequest request) { - Page 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 listByIds(@RequestBody QueryAPITestRequest request) { - return apiTestService.listByIds(request); - } - - @GetMapping("/list/{projectId}") - public List 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 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 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 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 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 getProviders(@RequestBody RegistryCenter registry) { - return apiTestService.getProviders(registry); - } - - @PostMapping("/list/schedule/{goPage}/{pageSize}") - public List listSchedule(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryScheduleRequest request) { - Page page = PageHelper.startPage(goPage, pageSize, true); - return apiTestService.listSchedule(request); - } - - @PostMapping("/list/schedule") - public List listSchedule(@RequestBody QueryScheduleRequest request) { - return apiTestService.listSchedule(request); - } - - @PostMapping("/getJsonPaths") - public List 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 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(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryAPITestRequest request) { + Page 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 listByIds(@RequestBody QueryAPITestRequest request) { + return apiTestService.listByIds(request); + } + + @GetMapping("/list/{projectId}") + public List 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 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 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 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 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 getProviders(@RequestBody RegistryCenter registry) { + return apiTestService.getProviders(registry); + } + + @PostMapping("/list/schedule/{goPage}/{pageSize}") + public List listSchedule(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody QueryScheduleRequest request) { + Page page = PageHelper.startPage(goPage, pageSize, true); + return apiTestService.listSchedule(request); + } + + @PostMapping("/list/schedule") + public List listSchedule(@RequestBody QueryScheduleRequest request) { + return apiTestService.listSchedule(request); + } + + @PostMapping("/getJsonPaths") + public List getJsonPaths(@RequestBody QueryJsonPathRequest request) { + return getListJson(request.getJsonPath()); + } +} diff --git a/backend/src/main/java/io/metersphere/api/dto/dataCount/ExecutedCaseInfoResult.java b/backend/src/main/java/io/metersphere/api/dto/dataCount/ExecutedCaseInfoResult.java new file mode 100644 index 0000000000..2a499dc49f --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/dataCount/ExecutedCaseInfoResult.java @@ -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; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/dataCount/request/ScheduleInfoRequest.java b/backend/src/main/java/io/metersphere/api/dto/dataCount/request/ScheduleInfoRequest.java new file mode 100644 index 0000000000..df66955556 --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/dataCount/request/ScheduleInfoRequest.java @@ -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; +} diff --git a/backend/src/main/java/io/metersphere/api/dto/dataCount/response/ExecutedCaseInfoDTO.java b/backend/src/main/java/io/metersphere/api/dto/dataCount/response/ExecutedCaseInfoDTO.java new file mode 100644 index 0000000000..bdd267efed --- /dev/null +++ b/backend/src/main/java/io/metersphere/api/dto/dataCount/response/ExecutedCaseInfoDTO.java @@ -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; +} diff --git a/backend/src/main/java/io/metersphere/api/service/APIReportService.java b/backend/src/main/java/io/metersphere/api/service/APIReportService.java index 20fd1834db..38db544c3b 100644 --- a/backend/src/main/java/io/metersphere/api/service/APIReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/APIReportService.java @@ -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 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); + } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java index f5a175d62e..0df58b52ea 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java @@ -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()); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ApiTestReportMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ApiTestReportMapper.java index 6888e61a55..a736d54ecb 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ApiTestReportMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ApiTestReportMapper.java @@ -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); } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java b/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java index 264e2f2e58..ee050eeccd 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/ScheduleMapper.java @@ -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 findRunningTaskInfoByWorkspaceID(String workspaceID); + } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/commons/utils/CronUtils.java b/backend/src/main/java/io/metersphere/commons/utils/CronUtils.java new file mode 100644 index 0000000000..2d03a1de56 --- /dev/null +++ b/backend/src/main/java/io/metersphere/commons/utils/CronUtils.java @@ -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; + } + + +} diff --git a/backend/src/main/java/io/metersphere/service/ScheduleService.java b/backend/src/main/java/io/metersphere/service/ScheduleService.java index 6ba14f21db..7db0a4fd79 100644 --- a/backend/src/main/java/io/metersphere/service/ScheduleService.java +++ b/backend/src/main/java/io/metersphere/service/ScheduleService.java @@ -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 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 findRunningTaskInfoByWorkspaceID(String workspaceID) { + List runningTaskInfoList = scheduleMapper.findRunningTaskInfoByWorkspaceID(workspaceID); + return runningTaskInfoList; + } }