fix(接口自动化) 场景批量下载jmx打包成一个zip下载

--bug=1006480 --user=赵勇 【github#6012】导出接口场景(jmeter格式)导出场景数量不正确 https://www.tapd.cn/55049933/s/1048981
This commit is contained in:
fit2-zhao 2021-09-22 11:13:01 +08:00 committed by fit2-zhao
parent 738e9bfda7
commit d0b7921cee
4 changed files with 69 additions and 11 deletions

View File

@ -283,8 +283,12 @@ public class ApiAutomationController {
@PostMapping(value = "/export/jmx")
@RequiresPermissions(PermissionConstants.PROJECT_API_SCENARIO_READ_EXPORT_SCENARIO)
@MsAuditLog(module = "api_automation", type = OperLogConstants.EXPORT, sourceId = "#request.id", title = "#request.name", project = "#request.projectId")
public List<ApiScenrioExportJmx> exportJmx(@RequestBody ApiScenarioBatchRequest request) {
return apiAutomationService.exportJmx(request);
public ResponseEntity<byte[]> downloadBodyFiles(@RequestBody ApiScenarioBatchRequest request) {
byte[] bytes = apiAutomationService.exportZip(request);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + "场景JMX文件集.zip")
.body(bytes);
}
}

View File

@ -66,6 +66,7 @@ import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@ -145,6 +146,30 @@ public class ApiAutomationService {
return list;
}
public byte[] exportZip(ApiScenarioBatchRequest request) {
List<ApiScenarioWithBLOBs> apiScenarioWithBLOBs = getExportResult(request);
// 生成jmx
Map<String, byte[]> files = new LinkedHashMap<>();
apiScenarioWithBLOBs.forEach(item -> {
if (StringUtils.isNotEmpty(item.getScenarioDefinition())) {
String jmx = generateJmx(item);
if (StringUtils.isNotEmpty(jmx)) {
ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), apiTestService.updateJmxString(jmx, null, true).getXml());
String fileName = item.getName() + ".jmx";
String jmxStr = scenrioExportJmx.getJmx();
files.put(fileName, jmxStr.getBytes(StandardCharsets.UTF_8));
}
}
});
if (CollectionUtils.isNotEmpty(apiScenarioWithBLOBs)) {
List<String> names = apiScenarioWithBLOBs.stream().map(ApiScenarioWithBLOBs::getName).collect(Collectors.toList());
request.setName(String.join(",", names));
List<String> ids = apiScenarioWithBLOBs.stream().map(ApiScenarioWithBLOBs::getId).collect(Collectors.toList());
request.setId(JSON.toJSONString(ids));
}
return FileUtils.listBytesToZip(files);
}
public List<ApiScenarioWithBLOBs> listAll(ApiScenarioBatchRequest request) {
ServiceUtils.getSelectAllIds(request, request.getCondition(),
(query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query));

View File

@ -9,12 +9,36 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class FileUtils {
public static final String BODY_FILE_DIR = "/opt/metersphere/data/body";
public static final String MD_IMAGE_DIR = "/opt/metersphere/data/image/markdown";
public static byte[] listBytesToZip(Map<String, byte[]> mapReport) {
try {
if (!mapReport.isEmpty()) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(baos);
for (Map.Entry<String, byte[]> report : mapReport.entrySet()) {
ZipEntry entry = new ZipEntry(report.getKey());
entry.setSize(report.getValue().length);
zos.putNextEntry(entry);
zos.write(report.getValue());
}
zos.closeEntry();
zos.close();
return baos.toByteArray();
}
} catch (Exception e) {
return new byte[10];
}
return new byte[10];
}
private static void create(List<String> bodyUploadIds, List<MultipartFile> bodyFiles, String path) {
String filePath = BODY_FILE_DIR;
if (StringUtils.isNotEmpty(path)) {

View File

@ -210,6 +210,7 @@ import {API_SCENARIO_LIST, PROJECT_NAME, WORKSPACE_ID} from "../../../../../comm
import EnvironmentSelect from "../../definition/components/environment/EnvironmentSelect";
import BatchMove from "../../../track/case/components/BatchMove";
import MsRunMode from "./common/RunMode";
import axios from "axios";
import {
getCustomTableHeader, getCustomTableWidth, getLastTableSortField, saveLastTableSortField
@ -848,6 +849,17 @@ export default {
});
});
},
fileDownload(url, param) {
axios.post(url, param, {responseType: 'blob'})
.then(response => {
let link = document.createElement("a");
link.href = window.URL.createObjectURL(new Blob([response.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"}));
link.download = "场景JMX文件集.zip";
this.result.loading = false;
link.click();
});
},
exportJmx() {
let param = {};
this.buildBatchParam(param);
@ -856,16 +868,9 @@ export default {
return;
}
this.result.loading = true;
this.result = this.$post("/api/automation/export/jmx", param, response => {
this.result.loading = false;
let obj = response.data;
if (obj && obj.length > 0) {
obj.forEach(item => {
downloadFile(item.name + ".jmx", item.jmx);
});
}
});
this.fileDownload("/api/automation/export/jmx", param);
},
getConditions() {
return this.condition;
},