fix(项目报告): 优化项目报告

优化项目报告调用selenium的方法,由一张图表调用一次生成一个图表,改为一次报告调用一次生成一批图表
This commit is contained in:
song-tianyang 2021-12-21 21:48:04 +08:00 committed by song-tianyang
parent 06360cbfda
commit 052c96668e
5 changed files with 67 additions and 43 deletions

View File

@ -2,15 +2,20 @@ package io.metersphere.reportstatistics.dto;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Map;
@Getter @Getter
@Setter @Setter
public class HeadlessRequest { public class HeadlessRequest {
private String url; private Map<String, String> urlMap;
private String remoteDriverUrl; private String remoteDriverUrl;
public boolean isEmpty() { public boolean isEmpty() {
return StringUtils.isEmpty(this.url) || StringUtils.isEmpty(this.remoteDriverUrl); return MapUtils.isEmpty(this.urlMap) || StringUtils.isEmpty(this.remoteDriverUrl);
} }
} }

View File

@ -16,15 +16,13 @@ import io.metersphere.reportstatistics.dto.table.TestCaseCountTableItemDataDTO;
import io.metersphere.reportstatistics.dto.table.TestCaseCountTableRowDTO; import io.metersphere.reportstatistics.dto.table.TestCaseCountTableRowDTO;
import io.metersphere.reportstatistics.utils.ChromeUtils; import io.metersphere.reportstatistics.utils.ChromeUtils;
import io.metersphere.service.SystemParameterService; import io.metersphere.service.SystemParameterService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.UUID;
/** /**
* @author song.tianyang * @author song.tianyang
@ -73,6 +71,9 @@ public class ReportStatisticsService {
public ReportStatisticsWithBLOBs selectById(String id) { public ReportStatisticsWithBLOBs selectById(String id) {
ReportStatisticsWithBLOBs blob = reportStatisticsMapper.selectByPrimaryKey(id); ReportStatisticsWithBLOBs blob = reportStatisticsMapper.selectByPrimaryKey(id);
if (blob == null) {
return null;
}
JSONObject selectOption = JSONObject.parseObject(blob.getSelectOption()); JSONObject selectOption = JSONObject.parseObject(blob.getSelectOption());
JSONObject dataOption = JSONObject.parseObject(blob.getDataOption()); JSONObject dataOption = JSONObject.parseObject(blob.getDataOption());
boolean isReportNeedUpdate = this.isReportNeedUpdate(blob); boolean isReportNeedUpdate = this.isReportNeedUpdate(blob);
@ -250,7 +251,11 @@ public class ReportStatisticsService {
return reportStatisticsMapper.selectByExample(example); return reportStatisticsMapper.selectByExample(example);
} }
public String getImageContentById(ReportStatisticsWithBLOBs reportRecordId, String language) { public Map<String, String> getImageContentById(List<ReportStatisticsWithBLOBs> reportRecordIdList, String language) {
if (CollectionUtils.isEmpty(reportRecordIdList)) {
return new HashMap<>();
}
ChromeUtils chromeUtils = ChromeUtils.getInstance(); ChromeUtils chromeUtils = ChromeUtils.getInstance();
HeadlessRequest headlessRequest = new HeadlessRequest(); HeadlessRequest headlessRequest = new HeadlessRequest();
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo(); BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
@ -261,11 +266,16 @@ public class ReportStatisticsService {
platformUrl = baseInfo.getUrl(); platformUrl = baseInfo.getUrl();
remoteDriverUrl = baseInfo.getSeleniumDockerUrl(); remoteDriverUrl = baseInfo.getSeleniumDockerUrl();
} }
platformUrl += "/echartPic?shareId=" + reportRecordId.getId();
headlessRequest.setUrl(platformUrl); Map<String, String> urlMap = new HashMap<>();
for (ReportStatisticsWithBLOBs blob : reportRecordIdList) {
String url = platformUrl + "/echartPic?shareId=" + blob.getId();
urlMap.put(blob.getId(), url);
}
headlessRequest.setUrlMap(urlMap);
headlessRequest.setRemoteDriverUrl(remoteDriverUrl); headlessRequest.setRemoteDriverUrl(remoteDriverUrl);
String imageData = chromeUtils.getImageInfo(headlessRequest, language); Map<String, String> returnMap = chromeUtils.getImageInfo(headlessRequest, language);
return imageData; return returnMap;
} }
public boolean isReportNeedUpdate(ReportStatisticsWithBLOBs model) { public boolean isReportNeedUpdate(ReportStatisticsWithBLOBs model) {

View File

@ -23,18 +23,18 @@ public class ChromeUtils {
return chromeUtils; return chromeUtils;
} }
private synchronized WebDriver genWebDriver(HeadlessRequest headlessRequest, String language) { private synchronized WebDriver genWebDriver(String seleniumUrl, String language) {
if (headlessRequest.isEmpty()) { if (StringUtils.isEmpty(seleniumUrl)) {
LogUtil.error("Headless request is null! " + JSON.toJSONString(headlessRequest)); LogUtil.error("Headless request is null! " + seleniumUrl);
return null; return null;
} }
//初始化一个chrome浏览器实例driver //初始化一个chrome浏览器实例driver
ChromeOptions options = new ChromeOptions(); ChromeOptions options = new ChromeOptions();
if (StringUtils.isEmpty(language)) { if (StringUtils.isEmpty(language)) {
language = "zh-cn"; language = "zh_cn";
} }
if (StringUtils.equalsAnyIgnoreCase(language, "zh-cn")) { if (StringUtils.equalsAnyIgnoreCase(language, "zh_cn")) {
Map<String, Object> optionMap = new HashMap<>(); Map<String, Object> optionMap = new HashMap<>();
optionMap.put("intl.accept_languages", "zh-CN,en,en_US"); optionMap.put("intl.accept_languages", "zh-CN,en,en_US");
options.setExperimentalOption("prefs", optionMap); options.setExperimentalOption("prefs", optionMap);
@ -46,9 +46,7 @@ public class ChromeUtils {
WebDriver driver = null; WebDriver driver = null;
try { try {
driver = new RemoteWebDriver(new URL(headlessRequest.getRemoteDriverUrl()), options); driver = new RemoteWebDriver(new URL(seleniumUrl), options);
driver.get(headlessRequest.getUrl());
driver.manage().window().fullscreen();
} catch (Exception e) { } catch (Exception e) {
if (driver != null) { if (driver != null) {
driver.quit(); driver.quit();
@ -59,31 +57,42 @@ public class ChromeUtils {
return driver; return driver;
} }
public synchronized String getImageInfo(HeadlessRequest request, String langurage) { public synchronized Map<String, String> getImageInfo(HeadlessRequest request, String langurage) {
WebDriver driver = this.genWebDriver(request, langurage); Map<String, String> returnMap = new HashMap<>();
String files = null; if (request.isEmpty()) {
return returnMap;
}
WebDriver driver = this.genWebDriver(request.getRemoteDriverUrl(), langurage);
if (driver != null) { if (driver != null) {
try { for (Map.Entry<String, String> urlEntry : request.getUrlMap().entrySet()) {
//预留echart动画的加载时间 String id = urlEntry.getKey();
Thread.sleep(3 * 1000); String url = urlEntry.getValue();
String js = "var chartsCanvas = document.getElementById('picChart').getElementsByTagName('canvas')[0];" + try {
"var imageUrl = null;" + driver.get(url);
"if (chartsCanvas!= null) {" + driver.manage().window().fullscreen();
" imageUrl = chartsCanvas && chartsCanvas.toDataURL('image/png');" + //预留echart动画的加载时间
"return imageUrl;" + Thread.sleep(3 * 1000);
"}"; String js = "var chartsCanvas = document.getElementById('picChart').getElementsByTagName('canvas')[0];" +
files = ((JavascriptExecutor) driver).executeScript(js).toString(); "var imageUrl = null;" +
} catch (Exception e) { "if (chartsCanvas!= null) {" +
LogUtil.error(e); " imageUrl = chartsCanvas && chartsCanvas.toDataURL('image/png');" +
} finally { "return imageUrl;" +
"}";
String files = ((JavascriptExecutor) driver).executeScript(js).toString();
if (StringUtils.isNotEmpty(files)) {
returnMap.put(id, files);
}
Thread.sleep(1 * 1000);
} catch (Exception e) {
LogUtil.error(e);
}
}
if (driver != null) {
driver.quit(); driver.quit();
} }
} }
if (StringUtils.isNotEmpty(files)) { return returnMap;
return files;
} else {
LogUtil.error("获取报表图片失败!参数:" + JSON.toJSONString(request));
return null;
}
} }
} }

@ -1 +1 @@
Subproject commit 25fa8fc0d7972b56f86fb466417ec55588a4812d Subproject commit a1e39f9c1baf30f52da50ab34ea1bacbfbeff60c

@ -1 +1 @@
Subproject commit 4ab83e897bdbc55729fd7418b6a77e73e39b1df9 Subproject commit a2a8dde5beb470590e7860e9d4376a56ce41d791