public static IEnumerable<UserFlagAssignment> BulkAssignOverrideUsers(DiscoDataContext Database, UserFlag UserFlag, User Technician, string Comments, List<User> Users, IScheduledTaskStatus Status) { double progressInterval; const int databaseChunkSize = 100; string comments = string.IsNullOrWhiteSpace(Comments) ? null : Comments.Trim(); Status.UpdateStatus(0, "Calculating assignment changes"); var currentAssignments = Database.UserFlagAssignments.Include("User").Where(a => a.UserFlagId == UserFlag.Id && !a.RemovedDate.HasValue).ToList(); var removeAssignments = currentAssignments.Where(ca => !Users.Any(u => u.UserId.Equals(ca.UserId, StringComparison.OrdinalIgnoreCase))).ToList(); var addUsers = Users.Where(u => !currentAssignments.Any(ca => ca.UserId.Equals(u.UserId, StringComparison.OrdinalIgnoreCase))).ToList(); if (removeAssignments.Count > 0 || addUsers.Count > 0) { progressInterval = (double)100 / (removeAssignments.Count + addUsers.Count); var removedDateTime = DateTime.Now; // Remove Assignments removeAssignments.Chunk(databaseChunkSize).SelectMany((chunk, chunkIndex) => { var chunkIndexOffset = (chunkIndex * databaseChunkSize) + removeAssignments.Count; var chunkResults = chunk.Select((flagAssignment, index) => { Status.UpdateStatus((chunkIndexOffset + index) * progressInterval, string.Format("Removing Flag: {0}", flagAssignment.User.ToString())); flagAssignment.OnRemoveUnsafe(Database, Technician); return flagAssignment; }).ToList(); // Save Chunk Items to Database Database.SaveChanges(); return chunkResults; }).ToList(); // Add Assignments var addedUserAssignments = addUsers.Chunk(databaseChunkSize).SelectMany((chunk, chunkIndex) => { var chunkIndexOffset = (chunkIndex * databaseChunkSize) + removeAssignments.Count; var chunkResults = chunk.Select((user, index) => { Status.UpdateStatus((chunkIndexOffset + index) * progressInterval, string.Format("Assigning Flag: {0}", user.ToString())); return user.OnAddUserFlag(Database, UserFlag, Technician, comments); }).ToList(); // Save Chunk Items to Database Database.SaveChanges(); return chunkResults; }).ToList(); Status.SetFinishedMessage(string.Format("{0} Users/s Added; {1} User/s Removed; {2} User/s Skipped", addUsers.Count, removeAssignments.Count, (Users.Count - addUsers.Count))); return addedUserAssignments; } else { Status.SetFinishedMessage("No changes found"); return Enumerable.Empty<UserFlagAssignment>(); } }
public static UpdateResponseV2 Check(DiscoDataContext Database, bool UseProxy, IScheduledTaskStatus Status) { Status.UpdateStatus(10, "Gathering statistics and building update request"); var updateRequest = BuildRequest(Database); Status.UpdateStatus(40, "Sending statistics and update request"); var discoVersion = CurrentDiscoVersionFormatted(); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(UpdateUrl()); // Fix for Proxy Servers which don't support KeepAlive request.KeepAlive = false; if (!UseProxy) request.Proxy = new WebProxy(); request.ContentType = "application/json; charset=utf-8; encoding=gzip"; request.Method = WebRequestMethods.Http.Post; request.UserAgent = string.Format("Disco/{0} (Update)", discoVersion); using (var requestStream = request.GetRequestStream()) { using (var compressedStream = new GZipStream(requestStream, CompressionLevel.Optimal)) { using (var requestStreamWriter = new StreamWriter(compressedStream, Encoding.UTF8)) { JsonSerializer serializer = new JsonSerializer(); serializer.Serialize(requestStreamWriter, updateRequest); requestStreamWriter.Flush(); } } } Status.UpdateStatus(50, "Waiting for update response"); using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (response.StatusCode == HttpStatusCode.OK) { Status.UpdateStatus(90, "Reading update response"); string updateResultJson; UpdateResponseV2 updateResult; using (var responseStream = response.GetResponseStream()) { using (var responseReader = new StreamReader(responseStream)) { updateResultJson = responseReader.ReadToEnd(); } } updateResult = JsonConvert.DeserializeObject<UpdateResponseV2>(updateResultJson); Database.DiscoConfiguration.UpdateLastCheckResponse = updateResult; Database.SaveChanges(); Status.SetFinishedMessage(string.Format("The update server reported Version {0} is the latest.", updateResult.LatestVersion)); return updateResult; } else { Status.SetTaskException(new WebException(string.Format("Server responded with: [{0}] {1}", response.StatusCode, response.StatusDescription))); return null; } } }
public static IEnumerable<UserFlagAssignment> BulkAssignAddUsers(DiscoDataContext Database, UserFlag UserFlag, User Technician, string Comments, List<User> Users, IScheduledTaskStatus Status) { if (Users.Count > 0) { double progressInterval; const int databaseChunkSize = 100; string comments = string.IsNullOrWhiteSpace(Comments) ? null : Comments.Trim(); var addUsers = Users.Where(u => !u.UserFlagAssignments.Any(a => a.UserFlagId == UserFlag.Id && !a.RemovedDate.HasValue)).ToList(); progressInterval = (double)100 / addUsers.Count; var addedUserAssignments = addUsers.Chunk(databaseChunkSize).SelectMany((chunk, chunkIndex) => { var chunkIndexOffset = databaseChunkSize * chunkIndex; var chunkResults = chunk.Select((user, index) => { Status.UpdateStatus((chunkIndexOffset + index) * progressInterval, string.Format("Assigning Flag: {0}", user.ToString())); return user.OnAddUserFlag(Database, UserFlag, Technician, comments); }).ToList(); // Save Chunk Items to Database Database.SaveChanges(); return chunkResults; }).Where(fa => fa != null).ToList(); Status.SetFinishedMessage(string.Format("{0} Users/s Added; {1} User/s Skipped", addUsers.Count, (Users.Count - addUsers.Count))); return addedUserAssignments; } else { Status.SetFinishedMessage("No changes found"); return Enumerable.Empty<UserFlagAssignment>(); } }