From 6bca2f2d76d63b392594bc538fb171c9267b459d Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Fri, 21 Jun 2024 04:50:52 +0200 Subject: [PATCH] Disable showing total count on connections log table. Improves query time since it needs a full table scan and nobody cares about the count anyways. --- SS14.Admin/Helpers/PaginatedList.cs | 16 ++++++++-------- SS14.Admin/Helpers/PaginationState.cs | 10 ++++++---- SS14.Admin/Pages/Connections/Index.cshtml.cs | 2 +- SS14.Admin/Pages/Tables/Pagination.cshtml | 5 ++++- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/SS14.Admin/Helpers/PaginatedList.cs b/SS14.Admin/Helpers/PaginatedList.cs index bd7e868..197adbd 100644 --- a/SS14.Admin/Helpers/PaginatedList.cs +++ b/SS14.Admin/Helpers/PaginatedList.cs @@ -4,10 +4,10 @@ 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; } } @@ -15,21 +15,21 @@ public interface IPaginatedList public sealed class PaginatedList : 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(); } - 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; @@ -46,9 +46,9 @@ public static async Task> CreateAsync(IQueryable query, int public static async Task> CreateLinqAsync( IQueryable query, Func, IEnumerable> 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) diff --git a/SS14.Admin/Helpers/PaginationState.cs b/SS14.Admin/Helpers/PaginationState.cs index d1ff62c..59a8b01 100644 --- a/SS14.Admin/Helpers/PaginationState.cs +++ b/SS14.Admin/Helpers/PaginationState.cs @@ -10,16 +10,17 @@ public interface IPaginationState bool HasPrevPage { get; } int PageIndex { get; } int PerPage { get; } - int TotalCount { get; } + int? TotalCount { get; } int DefaultPerPage { get; } } public class PaginationState : 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 List = default!; public bool HasPrevPage => List.HasPrevPage; public bool HasNextPage => List.HasNextPage; @@ -27,8 +28,9 @@ public class PaginationState : IPaginationState public IDictionary AllRouteData { get; private set; } = ImmutableDictionary.Empty; - public PaginationState(int defaultPerPage) + public PaginationState(int defaultPerPage, bool showTotal = true) { + ShowTotal = showTotal; PerPage = DefaultPerPage = defaultPerPage; } @@ -54,7 +56,7 @@ public async Task LoadLinqAsync( IQueryable query, Func, IEnumerable> convert) { - List = await PaginatedList.CreateLinqAsync(query, convert, PageIndex, PerPage); + List = await PaginatedList.CreateLinqAsync(query, convert, PageIndex, PerPage, showTotal: ShowTotal); } } } diff --git a/SS14.Admin/Pages/Connections/Index.cshtml.cs b/SS14.Admin/Pages/Connections/Index.cshtml.cs index cab3a75..2998ae8 100644 --- a/SS14.Admin/Pages/Connections/Index.cshtml.cs +++ b/SS14.Admin/Pages/Connections/Index.cshtml.cs @@ -10,7 +10,7 @@ public class ConnectionsIndexModel : PageModel private readonly PostgresServerDbContext _dbContext; public ISortState SortState { get; private set; } = default!; - public PaginationState Pagination { get; } = new(100); + public PaginationState Pagination { get; } = new(100, showTotal: false); public Dictionary AllRouteData { get; } = new(); public string? CurrentFilter { get; set; } diff --git a/SS14.Admin/Pages/Tables/Pagination.cshtml b/SS14.Admin/Pages/Tables/Pagination.cshtml index 437d46e..2c63e94 100644 --- a/SS14.Admin/Pages/Tables/Pagination.cshtml +++ b/SS14.Admin/Pages/Tables/Pagination.cshtml @@ -43,7 +43,10 @@ Page: @(Model.PageIndex + 1) - Total: @Model.TotalCount + @if (Model.TotalCount is { } total) + { + @:Total: @total + } Shown: @{ await ShownCountButton(100); } |