Search in sources :

Example 46 with InsightsJobFailedException

use of com.cognizant.devops.platformreports.exception.InsightsJobFailedException in project Insights by CognizantOneDevOps.

the class GrafanaPDFHandler method prepareAndExportPDFFile.

/**
 * Method to generate grafana dashboard as PDF and save in database.
 *
 * @param assessmentReportDTO
 */
private void prepareAndExportPDFFile(InsightsAssessmentConfigurationDTO assessmentReportDTO) {
    try {
        long startTime = System.nanoTime();
        log.debug("Worlflow Detail ==== GrafanaDashboardPdfConfig from UI ===== ");
        assessmentReportDTO.setAsseementreportname(pdfconfigDto.getTitle());
        String exportedFilePath = assessmentReportDTO.getPdfReportDirPath() + File.separator + (assessmentReportDTO.getWorkflowId() + "_" + assessmentReportDTO.getExecutionId()) + "." + ReportEngineUtils.REPORT_TYPE;
        assessmentReportDTO.setPdfExportedFilePath(exportedFilePath);
        String pdfType = pdfconfigDto.getPdfType();
        if (pdfType.equals(DASHBOARD)) {
            grafanaDashboardAsPdf(assessmentReportDTO, exportedFilePath);
        } else {
            printableDashboardAsPdf(assessmentReportDTO, exportedFilePath);
        }
        long processingTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
        log.debug("Worlflow Detail ==== PDF generation completed for Type : ===== {} ", pdfType);
        log.debug(LOG_MESSAGE, assessmentReportDTO.getExecutionId(), assessmentReportDTO.getWorkflowId(), assessmentReportDTO.getConfigId(), pdfconfigDto.getWorkflowType(), "-", "-", processingTime, PDF_TYPE + pdfconfigDto.getPdfType() + SCHEDULE + pdfconfigDto.getScheduleType() + SOURCE + pdfconfigDto.getSource() + " PDF generation completed");
        deletePDFDirectory(new File(assessmentReportDTO.getPdfReportDirPath()));
    } catch (Exception e) {
        log.error("Workflow Detail ==== error while processing pdf data ", e);
        log.error(LOG_MESSAGE, assessmentReportDTO.getExecutionId(), assessmentReportDTO.getWorkflowId(), assessmentReportDTO.getConfigId(), "-", "-", "-", 0, " unable to prepare pdf data " + e.getMessage());
        throw new InsightsJobFailedException(" unable to prepare pdf data " + e);
    }
}
Also used : InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) File(java.io.File) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) IOException(java.io.IOException)

Example 47 with InsightsJobFailedException

use of com.cognizant.devops.platformreports.exception.InsightsJobFailedException in project Insights by CognizantOneDevOps.

the class GrafanaPDFHandler method grafanaDashboardAsPdf.

/**
 * Uses playwright to create current url of grafana dashboard as pdf
 * @param assessmentReportDTO
 * @param grafanaDashboardConfig
 * @param incomingTaskMessageJson
 * @param exportedFilePath
 */
