Search in sources :

Example 1 with RptMonitoringResultDto

use of eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class MonitoringReportExecutor method generateData.

@Override
protected IdmAttachmentDto generateData(RptReportDto report) {
    // prepare temp file for json stream
    File temp = getAttachmentManager().createTempFile();
    // 
    try (FileOutputStream outputStream = new FileOutputStream(temp)) {
        // write into json stream
        JsonGenerator jGenerator = getMapper().getFactory().createGenerator(outputStream, JsonEncoding.UTF8);
        try {
            // json will be array of identities
            jGenerator.writeStartArray();
            // 
            List<IdmMonitoringResultDto> lastResults = monitoringManager.getLastResults(null, null, IdmBasePermission.READ).getContent();
            counter = 0L;
            count = Long.valueOf(lastResults.size());
            boolean canContinue = true;
            for (IdmMonitoringResultDto result : lastResults) {
                IdmMonitoringDto monitoring = getLookupService().lookupEmbeddedDto(result, IdmMonitoringResult_.monitoring);
                // 
                RptMonitoringResultDto resultDto = new RptMonitoringResultDto(result);
                resultDto.setResultMessage(result.getResult().getModel() == null ? null : result.getResult().getModel().getMessage());
                resultDto.setOwnerType(result.getOwnerType());
                resultDto.setOwnerId(result.getOwnerId());
                resultDto.setValue(result.getValue());
                resultDto.setLevel(result.getLevel() == null ? NotificationLevel.SUCCESS : result.getLevel());
                resultDto.setEvaluatorType(result.getEvaluatorType());
                resultDto.setEvaluatorDescription(monitoring.getDescription());
                resultDto.setInstanceId(result.getInstanceId());
                // 
                // write dto into json
                getMapper().writeValue(jGenerator, resultDto);
                // 
                // supports cancel report generating (report extends long running task)
                ++counter;
                canContinue = updateState();
                if (!canContinue) {
                    break;
                }
            }
            // 
            // close array of identities
            jGenerator.writeEndArray();
        } finally {
            // close json stream
            jGenerator.close();
        }
        // save create temp file with array of identities in json as attachment
        return createAttachment(report, new FileInputStream(temp));
    } catch (IOException ex) {
        throw new ReportGenerateException(report.getName(), ex);
    } finally {
        FileUtils.deleteQuietly(temp);
    }
}
Also used : IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) FileOutputStream(java.io.FileOutputStream) RptMonitoringResultDto(eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) ReportGenerateException(eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException) File(java.io.File) FileInputStream(java.io.FileInputStream) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 2 with RptMonitoringResultDto

use of eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class MonitoringReportExecutorIntegrationTest method testExecutor.

