mirror of
https://github.com/Readarr/Readarr
synced 2025-12-27 02:32:27 +01:00
Fixed: Speed up book api
This commit is contained in:
parent
a59706ceb4
commit
b0a3ddef9c
3 changed files with 26 additions and 5 deletions
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<BookResource> 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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -112,10 +112,14 @@ private void FetchAndLinkBookStatistics(BookResource resource)
|
|||
|
||||
private void LinkAuthorStatistics(List<BookResource> resources, List<AuthorStatistics> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue