Skip to content

Commit

Permalink
Update TuneUpWindowViewModel.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
ivaylo-matov committed Nov 4, 2024
1 parent cd6d125 commit b767859
Showing 1 changed file with 98 additions and 103 deletions.
201 changes: 98 additions & 103 deletions TuneUp/TuneUpWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProfiledNodeViewModel>();
// Create group nodes for not executed
var processedNodesNotExecuted = new HashSet<ProfiledNodeViewModel>();

// 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);
Expand All @@ -591,66 +584,63 @@ private void CalculateGroupNodes()

private void CreateGroupNodesForCollection(ObservableCollection<ProfiledNodeViewModel> collection)
{
Task.Run(() =>
int executionCounter = 1;
var processedNodes = new HashSet<ProfiledNodeViewModel>();

var sortedNodes = collection.OrderBy(n => n.ExecutionOrderNumber).ToList();

foreach (var pNode in sortedNodes)
{
int executionCounter = 1;
var processedNodes = new HashSet<ProfiledNodeViewModel>();
// 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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -1321,15 +1322,9 @@ private void MoveNodeToCollection(ProfiledNodeViewModel profiledNode, Observable
/// </summary>
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
Expand Down

0 comments on commit b767859

Please sign in to comment.