Skip to content

Commit

Permalink
Reduce sorting/filtering needed by tracking state between renders.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 31, 2023
1 parent 469c91a commit 5f04782
Showing 1 changed file with 92 additions and 52 deletions.
144 changes: 92 additions & 52 deletions Server/Components/Devices/DevicesFrame.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ public partial class DevicesFrame : AuthComponentBase
private readonly List<DeviceGroup> _deviceGroups = new();
private readonly SemaphoreSlim _devicesLock = new(1,1);
private readonly List<Device> _filteredDevices = new();
private readonly List<Device> _prependedDevices = new();
private readonly List<PropertyInfo> _sortableProperties = new();
private int _currentPage = 1;
private int _devicesPerPage = 25;
private string? _filter;
private bool _hideOfflineDevices = true;
private string _lastFilterState = string.Empty;
private string? _selectedGroupId;
private string _selectedSortProperty = "DeviceName";
private ListSortDirection _sortDirection;
Expand All @@ -48,6 +50,20 @@ public partial class DevicesFrame : AuthComponentBase
[Inject]
private ICircuitConnection CircuitConnection { get; init; } = null!;

private string CurrentFilterState
{
get
{
return
$"{_filter}" +
$"{_selectedGroupId}|" +
$"{_selectedSortProperty}|" +
$"{_sortDirection}|" +
$"{_hideOfflineDevices}|" +
$"{_currentPage}|" +
$"{_devicesPerPage}|";
}
}
[Inject]
private IDataService DataService { get; init; } = null!;

Expand All @@ -66,6 +82,7 @@ public partial class DevicesFrame : AuthComponentBase

public async Task Refresh()
{
_lastFilterState = string.Empty;
await LoadDevices();
await InvokeAsync(StateHasChanged);
}
Expand Down Expand Up @@ -134,74 +151,83 @@ await Messenger.Send(
CircuitConnection.ConnectionId);
}

private Device[] GetDisplayedDevices()
private void FilterAndSortDevices()
{
_devicesLock.Wait();
try
{
_filteredDevices.Clear();
var appendDevices = new List<Device>();
_filteredDevices.Clear();
_prependedDevices.Clear();

foreach (var device in _allDevices)
foreach (var device in _allDevices)
{
if (CardStore.SelectedDevices.Contains(device.ID))
{
if (CardStore.SelectedDevices.Contains(device.ID))
{
appendDevices.Add(device);
}
_prependedDevices.Add(device);
}

if (!device.IsOnline && _hideOfflineDevices)
{
continue;
}
if (!device.IsOnline && _hideOfflineDevices)
{
continue;
}

if (!string.IsNullOrWhiteSpace(_filter) &&
device.Alias?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.CurrentUser?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.DeviceName?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.Notes?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.Platform?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.Tags?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true)
{
continue;
}
if (!string.IsNullOrWhiteSpace(_filter) &&
device.Alias?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.CurrentUser?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.DeviceName?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.Notes?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.Platform?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true &&
device.Tags?.Contains(_filter, StringComparison.OrdinalIgnoreCase) != true)
{
continue;
}

if (_selectedGroupId == _deviceGroupAll ||
_selectedGroupId == device.DeviceGroupID ||
(
_selectedGroupId == _deviceGroupNone &&
string.IsNullOrWhiteSpace(device.DeviceGroupID
)))
{
_filteredDevices.Add(device);
}
if (_selectedGroupId == _deviceGroupAll ||
_selectedGroupId == device.DeviceGroupID ||
(
_selectedGroupId == _deviceGroupNone &&
string.IsNullOrWhiteSpace(device.DeviceGroupID
)))
{
_filteredDevices.Add(device);
}
}

if (!string.IsNullOrWhiteSpace(_selectedSortProperty))
if (!string.IsNullOrWhiteSpace(_selectedSortProperty))
{
var direction = _sortDirection == ListSortDirection.Ascending ? 1 : -1;
_filteredDevices.Sort((a, b) =>
{
var direction = _sortDirection == ListSortDirection.Ascending ? 1 : -1;
_filteredDevices.Sort((a, b) =>
if (a.IsOnline != b.IsOnline)
{
if (a.IsOnline != b.IsOnline)
{
return b.IsOnline.CompareTo(a.IsOnline);
}
return b.IsOnline.CompareTo(a.IsOnline);
}

var propInfo = _sortableProperties.Find(x => x.Name == _selectedSortProperty);
var propInfo = _sortableProperties.Find(x => x.Name == _selectedSortProperty);

var valueA = propInfo?.GetValue(a);
var valueB = propInfo?.GetValue(b);
var valueA = propInfo?.GetValue(a);
var valueB = propInfo?.GetValue(b);

return Comparer.Default.Compare(valueA, valueB) * direction;
});
return Comparer.Default.Compare(valueA, valueB) * direction;
});
}
}

private Device[] GetDisplayedDevices()
{
_devicesLock.Wait();
try
{
if (CurrentFilterState != _lastFilterState)
{
_lastFilterState = CurrentFilterState;
FilterAndSortDevices();
}

var skipCount = (_currentPage - 1) * _devicesPerPage;
var devicesForPage = _filteredDevices
.Except(appendDevices)
.Except(_prependedDevices)
.Skip(skipCount)
.Take(_devicesPerPage);

return appendDevices.Concat(devicesForPage).ToArray();
return _prependedDevices.Concat(devicesForPage).ToArray();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -232,13 +258,27 @@ private async Task HandleDeviceStateChangedMessage(DeviceStateChangedMessage mes
{
var device = message.Device;

var index = _allDevices.FindIndex(x => x.ID == device.ID);
if (index > -1)
var collections = new[] { _allDevices, _filteredDevices };

foreach (var collection in collections)
{
_allDevices[index] = device;
var index = collection.FindIndex(x => x.ID == device.ID);
if (index > -1)
{
collection[index] = device;
}
else
{
collection.Add(device);
}
}

Debouncer.Debounce(TimeSpan.FromSeconds(2), Refresh);
Debouncer.Debounce(
TimeSpan.FromSeconds(2),
async () =>
{
await InvokeAsync(StateHasChanged);
});
}
finally
{
Expand Down

0 comments on commit 5f04782

Please sign in to comment.