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 void SendIterationCreated(Guid organizationId, WardenIterationDto iteration) { var groupName = GetWardenGroupName(organizationId, iteration.WardenName); _hub.Clients.Group(groupName).iterationCreated(iteration); }