From f40f0bbedec4735f53d634ab12ba0d100dc5f3ef Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 13 Nov 2020 16:28:53 +0800 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=E4=B8=8B=E8=BD=BDjtl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PerformanceReportController.java | 12 ++++++++ .../performance/service/ReportService.java | 14 +++++++-- .../report/PerformanceReportView.vue | 29 +++++++++++++++++++ frontend/src/i18n/en-US.js | 1 + frontend/src/i18n/zh-CN.js | 1 + frontend/src/i18n/zh-TW.js | 1 + 6 files changed, 55 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java b/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java index 2e2c5a651e..dd07e05aaa 100644 --- a/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java +++ b/backend/src/main/java/io/metersphere/performance/controller/PerformanceReportController.java @@ -16,6 +16,9 @@ import io.metersphere.performance.controller.request.ReportRequest; import io.metersphere.performance.service.ReportService; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresRoles; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @@ -130,4 +133,13 @@ public class PerformanceReportController { public void deleteReportBatch(@RequestBody DeleteReportRequest reportRequest) { reportService.deleteReportBatch(reportRequest); } + + @GetMapping("/jtl/download/{reportId}") + public ResponseEntity downloadJtl(@PathVariable String reportId) { + byte[] bytes = reportService.downloadJtl(reportId); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + reportId + ".jtl\"") + .body(bytes); + } } diff --git a/backend/src/main/java/io/metersphere/performance/service/ReportService.java b/backend/src/main/java/io/metersphere/performance/service/ReportService.java index a7dd4ca8b2..95ae5c3736 100644 --- a/backend/src/main/java/io/metersphere/performance/service/ReportService.java +++ b/backend/src/main/java/io/metersphere/performance/service/ReportService.java @@ -18,6 +18,7 @@ import io.metersphere.performance.controller.request.DeleteReportRequest; import io.metersphere.performance.controller.request.ReportRequest; import io.metersphere.performance.engine.Engine; import io.metersphere.performance.engine.EngineFactory; +import io.metersphere.service.FileService; import io.metersphere.service.TestResourceService; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -47,6 +48,8 @@ public class ReportService { private TestResourceService testResourceService; @Resource private LoadTestReportDetailMapper loadTestReportDetailMapper; + @Resource + private FileService fileService; public List getRecentReportList(ReportRequest request) { List orders = new ArrayList<>(); @@ -168,9 +171,9 @@ public class ReportService { public void checkReportStatus(String reportId) { LoadTestReport loadTestReport = loadTestReportMapper.selectByPrimaryKey(reportId); - String reportStatus=""; - if(loadTestReport!=null){ - reportStatus = loadTestReport.getStatus(); + String reportStatus = ""; + if (loadTestReport != null) { + reportStatus = loadTestReport.getStatus(); } if (StringUtils.equals(PerformanceTestStatus.Error.name(), reportStatus)) { MSException.throwException("Report generation error!"); @@ -271,4 +274,9 @@ public class ReportService { String content = getContent(id, ReportKeys.ResponseCodeChart); return JSON.parseArray(content, ChartsData.class); } + + public byte[] downloadJtl(String reportId) { + LoadTestReportWithBLOBs report = getReport(reportId); + return fileService.loadFileAsBytes(report.getFileId()); + } } diff --git a/frontend/src/business/components/performance/report/PerformanceReportView.vue b/frontend/src/business/components/performance/report/PerformanceReportView.vue index 0493330f33..2cd890a9c8 100644 --- a/frontend/src/business/components/performance/report/PerformanceReportView.vue +++ b/frontend/src/business/components/performance/report/PerformanceReportView.vue @@ -25,6 +25,9 @@ {{ $t('test_track.plan_view.export_report') }} + + 下载JTL + @@ -95,6 +98,7 @@ import MsMainContainer from "../../common/components/MsMainContainer"; import {checkoutTestManagerOrTestUser, exportPdf} from "@/common/js/utils"; import html2canvas from 'html2canvas'; import MsPerformanceReportExport from "./PerformanceReportExport"; +import {Message} from "element-ui"; export default { @@ -281,6 +285,31 @@ export default { this.reportExportVisible = false; this.result.loading = false; }, + downloadJtl() { + let config = { + url: "/performance/report/jtl/download/" + this.reportId, + method: 'get', + responseType: 'blob' + }; + this.result = this.$request(config).then(response => { + const content = response.data; + const blob = new Blob([content]); + if ("download" in document.createElement("a")) { + // 非IE下载 + // chrome/firefox + let aTag = document.createElement('a'); + aTag.download = this.reportId + ".jtl"; + aTag.href = URL.createObjectURL(blob); + aTag.click(); + URL.revokeObjectURL(aTag.href) + } else { + // IE10+下载 + navigator.msSaveBlob(blob, this.filename) + } + }).catch(e => { + Message.error({message: e.message, showClose: true}); + }); + } }, created() { this.isReadOnly = false; diff --git a/frontend/src/i18n/en-US.js b/frontend/src/i18n/en-US.js index 2fbdc80cc1..dfcd80d015 100644 --- a/frontend/src/i18n/en-US.js +++ b/frontend/src/i18n/en-US.js @@ -360,6 +360,7 @@ export default { test_stop_now_confirm: 'Are you sure you want to stop the current test immediately?', test_rerun_confirm: 'Are you sure you want to rerun the current test immediately?', test_stop_success: 'Test stop successfully', + downloadJtl: 'Download JTL', test_execute_again: 'Test Execute Again', export: 'Export', compare: 'Compare', diff --git a/frontend/src/i18n/zh-CN.js b/frontend/src/i18n/zh-CN.js index b77911f571..18ce960f57 100644 --- a/frontend/src/i18n/zh-CN.js +++ b/frontend/src/i18n/zh-CN.js @@ -360,6 +360,7 @@ export default { test_rerun_confirm: '确定要再次执行当前测试吗?', test_stop_success: '停止成功', test_execute_again: '再次执行', + downloadJtl: '下载JTL', export: '导出', compare: '比较', generation_error: '报告生成错误, 无法查看, 请检查日志详情!', diff --git a/frontend/src/i18n/zh-TW.js b/frontend/src/i18n/zh-TW.js index 43cc46c469..da33a1d4ca 100644 --- a/frontend/src/i18n/zh-TW.js +++ b/frontend/src/i18n/zh-TW.js @@ -360,6 +360,7 @@ export default { test_stop_now: '立即停止', test_stop_now_confirm: '確定要立即停止當前測試嗎?', test_rerun_confirm: '確定要再次執行當前測試嗎?', + downloadJtl: '下載JTL', test_stop_success: '停止成功', test_execute_again: '再次執行', export: '導出', From b9e79cec6e195ce77cd9094c82d70e6c9f145d76 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 13 Nov 2020 16:37:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?refactor(=E6=80=A7=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95):=20=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E4=BF=9D?= =?UTF-8?q?=E5=AD=98jtl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/performance/service/ReportService.java | 4 ++++ backend/src/main/resources/i18n/messages_en_US.properties | 1 + backend/src/main/resources/i18n/messages_zh_CN.properties | 1 + backend/src/main/resources/i18n/messages_zh_TW.properties | 1 + 4 files changed, 7 insertions(+) diff --git a/backend/src/main/java/io/metersphere/performance/service/ReportService.java b/backend/src/main/java/io/metersphere/performance/service/ReportService.java index 95ae5c3736..c92f68298e 100644 --- a/backend/src/main/java/io/metersphere/performance/service/ReportService.java +++ b/backend/src/main/java/io/metersphere/performance/service/ReportService.java @@ -13,6 +13,7 @@ import io.metersphere.commons.utils.ServiceUtils; import io.metersphere.controller.request.OrderRequest; import io.metersphere.dto.LogDetailDTO; import io.metersphere.dto.ReportDTO; +import io.metersphere.i18n.Translator; import io.metersphere.performance.base.*; import io.metersphere.performance.controller.request.DeleteReportRequest; import io.metersphere.performance.controller.request.ReportRequest; @@ -277,6 +278,9 @@ public class ReportService { public byte[] downloadJtl(String reportId) { LoadTestReportWithBLOBs report = getReport(reportId); + if (StringUtils.isBlank(report.getFileId())) { + MSException.throwException(Translator.get("load_test_report_file_not_exist")); + } return fileService.loadFileAsBytes(report.getFileId()); } } diff --git a/backend/src/main/resources/i18n/messages_en_US.properties b/backend/src/main/resources/i18n/messages_en_US.properties index ea9fe501cc..604cf385c7 100644 --- a/backend/src/main/resources/i18n/messages_en_US.properties +++ b/backend/src/main/resources/i18n/messages_en_US.properties @@ -48,6 +48,7 @@ related_case_del_fail_prefix=Connected to related_case_del_fail_suffix=TestCase, please disassociate first jmx_content_valid=JMX content is invalid container_delete_fail=The container failed to stop, please try again +load_test_report_file_not_exist=There is no JTL file in the current report, please execute it again to get it #workspace workspace_name_is_null=Workspace name cannot be null workspace_name_already_exists=The workspace name already exists diff --git a/backend/src/main/resources/i18n/messages_zh_CN.properties b/backend/src/main/resources/i18n/messages_zh_CN.properties index 7745c3acd2..72ae06238d 100644 --- a/backend/src/main/resources/i18n/messages_zh_CN.properties +++ b/backend/src/main/resources/i18n/messages_zh_CN.properties @@ -48,6 +48,7 @@ related_case_del_fail_prefix=已关联到 related_case_del_fail_suffix=测试用例,请先解除关联 jmx_content_valid=JMX 内容无效,请检查 container_delete_fail=容器由于网络原因停止失败,请重试 +load_test_report_file_not_exist=当前报告没有JTL文件,请重新执行以便获取 #workspace workspace_name_is_null=工作空间名不能为空 workspace_name_already_exists=工作空间名已存在 diff --git a/backend/src/main/resources/i18n/messages_zh_TW.properties b/backend/src/main/resources/i18n/messages_zh_TW.properties index 32411f615d..1d6e4d8889 100644 --- a/backend/src/main/resources/i18n/messages_zh_TW.properties +++ b/backend/src/main/resources/i18n/messages_zh_TW.properties @@ -48,6 +48,7 @@ related_case_del_fail_prefix=已關聯到 related_case_del_fail_suffix=測試用例,請先解除關聯 jmx_content_valid=JMX 內容無效,請檢查 container_delete_fail=容器由於網絡原因停止失敗,請重試 +load_test_report_file_not_exist=當前報告沒有JTL文件,請重新執行以便獲取 #workspace workspace_name_is_null=工作空間名不能為空 workspace_name_already_exists=工作空間名已存在 From abd12d5d77b60bb0b9c206dea185fcc1a20e71ba Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 13 Nov 2020 16:58:57 +0800 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20=E4=B8=8B=E8=BD=BDjtl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/performance/service/ReportService.java | 2 +- .../performance/report/PerformanceReportView.vue | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/backend/src/main/java/io/metersphere/performance/service/ReportService.java b/backend/src/main/java/io/metersphere/performance/service/ReportService.java index c92f68298e..3bc2fabaf6 100644 --- a/backend/src/main/java/io/metersphere/performance/service/ReportService.java +++ b/backend/src/main/java/io/metersphere/performance/service/ReportService.java @@ -279,7 +279,7 @@ public class ReportService { public byte[] downloadJtl(String reportId) { LoadTestReportWithBLOBs report = getReport(reportId); if (StringUtils.isBlank(report.getFileId())) { - MSException.throwException(Translator.get("load_test_report_file_not_exist")); + throw new RuntimeException(Translator.get("load_test_report_file_not_exist")); } return fileService.loadFileAsBytes(report.getFileId()); } diff --git a/frontend/src/business/components/performance/report/PerformanceReportView.vue b/frontend/src/business/components/performance/report/PerformanceReportView.vue index 2cd890a9c8..b4a87e4dfd 100644 --- a/frontend/src/business/components/performance/report/PerformanceReportView.vue +++ b/frontend/src/business/components/performance/report/PerformanceReportView.vue @@ -26,7 +26,7 @@ {{ $t('test_track.plan_view.export_report') }} - 下载JTL + {{ $t('report.downloadJtl') }} @@ -307,7 +307,10 @@ export default { navigator.msSaveBlob(blob, this.filename) } }).catch(e => { - Message.error({message: e.message, showClose: true}); + let text = e.response.data.text(); + text.then((data) => { + Message.error({message: JSON.parse(data).message || e.message, showClose: true}); + }); }); } },