Search in sources :

Example 56 with IdmMonitoringResultDto

use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class DefaultMonitoringManager method evaluate.

@Override
public IdmMonitoringResultDto evaluate(IdmMonitoringDto monitoring) {
    Assert.notNull(monitoring, "Monitoring is required.");
    UUID monitoringId = monitoring.getId();
    Assert.notNull(monitoringId, "Persisted monitoring is required.");
    // 
    MonitoringEvaluator evaluator = getEvaluator(monitoring);
    if (evaluator == null) {
        LOG.warn("Monitoring evaluator for motitoring configuration [{}] not exists.", monitoringId);
        return null;
    }
    // 
    ZonedDateTime monitoringStarted = ZonedDateTime.now();
    IdmMonitoringResultDto result = null;
    try {
        result = evaluator.evaluate(monitoring);
    } catch (AcceptedException ex) {
        result = new IdmMonitoringResultDto(new OperationResultDto.Builder(OperationState.RUNNING).setException(ex).build());
        result.setLevel(NotificationLevel.INFO);
    } catch (ResultCodeException ex) {
        result = new IdmMonitoringResultDto(new OperationResultDto.Builder(OperationState.EXCEPTION).setException(ex).build());
        result.setLevel(NotificationLevel.ERROR);
    } catch (Exception ex) {
        Throwable resolvedException = ExceptionUtils.resolveException(ex);
        if (resolvedException instanceof ResultCodeException) {
            result = new IdmMonitoringResultDto(// 
            new OperationResultDto.Builder(OperationState.EXCEPTION).setException(// 
            (ResultCodeException) resolvedException).build());
            result.setLevel(NotificationLevel.ERROR);
        } else {
            result = new IdmMonitoringResultDto(new OperationResultDto.Builder(OperationState.EXCEPTION).setCause(ex).build());
        }
    }
    // 
    if (result != null) {
        result.setMonitoring(monitoringId);
        result.setEvaluatorType(monitoring.getEvaluatorType());
        result.setProcessedOrder(monitoring.getSeq());
        result.setEvaluatorProperties(monitoring.getEvaluatorProperties());
        result.setInstanceId(monitoring.getInstanceId());
        // default result state
        if (result.getResult() == null) {
            result.setResult(new OperationResultDto(OperationState.EXECUTED));
        }
        // default level by result model
        if (result.getLevel() == null) {
            ResultModel resultModel = result.getResult().getModel();
            if (resultModel != null) {
                result.setLevel(resultModel.getLevel());
            }
        }
        // 
        result.setMonitoringStarted(monitoringStarted);
        result.setMonitoringEnded(ZonedDateTime.now());
        result.setLastResult(true);
        // reset last result flag
        monitoringResultService.resetLastResult(monitoringId);
        // save new last result
        result = monitoringResultService.save(result);
        // 
        cacheManager.cacheValue(LAST_RESULT_CACHE_NAME, monitoringId, result);
    } else {
        LOG.debug("Monitoring [{}] ended without result - result will not be persisted.", monitoringId);
    }
    // 
    return result;
}
Also used : MonitoringEvaluator(eu.bcvsolutions.idm.core.monitoring.api.service.MonitoringEvaluator) ZonedDateTime(java.time.ZonedDateTime) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) UUID(java.util.UUID) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 57 with IdmMonitoringResultDto

use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class DefaultMonitoringManager method execute.

