Skip to content

Commit

Permalink
Disable showing total count on connections log table.
Browse files Browse the repository at this point in the history
Improves query time since it needs a full table scan and nobody cares about the count anyways.
  • Loading branch information
PJB3005 committed Jun 21, 2024
1 parent 92aceb8 commit 6bca2f2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
16 changes: 8 additions & 8 deletions SS14.Admin/Helpers/PaginatedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ namespace SS14.Admin.Helpers
{
public interface IPaginatedList
{
int TotalCount { get; }
int? TotalCount { get; }
int PageIndex { get; }
int PageSize { get; }
int PageCount { get; }
int? PageCount { get; }
bool HasNextPage { get; }
bool HasPrevPage { get; }
}

public sealed class PaginatedList<T> : IPaginatedList
{
public T[] PaginatedItems { get; }
public int TotalCount { get; }
public int? TotalCount { get; }
public int PageIndex { get; }
public int PageSize { get; }
public int PageCount { get; }
public int? PageCount { get; }

public PaginatedList()
{
PaginatedItems = Array.Empty<T>();
}

public PaginatedList(T[] paginatedItems, int totalCount, int pageIndex, int pageSize)
public PaginatedList(T[] paginatedItems, int? totalCount, int pageIndex, int pageSize)
{
PageIndex = pageIndex;
PageSize = pageSize;
PageCount = (int) Math.Ceiling(totalCount / (double) pageSize);
PageCount = totalCount == null ? null : (int) Math.Ceiling(totalCount.Value / (double) pageSize);

PaginatedItems = paginatedItems;
TotalCount = totalCount;
Expand All @@ -46,9 +46,9 @@ public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> query, int
public static async Task<PaginatedList<T>> CreateLinqAsync<TQuery>(
IQueryable<TQuery> query,
Func<IEnumerable<TQuery>, IEnumerable<T>> convert,
int pageIndex, int pageSize, int? optCount = null)
int pageIndex, int pageSize, int? optCount = null, bool showTotal = true)
{
var count = optCount ?? await query.CountAsync();
var count = showTotal ? optCount ?? await query.CountAsync() : (int?) null;
var items = await query
.Skip(pageIndex * pageSize)
.Take(pageSize)
Expand Down
10 changes: 6 additions & 4 deletions SS14.Admin/Helpers/PaginationState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ public interface IPaginationState
bool HasPrevPage { get; }
int PageIndex { get; }
int PerPage { get; }
int TotalCount { get; }
int? TotalCount { get; }
int DefaultPerPage { get; }
}

public class PaginationState<T> : IPaginationState
{
public bool ShowTotal { get; }
public int DefaultPerPage { get; }
public int PageIndex { get; private set; }
public int PerPage { get; private set; }
public int TotalCount => List.TotalCount;
public int? TotalCount => List.TotalCount;
public PaginatedList<T> List = default!;
public bool HasPrevPage => List.HasPrevPage;
public bool HasNextPage => List.HasNextPage;

public IDictionary<string, string?> AllRouteData { get; private set; } =
ImmutableDictionary<string, string?>.Empty;

public PaginationState(int defaultPerPage)
public PaginationState(int defaultPerPage, bool showTotal = true)
{
ShowTotal = showTotal;
PerPage = DefaultPerPage = defaultPerPage;
}

Expand All @@ -54,7 +56,7 @@ public async Task LoadLinqAsync<TQuery>(
IQueryable<TQuery> query,
Func<IEnumerable<TQuery>, IEnumerable<T>> convert)
{
List = await PaginatedList<T>.CreateLinqAsync(query, convert, PageIndex, PerPage);
List = await PaginatedList<T>.CreateLinqAsync(query, convert, PageIndex, PerPage, showTotal: ShowTotal);
}
}
}
2 changes: 1 addition & 1 deletion SS14.Admin/Pages/Connections/Index.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ConnectionsIndexModel : PageModel
private readonly PostgresServerDbContext _dbContext;

public ISortState SortState { get; private set; } = default!;
public PaginationState<Connection> Pagination { get; } = new(100);
public PaginationState<Connection> Pagination { get; } = new(100, showTotal: false);
public Dictionary<string, string?> AllRouteData { get; } = new();

public string? CurrentFilter { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion SS14.Admin/Pages/Tables/Pagination.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@

<span class="small">
Page: @(Model.PageIndex + 1)
Total: @Model.TotalCount
@if (Model.TotalCount is { } total)
{
@:Total: @total
}
Shown:
@{ await ShownCountButton(100); }
|
Expand Down

0 comments on commit 6bca2f2

Please sign in to comment.