use of com.microsoft.playwright.ElementHandle in project Insights by CognizantOneDevOps.
the class GrafanaPDFHandler method printableDashboardAsPdf.
/**
* Generates printable pdf from Grafana dashboard panels.
* @param assessmentReportDTO
* @param grafanaDashboardConfig
* @param incomingTaskMessageJson
* @param exportedFilePath
*/
private synchronized void printableDashboardAsPdf(InsightsAssessmentConfigurationDTO assessmentReportDTO, String exportedFilePath) {
Playwright playwright = null;
try {
playwright = Playwright.create();
long startTime = System.nanoTime();
BrowserType browserType = playwright.chromium();
LaunchOptions launchOptions = new LaunchOptions();
launchOptions.setHeadless(Boolean.TRUE);
launchOptions.setDevtools(Boolean.FALSE);
Browser browser = browserType.launch(launchOptions);
BrowserContext context = browser.newContext();
Page page = context.newPage();
JsonElement config = JsonUtils.parseStringAsJsonElement(pdfconfigDto.getDashboardJson());
int loadTime = config.getAsJsonObject().get("loadTime").getAsInt() * 1000;
log.debug("Worlflow Detail ==== LoadTIme configured for Grafana in milliseconds ===== {} ", loadTime);
String grafanaEndpoint = getGrafanaEndPoint();
log.debug("Worlflow Detail ==== grafanaEndpoint from config ===== {} ", grafanaEndpoint);
JsonArray panelArray = config.getAsJsonObject().get("panelUrls").getAsJsonArray();
List<String> grafanaPanelList = new ArrayList<>(0);
panelArray.forEach(e -> {
String panelUrl = e.getAsString().replace("<GRAFANA_URL>", grafanaEndpoint);
log.debug("Worlflow Detail ==== Panel url ===== {} ", panelUrl);
grafanaPanelList.add(panelUrl);
});
Map<String, String> headers = getGrafanaHeaders(config);
NavigateOptions navigateOptions = new NavigateOptions();
navigateOptions.setWaitUntil(WaitUntilState.NETWORKIDLE);
String dashboardElement = readDashboardElementJson();
log.debug("Worlflow Detail ==== dashboardReportElement.json fetched successfully");
JsonObject dashboardElementJson = JsonUtils.parseStringAsJsonObject(dashboardElement).get("Printable").getAsJsonObject();
JsonArray elementList = dashboardElementJson.get("elements").getAsJsonArray();
List<byte[]> imageList = new ArrayList<>();
for (int idx = 0; idx < grafanaPanelList.size(); idx++) {
page.setExtraHTTPHeaders(headers);
page.waitForTimeout(TRANSITION_TIME);
page.setViewportSize(1200, 800);
page.route("**", route -> route.resume());
page.onRequest(request -> log.debug("Request >> {} {} ", request.method(), request.url()));
page.onResponse(response -> log.debug("Response << {} {} ", response.status(), response.url()));
page.navigate(grafanaPanelList.get(idx), navigateOptions);
Page.WaitForSelectorOptions waitForSelectorOptions = new Page.WaitForSelectorOptions();
waitForSelectorOptions.setState(WaitForSelectorState.ATTACHED);
log.debug("Waiting for panel {} to load completely before screenshot == {} ", idx, Instant.now());
page.waitForTimeout(loadTime);
log.debug("Waiting time completed for dashboard == {} ", Instant.now());
for (JsonElement ele : elementList) {
page.evaluate(ele.getAsString());
}
ElementHandle elementHandle = page.querySelector(dashboardElementJson.get("screenshotElement").getAsString());
page.waitForTimeout(TRANSITION_TIME);
byte[] image = elementHandle.screenshot();
imageList.add(image);
}
String dynamicTemplate = assessmentReportDTO.getPdfReportDirPath() + File.separator + assessmentReportDTO.getAsseementreportname() + "." + ReportEngineUtils.HTML_EXTENSION;
log.debug("Worlflow Detail ==== Generated DynamicTemplate.html name ===== {} ", dynamicTemplate);
prepareHtml(imageList, dynamicTemplate);
page = context.newPage();
page.navigate(new File(dynamicTemplate).getAbsolutePath());
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setPrintBackground(Boolean.TRUE);
pdfOptions.setDisplayHeaderFooter(true);
pdfOptions.setHeaderTemplate(updateLogoImgInHeaderTemplate(false));
pdfOptions.setFooterTemplate(fetchTemplate(FOOTER_HTML));
pdfOptions.setMargin(new Margin().setTop("85").setRight("10").setBottom("5").setLeft("10"));
byte[] dashboardPdf = page.pdf(pdfOptions);
String frontPagePath = assessmentReportDTO.getPdfReportDirPath() + File.separator + MODIFIED_FRONT_PAGE_TEMPLATE;
modifyFrontPageTemplate(frontPagePath);
page = context.newPage();
page.navigate("file:" + new File(frontPagePath).getAbsolutePath());
pdfOptions.setHeaderTemplate(updateLogoImgInHeaderTemplate(true));
pdfOptions.setFooterTemplate(fetchTemplate("frontPagefooter.html"));
byte[] frontPagePdf = page.pdf(pdfOptions);
page.close();
context.close();
browser.close();
byte[] finalPdf = mergePDFFiles(frontPagePdf, dashboardPdf, exportedFilePath);
pdfExecutionUtils.saveToVisualizationContainer(assessmentReportDTO, finalPdf);
long processingTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
log.debug(LOG_MESSAGE, assessmentReportDTO.getExecutionId(), assessmentReportDTO.getWorkflowId(), assessmentReportDTO.getConfigId(), pdfconfigDto.getWorkflowType(), "-", "-", processingTime, PDF_TYPE + pdfconfigDto.getPdfType() + SCHEDULE + pdfconfigDto.getScheduleType() + SOURCE + pdfconfigDto.getSource());
} catch (Exception e) {
log.error("Worlflow Detail ==== Grafana Dashboard export as Printable PDF Completed with error {} ", e.getMessage());
log.error(LOG_MESSAGE, assessmentReportDTO.getExecutionId(), assessmentReportDTO.getWorkflowId(), assessmentReportDTO.getConfigId(), "-", "-", "-", 0, " Grafana Dashboard export as Printable PDF Completed with error " + e.getMessage());
log.error(e);
throw new InsightsJobFailedException(e.getMessage());
} finally {
try {
playwright.close();
} catch (Exception e) {
log.error("Worlflow Detail ==== Unable to close Playwright {} ", e.getMessage());
log.error(LOG_MESSAGE, assessmentReportDTO.getExecutionId(), assessmentReportDTO.getWorkflowId(), assessmentReportDTO.getConfigId(), "-", "-", "-", 0, " Unable to close Playwright " + e.getMessage());
}
}
}
use of com.microsoft.playwright.ElementHandle in project playwright-java by microsoft.
the class ElementHandleImpl method querySelectorAll.
@Override
public List<ElementHandle> querySelectorAll(String selector) {
return withLogging("ElementHandle.<", () -> {
JsonObject params = new JsonObject();
params.addProperty("selector", selector);
JsonElement json = sendMessage("querySelectorAll", params);
JsonArray elements = json.getAsJsonObject().getAsJsonArray("elements");
if (elements == null) {
return null;
}
List<ElementHandle> handles = new ArrayList<>();
for (JsonElement item : elements) {
handles.add(connection.getExistingObject(item.getAsJsonObject().get("guid").getAsString()));
}
return handles;
});
}
use of com.microsoft.playwright.ElementHandle in project playwright-java by microsoft.
the class Serialization method toProtocol.
static JsonArray toProtocol(ElementHandle[] handles) {
JsonArray jsonElements = new JsonArray();
for (ElementHandle handle : handles) {
JsonObject jsonHandle = new JsonObject();
jsonHandle.addProperty("guid", ((ElementHandleImpl) handle).guid);
jsonElements.add(jsonHandle);
}
return jsonElements;
}
Aggregations