@Override
public IdmMonitoringResultDto execute(IdmMonitoringDto monitoring, BasePermission... permission) {
    Assert.notNull(monitoring, "Monitoring is required.");
    // 
    EventContext<IdmMonitoringDto> context = monitoringService.publish(new MonitoringEvent(MonitoringEventType.EXECUTE, monitoring), permission);
    // with result
    for (EventResult<IdmMonitoringDto> eventResult : context.getResults()) {
        for (OperationResult operationResult : eventResult.getResults()) {
            ResultModel model = operationResult.getModel();
            if (model != null && CoreResultCode.MONITORING_RESULT.getCode().equals(model.getStatusEnum()) && model.getParameters().containsKey(EventResult.EVENT_PROPERTY_RESULT)) {
                return (IdmMonitoringResultDto) model.getParameters().get(EventResult.EVENT_PROPERTY_RESULT);
            }
        }
    }
    // without result (just for sure - e.g. wrong exception handling in custom monitoring evaluator)
    return null;
}
Also used : IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) MonitoringEvent(eu.bcvsolutions.idm.core.monitoring.api.event.MonitoringEvent) OperationResult(eu.bcvsolutions.idm.core.api.entity.OperationResult) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 58 with IdmMonitoringResultDto

use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class DefaultMonitoringManager method scheduleExecute.

/**
 * Spring schedule run all registered monitoring evaluators
 */
@Scheduled(fixedDelay = 10000)
public void scheduleExecute() {
    String instanceId = configurationService.getInstanceId();
    LOG.debug("Processing monitoring evaluators on instance id [{}]", instanceId);
    // 
    IdmMonitoringFilter filter = new IdmMonitoringFilter();
    filter.setDisabled(Boolean.FALSE);
    filter.setInstanceId(instanceId);
    // 
    List<IdmMonitoringDto> monitorings = monitoringService.find(filter, PageRequest.of(0, Integer.MAX_VALUE, Sort.by(IdmMonitoring_.seq.getName()))).getContent();
    // 
    for (IdmMonitoringDto monitoring : monitorings) {
        UUID monitoringId = monitoring.getId();
        // 
        // check execute date
        ZonedDateTime executeDate = monitoring.getExecuteDate();
        if (executeDate != null && ZonedDateTime.now().isBefore(executeDate)) {
            LOG.trace("Monitoring evaluator [{}] will be evaluted after [{}].", monitoringId, executeDate);
            // 
            continue;
        }
        // 
        // check last result is in period
        // seconds
        Long checkPeriod = monitoring.getCheckPeriod();
        if (checkPeriod == null) {
            checkPeriod = 0L;
        }
        // 
        IdmMonitoringResultDto lastResult = getLastResult(monitoringId);
        if (lastResult != null) {
            if (checkPeriod == 0 && firstExecuted) {
                LOG.trace("Monitoring evaluator [{}] was already executed.", monitoringId);
                // 
                continue;
            }
            // 
            if (lastResult.getCreated().isAfter(ZonedDateTime.now().minusSeconds(checkPeriod))) {
                LOG.trace("Monitoring evaluator [{}] was already executed.", monitoringId);
                // 
                continue;
            }
        }
        // 
        execute(monitoring);
    }
    // 
    firstExecuted = true;
}
Also used : IdmMonitoringFilter(eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringFilter) IdmMonitoringDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto) ZonedDateTime(java.time.ZonedDateTime) UUID(java.util.UUID) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 59 with IdmMonitoringResultDto

use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class DefaultMonitoringManager method getLastResult.

/**
 * Find last monitoring result.
 *
 * @param monitoringId monitoring evaluator identifier
 * @return last result
 */
private IdmMonitoringResultDto getLastResult(UUID monitoringId, BasePermission... permission) {
    // try to get cached value
    ValueWrapper value = cacheManager.getValue(LAST_RESULT_CACHE_NAME, monitoringId);
    if (value != null) {
        return monitoringResultService.checkAccess((IdmMonitoringResultDto) value.get(), permission);
    }
    // or load from database
    IdmMonitoringResultFilter resultFilter = new IdmMonitoringResultFilter();
    resultFilter.setMonitoring(monitoringId);
    List<IdmMonitoringResultDto> monitoringResults = monitoringResultService.find(resultFilter, PageRequest.of(0, 1, Sort.by(Direction.DESC, IdmMonitoringResult_.created.getName())), permission).getContent();
    if (monitoringResults.isEmpty()) {
        // null => prevent to load again
        cacheManager.cacheValue(LAST_RESULT_CACHE_NAME, monitoringId, null);
        // 
        return null;
    }
    // 
    IdmMonitoringResultDto lastResult = monitoringResults.get(0);
    cacheManager.cacheValue(LAST_RESULT_CACHE_NAME, monitoringId, lastResult);
    // 
    return lastResult;
}
Also used : ValueWrapper(eu.bcvsolutions.idm.core.api.config.cache.domain.ValueWrapper) IdmMonitoringResultFilter(eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringResultFilter) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Example 60 with IdmMonitoringResultDto

