From b767859e3ec418f12ab57c8a9b0fb6cb4c553e4b Mon Sep 17 00:00:00 2001 From: Ivo Petrov Date: Mon, 4 Nov 2024 18:26:13 +0000 Subject: [PATCH] Update TuneUpWindowViewModel.cs --- TuneUp/TuneUpWindowViewModel.cs | 201 ++++++++++++++++---------------- 1 file changed, 98 insertions(+), 103 deletions(-) diff --git a/TuneUp/TuneUpWindowViewModel.cs b/TuneUp/TuneUpWindowViewModel.cs index b420230..1a23bca 100644 --- a/TuneUp/TuneUpWindowViewModel.cs +++ b/TuneUp/TuneUpWindowViewModel.cs @@ -520,67 +520,60 @@ private void CalculateGroupNodes() { Task.Run(() => { - // Clean the collections from all group and time nodes - foreach (var node in groupDictionary.Values) + // Apply all removals and additions on the UI thread + uiContext.Post(_ => { - RemoveNodeFromStateCollection(node, node.State); - - if (groupModelDictionary.TryGetValue(node.GroupGUID, out var groupNodes)) + // Clean the collections from all group and time nodes + foreach (var node in groupDictionary.Values) { - groupNodes.Remove(node); - } - } - groupDictionary.Clear(); + RemoveNodeFromStateCollection(node, node.State); - var a1 = ProfiledNodesLatestRun; - var a2 = ProfiledNodesPreviousRun; - var a3 = ProfiledNodesNotExecuted; - - // Create group and time nodes for latest and previous runs - CreateGroupNodesForCollection(ProfiledNodesLatestRun); - CreateGroupNodesForCollection(ProfiledNodesPreviousRun); + if (groupModelDictionary.TryGetValue(node.GroupGUID, out var groupNodes)) + { + groupNodes.Remove(node); + } + } + groupDictionary.Clear(); - var b1 = ProfiledNodesLatestRun; - var b2 = ProfiledNodesPreviousRun; - var b3 = ProfiledNodesNotExecuted; + // Create group and time nodes for latest and previous runs + CreateGroupNodesForCollection(ProfiledNodesLatestRun); + CreateGroupNodesForCollection(ProfiledNodesPreviousRun); - // Create group nodes for not executed - var processedNodesNotExecuted = new HashSet(); + // Create group nodes for not executed + var processedNodesNotExecuted = new HashSet(); - // Create a copy of ProfiledNodesNotExecuted to iterate over - var profiledNodesCopy = ProfiledNodesNotExecuted.ToList(); + // Create a copy of ProfiledNodesNotExecuted to iterate over + var profiledNodesCopy = ProfiledNodesNotExecuted.ToList(); - foreach (var pNode in profiledNodesCopy) - { - if (pNode.GroupGUID != Guid.Empty && !processedNodesNotExecuted.Contains(pNode)) + foreach (var pNode in profiledNodesCopy) { - // get the other nodes from this group - var nodesInGroup = ProfiledNodesNotExecuted - .Where(n => n.GroupGUID == pNode.GroupGUID) - .ToList(); - - foreach (var node in nodesInGroup) + if (pNode.GroupGUID != Guid.Empty && !processedNodesNotExecuted.Contains(pNode)) { - processedNodesNotExecuted.Add(node); - } + // get the other nodes from this group + var nodesInGroup = ProfiledNodesNotExecuted + .Where(n => n.GroupGUID == pNode.GroupGUID) + .ToList(); - // create new group node - var pGroup = new ProfiledNodeViewModel(pNode); + foreach (var node in nodesInGroup) + { + processedNodesNotExecuted.Add(node); + } - groupDictionary[pGroup.NodeGUID] = pGroup; - groupModelDictionary[pNode.GroupGUID].Add(pGroup); + // create new group node + var pGroup = new ProfiledNodeViewModel(pNode); - uiContext.Post(_ => - { - ProfiledNodesNotExecuted.Add(pGroup); - }, null); + groupDictionary[pGroup.NodeGUID] = pGroup; + groupModelDictionary[pNode.GroupGUID].Add(pGroup); + + ProfiledNodesCollectionLatestRun.Dispatcher.Invoke(() => + { + ProfiledNodesNotExecuted.Add(pGroup); + }); + } } - } - // Additional sorting to prevent group nodes from appearing at the bottom of the DataGrid - // when consecutive graphs are opened while TuneUp is enabled. - uiContext.Post(_ => - { + // Additional sorting to prevent group nodes from appearing at the bottom of the DataGrid + // when consecutive graphs are opened while TuneUp is enabled. ApplyCustomSorting(ProfiledNodesCollectionLatestRun); RaisePropertyChanged(nameof(ProfiledNodesCollectionLatestRun)); ApplyCustomSorting(ProfiledNodesCollectionPreviousRun); @@ -591,66 +584,63 @@ private void CalculateGroupNodes() private void CreateGroupNodesForCollection(ObservableCollection collection) { - Task.Run(() => + int executionCounter = 1; + var processedNodes = new HashSet(); + + var sortedNodes = collection.OrderBy(n => n.ExecutionOrderNumber).ToList(); + + foreach (var pNode in sortedNodes) { - int executionCounter = 1; - var processedNodes = new HashSet(); + // Process the standalone nodes + if (pNode.GroupGUID == Guid.Empty && !processedNodes.Contains(pNode)) + { + pNode.GroupExecutionMilliseconds = pNode.ExecutionMilliseconds; + pNode.ExecutionOrderNumber = executionCounter; + pNode.GroupExecutionOrderNumber = executionCounter++; - var sortedNodes = collection.OrderBy(n => n.ExecutionOrderNumber).ToList(); + processedNodes.Add(pNode); + } - foreach (var pNode in sortedNodes) + // Process the grouped nodes + else if (pNode.GroupGUID != Guid.Empty && !processedNodes.Contains(pNode)) { - // Process the standalone nodes - if (pNode.GroupGUID == Guid.Empty && !processedNodes.Contains(pNode)) - { - pNode.GroupExecutionMilliseconds = pNode.ExecutionMilliseconds; - pNode.ExecutionOrderNumber = executionCounter; - pNode.GroupExecutionOrderNumber = executionCounter++; + // Get all nodes in the same group and calculate the group execution time + int groupExecTime = 0; + var nodesInGroup = sortedNodes.Where(n => n.GroupGUID == pNode.GroupGUID).ToList(); - processedNodes.Add(pNode); + foreach (var node in nodesInGroup) + { + processedNodes.Add(node); + groupExecTime += node.ExecutionMilliseconds; } - // Process the grouped nodes - else if (pNode.GroupGUID != Guid.Empty && !processedNodes.Contains(pNode)) + // Create and register a new group node using the current profiled node + var pGroup = new ProfiledNodeViewModel(pNode) { - // Get all nodes in the same group and calculate the group execution time - int groupExecTime = 0; - var nodesInGroup = sortedNodes.Where(n => n.GroupGUID == pNode.GroupGUID).ToList(); - - foreach (var node in nodesInGroup) - { - processedNodes.Add(node); - groupExecTime += node.ExecutionMilliseconds; - } - - // Create and register a new group node using the current profiled node - var pGroup = new ProfiledNodeViewModel(pNode) - { - GroupExecutionOrderNumber = executionCounter++, - GroupExecutionMilliseconds = groupExecTime - }; + GroupExecutionOrderNumber = executionCounter++, + GroupExecutionMilliseconds = groupExecTime + }; - groupDictionary[pGroup.NodeGUID] = pGroup; - groupModelDictionary[pNode.GroupGUID].Add(pGroup); + groupDictionary[pGroup.NodeGUID] = pGroup; + groupModelDictionary[pNode.GroupGUID].Add(pGroup); - // Create an register a new time node - var timeNode = CreateAndRegisterGroupTimeNode(pGroup); + // Create an register a new time node + var timeNode = CreateAndRegisterGroupTimeNode(pGroup); - uiContext.Post(_ => - { - collection.Add(timeNode); - collection.Add(pGroup); - }, null); + GetCollectionViewSource(collection).Dispatcher.Invoke(() => + { + collection.Add(timeNode); + collection.Add(pGroup); + }); - // Update group-related properties for all nodes in the group - foreach (var node in nodesInGroup) - { - node.GroupExecutionOrderNumber = pGroup.GroupExecutionOrderNumber; - node.GroupExecutionMilliseconds = pGroup.GroupExecutionMilliseconds; - } + // Update group-related properties for all nodes in the group + foreach (var node in nodesInGroup) + { + node.GroupExecutionOrderNumber = pGroup.GroupExecutionOrderNumber; + node.GroupExecutionMilliseconds = pGroup.GroupExecutionMilliseconds; } } - }); + } } internal void OnNodeExecutionBegin(NodeModel nm) @@ -964,7 +954,9 @@ private void CurrentWorkspaceModel_GroupAdded(AnnotationModel group) // Create and register time node var timeNode = CreateAndRegisterGroupTimeNode(pGroup); - collection.Add(timeNode); + + uiContext.Send(_ => { collection.Add(timeNode); }, null); + } // Apply group properties @@ -978,7 +970,16 @@ private void CurrentWorkspaceModel_GroupAdded(AnnotationModel group) } // Ensure new group nodes are sorted properly - ApplyCustomSorting(ProfiledNodesCollectionNotExecuted, SortByName); + uiContext.Post(_ => + { + ApplyCustomSorting(ProfiledNodesCollectionLatestRun); + ProfiledNodesCollectionLatestRun.View?.Refresh(); + ApplyCustomSorting(ProfiledNodesCollectionPreviousRun); + ProfiledNodesCollectionPreviousRun.View?.Refresh(); + ApplyCustomSorting(ProfiledNodesCollectionNotExecuted, SortByName); + ProfiledNodesCollectionNotExecuted.View?.Refresh(); + }, null); + } private void CurrentWorkspaceModel_GroupRemoved(AnnotationModel group) @@ -1321,15 +1322,9 @@ private void MoveNodeToCollection(ProfiledNodeViewModel profiledNode, Observable /// private void RemoveNodeFromStateCollection(ProfiledNodeViewModel pNode, ProfiledNodeState state) { - Task.Run(() => - { - var collection = GetObservableCollectionFromState(state); + var collection = GetObservableCollectionFromState(state); - uiContext.Post(_ => - { - collection?.Remove(pNode); - }, null); - }); + collection?.Remove(pNode); } #endregion