From ddf19fd14519266d94b976a244de67edbd778d57 Mon Sep 17 00:00:00 2001 From: Lior Banai Date: Fri, 25 Aug 2023 19:36:01 +0300 Subject: [PATCH] Change Time Offset is very slow fix #1769 --- .../Managers/PagingManager.cs | 12 +- .../UserControls/LogMessagesUC.cs | 8 +- .../UserControls/ServerSideLogMessagesUC.cs | 8 +- Analogy.CommonControls/Utils.cs | 15 + Analogy/CommonChangeLog.cs | 3 +- Analogy/Managers/PagingManager.cs | 346 ------------------ 6 files changed, 21 insertions(+), 371 deletions(-) delete mode 100644 Analogy/Managers/PagingManager.cs diff --git a/Analogy.CommonControls/Managers/PagingManager.cs b/Analogy.CommonControls/Managers/PagingManager.cs index 91311b19..88659147 100644 --- a/Analogy.CommonControls/Managers/PagingManager.cs +++ b/Analogy.CommonControls/Managers/PagingManager.cs @@ -141,7 +141,7 @@ public DataRow AppendMessage(IAnalogyLogMessage message, string dataSource) try { lockSlim.EnterWriteLock(); - DataRow dtr = Utils.CreateRow(table, message, dataSource,Settings.TimeOffsetType,Settings.TimeOffset); + DataRow dtr = Utils.CreateRow(table, message, dataSource, Settings.TimeOffsetType, Settings.TimeOffset); table.Rows.Add(dtr); return dtr; @@ -357,15 +357,7 @@ public void UpdateOffsets() lockSlim.EnterWriteLock(); foreach (DataTable dataTable in pages) { - dataTable.BeginLoadData(); - foreach (DataRow dataTableRow in dataTable.Rows) - { - dataTableRow.BeginEdit(); - AnalogyLogMessage m = (AnalogyLogMessage)dataTableRow[Common.CommonUtils.AnalogyMessageColumn]; - dataTableRow["Date"] = Utils.GetOffsetTime(m.Date, Settings.TimeOffsetType, Settings.TimeOffset); - dataTableRow.EndEdit(); - } - dataTable.EndLoadData(); + Utils.ChangeOffset(dataTable, Settings); } lockSlim.ExitWriteLock(); } diff --git a/Analogy.CommonControls/UserControls/LogMessagesUC.cs b/Analogy.CommonControls/UserControls/LogMessagesUC.cs index a7e76b63..c11d4807 100644 --- a/Analogy.CommonControls/UserControls/LogMessagesUC.cs +++ b/Analogy.CommonControls/UserControls/LogMessagesUC.cs @@ -1034,13 +1034,7 @@ private void GridView_ShownEditor(object sender, System.EventArgs e) private void RefreshTimeOffset() { PagingManager.UpdateOffsets(); - foreach (DataRow dataTableRow in _bookmarkedMessages.Rows) - { - dataTableRow.BeginEdit(); - AnalogyLogMessage m = (AnalogyLogMessage)dataTableRow[Common.CommonUtils.AnalogyMessageColumn]; - dataTableRow["Date"] = Utils.GetOffsetTime(m.Date, Settings.TimeOffsetType, Settings.TimeOffset); - dataTableRow.EndEdit(); - } + Utils.ChangeOffset(_bookmarkedMessages, Settings); } diff --git a/Analogy.CommonControls/UserControls/ServerSideLogMessagesUC.cs b/Analogy.CommonControls/UserControls/ServerSideLogMessagesUC.cs index a139e4f8..adbc7134 100644 --- a/Analogy.CommonControls/UserControls/ServerSideLogMessagesUC.cs +++ b/Analogy.CommonControls/UserControls/ServerSideLogMessagesUC.cs @@ -826,13 +826,7 @@ private void GridView_ShownEditor(object sender, System.EventArgs e) private void RefreshTimeOffset() { PagingManager.UpdateOffsets(); - foreach (DataRow dataTableRow in _bookmarkedMessages.Rows) - { - dataTableRow.BeginEdit(); - AnalogyLogMessage m = (AnalogyLogMessage)dataTableRow[Common.CommonUtils.AnalogyMessageColumn]; - dataTableRow["Date"] = Utils.GetOffsetTime(m.Date, Settings.TimeOffsetType, Settings.TimeOffset); - dataTableRow.EndEdit(); - } + Utils.ChangeOffset(_bookmarkedMessages, Settings); } diff --git a/Analogy.CommonControls/Utils.cs b/Analogy.CommonControls/Utils.cs index f18d67a5..df699d29 100644 --- a/Analogy.CommonControls/Utils.cs +++ b/Analogy.CommonControls/Utils.cs @@ -6,6 +6,7 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Analogy.Common.DataTypes; +using Analogy.Common.Interfaces; using Analogy.CommonControls.DataTypes; using Analogy.Interfaces; using Analogy.Interfaces.DataTypes; @@ -114,6 +115,20 @@ public static DateTime GetOffsetTime(DateTime time, TimeOffsetType timeOffsetTyp _ => time }; } + + public static void ChangeOffset(DataTable dataTable, IUserSettingsManager settings) + { + dataTable.BeginLoadData(); + foreach (DataRow row in dataTable.Rows) + { + row.BeginEdit(); + AnalogyLogMessage m = (AnalogyLogMessage)row[Common.CommonUtils.AnalogyMessageColumn]; + row["Date"] = Utils.GetOffsetTime(m.Date, settings.TimeOffsetType, settings.TimeOffset); + row.EndEdit(); + row.AcceptChanges(); + } + dataTable.EndLoadData(); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static DataTable DataTableConstructor() { diff --git a/Analogy/CommonChangeLog.cs b/Analogy/CommonChangeLog.cs index 58f6ce0f..9a64b648 100644 --- a/Analogy/CommonChangeLog.cs +++ b/Analogy/CommonChangeLog.cs @@ -8,7 +8,8 @@ public static class CommonChangeLog public static IEnumerable GetChangeLog() { return new List - { + { + new ("V5.0.0 - Change Time Offset is very slow #1769", AnalogChangeLogType.Bug,"Lior Banai",new DateTime(2023,08,25),"5.0.0.0"), new ("V5.0.0 - Message viewer doesn't understand Linux-style new lines #1770", AnalogChangeLogType.Improvement,"Lior Banai",new DateTime(2023,08,25),"5.0.0.0"), new ("V5.0.0 - Add Search everywhere checkbox #1812",AnalogChangeLogType.Improvement,"Lior Banai",new DateTime(2023,07,18), "5.0.0.0"), new ("V5.0.0 - Add NET8 initial Support #1814",AnalogChangeLogType.Improvement,"Lior Banai",new DateTime(2023,07,18), "5.0.0.0"), diff --git a/Analogy/Managers/PagingManager.cs b/Analogy/Managers/PagingManager.cs deleted file mode 100644 index 5be18be4..00000000 --- a/Analogy/Managers/PagingManager.cs +++ /dev/null @@ -1,346 +0,0 @@ -using Analogy.DataTypes; -using Analogy.Interfaces; -using System.Collections.Generic; -using System.Data; -using System.Windows.Forms; -using Analogy.Common.Interfaces; -using Analogy.CommonControls.DataTypes; - -namespace Analogy -{ - public class PagingManager - { - //public List CurrentColumns { get; set; } - private static ManualResetEvent columnAdderSync = new ManualResetEvent(false); - public ReaderWriterLockSlim columnsLockSlim = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - private readonly UserControl owner; - public event EventHandler OnHistoryCleared; - public event EventHandler OnPageChanged; - public ReaderWriterLockSlim lockSlim = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - private IUserSettingsManager Settings => ServicesProvider.Instance.GetService(); - private List pages; - private readonly List allMessages; - private readonly int pageSize; - private int currentPageStartRowIndex; - private int currentPageNumber; - - private DataTable currentTable; - private static int _totalMissedMessages; - - public int TotalPages - { - get - { - lockSlim.EnterReadLock(); - var count = pages.Count; - lockSlim.ExitReadLock(); - return count; - - } - } - - public static int TotalMissedMessages => _totalMissedMessages; - - public PagingManager(UserControl owner) - { - //CurrentColumns = new List(); - this.owner = owner; - pageSize = Settings.PagingEnabled ? Settings.PagingSize : int.MaxValue; - pages = new List(); - - currentTable = Analogy.CommonControls.Utils.DataTableConstructor(); - //foreach (DataColumn column in currentTable.Columns) - //{ - // CurrentColumns.Add(column.ColumnName); - //} - pages.Add(currentTable); - currentPageNumber = 1; - allMessages = new List(); - } - - - public IEnumerable> SplitList(List locations) - { - for (int i = 0; i < locations.Count; i += pageSize) - { - yield return locations.GetRange(i, Math.Min(pageSize, locations.Count - i)); - } - } - - - public void ClearLogs() - { - lockSlim.EnterWriteLock(); - var oldMessages = allMessages.ToList(); - try - { - pages = new List(); - currentPageStartRowIndex = 0; - currentPageNumber = 1; - var first = Analogy.CommonControls.Utils.DataTableConstructor(); - currentTable = first; - pages.Add(first); - } - finally - { - lockSlim.ExitWriteLock(); - } - OnHistoryCleared?.Invoke(this, new AnalogyClearedHistoryEventArgs(oldMessages)); - AnalogyPageInformation analogyPage = new AnalogyPageInformation(currentTable, 1, currentPageStartRowIndex); - OnPageChanged?.Invoke(this, new AnalogyPagingChanged(analogyPage)); - } - - public DataRow AppendMessage(AnalogyLogMessage message, string dataSource) - { - try - { - lockSlim.EnterWriteLock(); - allMessages.Add(message); - } - finally - { - lockSlim.ExitWriteLock(); - } - var table = pages.Last(); - if (table.Rows.Count + 1 > pageSize) - { - table = Analogy.CommonControls.Utils.DataTableConstructor(); - pages.Add(table); - var pageStartRowIndex = (pages.Count - 1) * pageSize; - OnPageChanged?.Invoke(this, new AnalogyPagingChanged(new AnalogyPageInformation(table, pages.Count, pageStartRowIndex))); - } - - if (message.AdditionalProperties != null && message.AdditionalProperties.Any() && Settings.CheckAdditionalInformation) - { - AddExtraColumnsIfNeeded(table, message); - } - try - { - lockSlim.EnterWriteLock(); - DataRow dtr = CommonControls.Utils.CreateRow(table, message, dataSource, Settings.TimeOffsetType,Settings.TimeOffset); - table.Rows.Add(dtr); - return dtr; - - } - finally - { - lockSlim.ExitWriteLock(); - } - } - - public List<(DataRow, AnalogyLogMessage)> AppendMessages(List messages, string dataSource) - { - - var table = pages.Last(); - var countInsideTable = table.Rows.Count; - List<(DataRow row, AnalogyLogMessage message)> rows = new List<(DataRow row, AnalogyLogMessage message)>(messages.Count); - foreach (var message in messages) - { - if (message.Level == AnalogyLogLevel.None) - { - continue; //ignore those messages - } - try - { - lockSlim.EnterWriteLock(); - allMessages.Add(message); - } - finally - { - lockSlim.ExitWriteLock(); - } - if (countInsideTable + 1 > pageSize) - { - table = Analogy.CommonControls.Utils.DataTableConstructor(); - pages.Add(table); - countInsideTable = 0; - var pageStartRowIndex = (pages.Count - 1) * pageSize; - OnPageChanged?.Invoke(this, - new AnalogyPagingChanged(new AnalogyPageInformation(table, pages.Count, - pageStartRowIndex))); - } - - if (message.AdditionalProperties != null && message.AdditionalProperties.Any() && - Settings.CheckAdditionalInformation) - { - AddExtraColumnsIfNeeded(table, message); - } - - countInsideTable++; - try - { - lockSlim.EnterWriteLock(); - DataRow dtr =CommonControls.Utils.CreateRow(table, message, dataSource, Settings.TimeOffsetType,Settings.TimeOffset); - table.Rows.Add(dtr); - rows.Add((dtr, message)); - } - finally - { - lockSlim.ExitWriteLock(); - } - - } - return rows; - } - - private void AddExtraColumnsIfNeeded(DataTable table, AnalogyLogMessage message) - { - if (message.AdditionalProperties != null && message.AdditionalProperties.Any() && Settings.CheckAdditionalInformation) - { - foreach (KeyValuePair info in message.AdditionalProperties) - { - - if (!table.Columns.Contains(info.Key)) - { - if (!owner.InvokeRequired) - { - try - { - columnsLockSlim.EnterWriteLock(); - if (!table.Columns.Contains(info.Key)) - { - table.Columns.Add(info.Key); - } - } - finally - { - columnsLockSlim.ExitWriteLock(); - } - - - - } - else - { - owner.BeginInvoke(new MethodInvoker(() => - { - try - { - - columnsLockSlim.EnterWriteLock(); - if (!table.Columns.Contains(info.Key)) - { - columnsLockSlim.EnterWriteLock(); - if (!table.Columns.Contains(info.Key)) - { - table.Columns.Add(info.Key); - columnAdderSync.Set(); - } - - } - } - finally - { - columnsLockSlim.ExitWriteLock(); - } - })); - columnAdderSync.WaitOne(); - columnAdderSync.Reset(); - } - } - } - } - } - - public AnalogyPageInformation NextPage() - { - lockSlim.EnterReadLock(); - - if (pages.Last() != currentTable) - { - currentPageNumber++; - currentTable = pages[currentPageNumber - 1]; - currentPageStartRowIndex = pages.IndexOf(currentTable) * pageSize; - } - lockSlim.ExitReadLock(); - return new AnalogyPageInformation(currentTable, currentPageNumber, currentPageStartRowIndex); - - } - - public AnalogyPageInformation PrevPage() - { - lockSlim.EnterReadLock(); - if (pages.First() != currentTable) - { - currentPageNumber--; - currentTable = pages[currentPageNumber - 1]; - currentPageStartRowIndex = pages.IndexOf(currentTable) * pageSize; - } - lockSlim.ExitReadLock(); - return new AnalogyPageInformation(currentTable, currentPageNumber, currentPageStartRowIndex); - - } - - public DataTable FirstPage() - { - lockSlim.EnterReadLock(); - currentTable = pages.First(); - currentPageNumber = 1; - lockSlim.ExitReadLock(); - return currentTable; - - } - public DataTable LastPage() - { - lockSlim.EnterReadLock(); - currentTable = pages.Last(); - currentPageNumber = pages.Count; - lockSlim.ExitReadLock(); - return currentTable; - - } - - public List GetAllMessages() - { - try - { - lockSlim.EnterReadLock(); - var items = allMessages.ToList(); - return items; - } - finally - { - lockSlim.ExitReadLock(); - } - } - - public DataTable CurrentPage() - { - lockSlim.EnterReadLock(); - var table = currentTable; - lockSlim.ExitReadLock(); - return table; - } - - public bool IsCurrentPageInView(DataTable currentView) - { - lockSlim.EnterReadLock(); - var current = currentTable == currentView; - lockSlim.ExitReadLock(); - return current; - - } - - public void IncrementTotalMissedMessages() - { - Interlocked.Increment(ref _totalMissedMessages); - } - - public void UpdateOffsets() - { - lockSlim.EnterWriteLock(); - foreach (DataTable dataTable in pages) - { - foreach (DataRow dataTableRow in dataTable.Rows) - { - dataTableRow.BeginEdit(); - AnalogyLogMessage m = (AnalogyLogMessage)dataTableRow[Common.CommonUtils.AnalogyMessageColumn]; - dataTableRow["Date"] = Utils.GetOffsetTime(m.Date); - dataTableRow.EndEdit(); - } - } - lockSlim.ExitWriteLock(); - } - } - -}