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.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Map;
@Getter
@Setter
public class HeadlessRequest {
private String url;
private Map<String, String> urlMap;
private String remoteDriverUrl;
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.utils.ChromeUtils;
import io.metersphere.service.SystemParameterService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.*;
/**
* @author song.tianyang
@ -73,6 +71,9 @@ public class ReportStatisticsService {
public ReportStatisticsWithBLOBs selectById(String id) {
ReportStatisticsWithBLOBs blob = reportStatisticsMapper.selectByPrimaryKey(id);
if (blob == null) {
return null;
}
JSONObject selectOption = JSONObject.parseObject(blob.getSelectOption());
JSONObject dataOption = JSONObject.parseObject(blob.getDataOption());
boolean isReportNeedUpdate = this.isReportNeedUpdate(blob);
@ -250,7 +251,11 @@ public class ReportStatisticsService {
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();
HeadlessRequest headlessRequest = new HeadlessRequest();
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
@ -261,11 +266,16 @@ public class ReportStatisticsService {
platformUrl = baseInfo.getUrl();
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);
String imageData = chromeUtils.getImageInfo(headlessRequest, language);
return imageData;
Map<String, String> returnMap = chromeUtils.getImageInfo(headlessRequest, language);
return returnMap;
}
public boolean isReportNeedUpdate(ReportStatisticsWithBLOBs model) {

View File

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

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

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