From 2eee9e2617f149d63d2c583c5851fa07d6540a97 Mon Sep 17 00:00:00 2001 From: q4speed Date: Thu, 21 May 2020 18:09:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A5=BC=E5=9B=BE?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=EF=BC=8C=E4=B8=8D=E9=9C=80=E8=A6=81=E6=98=BE?= =?UTF-8?q?=E7=A4=BAname?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../business/components/api/report/components/MetricChart.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/business/components/api/report/components/MetricChart.vue b/frontend/src/business/components/api/report/components/MetricChart.vue index e1b59cb06e..ddce085657 100644 --- a/frontend/src/business/components/api/report/components/MetricChart.vue +++ b/frontend/src/business/components/api/report/components/MetricChart.vue @@ -96,8 +96,8 @@ show: false }, data: [ - {value: this.content.success, name: this.$t('api_report.success')}, - {value: this.content.error, name: this.$t('api_report.fail')}, + {value: this.content.success}, + {value: this.content.error}, ] } ] From 601e329f0d8899e0dd22be140326db9c9e90b79b Mon Sep 17 00:00:00 2001 From: q4speed Date: Thu, 21 May 2020 18:10:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E5=90=8E=E8=87=AA=E5=8A=A8=E8=B7=B3=E8=BD=AC=E5=88=B0=E6=8A=A5?= =?UTF-8?q?=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/APITestController.java | 5 +- .../api/jmeter/APIBackendListenerClient.java | 4 +- .../io/metersphere/api/jmeter/TestResult.java | 2 +- .../api/service/APIReportService.java | 65 ++++++++++++++----- .../api/service/APITestService.java | 9 ++- .../resources/i18n/messages_en_US.properties | 1 + .../resources/i18n/messages_zh_CN.properties | 1 + .../resources/i18n/messages_zh_TW.properties | 1 + .../components/api/report/ApiReportView.vue | 15 ++++- .../components/api/test/ApiTestConfig.vue | 5 +- 10 files changed, 83 insertions(+), 25 deletions(-) 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 219704fcf6..8d29f8dc44 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; + import java.util.List; @RestController @@ -64,7 +65,7 @@ public class APITestController { } @PostMapping(value = "/run") - public void run(@RequestBody SaveAPITestRequest request) { - apiTestService.run(request); + public String run(@RequestBody SaveAPITestRequest request) { + return apiTestService.run(request); } } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index d5b8a9476b..ff556e6a0e 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -61,7 +61,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl queue.forEach((id, sampleResults) -> { TestResult testResult = new TestResult(); - testResult.setId(id); + testResult.setTestId(id); testResult.setTotal(sampleResults.size()); // key: 场景Id @@ -104,7 +104,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl testResult.getScenarios().addAll(scenarios.values()); testResult.getScenarios().sort(Comparator.comparing(ScenarioResult::getOrder)); apiTestService.changeStatus(id, APITestStatus.Completed); - apiReportService.save(testResult); + apiReportService.complete(testResult); }); queue.clear(); super.teardownTest(context); diff --git a/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java b/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java index d74e04ead9..54d3411147 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/TestResult.java @@ -8,7 +8,7 @@ import java.util.List; @Data public class TestResult { - private String id; + private String testId; private int success = 0; 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 837f6cfe59..10f33218a6 100644 --- a/backend/src/main/java/io/metersphere/api/service/APIReportService.java +++ b/backend/src/main/java/io/metersphere/api/service/APIReportService.java @@ -5,12 +5,15 @@ import io.metersphere.api.dto.APIReportResult; import io.metersphere.api.dto.DeleteAPIReportRequest; import io.metersphere.api.dto.QueryAPIReportRequest; import io.metersphere.api.jmeter.TestResult; +import io.metersphere.base.domain.ApiTest; import io.metersphere.base.domain.ApiTestReport; -import io.metersphere.base.domain.ApiTestWithBLOBs; +import io.metersphere.base.domain.ApiTestReportExample; import io.metersphere.base.mapper.ApiTestReportMapper; import io.metersphere.base.mapper.ext.ExtApiTestReportMapper; import io.metersphere.commons.constants.APITestStatus; +import io.metersphere.commons.exception.MSException; import io.metersphere.dto.DashboardTestDTO; +import io.metersphere.i18n.Translator; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -25,8 +28,6 @@ import javax.annotation.Resource; @Transactional(rollbackFor = Exception.class) public class APIReportService { - @Resource - private APITestService apiTestService; @Resource private ApiTestReportMapper apiTestReportMapper; @Resource @@ -52,24 +53,58 @@ public class APIReportService { apiTestReportMapper.deleteByPrimaryKey(request.getId()); } - public void save(TestResult result) { - ApiTestWithBLOBs test = apiTestService.get(result.getId()); - ApiTestReport report = new ApiTestReport(); - report.setId(UUID.randomUUID().toString()); - report.setTestId(result.getId()); - report.setName(test.getName()); - report.setDescription(test.getDescription()); - report.setContent(JSONObject.toJSONString(result)); - report.setCreateTime(System.currentTimeMillis()); - report.setUpdateTime(System.currentTimeMillis()); - report.setStatus(APITestStatus.Completed.name()); - apiTestReportMapper.insert(report); + public void deleteByTestId(String testId) { + ApiTestReportExample example = new ApiTestReportExample(); + example.createCriteria().andTestIdEqualTo(testId); + apiTestReportMapper.deleteByExample(example); } + public void complete(TestResult result) { + ApiTestReport report = getRunningReport(result.getTestId()); + if (report == null) { + MSException.throwException(Translator.get("api_report_is_null")); + } + report.setContent(JSONObject.toJSONString(result)); + report.setUpdateTime(System.currentTimeMillis()); + report.setStatus(APITestStatus.Completed.name()); + apiTestReportMapper.updateByPrimaryKeySelective(report); + } + + public String create(ApiTest test) { + ApiTestReport running = getRunningReport(test.getId()); + if (running != null) { + return running.getId(); + } + + ApiTestReport report = new ApiTestReport(); + report.setId(UUID.randomUUID().toString()); + report.setTestId(test.getId()); + report.setName(test.getName()); + report.setDescription(test.getDescription()); + report.setCreateTime(System.currentTimeMillis()); + report.setUpdateTime(System.currentTimeMillis()); + report.setStatus(APITestStatus.Running.name()); + apiTestReportMapper.insert(report); + + return report.getId(); + } + + public ApiTestReport getRunningReport(String testId) { + ApiTestReportExample example = new ApiTestReportExample(); + example.createCriteria().andTestIdEqualTo(testId).andStatusEqualTo(APITestStatus.Running.name()); + List apiTestReports = apiTestReportMapper.selectByExample(example); + if (apiTestReports.size() > 0) { + return apiTestReports.get(0); + } else { + return null; + } + } public List dashboardTests(String workspaceId) { Instant oneYearAgo = Instant.now().plus(-365, ChronoUnit.DAYS); long startTimestamp = oneYearAgo.toEpochMilli(); return extApiTestReportMapper.selectDashboardTests(workspaceId, startTimestamp); } + + } diff --git a/backend/src/main/java/io/metersphere/api/service/APITestService.java b/backend/src/main/java/io/metersphere/api/service/APITestService.java index db74c6e823..b40405be29 100644 --- a/backend/src/main/java/io/metersphere/api/service/APITestService.java +++ b/backend/src/main/java/io/metersphere/api/service/APITestService.java @@ -39,6 +39,8 @@ public class APITestService { private FileService fileService; @Resource private JMeterService jMeterService; + @Resource + private APIReportService apiReportService; public List list(QueryAPITestRequest request) { return extApiTestMapper.list(request); @@ -71,18 +73,23 @@ public class APITestService { public void delete(DeleteAPITestRequest request) { deleteFileByTestId(request.getId()); + apiReportService.deleteByTestId(request.getId()); apiTestMapper.deleteByPrimaryKey(request.getId()); } - public void run(SaveAPITestRequest request) { + public String run(SaveAPITestRequest request) { ApiTestFile file = getFileByTestId(request.getId()); if (file == null) { MSException.throwException(Translator.get("file_cannot_be_null")); } byte[] bytes = fileService.loadFileAsBytes(file.getFileId()); InputStream is = new ByteArrayInputStream(bytes); + + String reportId = apiReportService.create(get(request.getId())); changeStatus(request.getId(), APITestStatus.Running); + jMeterService.run(is); + return reportId; } public void changeStatus(String id, APITestStatus status) { diff --git a/backend/src/main/resources/i18n/messages_en_US.properties b/backend/src/main/resources/i18n/messages_en_US.properties index 9ea76aedaf..29d5ae4ff8 100644 --- a/backend/src/main/resources/i18n/messages_en_US.properties +++ b/backend/src/main/resources/i18n/messages_en_US.properties @@ -37,6 +37,7 @@ organization_does_not_belong_to_user=The current organization does not belong to organization_id_is_null=Organization ID cannot be null #api api_load_script_error=Load script error +api_report_is_null="Report is null, can't update" #test case test_case_node_level=level test_case_node_level_tip=The node tree maximum depth is diff --git a/backend/src/main/resources/i18n/messages_zh_CN.properties b/backend/src/main/resources/i18n/messages_zh_CN.properties index 0031ca23b4..7846912cf2 100644 --- a/backend/src/main/resources/i18n/messages_zh_CN.properties +++ b/backend/src/main/resources/i18n/messages_zh_CN.properties @@ -37,6 +37,7 @@ organization_does_not_belong_to_user=当前组织不属于当前用户 organization_id_is_null=组织 ID 不能为空 #api api_load_script_error=读取脚本失败 +api_report_is_null="测试报告是未生成,无法更新" #test case test_case_node_level=层 test_case_node_level_tip=模块树最大深度为 diff --git a/backend/src/main/resources/i18n/messages_zh_TW.properties b/backend/src/main/resources/i18n/messages_zh_TW.properties index d4a5cd2cac..dab39d2d45 100644 --- a/backend/src/main/resources/i18n/messages_zh_TW.properties +++ b/backend/src/main/resources/i18n/messages_zh_TW.properties @@ -37,6 +37,7 @@ organization_does_not_belong_to_user=當前組織不屬於當前用戶 organization_id_is_null=組織 ID 不能為空 #api api_load_script_error=讀取腳本失敗 +api_report_is_null="測試報告是未生成,無法更新" #test case test_case_node_level=層 test_case_node_level_tip=模塊樹最大深度為 diff --git a/frontend/src/business/components/api/report/ApiReportView.vue b/frontend/src/business/components/api/report/ApiReportView.vue index 20c376851b..08d6aa1bf8 100644 --- a/frontend/src/business/components/api/report/ApiReportView.vue +++ b/frontend/src/business/components/api/report/ApiReportView.vue @@ -5,9 +5,9 @@
{{report.projectName}} / - {{report.testName}} + {{report.testName}} [{{report.createTime | timestampFormatDate}}]
-
+
@@ -59,11 +59,17 @@ methods: { getReport() { + this.report = {}; + this.content = {}; if (this.reportId) { let url = "/api/report/get/" + this.reportId; this.result = this.$get(url, response => { this.report = response.data || {}; - this.content = JSON.parse(this.report.content); + if (this.isCompleted) { + this.content = JSON.parse(this.report.content); + } else { + setTimeout(this.getReport, 2000) + } }); } } @@ -83,6 +89,9 @@ }, path() { return "/api/test/edit?id=" + this.report.testId; + }, + isCompleted() { + return "Completed" === this.report.status; } } } diff --git a/frontend/src/business/components/api/test/ApiTestConfig.vue b/frontend/src/business/components/api/test/ApiTestConfig.vue index 31b72b680e..6a2e455867 100644 --- a/frontend/src/business/components/api/test/ApiTestConfig.vue +++ b/frontend/src/business/components/api/test/ApiTestConfig.vue @@ -120,8 +120,11 @@ }) }, runTest: function () { - this.result = this.$post("/api/run", {id: this.test.id}, () => { + this.result = this.$post("/api/run", {id: this.test.id}, (response) => { this.$success(this.$t('api_test.running')); + this.$router.push({ + path: '/api/report/view/' + response.data + }) }); }, saveRunTest: function () {