日志详情和日志下载

This commit is contained in:
Captain.B 2020-05-11 17:29:25 +08:00
parent bd7841ddcc
commit 2903d6d593
4 changed files with 80 additions and 37 deletions

View File

@ -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<String, String> stop(@PathVariable String reportId) {
return reportService.log(reportId);
public List<LogDetailDTO> logs(@PathVariable String reportId) {
return reportService.logs(reportId);
}
@GetMapping("log/download/{logId}")
public ResponseEntity<byte[]> 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);
}
}

View File

@ -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;
}

View File

@ -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<String, String> log(String reportId) {
Map<String, String> logMap = new HashMap<>();
public List<LogDetailDTO> logs(String reportId) {
LoadTestReportLogExample example = new LoadTestReportLogExample();
example.createCriteria().andReportIdEqualTo(reportId);
List<LoadTestReportLog> loadTestReportLogs = loadTestReportLogMapper.selectByExampleWithBLOBs(example);
loadTestReportLogs.stream().map(log -> {
Map<String, String> 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];
}
}

View File

@ -1,8 +1,8 @@
<template>
<div v-loading="result.loading">
<el-tabs type="border-card" :stretch="true">
<el-tab-pane v-for="(item, key) in logContent" :key="key" :label="key" class="logging-content">
{{item.substring(0, 2048) }}...
<el-tab-pane v-for="item in logContent" :key="item.id" :label="item.resourceName" class="logging-content">
{{item.content}}...
<el-link type="primary" @click="downloadLogFile(item)">{{$t('load_test.download_log_file')}}</el-link>
</el-tab-pane>
</el-tabs>
@ -25,21 +25,28 @@
this.logContent = res.data;
})
},
downloadLogFile(content) {
const filename = 'jmeter.log'
const blob = new Blob([content]);
if ("download" in document.createElement("a")) {
// IE
// chrome/firefox
let aTag = document.createElement('a');
aTag.download = filename;
aTag.href = URL.createObjectURL(blob);
aTag.click();
URL.revokeObjectURL(aTag.href)
} else {
// IE10+
navigator.msSaveBlob(blob, filename);
}
downloadLogFile(item) {
let config = {
url: '/performance/report/log/download/' + item.id,
method: 'get',
responseType: 'blob'
};
this.result = this.$request(config).then(response => {
const filename = 'jmeter.log'
const blob = new Blob([response.data]);
if ("download" in document.createElement("a")) {
// IE
// chrome/firefox
let aTag = document.createElement('a');
aTag.download = filename;
aTag.href = URL.createObjectURL(blob);
aTag.click();
URL.revokeObjectURL(aTag.href)
} else {
// IE10+
navigator.msSaveBlob(blob, filename);
}
});
}
},
watch: {