From b0a3ddef9cdf0aa1f6057f9ce8cb6193899da5ab Mon Sep 17 00:00:00 2001 From: ta264 Date: Thu, 26 May 2022 21:51:37 +0100 Subject: [PATCH] Fixed: Speed up book api --- .../Migration/022_edition_monitored_index.cs | 14 ++++++++++++++ src/Readarr.Api.V1/Books/BookController.cs | 9 ++++++--- .../Books/BookControllerWithSignalR.cs | 8 ++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/022_edition_monitored_index.cs diff --git a/src/NzbDrone.Core/Datastore/Migration/022_edition_monitored_index.cs b/src/NzbDrone.Core/Datastore/Migration/022_edition_monitored_index.cs new file mode 100644 index 000000000..e8f08e1d1 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/022_edition_monitored_index.cs @@ -0,0 +1,14 @@ +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(22)] + public class EditionMonitoredIndex : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Create.Index().OnTable("Editions").OnColumn("Monitored"); + } + } +} diff --git a/src/Readarr.Api.V1/Books/BookController.cs b/src/Readarr.Api.V1/Books/BookController.cs index 20ccdce25..5b8281f7d 100644 --- a/src/Readarr.Api.V1/Books/BookController.cs +++ b/src/Readarr.Api.V1/Books/BookController.cs @@ -1,6 +1,6 @@ -using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using FluentValidation; using Microsoft.AspNetCore.Mvc; using NzbDrone.Common.Extensions; @@ -70,10 +70,13 @@ public List GetBooks([FromQuery]int? authorId, { if (!authorId.HasValue && !bookIds.Any() && titleSlug.IsNullOrWhiteSpace()) { + var editionTask = Task.Run(() => _editionService.GetAllMonitoredEditions()); + var metadataTask = Task.Run(() => _authorService.GetAllAuthors()); var books = _bookService.GetAllBooks(); - var authors = _authorService.GetAllAuthors().ToDictionary(x => x.AuthorMetadataId); - var editions = _editionService.GetAllMonitoredEditions().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList()); + var editions = editionTask.GetAwaiter().GetResult().GroupBy(x => x.BookId).ToDictionary(x => x.Key, y => y.ToList()); + + var authors = metadataTask.GetAwaiter().GetResult().ToDictionary(x => x.AuthorMetadataId); foreach (var book in books) { diff --git a/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs b/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs index f3fab4e93..c52e20e06 100644 --- a/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs +++ b/src/Readarr.Api.V1/Books/BookControllerWithSignalR.cs @@ -112,10 +112,14 @@ private void FetchAndLinkBookStatistics(BookResource resource) private void LinkAuthorStatistics(List resources, List authorStatistics) { + var bookStatsDict = authorStatistics.SelectMany(x => x.BookStatistics).ToDictionary(x => x.BookId); + foreach (var book in resources) { - var stats = authorStatistics.SingleOrDefault(ss => ss.AuthorId == book.AuthorId); - LinkAuthorStatistics(book, stats); + if (bookStatsDict.TryGetValue(book.Id, out var stats)) + { + book.Statistics = stats.ToResource(); + } } }