private void grafanaDashboardAsPdf(InsightsAssessmentConfigurationDTO assessmentReportDTO, String exportedFilePath) {
    JsonElement config = JsonUtils.parseStringAsJsonElement(pdfconfigDto.getDashboardJson());
    String grafanaEndpoint = getGrafanaEndPoint();
    int loadTime = config.getAsJsonObject().get("loadTime").getAsInt() * 1000;
    log.debug("Worlflow Detail ==== LoadTIme configured for Grafana in milliseconds ===== {} ", loadTime);
    String grafanaUrl = config.getAsJsonObject().get("dashUrl").getAsString().replace("<GRAFANA_URL>", grafanaEndpoint);
    log.debug("Worlflow Detail ==== grafanadashboard Url ===== {} ", grafanaUrl);
    Playwright playwright = null;
    String pageContent = "";
    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();
        Map<String, String> headers = getGrafanaHeaders(config);
        page.setExtraHTTPHeaders(headers);
        page.onRequest(request -> log.debug("Request >> {} {} ", request.method(), request.url()));
        page.onResponse(response -> log.debug("Response << {} {} ", response.status(), response.url()));
        NavigateOptions navigateOptions = new NavigateOptions();
        navigateOptions.setWaitUntil(WaitUntilState.NETWORKIDLE);
        page.navigate(grafanaUrl, navigateOptions);
        pageContent = page.content();
        page.waitForLoadState();
        log.debug("Worlflow Detail ==== Waiting for dashboard to load == loadTime {} time {} ", loadTime, Instant.now());
        page.waitForTimeout(loadTime);
        log.debug("Worlflow Detail ====Waiting exit post configured time to load Dashboard == {} ", Instant.now());
        String dashboardElement = readDashboardElementJson();
        log.debug("Worlflow Detail ==== reading elements from dashboardReportElement.json");
        JsonObject dashboardElementJson = JsonUtils.parseStringAsJsonObject(dashboardElement).get("dashboard").getAsJsonObject();
        JsonArray elementList = dashboardElementJson.get("elements").getAsJsonArray();
        for (JsonElement ele : elementList) {
            page.evaluate(ele.getAsString());
        }
        Object width = page.evaluate(dashboardElementJson.get("width").getAsString());
        log.debug("Worlflow Detail ==== Grafana grid Width ===== {} ", width);
        int dashboardwidth = width instanceof Integer ? (int) width + 40 : Integer.parseInt(width.toString());
        log.debug("Worlflow Detail ==== Grafana Dashboard width ===== {} ", dashboardwidth);
        Object height = page.evaluate(dashboardElementJson.get("height").getAsString());
        log.debug("Worlflow Detail ==== Grafana grid height ===== {} ", height);
        int dashboardlength = height instanceof Integer ? (int) height + 160 : Integer.parseInt(height.toString());
        log.debug("Worlflow Detail ==== Grafana Dashboard length ===== {} ", dashboardlength);
        page.evaluate(dashboardElementJson.get("screenshotElement").getAsString());
        log.debug("Waiting for dashboard to load completely before screenshot == {} ", Instant.now());
        page.waitForTimeout(loadTime);
        log.debug("Waiting time completed for dashboard == {} ", Instant.now());
        page.waitForLoadState(LoadState.NETWORKIDLE);
        PdfOptions pdfOptions = new PdfOptions();
        pdfOptions.setPrintBackground(Boolean.TRUE);
        pdfOptions.setWidth(dashboardwidth + "px");
        pdfOptions.setHeight(dashboardlength + "px");
        pdfOptions.setScale(1);
        pdfOptions.setDisplayHeaderFooter(true);
        pdfOptions.setHeaderTemplate(updateLogoImgInHeaderTemplate(false));
        pdfOptions.setFooterTemplate(fetchTemplate(FOOTER_HTML));
        pdfOptions.setMargin(new Margin().setTop("90").setRight("0").setBottom("50").setLeft("0"));
        page.emulateMedia(new Page.EmulateMediaOptions().setMedia(Media.SCREEN));
        byte[] dashboardPdf = page.pdf(pdfOptions);
        log.debug("Worlflow Detail ==== dashboard pdf generated ===");
        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"));
        pdfOptions.setHeight("1600px");
        byte[] frontPagePdf = page.pdf(pdfOptions);
        page.close();
        context.close();
        browser.close();
        byte[] finalPdf = mergePDFFiles(frontPagePdf, dashboardPdf, exportedFilePath);
        if (Boolean.FALSE.equals(pdfconfigDto.getIsAssessmentReport())) {
            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 PDF Completed with error {} ", e.getMessage());
        log.error(LOG_MESSAGE, assessmentReportDTO.getExecutionId(), assessmentReportDTO.getWorkflowId(), assessmentReportDTO.getConfigId(), "-", "-", "-", 0, " Grafana Dashboard export as PDF Completed with error " + e.getMessage());
        log.error("Dashboard page content ========== {} ", pageContent);
        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());
        }
    }
}
Also used : Playwright(com.microsoft.playwright.Playwright) JsonObject(com.google.gson.JsonObject) PdfOptions(com.microsoft.playwright.Page.PdfOptions) Page(com.microsoft.playwright.Page) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) LaunchOptions(com.microsoft.playwright.BrowserType.LaunchOptions) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) BrowserType(com.microsoft.playwright.BrowserType) JsonElement(com.google.gson.JsonElement) BrowserContext(com.microsoft.playwright.BrowserContext) NavigateOptions(com.microsoft.playwright.Page.NavigateOptions) JsonObject(com.google.gson.JsonObject) File(java.io.File) Browser(com.microsoft.playwright.Browser) Margin(com.microsoft.playwright.options.Margin)