@Test
@Transactional
public void testExecutor() throws IOException {
    IdmMonitoringDto monitoring = new IdmMonitoringDto();
    monitoring.setCheckPeriod(0L);
    monitoring.setEvaluatorType(AutowireHelper.getTargetType(h2DatabaseMonitoringEvaluator));
    monitoring.setInstanceId(configurationService.getInstanceId());
    monitoring = monitoringService.save(monitoring);
    monitoringManager.execute(monitoring);
    // 
    // generate report
    RptReportDto report = reportExecutor.generate(new RptReportDto(UUID.randomUUID()));
    Assert.assertNotNull(report.getData());
    List<RptMonitoringResultDto> results = mapper.readValue(attachmentManager.getAttachmentData(report.getData()), new TypeReference<List<RptMonitoringResultDto>>() {
    });
    // 
    // test
    Assert.assertFalse(results.isEmpty());
    Assert.assertTrue(results.stream().anyMatch(r -> r.getEvaluatorType().equals(AutowireHelper.getTargetType(h2DatabaseMonitoringEvaluator))));
    // 
    attachmentManager.deleteAttachments(report);
}
Also used : MonitoringManager(eu.bcvsolutions.idm.core.monitoring.api.service.MonitoringManager) IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) IdmMonitoringService(eu.bcvsolutions.idm.core.monitoring.api.service.IdmMonitoringService) RptMonitoringResultDto(eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto) AttachmentManager(eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager) H2DatabaseMonitoringEvaluator(eu.bcvsolutions.idm.core.monitoring.service.impl.H2DatabaseMonitoringEvaluator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Autowired(org.springframework.beans.factory.annotation.Autowired) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) ConfigurationService(eu.bcvsolutions.idm.core.api.service.ConfigurationService) RptReportDto(eu.bcvsolutions.idm.rpt.api.dto.RptReportDto) AutowireHelper(eu.bcvsolutions.idm.core.api.utils.AutowireHelper) List(java.util.List) After(org.junit.After) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Assert(org.junit.Assert) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Before(org.junit.Before) Transactional(org.springframework.transaction.annotation.Transactional) IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) RptMonitoringResultDto(eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto) List(java.util.List) RptReportDto(eu.bcvsolutions.idm.rpt.api.dto.RptReportDto) Test(org.junit.Test) AbstractIntegrationTest(eu.bcvsolutions.idm.test.api.AbstractIntegrationTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with RptMonitoringResultDto

use of eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class MonitoringReportXlsxRenderer method render.

@Override
public InputStream render(RptReportDto report) {
    try {
        // read json stream
        JsonParser jParser = getMapper().getFactory().createParser(getReportData(report));
        XSSFWorkbook workbook = new XSSFWorkbook();
        CreationHelper createHelper = workbook.getCreationHelper();
        XSSFSheet sheet = workbook.createSheet("Report");
        sheet.setDefaultColumnWidth(15);
        // header
        XSSFFont headerFont = workbook.createFont();
        headerFont.setBold(true);
        headerFont.setFontHeightInPoints((short) 15);
        // 
        Row row = sheet.createRow(2);
        row.setHeightInPoints((short) 20);
        Cell cell = row.createCell(0);
        XSSFRichTextString headerColumn = new XSSFRichTextString();
        headerColumn.append("Detail", headerFont);
        cell.setCellValue(headerColumn);
        cell = row.createCell(1);
        headerColumn = new XSSFRichTextString();
        headerColumn.append("Level", headerFont);
        cell.setCellValue(headerColumn);
        cell = row.createCell(2);
        headerColumn = new XSSFRichTextString();
        headerColumn.append("Result message", headerFont);
        cell.setCellValue(headerColumn);
        cell = row.createCell(3);
        headerColumn = new XSSFRichTextString();
        headerColumn.append("Value", headerFont);
        cell.setCellValue(headerColumn);
        cell = row.createCell(4);
        headerColumn = new XSSFRichTextString();
        headerColumn.append("Monitoring type", headerFont);
        cell.setCellValue(headerColumn);
        // 
        int rowNum = 3;
        NotificationLevel summaryLevel = NotificationLevel.SUCCESS;
        // 
        XSSFCellStyle errorStyle = workbook.createCellStyle();
        errorStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        errorStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 
        XSSFCellStyle warningStyle = workbook.createCellStyle();
        warningStyle.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());
        warningStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        // 
        XSSFCellStyle defaultStyle = workbook.createCellStyle();
        // json is array of identities
        if (jParser.nextToken() == JsonToken.START_ARRAY) {
            // write single identity
            while (jParser.nextToken() == JsonToken.START_OBJECT) {
                RptMonitoringResultDto monitoringResult = getMapper().readValue(jParser, RptMonitoringResultDto.class);
                String[] monitoringEvaluatorType = StringUtils.split(monitoringResult.getEvaluatorType(), '.');
                String frontendUrl = configurationService.getFrontendUrl(String.format("monitoring/monitoring-results/%s", monitoringResult.getId()));
                NotificationLevel level = monitoringResult.getLevel();
                // 
                XSSFCellStyle cellStyle = null;
                if (level == NotificationLevel.ERROR) {
                    cellStyle = errorStyle;
                } else if (level == NotificationLevel.WARNING) {
                    cellStyle = warningStyle;
                } else {
                    cellStyle = defaultStyle;
                }
                // 
                if (summaryLevel.ordinal() < level.ordinal()) {
                    summaryLevel = level;
                }
                // 
                row = sheet.createRow(rowNum++);
                cell = row.createCell(0);
                cell.setCellValue(frontendUrl);
                Hyperlink link = createHelper.createHyperlink(XSSFHyperlink.LINK_URL);
                link.setAddress(frontendUrl);
                cell.setHyperlink(link);
                cell.setCellValue("Show detail");
                cell = row.createCell(1);
                cell.setCellStyle(cellStyle);
                cell.setCellValue(level.name());
                cell = row.createCell(2);
                cell.setCellValue(monitoringResult.getResultMessage());
                cell = row.createCell(3);
                cell.setCellValue(monitoringResult.getValue());
                cell = row.createCell(4);
                cell.setCellValue(String.format("%s - %s", monitoringEvaluatorType[monitoringEvaluatorType.length - 1], monitoringResult.getEvaluatorDescription()));
            }
        }
        // set auto size column
        for (int index = 0; index <= 4; index++) {
            sheet.autoSizeColumn(index);
        }
        // 
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));
        row = sheet.createRow(0);
        row.setHeightInPoints((short) 20);
        cell = row.createCell(0);
        headerColumn = new XSSFRichTextString();
        if (summaryLevel == NotificationLevel.ERROR) {
            headerColumn.append("Status CzechIdM - monitoring contains errors", headerFont);
        } else if (summaryLevel == NotificationLevel.WARNING) {
            headerColumn.append("Status CzechIdM - monitoring contains warnings", headerFont);
        } else {
            headerColumn.append("Status CzechIdM - monitoring is ok", headerFont);
        }
        cell.setCellValue(headerColumn);
        // 
        // close json stream
        jParser.close();
        // close and return input stream
        return getInputStream(workbook);
    } catch (IOException ex) {
        throw new ReportRenderException(report.getName(), ex);
    }
}
Also used : CreationHelper(org.apache.poi.ss.usermodel.CreationHelper) RptMonitoringResultDto(eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto) ReportRenderException(eu.bcvsolutions.idm.rpt.api.exception.ReportRenderException) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) IOException(java.io.IOException) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFCellStyle(org.apache.poi.xssf.usermodel.XSSFCellStyle) XSSFFont(org.apache.poi.xssf.usermodel.XSSFFont) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) NotificationLevel(eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Cell(org.apache.poi.ss.usermodel.Cell) JsonParser(com.fasterxml.jackson.core.JsonParser) XSSFHyperlink(org.apache.poi.xssf.usermodel.XSSFHyperlink) Hyperlink(org.apache.poi.ss.usermodel.Hyperlink)

Aggregations

RptMonitoringResultDto (eu.bcvsolutions.idm.rpt.dto.RptMonitoringResultDto)3 IOException (java.io.IOException)3 IdmMonitoringDto (eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ConfigurationService (eu.bcvsolutions.idm.core.api.service.ConfigurationService)1 AutowireHelper (eu.bcvsolutions.idm.core.api.utils.AutowireHelper)1 AttachmentManager (eu.bcvsolutions.idm.core.ecm.api.service.AttachmentManager)1 IdmMonitoringResultDto (eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)1 IdmMonitoringService (eu.bcvsolutions.idm.core.monitoring.api.service.IdmMonitoringService)1 MonitoringManager (eu.bcvsolutions.idm.core.monitoring.api.service.MonitoringManager)1 H2DatabaseMonitoringEvaluator (eu.bcvsolutions.idm.core.monitoring.service.impl.H2DatabaseMonitoringEvaluator)1 NotificationLevel (eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel)1 RptReportDto (eu.bcvsolutions.idm.rpt.api.dto.RptReportDto)1 ReportGenerateException (eu.bcvsolutions.idm.rpt.api.exception.ReportGenerateException)1 ReportRenderException (eu.bcvsolutions.idm.rpt.api.exception.ReportRenderException)1 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)1 File (java.io.File)1