From f40f0bbedec4735f53d634ab12ba0d100dc5f3ef Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Fri, 13 Nov 2020 16:28:53 +0800 Subject: [PATCH] =?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: '導出',