Skip to content

Commit ba865f7

Browse files
committed
Clean up
1 parent a06f2a5 commit ba865f7

File tree

3 files changed

+54
-52
lines changed

3 files changed

+54
-52
lines changed

src/Aspire.Dashboard/Components/Pages/Resources.razor.cs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -133,57 +133,6 @@ static bool UnionWithKeys(ConcurrentDictionary<string, bool> left, ConcurrentDic
133133
}
134134
}
135135

136-
private static string? GetResourcePropertyValue(ResourceViewModel resourceViewModel)
137-
{
138-
if (resourceViewModel.Properties.TryGetValue(KnownProperties.Resource.ParentName, out var value))
139-
{
140-
if (value.Value.TryConvertToString(out var s))
141-
{
142-
return s;
143-
}
144-
}
145-
146-
return null;
147-
}
148-
149-
private List<ResourceGridViewModel> OrderNestedResources(List<ResourceGridViewModel> initialGridVMs)
150-
{
151-
// This method loops over the list of grid view models to build the nested list.
152-
// Apps shouldn't have a huge number of resources (thousands) so this shouldn't impact performance,
153-
// but if that changes then this method will need to be improved.
154-
155-
var gridViewModels = new List<ResourceGridViewModel>();
156-
var depth = 0;
157-
158-
foreach (var gridVM in initialGridVMs.Where(r => GetResourcePropertyValue(r.Resource) is null))
159-
{
160-
gridVM.Depth = depth;
161-
gridVM.IsCollapsed = !_expandedResourceNames.Contains(gridVM.Resource.Name);
162-
gridVM.IsHidden = false;
163-
164-
gridViewModels.Add(gridVM);
165-
166-
AddChildViewModel(gridVM.Resource, gridVM, depth + 1, hidden: gridVM.IsCollapsed);
167-
}
168-
169-
return gridViewModels;
170-
171-
void AddChildViewModel(ResourceViewModel resource, ResourceGridViewModel parent, int depth, bool hidden)
172-
{
173-
foreach (var childGridVM in initialGridVMs.Where(r => GetResourcePropertyValue(r.Resource) == resource.Name))
174-
{
175-
childGridVM.Depth = depth;
176-
childGridVM.IsCollapsed = !_expandedResourceNames.Contains(childGridVM.Resource.Name);
177-
childGridVM.IsHidden = hidden;
178-
179-
parent.Children.Add(childGridVM);
180-
gridViewModels.Add(childGridVM);
181-
182-
AddChildViewModel(childGridVM.Resource, childGridVM, depth + 1, hidden: childGridVM.IsHidden || childGridVM.IsCollapsed);
183-
}
184-
}
185-
}
186-
187136
private readonly GridSort<ResourceGridViewModel> _nameSort = GridSort<ResourceGridViewModel>.ByAscending(p => p.Resource, ResourceViewModelNameComparer.Instance);
188137
private readonly GridSort<ResourceGridViewModel> _stateSort = GridSort<ResourceGridViewModel>.ByAscending(p => p.Resource.State).ThenAscending(p => p.Resource, ResourceViewModelNameComparer.Instance);
189138
private readonly GridSort<ResourceGridViewModel> _startTimeSort = GridSort<ResourceGridViewModel>.ByDescending(p => p.Resource.CreationTimeStamp).ThenAscending(p => p.Resource, ResourceViewModelNameComparer.Instance);
@@ -289,7 +238,9 @@ private ValueTask<GridItemsProviderResult<ResourceGridViewModel>> GetData(GridIt
289238
// Rearrange resources based on parent information.
290239
// This must happen after resources are ordered so nested resources are in the right order.
291240
// Collapsed resources are filtered out of results.
292-
var orderedResources = OrderNestedResources(filteredResources.ToList()).Where(r => !r.IsHidden).ToList();
241+
var orderedResources = ResourceGridViewModel.OrderNestedResources(filteredResources.ToList(), r => !_expandedResourceNames.Contains(r.Name))
242+
.Where(r => !r.IsHidden)
243+
.ToList();
293244

294245
// Paging visible resources.
295246
var query = orderedResources.Skip(request.StartIndex);

src/Aspire.Dashboard/Model/ResourceGridViewModel.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,42 @@ private void UpdateHidden(bool isParentCollapsed = false)
3333
child.UpdateHidden(isParentCollapsed || IsCollapsed);
3434
}
3535
}
36+
37+
public static List<ResourceGridViewModel> OrderNestedResources(List<ResourceGridViewModel> initialGridVMs, Func<ResourceViewModel, bool> isCollapsed)
38+
{
39+
// This method loops over the list of grid view models to build the nested list.
40+
// Apps shouldn't have a huge number of resources so this shouldn't impact performance,
41+
// but if that changes then this method will need to be improved.
42+
43+
var gridViewModels = new List<ResourceGridViewModel>();
44+
var depth = 0;
45+
46+
foreach (var gridVM in initialGridVMs.Where(r => r.Resource.GetResourcePropertyValue(KnownProperties.Resource.ParentName) is null))
47+
{
48+
gridVM.Depth = depth;
49+
gridVM.IsCollapsed = isCollapsed(gridVM.Resource);
50+
gridVM.IsHidden = false;
51+
52+
gridViewModels.Add(gridVM);
53+
54+
AddChildViewModel(gridVM.Resource, gridVM, depth + 1, hidden: gridVM.IsCollapsed);
55+
}
56+
57+
return gridViewModels;
58+
59+
void AddChildViewModel(ResourceViewModel resource, ResourceGridViewModel parent, int depth, bool hidden)
60+
{
61+
foreach (var childGridVM in initialGridVMs.Where(r => r.Resource.GetResourcePropertyValue(KnownProperties.Resource.ParentName) == resource.Name))
62+
{
63+
childGridVM.Depth = depth;
64+
childGridVM.IsCollapsed = isCollapsed(childGridVM.Resource);
65+
childGridVM.IsHidden = hidden;
66+
67+
parent.Children.Add(childGridVM);
68+
gridViewModels.Add(childGridVM);
69+
70+
AddChildViewModel(childGridVM.Resource, childGridVM, depth + 1, hidden: childGridVM.IsHidden || childGridVM.IsCollapsed);
71+
}
72+
}
73+
}
3674
}

src/Aspire.Dashboard/Model/ResourceViewModel.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ internal bool MatchesFilter(string filter)
6666
return Name.Contains(filter, StringComparisons.UserTextSearch);
6767
}
6868

69+
public string? GetResourcePropertyValue(string propertyName)
70+
{
71+
if (Properties.TryGetValue(propertyName, out var value))
72+
{
73+
if (value.Value.TryConvertToString(out var s))
74+
{
75+
return s;
76+
}
77+
}
78+
79+
return null;
80+
}
81+
6982
internal static HealthStatus? ComputeHealthStatus(ImmutableArray<HealthReportViewModel> healthReports, KnownResourceState? state)
7083
{
7184
if (state != KnownResourceState.Running)

0 commit comments

Comments
 (0)