refactor: 导出html改为非xpack

This commit is contained in:
chenjianxing 2021-07-27 13:59:05 +08:00 committed by jianxing
parent 191a74507a
commit abb446dbef
4 changed files with 106 additions and 4 deletions

View File

@ -26,6 +26,8 @@ import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.UUID;
@ -166,4 +168,9 @@ public class TestPlanController {
public TestPlan copy(@PathVariable String id) {
return testPlanService.copy(id);
}
@GetMapping("/report/export/{planId}")
public void exportHtmlReport(@PathVariable String planId, HttpServletResponse response) throws UnsupportedEncodingException {
testPlanService.exportPlanReport(planId, response);
}
}

View File

@ -0,0 +1,41 @@
package io.metersphere.track.dto;
import lombok.Getter;
import lombok.Setter;
import java.util.HashMap;
import java.util.Map;
@Getter
@Setter
public class TestPlanPreviewsDTO {
private static final Map<Integer, Preview> previewMap = new HashMap(6);
static {
previewMap.put(1, new Preview(1, "基础信息", "system"));
previewMap.put(2, new Preview(2, "测试结果", "system"));
previewMap.put(3, new Preview(3, "测试结果分布", "system"));
previewMap.put(4, new Preview(4, "失败用例", "system"));
previewMap.put(5, new Preview(5, "缺陷列表", "system"));
previewMap.put(6, new Preview(6, "自定义模块", "custom"));
}
public static Preview get(Integer id) {
return previewMap.get(id);
}
@Getter
@Setter
public static class Preview {
private String name;
private Integer id;
private String type;
public Preview(Integer id, String name, String type) {
this.name = name;
this.id = id;
this.type = type;
}
}
}

View File

@ -56,6 +56,13 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
@ -88,7 +95,7 @@ public class TestPlanService {
@Resource
TestCaseReportMapper testCaseReportMapper;
@Resource
TestPlanProjectMapper testPlanProjectMapper;
TestCaseReportService testCaseReportService;
@Resource
TestPlanProjectService testPlanProjectService;
@Resource
@ -1186,7 +1193,7 @@ public class TestPlanService {
TestPlanTestCaseExample testPlanTestCaseExample = new TestPlanTestCaseExample();
testPlanTestCaseExample.createCriteria().andPlanIdEqualTo(sourcePlanId);
List<TestPlanTestCase> testPlanTestCases = testPlanTestCaseMapper.selectByExample(testPlanTestCaseExample);
try(SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)){
try (SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
TestPlanTestCaseMapper testCaseMapper = sqlSession.getMapper(TestPlanTestCaseMapper.class);
testPlanTestCases.forEach(testCase -> {
TestPlanTestCaseWithBLOBs testPlanTestCase = new TestPlanTestCaseWithBLOBs();
@ -1254,4 +1261,51 @@ public class TestPlanService {
sqlSession.flushStatements();
}
}
public void exportPlanReport(String planId, HttpServletResponse response) throws UnsupportedEncodingException {
TestPlan testPlan = getTestPlan(planId);
if (StringUtils.isBlank(testPlan.getReportId())) {
MSException.throwException("请先创建报告");
}
TestCaseReport testCaseReport = testCaseReportService.getTestCaseReport(testPlan.getReportId());
String content = testCaseReport.getContent();
JSONArray components = JSONObject.parseObject(content).getJSONArray("components");
List<TestPlanPreviewsDTO.Preview> previews = new ArrayList<>();
components.forEach(item -> {
previews.add(TestPlanPreviewsDTO.get((Integer) item));
});
TestCaseReportMetricDTO metric = getMetric(planId);
render(previews, metric, response);
}
public void render(List<TestPlanPreviewsDTO.Preview> previews, TestCaseReportMetricDTO metric, HttpServletResponse response) throws UnsupportedEncodingException {
response.reset();
response.setContentType("application/octet-stream");
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("test", StandardCharsets.UTF_8.name()));
try (InputStreamReader isr = new InputStreamReader(getClass().getResourceAsStream("/public/plan-report.html"), StandardCharsets.UTF_8);
ServletOutputStream outputStream = response.getOutputStream()) {
BufferedReader bufferedReader = new BufferedReader(isr);
String line = null;
while (null != (line = bufferedReader.readLine())) {
line = line.replace("\"#metric\"", JSONObject.toJSONString(metric));
line = line.replace("\"#preview\"", JSONObject.toJSONString(previews));
line += "\n";
byte[] lineBytes = line.getBytes(StandardCharsets.UTF_8);
int start = 0;
while (start < lineBytes.length) {
if (start + 1024 < lineBytes.length) {
outputStream.write(lineBytes, start, 1024);
} else {
outputStream.write(lineBytes, start, lineBytes.length - start);
}
outputStream.flush();
start += 1024;
}
}
} catch (Exception e) {
MSException.throwException(e.getMessage());
LogUtil.error(e.getMessage(), e);
}
}
}

View File

@ -19,7 +19,7 @@
<el-button :disabled="!isTestManagerOrTestUser" plain size="mini" @click="handleExport(report.name)">
{{$t('test_track.plan_view.export_report')}}
</el-button>
<el-button v-xpack :disabled="!isTestManagerOrTestUser" plain size="mini" @click="handleExportHtml(report.name)">
<el-button :disabled="!isTestManagerOrTestUser" plain size="mini" @click="handleExportHtml(report.name)">
{{'导出HTML'}}
</el-button>
</el-col>
@ -217,7 +217,7 @@
},
handleExportHtml(name) {
let config = {
url: '/export/template/plan/report/' + this.planId,
url: '/test/plan/report/export/' + this.planId,
method: 'get',
responseType: 'blob'
};