Skip to content

Commit

Permalink
[DYN-7577] Removed groups with only notes inside (#82)
Browse files Browse the repository at this point in the history
* note groups sorted

* create group node methods
  • Loading branch information
ivaylo-matov authored Nov 8, 2024
1 parent faeb5e8 commit 460c280
Showing 1 changed file with 67 additions and 43 deletions.
110 changes: 67 additions & 43 deletions TuneUp/TuneUpWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,18 +327,22 @@ internal void ResetProfiledNodes()
// Create a profiled node for each AnnotationModel
foreach (var group in CurrentWorkspace.Annotations)
{
var pGroup = new ProfiledNodeViewModel(group);
ProfiledNodesNotExecuted.Add(pGroup);
groupDictionary[pGroup.NodeGUID] = (pGroup);
groupModelDictionary[group.GUID] = new List<ProfiledNodeViewModel> { pGroup };
// Initialize an empty entry for each group in groupModelDictionary
groupModelDictionary[group.GUID] = new List<ProfiledNodeViewModel> ();

var groupedNodeGUIDs = group.Nodes.OfType<NodeModel>().Select(n => n.GUID);

foreach (var nodeGuid in groupedNodeGUIDs)
// Only create and add groups to ProfiledNodesNotExecuted if they contain NodeModel instances
if (group.Nodes.Any(n => n is NodeModel))
{
if (nodeDictionary.TryGetValue(nodeGuid, out var pNode))
var pGroup = CreateAndRegisterGroupNode(group, ProfiledNodesNotExecuted);

var groupedNodeGUIDs = group.Nodes.OfType<NodeModel>().Select(n => n.GUID);

foreach (var nodeGuid in groupedNodeGUIDs)
{
ApplyGroupPropertiesAndRegisterNode(pNode, pGroup);
if (nodeDictionary.TryGetValue(nodeGuid, out var pNode))
{
ApplyGroupPropertiesAndRegisterNode(pNode, pGroup);
}
}
}
}
Expand Down Expand Up @@ -542,11 +546,7 @@ private void CalculateGroupNodes()
}

// create new group node
var pGroup = new ProfiledNodeViewModel(pNode);

groupDictionary[pGroup.NodeGUID] = pGroup;
groupModelDictionary[pNode.GroupGUID].Add(pGroup);

var pGroup = CreateAndRegisterGroupNode(pNode);
uiContext.Send(_ => ProfiledNodesNotExecuted.Add(pGroup), null);
}
}
Expand Down Expand Up @@ -589,17 +589,12 @@ private void CreateGroupNodesForCollection(ObservableCollection<ProfiledNodeView
}

// Create and register a new group node using the current profiled node
var pGroup = new ProfiledNodeViewModel(pNode)
{
GroupExecutionOrderNumber = executionCounter++,
GroupExecutionMilliseconds = groupExecTime,
GroupModel = CurrentWorkspace.Annotations.First(n => n.GUID.Equals(pNode.GroupGUID))
};
var pGroup = CreateAndRegisterGroupNode(pNode);
pGroup.GroupExecutionOrderNumber = executionCounter++;
pGroup.GroupExecutionMilliseconds = groupExecTime;
pGroup.GroupModel = CurrentWorkspace.Annotations.First(n => n.GUID.Equals(pNode.GroupGUID));
collection.Add(pGroup);

groupDictionary[pGroup.NodeGUID] = pGroup;
groupModelDictionary[pNode.GroupGUID].Add(pGroup);

// Create an register a new time node
var timeNode = CreateAndRegisterGroupTimeNode(pGroup);
collection.Add(timeNode);
Expand Down Expand Up @@ -699,20 +694,19 @@ internal void OnGroupPropertyChanged(object sender, PropertyChangedEventArgs e)
}
}

