From 2d4373c99fb5bf69bab2535a2509da25a45f12ca Mon Sep 17 00:00:00 2001 From: ychung-mot Date: Fri, 20 Sep 2024 15:25:47 -0700 Subject: [PATCH] chore: grouped listings performance --- .../Repositories/RentalListingRepository.cs | 2 +- .../Repositories/RepositoryBase.cs | 57 +++++++++++++------ 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/server/StrDss.Data/Repositories/RentalListingRepository.cs b/server/StrDss.Data/Repositories/RentalListingRepository.cs index 78016832..74a3c4c9 100644 --- a/server/StrDss.Data/Repositories/RentalListingRepository.cs +++ b/server/StrDss.Data/Repositories/RentalListingRepository.cs @@ -109,7 +109,7 @@ public async Task> GetGroupedRentalListings(stri var extraSort = ""; - var groupedListings = await Page(groupedQuery, pageSize, pageNumber, orderBy, direction, extraSort); + var groupedListings = await Page(groupedQuery, pageSize, pageNumber, orderBy, direction, extraSort, false); foreach (var group in groupedListings.SourceList) diff --git a/server/StrDss.Data/Repositories/RepositoryBase.cs b/server/StrDss.Data/Repositories/RepositoryBase.cs index 1a3a8dc4..1f3a67f4 100644 --- a/server/StrDss.Data/Repositories/RepositoryBase.cs +++ b/server/StrDss.Data/Repositories/RepositoryBase.cs @@ -11,7 +11,7 @@ namespace StrDss.Data.Repositories public interface IRepositoryBase where TEntity : class { - Task> Page(IQueryable list, int pageSize, int pageNumber, string orderBy, string direction, string extraSort = ""); + Task> Page(IQueryable list, int pageSize, int pageNumber, string orderBy, string direction, string extraSort = "", bool count = true); } public class RepositoryBase : IRepositoryBase where TEntity : class @@ -34,44 +34,60 @@ public RepositoryBase(DssDbContext dbContext, IMapper mapper, ICurrentUser curre _logger = logger; } - public async Task> Page(IQueryable list, int pageSize, int pageNumber, string orderBy, string direction = "", string extraSort = "") + public async Task> Page(IQueryable list, int pageSize, int pageNumber, string orderBy, string direction = "", string extraSort = "", bool count = true) { var stopwatch = Stopwatch.StartNew(); - var sort = string.IsNullOrEmpty(extraSort) - ? $"{orderBy} {direction}" - : $"{orderBy} {direction}, {extraSort}"; + var totalRecords = await list.CountAsync(); - var pagedList = list.DynamicOrderBy(sort) as IQueryable; + if (pageNumber <= 0) pageNumber = 1; + + var sort = ""; + + if (extraSort.IsEmpty()) + { + sort = $"{orderBy} {direction}"; + } + else if (orderBy.IsNotEmpty()) + { + sort = $"{orderBy} {direction}, {extraSort}"; + } + else + { + sort = $"{extraSort}"; + } + + var pagedList = list.DynamicOrderBy($"{sort}") as IQueryable; if (pageSize > 0) { var skipRecordCount = (pageNumber - 1) * pageSize; - pagedList = pagedList.Skip(skipRecordCount).Take(pageSize); + pagedList = pagedList.Skip(skipRecordCount) + .Take(pageSize); } - // Run the counting and fetching data in parallel - var countTask = list.CountAsync(); - var fetchTask = pagedList.ToListAsync(); + stopwatch.Stop(); + + _logger.LogDebug($"Get Grouped Listings (group) - Counting groups. Page Size: {pageSize}, Page Number: {pageNumber}, Time: {stopwatch.Elapsed.TotalSeconds} seconds"); - // Await both tasks in parallel - await Task.WhenAll(countTask, fetchTask); + stopwatch.Restart(); - var totalRecords = countTask.Result; - var result = fetchTask.Result; + var result = await pagedList.ToListAsync(); stopwatch.Stop(); - _logger.LogDebug($"Mapping groups to DTO. Time: {stopwatch.Elapsed.TotalSeconds} seconds"); + + _logger.LogDebug($"Get Grouped Listings (group) - Getting groups. Time: {stopwatch.Elapsed.TotalSeconds} seconds"); + + stopwatch.Restart(); IEnumerable outputList; - // Map the result if necessary if (typeof(TOutput) != typeof(TInput)) outputList = _mapper.Map, IEnumerable>(result); else outputList = (IEnumerable)result; - return new PagedDto + var pagedDTO = new PagedDto { SourceList = outputList, PageInfo = new PageInfo @@ -84,7 +100,12 @@ public async Task> Page(IQueryable li ItemCount = outputList.Count() } }; - } + stopwatch.Stop(); + + _logger.LogDebug($"Get Grouped Listings (group) - Mapping groups to DTO. Time: {stopwatch.Elapsed.TotalSeconds} seconds"); + + return pagedDTO; + } } }