Example 48 with InsightsJobFailedException

use of com.cognizant.devops.platformreports.exception.InsightsJobFailedException in project Insights by CognizantOneDevOps.

the class GrafanaPDFHandler method mergePDFFiles.

/**
 * Method to merge two PDF files, save in destination folder and returns merged pdf byte array.
 *
 * @param pdf1
 * @param pdf2
 * @param destinationFilePath
 * @return
 */
private byte[] mergePDFFiles(byte[] pdf1, byte[] pdf2, String destinationFilePath) {
    try {
        PDFMergerUtility merger = new PDFMergerUtility();
        merger.addSource(new ByteArrayInputStream(pdf1));
        merger.addSource(new ByteArrayInputStream(pdf2));
        merger.setDestinationFileName(destinationFilePath);
        merger.mergeDocuments(null);
        log.debug("Worlflow Detail ==== Pdf files merged successfully ===== ");
        FileInputStream inputStream = new FileInputStream(destinationFilePath);
        return IOUtils.toByteArray(inputStream);
    } catch (Exception e) {
        log.error("Workflow Detail ==== Error while merging PDF Files  ", e);
        throw new InsightsJobFailedException(e.getMessage());
    }
}
Also used : InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) ByteArrayInputStream(java.io.ByteArrayInputStream) PDFMergerUtility(org.apache.pdfbox.multipdf.PDFMergerUtility) FileInputStream(java.io.FileInputStream) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) IOException(java.io.IOException)

Example 49 with InsightsJobFailedException

use of com.cognizant.devops.platformreports.exception.InsightsJobFailedException in project Insights by CognizantOneDevOps.

the class GrafanaPDFHandler method preparePDFDTO.

private void preparePDFDTO(InsightsAssessmentConfigurationDTO assessmentReportDTO) {
    try {
        GrafanaDashboardPdfConfig grafanaDashboardPdfConfig = grafanaDashboardConfigDAL.fetchGrafanaDashboardDetailsByWorkflowId(assessmentReportDTO.getWorkflowId());
        if (grafanaDashboardPdfConfig != null) {
            JsonObject dashboardConfigJson = JsonUtils.parseStringAsJsonObject(grafanaDashboardPdfConfig.getDashboardJson());
            pdfconfigDto.setDashboardJson(grafanaDashboardPdfConfig.getDashboardJson());
            pdfconfigDto.setOrganisation(dashboardConfigJson.get(ORGANISATION).getAsString());
            pdfconfigDto.setPdfType(grafanaDashboardPdfConfig.getPdfType());
            pdfconfigDto.setScheduleType(grafanaDashboardPdfConfig.getScheduleType());
            pdfconfigDto.setSource(grafanaDashboardPdfConfig.getSource());
            pdfconfigDto.setTitle(grafanaDashboardPdfConfig.getTitle());
            pdfconfigDto.setVariables(grafanaDashboardPdfConfig.getVariables());
            pdfconfigDto.setWorkflowType(grafanaDashboardPdfConfig.getWorkflowConfig().getWorkflowType());
        } else {
            InsightsWorkflowConfiguration workflowConfig = workflowDAL.getWorkflowConfigByWorkflowId(assessmentReportDTO.getWorkflowId());
            InsightsAssessmentConfiguration assessmentConfig = workflowConfig.getAssessmentConfig();
            if (workflowConfig != null && assessmentConfig != null) {
                JsonObject dashboardConfigJson = JsonUtils.parseStringAsJsonObject(assessmentConfig.getAdditionalDetail());
                pdfconfigDto.setDashboardJson(assessmentConfig.getAdditionalDetail());
                pdfconfigDto.setOrganisation(dashboardConfigJson.get(ORGANISATION).getAsString());
                pdfconfigDto.setPdfType(dashboardConfigJson.get("pdfType").getAsString());
                pdfconfigDto.setScheduleType(workflowConfig.getScheduleType());
                pdfconfigDto.setSource(dashboardConfigJson.get("source").getAsString());
                pdfconfigDto.setTitle(assessmentConfig.getAsseementReportDisplayName());
                pdfconfigDto.setVariables(dashboardConfigJson.get("variables").getAsString());
                pdfconfigDto.setWorkflowType(workflowConfig.getWorkflowType());
                pdfconfigDto.setIsAssessmentReport(true);
            }
        }
    } catch (Exception e) {
        log.error("Workflow Detail ==== error while processing pdf data ", e);
        log.error(LOG_MESSAGE, assessmentReportDTO.getExecutionId(), assessmentReportDTO.getWorkflowId(), assessmentReportDTO.getConfigId(), "-", "-", "-", 0, " exeception in preparePDFDTO  " + e.getMessage());
        throw new InsightsJobFailedException(" exeception in preparePDFDTO " + e);
    }
}
Also used : GrafanaDashboardPdfConfig(com.cognizant.devops.platformdal.grafana.pdf.GrafanaDashboardPdfConfig) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) InsightsWorkflowConfiguration(com.cognizant.devops.platformdal.workflow.InsightsWorkflowConfiguration) JsonObject(com.google.gson.JsonObject) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) IOException(java.io.IOException) InsightsAssessmentConfiguration(com.cognizant.devops.platformdal.assessmentreport.InsightsAssessmentConfiguration)

