public WardenIterationDto(WardenIteration iteration) { Id = iteration.Id; WardenName = iteration.Warden.Name; Ordinal = iteration.Ordinal; StartedAt = iteration.StartedAt; CompletedAt = iteration.CompletedAt; ExecutionTime = iteration.ExecutionTime; IsValid = iteration.IsValid; Results = iteration.Results.Select(x => new WardenCheckResultDto(x) { IterationId = iteration.Id }); }
/// <summary> /// Run a single iteration (cycle) that will execute all of the watchers and theirs hooks. /// </summary> /// <param name="wardenName">Name of the Warden that will execute the iteration.</param> /// <param name="ordinal">Number (ordinal) of executed iteration</param> /// <returns>Single iteration containing its ordinal and results of all executed watcher checks</returns> public async Task <IWardenIteration> ExecuteAsync(string wardenName, long ordinal) { var iterationStartedAt = _configuration.DateTimeProvider(); var iterationTasks = _configuration.Watchers.Select(TryExecuteWatcherChecksAndHooksAsync); var aggregatedResults = await Task.WhenAll(iterationTasks); var results = aggregatedResults.SelectMany(x => x).ToList(); await ExecuteAggregatedHooksAsync(results); var iterationCompletedAt = _configuration.DateTimeProvider(); var iteration = WardenIteration.Create(wardenName, ordinal, results.Select(x => x.WardenCheckResult), iterationStartedAt, iterationCompletedAt); return(iteration); }
public async Task <WardenIterationDto> SaveIterationAsync(WardenIterationDto iteration, Guid organizationId) { if (iteration == null) { return(null); } var wardenCheckResults = (iteration.Results ?? Enumerable.Empty <WardenCheckResultDto>()).ToList(); var watcherCheckResults = wardenCheckResults.Select(x => x.WatcherCheckResult).ToList(); watcherCheckResults.ForEach(SetWatcherTypeFromFullNamespace); var organization = await _database.Organizations().GetByIdAsync(organizationId); if (organization == null) { throw new ServiceException($"Organization has not been found for given id: '{organizationId}'."); } var warden = organization.GetWardenByName(iteration.WardenName); if (warden == null && !organization.AutoRegisterNewWarden) { throw new ServiceException($"Warden with name: '{iteration.WardenName}' has not been registered."); } var updateOrganization = false; if (warden == null) { if (organization.Wardens.Count() >= _featureSettings.MaxWardensInOrganization) { throw new ServiceException($"Limit of {_featureSettings.MaxWardensInOrganization} " + "wardens in organization has been reached."); } updateOrganization = true; organization.AddWarden(iteration.WardenName); warden = organization.GetWardenByName(iteration.WardenName); } else if (!warden.Enabled) { throw new ServiceException($"Warden with name: '{iteration.WardenName}' is disabled."); } var wardenIteration = new WardenIteration(warden.Name, organization, iteration.Ordinal, iteration.StartedAt, iteration.CompletedAt, iteration.IsValid); foreach (var result in wardenCheckResults) { WatcherType watcherType; var watcherName = result.WatcherCheckResult.WatcherName; if (!Enum.TryParse(result.WatcherCheckResult.WatcherType, true, out watcherType)) { watcherType = WatcherType.Custom; } var watcher = warden.GetWatcherByName(watcherName); if (watcher == null) { if (warden.Watchers.Count() >= _featureSettings.MaxWatchersInWarden) { throw new ServiceException($"Limit of {_featureSettings.MaxWatchersInWarden} " + "watchers in Warden has been reached."); } updateOrganization = true; warden.AddWatcher(watcherName, watcherType); watcher = warden.GetWatcherByName(watcherName); } var watcherCheckResult = result.WatcherCheckResult.IsValid ? WatcherCheckResult.Valid(watcher, result.WatcherCheckResult.Description) : WatcherCheckResult.Invalid(watcher, result.WatcherCheckResult.Description); var checkResult = result.IsValid ? WardenCheckResult.Valid(watcherCheckResult, result.StartedAt, result.CompletedAt) : WardenCheckResult.Invalid(watcherCheckResult, result.StartedAt, result.CompletedAt, CreatExceptionInfo(result.Exception)); wardenIteration.AddResult(checkResult); } if (updateOrganization) { await _database.Organizations().ReplaceOneAsync(x => x.Id == organizationId, organization); } await _database.WardenIterations().InsertOneAsync(wardenIteration); Logger.Info($"Warden iteration: '{wardenIteration.Id}' was created " + $"for organization: '{organization.Name}' with id: '{organization.Id}'."); return(new WardenIterationDto(wardenIteration)); }
public async Task<WardenIterationDto> SaveIterationAsync(WardenIterationDto iteration, Guid organizationId) { if (iteration == null) return null; var wardenCheckResults = (iteration.Results ?? Enumerable.Empty<WardenCheckResultDto>()).ToList(); var watcherCheckResults = wardenCheckResults.Select(x => x.WatcherCheckResult).ToList(); watcherCheckResults.ForEach(SetWatcherTypeFromFullNamespace); var organization = await _database.Organizations().GetByIdAsync(organizationId); if (organization == null) throw new ServiceException($"Organization has not been found for given id: '{organizationId}'."); var warden = organization.GetWardenByName(iteration.WardenName); if (warden == null && !organization.AutoRegisterNewWarden) throw new ServiceException($"Warden with name: '{iteration.WardenName}' has not been registered."); var updateOrganization = false; if (warden == null) { if (organization.Wardens.Count() >= _featureSettings.MaxWardensInOrganization) { throw new ServiceException($"Limit of {_featureSettings.MaxWardensInOrganization} " + "wardens in organization has been reached."); } updateOrganization = true; organization.AddWarden(iteration.WardenName); warden = organization.GetWardenByName(iteration.WardenName); } else if(!warden.Enabled) throw new ServiceException($"Warden with name: '{iteration.WardenName}' is disabled."); var wardenIteration = new WardenIteration(warden.Name, organization, iteration.Ordinal, iteration.StartedAt, iteration.CompletedAt, iteration.IsValid); foreach (var result in wardenCheckResults) { WatcherType watcherType; var watcherName = result.WatcherCheckResult.WatcherName; if(!Enum.TryParse(result.WatcherCheckResult.WatcherType, true, out watcherType)) watcherType = WatcherType.Custom; var watcher = warden.GetWatcherByName(watcherName); if (watcher == null) { if (warden.Watchers.Count() >= _featureSettings.MaxWatchersInWarden) { throw new ServiceException($"Limit of {_featureSettings.MaxWatchersInWarden} " + "watchers in Warden has been reached."); } updateOrganization = true; warden.AddWatcher(watcherName, watcherType); watcher = warden.GetWatcherByName(watcherName); } var watcherCheckResult = result.WatcherCheckResult.IsValid ? WatcherCheckResult.Valid(watcher, result.WatcherCheckResult.Description) : WatcherCheckResult.Invalid(watcher, result.WatcherCheckResult.Description); var checkResult = result.IsValid ? WardenCheckResult.Valid(watcherCheckResult, result.StartedAt, result.CompletedAt) : WardenCheckResult.Invalid(watcherCheckResult, result.StartedAt, result.CompletedAt, CreatExceptionInfo(result.Exception)); wardenIteration.AddResult(checkResult); } if (updateOrganization) await _database.Organizations().ReplaceOneAsync(x => x.Id == organizationId, organization); await _database.WardenIterations().InsertOneAsync(wardenIteration); Logger.Info($"Warden iteration: '{wardenIteration.Id}' was created " + $"for organization: '{organization.Name}' with id: '{organization.Id}'."); return new WardenIterationDto(wardenIteration); }