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);
}
}
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);
}
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);
}
}
Aggregations