Example 50 with InsightsJobFailedException

use of com.cognizant.devops.platformreports.exception.InsightsJobFailedException in project Insights by CognizantOneDevOps.

the class LedgerPDFChartHandler method prepareAndExportPDFFile.

private void prepareAndExportPDFFile(InsightsAssessmentConfigurationDTO assessmentReportDTO) {
    try {
        String exportedFilePath = assessmentReportDTO.getPdfReportDirPath() + File.separator + assessmentReportDTO.getAsseementreportname() + "." + ReportEngineUtils.REPORT_TYPE;
        assessmentReportDTO.setPdfExportedFilePath(exportedFilePath);
        PdfTableUtil pdfTableUtil = new PdfTableUtil();
        byte[] pdfResponse = pdfTableUtil.generateLedgerReport(assessmentReportDTO.getLedgerRecords(), assessmentReportDTO.getAsseementreportname() + ".pdf");
        File extractedPdfFile = new File(exportedFilePath);
        savePDFFile(extractedPdfFile, pdfResponse);
        pdfExecutionUtils.saveToVisualizationContainer(assessmentReportDTO, pdfResponse);
    } catch (Exception e) {
        log.error("Workflow Detail ==== error while processing pdf data ", e);
        throw new InsightsJobFailedException(" unable to prepare pdf data " + e);
    }
}
Also used : InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException) PdfTableUtil(com.cognizant.devops.platformauditing.util.PdfTableUtil) File(java.io.File) InsightsJobFailedException(com.cognizant.devops.platformreports.exception.InsightsJobFailedException)

Aggregations

InsightsJobFailedException (com.cognizant.devops.platformreports.exception.InsightsJobFailedException)63 IOException (java.io.IOException)31 JsonObject (com.google.gson.JsonObject)29 InsightsCustomException (com.cognizant.devops.platformcommons.exception.InsightsCustomException)21 ArrayList (java.util.ArrayList)16 File (java.io.File)14 JsonArray (com.google.gson.JsonArray)12 HashMap (java.util.HashMap)11 InsightsKPIResultDetails (com.cognizant.devops.platformreports.assessment.datamodel.InsightsKPIResultDetails)8 List (java.util.List)8 InsightsContentDetail (com.cognizant.devops.platformreports.assessment.datamodel.InsightsContentDetail)7 ReportEngineEnum (com.cognizant.devops.platformreports.assessment.util.ReportEngineEnum)7 ExecutionException (java.util.concurrent.ExecutionException)7 ContentConfigDefinition (com.cognizant.devops.platformreports.assessment.datamodel.ContentConfigDefinition)6 InsightsAssessmentConfigurationDTO (com.cognizant.devops.platformreports.assessment.datamodel.InsightsAssessmentConfigurationDTO)5 FileNotFoundException (java.io.FileNotFoundException)5 NoSuchFileException (java.nio.file.NoSuchFileException)5 Map (java.util.Map)5 InsightsContentConfig (com.cognizant.devops.platformdal.assessmentreport.InsightsContentConfig)4 InsightsReportVisualizationContainer (com.cognizant.devops.platformdal.assessmentreport.InsightsReportVisualizationContainer)4