From 6ad8cac6463c7627dbbfffdaebc1395ee6d83a20 Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Fri, 15 May 2020 15:22:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TestCaseReportController.java | 4 - .../track/controller/TestPlanController.java | 6 +- .../track/service/TestCaseReportService.java | 119 ------------------ .../track/service/TestPlanService.java | 118 ++++++++++++++++- .../view/comonents/TestCaseReportView.vue | 2 +- 5 files changed, 123 insertions(+), 126 deletions(-) diff --git a/backend/src/main/java/io/metersphere/track/controller/TestCaseReportController.java b/backend/src/main/java/io/metersphere/track/controller/TestCaseReportController.java index c57e4dbda..75f040a4d 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestCaseReportController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestCaseReportController.java @@ -41,8 +41,4 @@ public class TestCaseReportController { return testCaseReportService.deleteTestCaseReport(id); } - @GetMapping("/get/metric/{planId}") - public TestCaseReportMetricDTO getMetric(@PathVariable String planId){ - return testCaseReportService.getMetric(planId); - } } diff --git a/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java b/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java index 6f5d04032..1ed384d60 100644 --- a/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java +++ b/backend/src/main/java/io/metersphere/track/controller/TestPlanController.java @@ -6,6 +6,7 @@ import io.metersphere.base.domain.TestPlan; import io.metersphere.commons.utils.PageUtils; import io.metersphere.commons.utils.Pager; import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.track.dto.TestCaseReportMetricDTO; import io.metersphere.track.dto.TestPlanDTO; import io.metersphere.track.request.testcase.PlanCaseRelevanceRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; @@ -73,5 +74,8 @@ public class TestPlanController { testPlanService.testPlanRelevance(request); } - + @GetMapping("/get/metric/{planId}") + public TestCaseReportMetricDTO getMetric(@PathVariable String planId){ + return testPlanService.getMetric(planId); + } } diff --git a/backend/src/main/java/io/metersphere/track/service/TestCaseReportService.java b/backend/src/main/java/io/metersphere/track/service/TestCaseReportService.java index b11264347..f5035782a 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestCaseReportService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestCaseReportService.java @@ -35,21 +35,6 @@ public class TestCaseReportService { @Resource TestCaseReportTemplateMapper testCaseReportTemplateMapper; - @Resource - TestCaseNodeService testCaseNodeService; - - @Resource - TestCaseNodeMapper testCaseNodeMapper; - - @Resource - ExtTestPlanTestCaseMapper extTestPlanTestCaseMapper; - - @Resource - TestPlanTestCaseMapper testPlanTestCaseMapper; - - @Resource - ExtTestCaseMapper extTestCaseMapper; - public List listTestCaseReport(TestCaseReport request) { TestCaseReportExample example = new TestCaseReportExample(); if ( StringUtils.isNotBlank(request.getName()) ) { @@ -88,110 +73,6 @@ public class TestCaseReportService { return report.getId(); } - public TestCaseReportMetricDTO getMetric(String planId) { - - QueryTestPlanRequest queryTestPlanRequest = new QueryTestPlanRequest(); - queryTestPlanRequest.setId(planId); - TestPlanDTO testPlan = extTestPlanMapper.list(queryTestPlanRequest).get(0); - - Set executors = new HashSet<>(); - Map reportStatusResultMap = new HashMap<>(); - - TestCaseNodeExample testCaseNodeExample = new TestCaseNodeExample(); - testCaseNodeExample.createCriteria().andProjectIdEqualTo(testPlan.getProjectId()); - List nodes = testCaseNodeMapper.selectByExample(testCaseNodeExample); - - List nodeTrees = testCaseNodeService.getNodeTrees(nodes); - Map> childIdMap = new HashMap<>(); - nodeTrees.forEach(item -> { - Set childIds = new HashSet<>(); - getChildIds(item, childIds); - childIdMap.put(item.getId(), childIds); - }); - - QueryTestPlanCaseRequest request = new QueryTestPlanCaseRequest(); - request.setPlanId(planId); - List testPlanTestCases = extTestPlanTestCaseMapper.list(request); - - Map moduleResultMap = new HashMap<>(); - - for (TestPlanCaseDTO testCase: testPlanTestCases) { - executors.add(testCase.getExecutor()); - getStatusResultMap(reportStatusResultMap, testCase); - getModuleResultMap(childIdMap, moduleResultMap, testCase, nodeTrees); - } - - - nodeTrees.forEach(rootNode -> { - TestCaseReportModuleResultDTO moduleResult = moduleResultMap.get(rootNode.getId()); - if (moduleResult != null) { - moduleResult.setModuleName(rootNode.getName()); - } - }); - - for (TestCaseReportModuleResultDTO moduleResult : moduleResultMap.values()) { - moduleResult.setPassRate(new BigDecimal(moduleResult.getPassCount()*1.0f/moduleResult.getCaseCount()) - .setScale(2, BigDecimal.ROUND_HALF_UP) - .doubleValue() * 100); - if (moduleResult.getCaseCount() <= 0) { - moduleResultMap.remove(moduleResult.getModuleId()); - } - } - - TestCaseReportMetricDTO testCaseReportMetricDTO = new TestCaseReportMetricDTO(); - testCaseReportMetricDTO.setProjectName(testPlan.getProjectName()); - testCaseReportMetricDTO.setPrincipal(testPlan.getPrincipal()); - testCaseReportMetricDTO.setExecutors(new ArrayList<>(executors)); - testCaseReportMetricDTO.setExecuteResult(new ArrayList<>(reportStatusResultMap.values())); - testCaseReportMetricDTO.setModuleExecuteResult(new ArrayList<>(moduleResultMap.values())); - - return testCaseReportMetricDTO; - } - - private void getStatusResultMap(Map reportStatusResultMap, TestPlanCaseDTO testCase) { - TestCaseReportStatusResultDTO statusResult = reportStatusResultMap.get(testCase.getStatus()); - if (statusResult == null) { - statusResult = new TestCaseReportStatusResultDTO(); - statusResult.setStatus(testCase.getStatus()); - statusResult.setCount(0); - } - statusResult.setCount(statusResult.getCount() + 1); - reportStatusResultMap.put(testCase.getStatus(), statusResult); - } - - private void getModuleResultMap(Map> childIdMap, Map moduleResultMap, TestPlanCaseDTO testCase, List nodeTrees) { - childIdMap.forEach((rootNodeId, childIds) -> { - if (childIds.contains(testCase.getNodeId())) { - TestCaseReportModuleResultDTO moduleResult = moduleResultMap.get(rootNodeId); - if (moduleResult == null) { - moduleResult = new TestCaseReportModuleResultDTO(); - moduleResult.setCaseCount(0); - moduleResult.setPassCount(0); - moduleResult.setModuleId(rootNodeId); - } - moduleResult.setCaseCount(moduleResult.getCaseCount() + 1); - if (StringUtils.equals(testCase.getStatus(), TestPlanTestCaseStatus.Pass.name())) { - moduleResult.setPassCount(moduleResult.getPassCount() + 1); - } - moduleResultMap.put(rootNodeId, moduleResult); - return; - } - }); - } - - private void getChildIds(TestCaseNodeDTO rootNode, Set childIds) { - - childIds.add(rootNode.getId()); - - List children = rootNode.getChildren(); - - if(children != null) { - Iterator iterator = children.iterator(); - while(iterator.hasNext()){ - getChildIds(iterator.next(), childIds); - } - } - } } diff --git a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java index 637a2f442..a8a663b64 100644 --- a/backend/src/main/java/io/metersphere/track/service/TestPlanService.java +++ b/backend/src/main/java/io/metersphere/track/service/TestPlanService.java @@ -3,18 +3,21 @@ package io.metersphere.track.service; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.TestCaseMapper; +import io.metersphere.base.mapper.TestCaseNodeMapper; import io.metersphere.base.mapper.TestPlanMapper; import io.metersphere.base.mapper.TestPlanTestCaseMapper; import io.metersphere.base.mapper.ext.ExtTestPlanMapper; import io.metersphere.base.mapper.ext.ExtTestPlanTestCaseMapper; import io.metersphere.commons.constants.TestPlanStatus; +import io.metersphere.commons.constants.TestPlanTestCaseStatus; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.user.SessionUser; import io.metersphere.commons.utils.SessionUtils; +import io.metersphere.track.dto.*; import io.metersphere.track.request.testcase.PlanCaseRelevanceRequest; import io.metersphere.track.request.testcase.QueryTestPlanRequest; -import io.metersphere.track.dto.TestPlanDTO; import io.metersphere.i18n.Translator; +import io.metersphere.track.request.testplancase.QueryTestPlanCaseRequest; import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -23,6 +26,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; @@ -48,6 +52,12 @@ public class TestPlanService { @Resource SqlSessionFactory sqlSessionFactory; + @Resource + TestCaseNodeMapper testCaseNodeMapper; + + @Resource + TestCaseNodeService testCaseNodeService; + public void addTestPlan(TestPlan testPlan) { testPlan.setId(UUID.randomUUID().toString()); testPlan.setStatus(TestPlanStatus.Prepare.name()); @@ -147,4 +157,110 @@ public class TestPlanService { request.setPlanIds(extTestPlanTestCaseMapper.findRelateTestPlanId(user.getId())); return extTestPlanMapper.listRelate(request); } + + public TestCaseReportMetricDTO getMetric(String planId) { + + QueryTestPlanRequest queryTestPlanRequest = new QueryTestPlanRequest(); + queryTestPlanRequest.setId(planId); + TestPlanDTO testPlan = extTestPlanMapper.list(queryTestPlanRequest).get(0); + + Set executors = new HashSet<>(); + Map reportStatusResultMap = new HashMap<>(); + + TestCaseNodeExample testCaseNodeExample = new TestCaseNodeExample(); + testCaseNodeExample.createCriteria().andProjectIdEqualTo(testPlan.getProjectId()); + List nodes = testCaseNodeMapper.selectByExample(testCaseNodeExample); + + List nodeTrees = testCaseNodeService.getNodeTrees(nodes); + + Map> childIdMap = new HashMap<>(); + nodeTrees.forEach(item -> { + Set childIds = new HashSet<>(); + getChildIds(item, childIds); + childIdMap.put(item.getId(), childIds); + }); + + QueryTestPlanCaseRequest request = new QueryTestPlanCaseRequest(); + request.setPlanId(planId); + List testPlanTestCases = extTestPlanTestCaseMapper.list(request); + + Map moduleResultMap = new HashMap<>(); + + for (TestPlanCaseDTO testCase: testPlanTestCases) { + executors.add(testCase.getExecutor()); + getStatusResultMap(reportStatusResultMap, testCase); + getModuleResultMap(childIdMap, moduleResultMap, testCase, nodeTrees); + } + + + nodeTrees.forEach(rootNode -> { + TestCaseReportModuleResultDTO moduleResult = moduleResultMap.get(rootNode.getId()); + if (moduleResult != null) { + moduleResult.setModuleName(rootNode.getName()); + } + }); + + for (TestCaseReportModuleResultDTO moduleResult : moduleResultMap.values()) { + moduleResult.setPassRate(new BigDecimal(moduleResult.getPassCount()*1.0f/moduleResult.getCaseCount()) + .setScale(2, BigDecimal.ROUND_HALF_UP) + .doubleValue() * 100); + if (moduleResult.getCaseCount() <= 0) { + moduleResultMap.remove(moduleResult.getModuleId()); + } + } + + TestCaseReportMetricDTO testCaseReportMetricDTO = new TestCaseReportMetricDTO(); + testCaseReportMetricDTO.setProjectName(testPlan.getProjectName()); + testCaseReportMetricDTO.setPrincipal(testPlan.getPrincipal()); + testCaseReportMetricDTO.setExecutors(new ArrayList<>(executors)); + testCaseReportMetricDTO.setExecuteResult(new ArrayList<>(reportStatusResultMap.values())); + testCaseReportMetricDTO.setModuleExecuteResult(new ArrayList<>(moduleResultMap.values())); + + return testCaseReportMetricDTO; + } + + private void getStatusResultMap(Map reportStatusResultMap, TestPlanCaseDTO testCase) { + TestCaseReportStatusResultDTO statusResult = reportStatusResultMap.get(testCase.getStatus()); + if (statusResult == null) { + statusResult = new TestCaseReportStatusResultDTO(); + statusResult.setStatus(testCase.getStatus()); + statusResult.setCount(0); + } + statusResult.setCount(statusResult.getCount() + 1); + reportStatusResultMap.put(testCase.getStatus(), statusResult); + } + + private void getModuleResultMap(Map> childIdMap, Map moduleResultMap, TestPlanCaseDTO testCase, List nodeTrees) { + childIdMap.forEach((rootNodeId, childIds) -> { + if (childIds.contains(testCase.getNodeId())) { + TestCaseReportModuleResultDTO moduleResult = moduleResultMap.get(rootNodeId); + if (moduleResult == null) { + moduleResult = new TestCaseReportModuleResultDTO(); + moduleResult.setCaseCount(0); + moduleResult.setPassCount(0); + moduleResult.setModuleId(rootNodeId); + } + moduleResult.setCaseCount(moduleResult.getCaseCount() + 1); + if (StringUtils.equals(testCase.getStatus(), TestPlanTestCaseStatus.Pass.name())) { + moduleResult.setPassCount(moduleResult.getPassCount() + 1); + } + moduleResultMap.put(rootNodeId, moduleResult); + return; + } + }); + } + + private void getChildIds(TestCaseNodeDTO rootNode, Set childIds) { + + childIds.add(rootNode.getId()); + + List children = rootNode.getChildren(); + + if(children != null) { + Iterator iterator = children.iterator(); + while(iterator.hasNext()){ + getChildIds(iterator.next(), childIds); + } + } + } } diff --git a/frontend/src/business/components/track/plan/view/comonents/TestCaseReportView.vue b/frontend/src/business/components/track/plan/view/comonents/TestCaseReportView.vue index a64fa1fac..aacf41ab3 100644 --- a/frontend/src/business/components/track/plan/view/comonents/TestCaseReportView.vue +++ b/frontend/src/business/components/track/plan/view/comonents/TestCaseReportView.vue @@ -150,7 +150,7 @@ } }, getMetric() { - this.result = this.$get('/case/report/get/metric/' + this.planId, response => { + this.result = this.$get('/test/plan/get/metric/' + this.planId, response => { this.metric = response.data; if (this.report.startTime) { this.metric.startTime = new Date(this.report.startTime); -- GitLab