private void SynchronizeStatisticTime(CommunicationStatistics stats) { if (stats.CurrentTime.Year < DateTime.Now.Year) { return; } TimeSpan timeDiff = (TimeSpan)(DateTime.Now - stats.CurrentTime); if (Math.Abs(timeDiff.TotalMinutes) > 1.0) { if (stats.LastExecutionTime.HasValue) { stats.LastExecutionTime = (stats.LastExecutionTime.Value + timeDiff); } if (stats.LastExecutionMessage != null) { stats.LastExecutionMessage.Time = stats.LastExecutionMessage.Time.HasValue ? new DateTime?(stats.LastExecutionMessage.Time.Value + timeDiff) : null; } if (stats.LastReceiveMessage != null) { stats.LastReceiveMessage.Time = stats.LastReceiveMessage.Time.HasValue ? new DateTime?(stats.LastReceiveMessage.Time.Value + timeDiff) : null; } if (stats.LastSendMessage != null) { stats.LastSendMessage.Time = stats.LastSendMessage.Time.HasValue ? new DateTime?(stats.LastSendMessage.Time.Value + timeDiff) : null; } } }
private string ReplyWithCampaignsForDate(DateTime dateTime) { PeopleStageSystemSummary firstSystem = apiClient.GetPeopleStageSystems()?.FirstOrDefault(); CommunicationStatistics commsStats = apiClient.GetPeopleStageCommunicationsStatsForProgramme(firstSystem); if (commsStats == null) { return("I don't know - I don't have any communication stats"); } int dateIndex = commsStats.Days.IndexOf(dateTime.ToString("yyyy-MM-dd")); if (dateIndex < 0) { return("I don't know - I don't have the number of campaigns for " + FormatDateForReply("", dateTime)); } long?campaigns = commsStats.CampaignsCounts.Count < dateIndex? null : commsStats.CampaignsCounts[dateIndex]; if (campaigns == null) { return("I don't know - I don't have the number of campaigns for " + FormatDateForReply("", dateTime)); } else { return(campaigns.Value.ToString("N0") + " campaigns have run " + FormatDateForReply("on", dateTime)); } }
/// <summary> /// Gets the statistics from every registered logger. /// </summary> /// <returns>Retrieved statistics.</returns> private CommunicationStatistics GetStatistics() { CommunicationStatistics stats = new CommunicationStatistics(); Logging.LogMessage msg; if (this.ExecutorLogger != null) { msg = this.ExecutorLogger.PopLastMessage(); if (msg != null) { stats.LastExecutionMessage = new MessageData(msg.Message, msg.MessageTime); } stats.LastExecutionTime = (DateTime?)this.ExecutorLogger.GetProperty("LastExecutionTime"); } if (this.ReceiverLogger != null) { msg = this.ReceiverLogger.PopLastMessage(); if (msg != null) { stats.LastReceiveMessage = new MessageData(msg.Message, msg.MessageTime); } } if (this.SenderLogger != null) { msg = this.SenderLogger.PopLastMessage(); if (msg != null) { stats.LastSendMessage = new MessageData(msg.Message, msg.MessageTime); } } using (IUnitOfWork uow = Manager.CreateUnitOfWork()) { uow.MapperFactory = this.mapperFactory; CommunicationPackageRepository repo = new CommunicationPackageRepository(uow); stats.PackagesToExecute = repo.GetUnprocessedPackagesQuantity(); stats.PackagesToSend = repo.GetUndeliveredPackagesQuantity(); if (String.IsNullOrEmpty(this.Manager.TransmitterConfiguration.AdditionalDataStoreProcedure) == false) { string newData = repo.GetAdditionalData(this.Manager.TransmitterConfiguration.AdditionalDataStoreProcedure).ToString(System.Xml.Linq.SaveOptions.DisableFormatting); if (newData != this.additionalData) { stats.AdditionalData = repo.GetAdditionalData(this.Manager.TransmitterConfiguration.AdditionalDataStoreProcedure).ToString(System.Xml.Linq.SaveOptions.DisableFormatting); this.additionalData = newData; } } } return(stats); }
/// <summary> /// Updates the communication statistics by pesristing them. /// </summary> /// <param name="statistics">The statistics data.</param> /// <param name="departmentId">The statistics target department id.</param> public virtual void UpdateStatistics(CommunicationStatistics statistics, Guid departmentId) { if (statistics == null) { throw new ArgumentNullException("statistics"); } if (departmentId == null) { throw new ArgumentNullException("departmentId"); } SynchronizeStatisticTime(statistics); DBRow row = new DBXml().AddTable("statistics").AddRow(); row.AddElement("databaseId", departmentId.ToUpperString()); row.AddElement("lastUpdate", DateTime.Now.ToIsoString()); row.AddElement("undeliveredPackagesQuantity", statistics.PackagesToSend); row.AddElement("unprocessedPackagesQuantity", statistics.PackagesToExecute); if (statistics.LastExecutionTime != null) { row.AddElement("lastExecutionTime", statistics.LastExecutionTime.Value.ToIsoString()); } if (statistics.LastSendMessage != null) { row.AddElement("lastSentMessage", statistics.LastSendMessage.Message); row.AddElement("sentMessageTime", statistics.LastSendMessage.Time.Value.ToIsoString()); } if (statistics.LastExecutionMessage != null) { row.AddElement("lastExecutionMessage", statistics.LastExecutionMessage.Message); row.AddElement("executionMessageTime", statistics.LastExecutionMessage.Time.Value.ToIsoString()); } if (statistics.LastReceiveMessage != null) { row.AddElement("lastReceiveMessage", statistics.LastReceiveMessage.Message); row.AddElement("receiveMessageTime", statistics.LastReceiveMessage.Time.Value.ToIsoString()); } SqlCommand cmd = this.helper.CreateCommand(StoredProcedure.communication_p_updateStatistics.ToProcedureName(), new SqlParameter("@xmlVar", SqlDbType.Xml), this.helper.CreateSqlXml(row.Table.Document.Xml)); using (this.Database.SynchronizeConnection()) { cmd.ExecuteNonQuery(); } }
/// <summary> /// Task main method. /// </summary> /// <remarks> /// Run method steps: /// 1. Sends gathered communication statistics. /// ... where are 2 and 3? /// 4. Waits before repeating process. /// </remarks> protected override void Run() { bool facadehelper_DisableThisCodeForDebugCondition = facadehelper_DisableThisCodeForDebugConditionSwitch; if (!facadehelper_DisableThisCodeForDebugCondition) { while (IsEnabled) { try { Wait(); lock (Makolab.Fractus.Communication.Transmitter.TransmitterSemaphore.locker) { CommunicationStatistics stats = GetStatistics(); stats.CurrentTime = DateTime.Now; RoboFramework.Tools.RandomLogHelper.GetLog().Debug("StatisticsUpdater: CommunicationServiceProxy.Instance.SendData(...)"); CommunicationServiceProxy.Instance.SendData(SynchronizationHelper.CreateMessage(new SendDataParameters { Statistics = stats, DepartmentIdentifier = this.Manager.DatabaseId }, "SendData", CommunicationServiceProxy.ProxyFactory.Endpoint.Binding.MessageVersion)); } } catch (TimeoutException e) { if (this.SenderLogger != null) { this.SenderLogger.Log.Error(e.ToString()); // TODO -1 change to false when we will have a general view often this happens. } } catch (System.ServiceModel.CommunicationException e) { if (this.SenderLogger != null) { this.SenderLogger.Log.Error(e.ToString()); } } catch (Exception e) { SenderLogger.Error(String.Format(CultureInfo.InvariantCulture, "Uncaught exception in StatisticsUpdater: {0}", e.ToString())); //throw; } } } }
/// <summary> /// Initializes a RSyncDir instance, and binds it to the source folder /// </summary> /// <param name="sourcefolder">The folders to create a backup from</param> /// <param name="stat">The status report object</param> /// <param name="filter">An optional filter that controls what files to include</param> public RSyncDir(string[] sourcefolder, CommunicationStatistics stat, Utility.FilenameFilter filter) { m_filter = filter; m_oldSignatures = new Dictionary<string, ArchiveWrapper>(Utility.Utility.ClientFilenameStringComparer); m_oldFolders = new Dictionary<string, DateTime>(Utility.Utility.ClientFilenameStringComparer); m_oldSymlinks = new Dictionary<string, string>(Utility.Utility.ClientFilenameStringComparer); m_lastVerificationTime = new Dictionary<string, DateTime>(Utility.Utility.ClientFilenameStringComparer); for (int i = 0; i < sourcefolder.Length; i++) { if (!System.IO.Path.IsPathRooted(sourcefolder[i])) sourcefolder[i] = System.IO.Path.GetFullPath(sourcefolder[i]); sourcefolder[i] = Utility.Utility.AppendDirSeparator(sourcefolder[i]); } m_stat = stat; m_sourcefolder = sourcefolder; if (m_filter == null) m_filter = new XervBackup.Library.Utility.FilenameFilter(new List<KeyValuePair<bool, string>>()); }
public PathCollector(Snapshots.ISnapshotService snapshot, Options.SymlinkStrategy symlinkHandling, FileAttributes attributeMask, Utility.FilenameFilter filter, CommunicationStatistics stat) { m_symlinkHandling = symlinkHandling; m_filter = filter; m_attributeMask = attributeMask; m_snapshot = snapshot; m_stat = stat; }
/// <summary> /// Initializes a RSyncDir instance, binds it to the source folder, and reads in the supplied patches /// </summary> /// <param name="sourcefolder">The folders to create a backup from</param> /// <param name="stat">The status report object</param> /// <param name="filter">An optional filter that controls what files to include</param> /// <param name="patches">A list of signature archives to read, MUST be sorted in the creation order, oldest first</param> public RSyncDir(string[] sourcefolder, CommunicationStatistics stat, Utility.FilenameFilter filter, List<KeyValuePair<ManifestEntry, Library.Interface.ICompression>> patches) : this(sourcefolder, stat, filter) { string[] prefixes = new string[] { Utility.Utility.AppendDirSeparator(COMBINED_SIGNATURE_ROOT), Utility.Utility.AppendDirSeparator(CONTENT_SIGNATURE_ROOT), Utility.Utility.AppendDirSeparator(DELTA_SIGNATURE_ROOT) }; m_patches = new List<XervBackup.Library.Interface.ICompression>(); foreach (KeyValuePair<ManifestEntry, Library.Interface.ICompression> patch in patches) m_patches.Add(patch.Value); foreach (KeyValuePair<ManifestEntry, Library.Interface.ICompression> patch in patches) { Library.Interface.ICompression z = patch.Value; if (z.FileExists(DELETED_FILES)) foreach (string s in FilenamesFromPlatformIndependant(z.ReadAllLines(DELETED_FILES))) { m_oldSignatures.Remove(s); m_lastVerificationTime.Remove(s); m_oldSymlinks.Remove(s); } foreach (string prefix in prefixes) { ArchiveWrapper aw = new ArchiveWrapper(z, patch.Key.Time.ToUniversalTime(), prefix); foreach (string f in FilenamesFromPlatformIndependant(z.ListFiles(prefix))) { string name = f.Substring(prefix.Length); m_oldSignatures[name] = aw; m_lastVerificationTime.Remove(name); } } string symlinkprefix = Utility.Utility.AppendDirSeparator(SYMLINK_ROOT); foreach(string s in FilenamesFromPlatformIndependant(patch.Value.ListFiles(symlinkprefix))) { string tmp = FilenamesFromPlatformIndependant( new string[] { Encoding.UTF8.GetString(patch.Value.ReadAllBytes(s)) })[0]; m_oldSymlinks[s.Substring(symlinkprefix.Length)] = tmp; } if (z.FileExists(UNMODIFIED_FILES)) foreach (string s in FilenamesFromPlatformIndependant(z.ReadAllLines(UNMODIFIED_FILES))) m_lastVerificationTime[s] = patch.Key.Time.ToUniversalTime(); if (z.FileExists(DELETED_FOLDERS)) foreach (string s in FilenamesFromPlatformIndependant(z.ReadAllLines(DELETED_FOLDERS))) m_oldFolders.Remove(s); if (z.FileExists(ADDED_FOLDERS)) { DateTime t = z.GetLastWriteTime(ADDED_FOLDERS).ToUniversalTime(); string[] filenames = FilenamesFromPlatformIndependant(z.ReadAllLines(ADDED_FOLDERS)); if (z.FileExists(ADDED_FOLDERS_TIMESTAMPS)) { string[] timestamps = z.ReadAllLines(ADDED_FOLDERS_TIMESTAMPS); long l; for(int i = 0; i < Math.Min(filenames.Length, timestamps.Length); i++) if (long.TryParse(timestamps[i], out l)) m_oldFolders[filenames[i]] = Library.Utility.Utility.EPOCH.AddSeconds(l); else m_oldFolders[filenames[i]] = t; } else { foreach (string s in filenames) m_oldFolders[s] = t; } } if (z.FileExists(UPDATED_FOLDERS) && z.FileExists(UPDATED_FOLDERS_TIMESTAMPS)) { string[] filenames = FilenamesFromPlatformIndependant(z.ReadAllLines(UPDATED_FOLDERS)); string[] timestamps = z.ReadAllLines(UPDATED_FOLDERS_TIMESTAMPS); long l; for (int i = 0; i < Math.Min(filenames.Length, timestamps.Length); i++) if (long.TryParse(timestamps[i], out l)) m_oldFolders[filenames[i]] = Utility.Utility.EPOCH.AddSeconds(l); } //The latest file is the most valid if (z.FileExists(USN_VALUES)) using (System.IO.Stream s = z.OpenRead(USN_VALUES)) m_lastUSN = new USNRecord(s); } }
/// <summary> /// Updates the communication statistics. /// </summary> /// <param name="statistics">The updated statistics.</param> /// <param name="departmentId">The source department id.</param> public void UpdateStatistics(CommunicationStatistics statistics, Guid departmentId) { this.statisticsMapper.UpdateStatistics(statistics, departmentId); }