use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto in project CzechIdMng by bcvsolutions.
the class IdmMonitoringController method getLastResult.
/**
* Get last monitoring result.
*
* @since 11.2.0
*/
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/{backendId}/last-result")
@PreAuthorize("hasAuthority('" + MonitoringGroupPermission.MONITORING_READ + "') " + "and hasAuthority('" + MonitoringGroupPermission.MONITORINGRESULT_READ + "')")
@ApiOperation(value = "Get last monitoring result", nickname = "getLastMonitoringResult", tags = { IdmMonitoringController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = MonitoringGroupPermission.MONITORING_READ, description = ""), @AuthorizationScope(scope = MonitoringGroupPermission.MONITORINGRESULT_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = MonitoringGroupPermission.MONITORING_READ, description = ""), @AuthorizationScope(scope = MonitoringGroupPermission.MONITORINGRESULT_READ, description = "") }) }, notes = "Get last monitoring result.")
public ResponseEntity<?> getLastResult(@ApiParam(value = "Monitoring codeable identifier.", required = true) @PathVariable @NotNull String backendId) {
IdmMonitoringDto monitoring = getDto(backendId);
if (monitoring == null) {
throw new ResultCodeException(CoreResultCode.NOT_FOUND, ImmutableMap.of("entity", backendId));
}
IdmMonitoringResultFilter filter = new IdmMonitoringResultFilter();
filter.setMonitoring(monitoring.getId());
List<IdmMonitoringResultDto> lastResults = monitoringManager.getLastResults(filter, PageRequest.of(0, 1), IdmBasePermission.READ).getContent();
// without result
if (lastResults.isEmpty()) {
new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
// return last result
return new ResponseEntity<>(monitoringResultController.toResource(lastResults.get(0)), HttpStatus.OK);
}
use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto in project CzechIdMng by bcvsolutions.
the class DefaultMonitoringManager method getLastResults.
@Override
public Page<IdmMonitoringResultDto> getLastResults(IdmMonitoringResultFilter filter, Pageable pageable, BasePermission... permission) {
// all instances => last results should be visible on each instance
IdmMonitoringFilter monitoringFilter = new IdmMonitoringFilter();
if (filter != null) {
monitoringFilter.setId(filter.getMonitoring());
}
monitoringFilter.setDisabled(Boolean.FALSE);
//
List<IdmMonitoringDto> monitorings = monitoringService.find(monitoringFilter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmMonitoring_.seq.getName())), PermissionUtils.isEmpty(permission) ? null : IdmBasePermission.AUTOCOMPLETE).getContent();
//
// last results sorted by monitoring order
List<IdmMonitoringResultDto> results = new ArrayList<>(monitorings.size());
for (IdmMonitoringDto monitoring : monitorings) {
MonitoringEvaluator evaluator = getEvaluator(monitoring);
if (evaluator == null) {
LOG.debug("Monitoring evaluator for motitoring configuration [{}] not exists.", monitoring.getId());
continue;
}
IdmMonitoringResultDto lastResult = getLastResult(monitoring.getId(), permission);
if (lastResult == null) {
continue;
}
// filter by level
NotificationLevel lastResultLevel = lastResult.getLevel();
List<NotificationLevel> levels = filter == null ? null : filter.getLevels();
if (CollectionUtils.isNotEmpty(levels) && !levels.contains(lastResultLevel)) {
continue;
}
lastResult.setTrimmed(true);
results.add(lastResult);
}
//
// pageable is required internally
Pageable internalPageable;
if (pageable == null) {
internalPageable = PageRequest.of(0, Integer.MAX_VALUE);
} else {
internalPageable = pageable;
}
//
// Sort by level desc
results.sort((r1, r2) -> {
return ObjectUtils.compare(r2.getLevel(), r1.getLevel());
});
//
// "naive" pagination
int first = internalPageable.getPageNumber() * internalPageable.getPageSize();
int last = internalPageable.getPageSize() + first;
List<IdmMonitoringResultDto> page = results.subList(first < results.size() ? first : results.size() > 0 ? results.size() - 1 : 0, last < results.size() ? last : results.size());
//
return new PageImpl<>(page, internalPageable, results.size());
}
use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto in project CzechIdMng by bcvsolutions.
the class DefaultMonitoringManager method init.
/**
* Init last result cache, when instance is started
*/
@Override
public void init() {
String instanceId = configurationService.getInstanceId();
int counter = 0;
//
IdmMonitoringFilter filter = new IdmMonitoringFilter();
filter.setDisabled(Boolean.FALSE);
filter.setInstanceId(instanceId);
for (IdmMonitoringDto monitoring : monitoringService.find(filter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmMonitoring_.seq.getName())))) {
if (getLastResult(monitoring.getId()) != null) {
counter++;
}
}
//
LOG.info("Last monitoring results [{}] on instance [{}] loaded into cache.", counter, instanceId);
}
use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto 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.core.monitoring.api.dto.IdmMonitoringDto in project CzechIdMng by bcvsolutions.
the class MonitoringReportExecutorIntegrationTest method testRenderers.
@Test
@Transactional
public void testRenderers() {
IdmMonitoringDto monitoring = new IdmMonitoringDto();
monitoring.setCheckPeriod(0L);
monitoring.setEvaluatorType(AutowireHelper.getTargetType(h2DatabaseMonitoringEvaluator));
monitoring.setInstanceId(configurationService.getInstanceId());
monitoring = monitoringService.save(monitoring);
monitoringManager.execute(monitoring);
//
RptReportDto report = new RptReportDto(UUID.randomUUID());
report.setExecutorName(reportExecutor.getName());
//
// generate report
report = reportExecutor.generate(report);
//
Assert.assertNotNull(xlsxRenderer.render(report));
}
Aggregations