// Detect change of nodes
if (e.PropertyName == nameof(groupModel.Nodes))
{
var allNodesInGroup = groupModelDictionary[groupModel.GUID];

var modelNodeGuids = groupModel.Nodes
.OfType<NodeModel>()
.Select(n => n.GUID)
.ToHashSet();

// Determine if we adding or removing a node
var pNodeToRemove = allNodesInGroup
var pNodeToRemove = nodesInGroup
.FirstOrDefault(n => !n.IsGroup && !n.IsGroupExecutionTime && !modelNodeGuids.Contains(n.NodeGUID));

var pNodeToAdd = nodeDictionary.FirstOrDefault(kvp => modelNodeGuids.Contains(kvp.Key) && !allNodesInGroup.Contains(kvp.Value)).Value;
var pNodeToAdd = nodeDictionary
.FirstOrDefault(kvp => modelNodeGuids.Contains(kvp.Key) && !nodesInGroup.Contains(kvp.Value)).Value;

var (pNodeModified, addNode) = pNodeToRemove == null ? (pNodeToAdd, true) : (pNodeToRemove, false);

Expand All @@ -723,7 +717,7 @@ internal void OnGroupPropertyChanged(object sender, PropertyChangedEventArgs e)
collection = GetObservableCollectionFromState(state);

// Get all nodes for this group in the same state
var allNodesInGroupForState = allNodesInGroup.Where(n => n.State == state).ToList();
var allNodesInGroupForState = nodesInGroup.Where(n => n.State == state).ToList();
var pGroupToModify = allNodesInGroupForState.FirstOrDefault(n => n.IsGroup);
var timeNodeToModify = allNodesInGroupForState.FirstOrDefault(n => n.IsGroupExecutionTime);
var pNodesOfSameState = allNodesInGroupForState.Where(n => !n.IsGroupExecutionTime && !n.IsGroup).ToList();
Expand All @@ -734,9 +728,16 @@ internal void OnGroupPropertyChanged(object sender, PropertyChangedEventArgs e)
ResetGroupPropertiesAndUnregisterNode(pNodeModified);
pNodesOfSameState.Remove(pNodeModified);

// Update group execution time
if (state != ProfiledNodeState.NotExecuted && pGroupToModify != null && timeNodeToModify != null)
// check if there are any nodes left of the same state
if (!pNodesOfSameState.Any())
{
// remove the group node and time node from the collection
collection.Remove(pGroupToModify);
collection.Remove(timeNodeToModify);
}
else if (state != ProfiledNodeState.NotExecuted && pGroupToModify != null && timeNodeToModify != null)
{
// Update group execution time
pGroupToModify.GroupExecutionMilliseconds -= pNodeModified.ExecutionMilliseconds;
pGroupToModify.ExecutionMilliseconds = pGroupToModify.GroupExecutionMilliseconds;
timeNodeToModify.GroupExecutionMilliseconds = pGroupToModify.GroupExecutionMilliseconds;
Expand All @@ -749,15 +750,10 @@ internal void OnGroupPropertyChanged(object sender, PropertyChangedEventArgs e)
// Create a new group node if it doesn't exist for this state
if (pGroupToModify == null)
{
pGroupToModify = new ProfiledNodeViewModel(groupModel) { State = state };
collection.Add(pGroupToModify);
groupDictionary[pGroupToModify.NodeGUID] = pGroupToModify;
groupModelDictionary[groupModel.GUID].Add(pGroupToModify);
pGroupToModify = CreateAndRegisterGroupNode(groupModel, collection);
pGroupToModify.State = state;

if (timeNodeToModify == null)
{
timeNodeToModify = CreateAndRegisterGroupTimeNode(pGroupToModify);
}
timeNodeToModify ??= CreateAndRegisterGroupTimeNode(pGroupToModify);
}

ApplyGroupPropertiesAndRegisterNode(pNodeModified, pGroupToModify);
Expand Down Expand Up @@ -798,6 +794,7 @@ internal void OnGroupPropertyChanged(object sender, PropertyChangedEventArgs e)
if (collection != null)
{
SortCollectionViewForProfiledNodesCollection(collection);
UpdateTableVisibility();
}
}
}
Expand Down Expand Up @@ -907,10 +904,8 @@ private void CurrentWorkspaceModel_GroupAdded(AnnotationModel group)
var collection = GetObservableCollectionFromState(state);

// Create and log new group node
var pGroup = new ProfiledNodeViewModel(group) { State = state };
groupModelDictionary[groupGUID].Add(pGroup);
groupDictionary[pGroup.NodeGUID] = pGroup;
collection.Add(pGroup);
var pGroup = CreateAndRegisterGroupNode(group, collection);
pGroup.State = state;

// Accumulate execution times and create a time node
if (collection != ProfiledNodesNotExecuted)
Expand Down Expand Up @@ -1103,6 +1098,35 @@ private ProfiledNodeViewModel CreateAndRegisterGroupTimeNode(ProfiledNodeViewMod
return timeNode;
}

/// <summary>
/// Creates and registers a group node for an AnnotationModel.
/// Adds it to the specified collection and dictionaries.
/// </summary>
private ProfiledNodeViewModel CreateAndRegisterGroupNode(
AnnotationModel groupModel,
ObservableCollection<ProfiledNodeViewModel> collection)
{
var pGroup = new ProfiledNodeViewModel(groupModel);
collection.Add(pGroup);
groupDictionary[pGroup.NodeGUID] = pGroup;
groupModelDictionary[groupModel.GUID].Add(pGroup);

return pGroup;
}

/// <summary>
/// Creates and registers a group node based on an existing ProfiledNodeViewModel.
/// Adding it to group-related dictionaries.
/// <returns></returns>
private ProfiledNodeViewModel CreateAndRegisterGroupNode(ProfiledNodeViewModel pNode)
{
var pGroup = new ProfiledNodeViewModel(pNode);
groupDictionary[pGroup.NodeGUID] = pGroup;
groupModelDictionary[pNode.GroupGUID].Add(pGroup);

return pGroup;
}

/// <summary>
/// Returns the appropriate ObservableCollection based on the node's profiling state.
/// </summary>
Expand Down

0 comments on commit 460c280

Please sign in to comment.