Search in sources :

Example 31 with IdmMonitoringDto

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmMonitoringResultFilter(eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringResultFilter) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 32 with IdmMonitoringDto

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());
}
Also used : IdmMonitoringFilter(eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringFilter) PageImpl(org.springframework.data.domain.PageImpl) IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) Pageable(org.springframework.data.domain.Pageable) MonitoringEvaluator(eu.bcvsolutions.idm.core.monitoring.api.service.MonitoringEvaluator) ArrayList(java.util.ArrayList) NotificationLevel(eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 33 with IdmMonitoringDto

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);
}
Also used : IdmMonitoringFilter(eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringFilter) IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto)

Example 34 with IdmMonitoringDto

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);
    }
}
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 35 with IdmMonitoringDto

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));
}
Also used : IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) 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)

Aggregations

IdmMonitoringDto (eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto)73 Test (org.junit.Test)46 IdmMonitoringResultDto (eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)45 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)24 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)16 UUID (java.util.UUID)13 IdmMonitoringResultFilter (eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringResultFilter)10 SysSystemDto (eu.bcvsolutions.idm.acc.dto.SysSystemDto)6 SysSystemMappingDto (eu.bcvsolutions.idm.acc.dto.SysSystemMappingDto)6 SysSyncConfigFilter (eu.bcvsolutions.idm.acc.dto.filter.SysSyncConfigFilter)6 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)6 SysSchemaObjectClassDto (eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto)5 SysSyncConfigDto (eu.bcvsolutions.idm.acc.dto.SysSyncConfigDto)5 ConfigurationMap (eu.bcvsolutions.idm.core.api.domain.ConfigurationMap)5 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)5 AbstractReadWriteDtoControllerRestTest (eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)5 IdmMonitoringFilter (eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringFilter)5 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)4 AutowireHelper (eu.bcvsolutions.idm.core.api.utils.AutowireHelper)4 IdmMonitoringService (eu.bcvsolutions.idm.core.monitoring.api.service.IdmMonitoringService)4