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