From 052c96668e985dec40ed99b2ffdb74ec80c24109 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 21 Dec 2021 21:48:04 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E9=A1=B9=E7=9B=AE=E6=8A=A5=E5=91=8A):=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A1=B9=E7=9B=AE=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化项目报告调用selenium的方法,由一张图表调用一次生成一个图表,改为一次报告调用一次生成一批图表 --- .../reportstatistics/dto/HeadlessRequest.java | 9 ++- .../service/ReportStatisticsService.java | 28 +++++--- .../reportstatistics/utils/ChromeUtils.java | 69 +++++++++++-------- backend/src/main/java/io/metersphere/xpack | 2 +- frontend/src/business/components/xpack | 2 +- 5 files changed, 67 insertions(+), 43 deletions(-) diff --git a/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java b/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java index 171a007233..9dd6c2e48c 100644 --- a/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java +++ b/backend/src/main/java/io/metersphere/reportstatistics/dto/HeadlessRequest.java @@ -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 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); } } diff --git a/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java b/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java index 2c517c2031..47fb8e80c8 100644 --- a/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java +++ b/backend/src/main/java/io/metersphere/reportstatistics/service/ReportStatisticsService.java @@ -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 getImageContentById(List 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 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 returnMap = chromeUtils.getImageInfo(headlessRequest, language); + return returnMap; } public boolean isReportNeedUpdate(ReportStatisticsWithBLOBs model) { diff --git a/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java b/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java index b117db965b..183422536d 100644 --- a/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java +++ b/backend/src/main/java/io/metersphere/reportstatistics/utils/ChromeUtils.java @@ -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 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 getImageInfo(HeadlessRequest request, String langurage) { + Map 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 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; } } diff --git a/backend/src/main/java/io/metersphere/xpack b/backend/src/main/java/io/metersphere/xpack index 25fa8fc0d7..a1e39f9c1b 160000 --- a/backend/src/main/java/io/metersphere/xpack +++ b/backend/src/main/java/io/metersphere/xpack @@ -1 +1 @@ -Subproject commit 25fa8fc0d7972b56f86fb466417ec55588a4812d +Subproject commit a1e39f9c1baf30f52da50ab34ea1bacbfbeff60c diff --git a/frontend/src/business/components/xpack b/frontend/src/business/components/xpack index 4ab83e897b..a2a8dde5be 160000 --- a/frontend/src/business/components/xpack +++ b/frontend/src/business/components/xpack @@ -1 +1 @@ -Subproject commit 4ab83e897bdbc55729fd7418b6a77e73e39b1df9 +Subproject commit a2a8dde5beb470590e7860e9d4376a56ce41d791