From 2903d6d593ef64cb6e8b94f8f4238cfac98a150c Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 11 May 2020 17:29:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AF=A6=E6=83=85=E5=92=8C?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PerformanceReportController.java | 18 ++++++-- .../java/io/metersphere/dto/LogDetailDTO.java | 12 +++++ .../io/metersphere/service/ReportService.java | 46 ++++++++++++------- .../report/components/LogDetails.vue | 41 ++++++++++------- 4 files changed, 80 insertions(+), 37 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/dto/LogDetailDTO.java diff --git a/backend/src/main/java/io/metersphere/controller/PerformanceReportController.java b/backend/src/main/java/io/metersphere/controller/PerformanceReportController.java index 1446640802..dbbacda10c 100644 --- a/backend/src/main/java/io/metersphere/controller/PerformanceReportController.java +++ b/backend/src/main/java/io/metersphere/controller/PerformanceReportController.java @@ -7,17 +7,20 @@ import io.metersphere.commons.constants.RoleConstants; import io.metersphere.commons.utils.PageUtils; import io.metersphere.commons.utils.Pager; import io.metersphere.controller.request.ReportRequest; +import io.metersphere.dto.LogDetailDTO; import io.metersphere.dto.ReportDTO; import io.metersphere.report.base.*; import io.metersphere.service.ReportService; import io.metersphere.user.SessionUtils; 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; import java.util.List; -import java.util.Map; @RestController @RequestMapping(value = "performance/report") @@ -97,7 +100,16 @@ public class PerformanceReportController { } @GetMapping("log/{reportId}") - public Map stop(@PathVariable String reportId) { - return reportService.log(reportId); + public List logs(@PathVariable String reportId) { + return reportService.logs(reportId); + } + + @GetMapping("log/download/{logId}") + public ResponseEntity downloadLog(@PathVariable String logId) { + byte[] bytes = reportService.downloadLog(logId); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"jmeter.log\"") + .body(bytes); } } diff --git a/backend/src/main/java/io/metersphere/dto/LogDetailDTO.java b/backend/src/main/java/io/metersphere/dto/LogDetailDTO.java new file mode 100644 index 0000000000..e10ea910b5 --- /dev/null +++ b/backend/src/main/java/io/metersphere/dto/LogDetailDTO.java @@ -0,0 +1,12 @@ +package io.metersphere.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class LogDetailDTO { + private String id; + private String resourceName; + private String content; +} diff --git a/backend/src/main/java/io/metersphere/service/ReportService.java b/backend/src/main/java/io/metersphere/service/ReportService.java index a77676995b..a8265df607 100644 --- a/backend/src/main/java/io/metersphere/service/ReportService.java +++ b/backend/src/main/java/io/metersphere/service/ReportService.java @@ -13,6 +13,7 @@ import io.metersphere.commons.constants.ReportKeys; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.LogUtil; import io.metersphere.controller.request.ReportRequest; +import io.metersphere.dto.LogDetailDTO; import io.metersphere.dto.ReportDTO; import io.metersphere.engine.Engine; import io.metersphere.engine.EngineFactory; @@ -22,6 +23,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -157,35 +159,45 @@ public class ReportService { return extLoadTestReportMapper.selectByPrimaryKey(id); } - public Map log(String reportId) { - Map logMap = new HashMap<>(); + public List logs(String reportId) { LoadTestReportLogExample example = new LoadTestReportLogExample(); example.createCriteria().andReportIdEqualTo(reportId); List loadTestReportLogs = loadTestReportLogMapper.selectByExampleWithBLOBs(example); - loadTestReportLogs.stream().map(log -> { - Map result = new HashMap<>(); + return loadTestReportLogs.stream().map(log -> { + LogDetailDTO detailDTO = new LogDetailDTO(); + detailDTO.setId(log.getId()); TestResource testResource = testResourceService.getTestResource(log.getResourceId()); + String content = log.getContent(); + // 显示前 2048 + content = StringUtils.substring(content, 0, 2048); + detailDTO.setContent(content); if (testResource == null) { - result.put(log.getResourceId(), log.getContent()); - return result; + detailDTO.setResourceName(log.getResourceId()); + return detailDTO; } String configuration = testResource.getConfiguration(); + if (StringUtils.isBlank(configuration)) { + detailDTO.setResourceName(log.getResourceId()); + return detailDTO; + } JSONObject object = JSON.parseObject(configuration); if (StringUtils.isNotBlank(object.getString("masterUrl"))) { - result.put(object.getString("masterUrl"), log.getContent()); - return result; + detailDTO.setResourceName(object.getString("masterUrl")); + return detailDTO; } if (StringUtils.isNotBlank(object.getString("ip"))) { - result.put(object.getString("ip"), log.getContent()); - return result; - + detailDTO.setResourceName(object.getString("ip")); + return detailDTO; } - result.put(log.getResourceId(), log.getContent()); - return result; - }).forEach(log -> logMap.putAll(log.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))) - ); + return detailDTO; + }).collect(Collectors.toList()); + } - return logMap; + public byte[] downloadLog(String logId) { + LoadTestReportLog loadTestReportLog = loadTestReportLogMapper.selectByPrimaryKey(logId); + if (loadTestReportLog != null) { + return loadTestReportLog.getContent().getBytes(); + } + return new byte[0]; } } diff --git a/frontend/src/business/components/performance/report/components/LogDetails.vue b/frontend/src/business/components/performance/report/components/LogDetails.vue index e9fa907835..b2c6c241a5 100644 --- a/frontend/src/business/components/performance/report/components/LogDetails.vue +++ b/frontend/src/business/components/performance/report/components/LogDetails.vue @@ -1,8 +1,8 @@