use of eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto in project CzechIdMng by bcvsolutions.

the class LongRunningTaskMonitoringEvaluator method evaluate.

@Override
public IdmMonitoringResultDto evaluate(IdmMonitoringDto monitoring) {
    IdmMonitoringResultDto result = new IdmMonitoringResultDto();
    ResultModel resultModel;
    // 
    IdmLongRunningTaskFilter filter = new IdmLongRunningTaskFilter();
    filter.setOperationState(OperationState.EXCEPTION);
    filter.setMonitoringIgnored(Boolean.FALSE);
    Long givenNumberOfDays = getParameterConverter().toLong(monitoring.getEvaluatorProperties(), PARAMETER_NUMBER_OF_DAYS);
    if (givenNumberOfDays != null) {
        filter.setCreatedFrom(ZonedDateTime.now().truncatedTo(ChronoUnit.DAYS).minusDays(givenNumberOfDays));
    }
    long count = longRunningTaskService.count(filter);
    // 
    if (count > 0) {
        resultModel = new DefaultResultModel(CoreResultCode.MONITORING_LONG_RUNNING_TASK_ERROR, ImmutableMap.of("count", Long.toString(count)));
    } else {
        resultModel = new DefaultResultModel(CoreResultCode.OK);
    }
    // 
    result.setResult(new OperationResultDto.Builder(OperationState.EXECUTED).setModel(resultModel).build());
    result.setValue(String.valueOf(count));
    // 
    return result;
}
Also used : DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) OperationResultDto(eu.bcvsolutions.idm.core.api.dto.OperationResultDto) DefaultResultModel(eu.bcvsolutions.idm.core.api.dto.DefaultResultModel) ResultModel(eu.bcvsolutions.idm.core.api.dto.ResultModel) IdmLongRunningTaskFilter(eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter) IdmMonitoringResultDto(eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)

Aggregations

IdmMonitoringResultDto (eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringResultDto)63 IdmMonitoringDto (eu.bcvsolutions.idm.core.monitoring.api.dto.IdmMonitoringDto)47 Test (org.junit.Test)39 AbstractUnitTest (eu.bcvsolutions.idm.test.api.AbstractUnitTest)21 OperationResultDto (eu.bcvsolutions.idm.core.api.dto.OperationResultDto)18 IdmMonitoringResultFilter (eu.bcvsolutions.idm.core.monitoring.api.dto.filter.IdmMonitoringResultFilter)13 UUID (java.util.UUID)13 ResultModel (eu.bcvsolutions.idm.core.api.dto.ResultModel)12 AbstractIntegrationTest (eu.bcvsolutions.idm.test.api.AbstractIntegrationTest)12 DefaultResultModel (eu.bcvsolutions.idm.core.api.dto.DefaultResultModel)10 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)7 SysSchemaObjectClassDto (eu.bcvsolutions.idm.acc.dto.SysSchemaObjectClassDto)6 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 SysSyncConfigDto (eu.bcvsolutions.idm.acc.dto.SysSyncConfigDto)5 AbstractReadWriteDtoControllerRestTest (eu.bcvsolutions.idm.core.api.rest.AbstractReadWriteDtoControllerRestTest)5 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)5 SysSyncActionLogDto (eu.bcvsolutions.idm.acc.dto.SysSyncActionLogDto)4 SysSyncLogDto (eu.bcvsolutions.idm.acc.dto.SysSyncLogDto)4