Fixed: Speed up book api

pull/1702/head
ta264 2 years ago
parent a59706ceb4
commit b0a3ddef9c

@ -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 @@ namespace Readarr.Api.V1.Books
{
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 @@ namespace Readarr.Api.V1.Books
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…
Cancel
Save