diff --git a/src/NzbDrone.Core.Test/ArtistStatsTests/ArtistStatisticsFixture.cs b/src/NzbDrone.Core.Test/AuthorStatsTests/AuthorStatisticsFixture.cs similarity index 59% rename from src/NzbDrone.Core.Test/ArtistStatsTests/ArtistStatisticsFixture.cs rename to src/NzbDrone.Core.Test/AuthorStatsTests/AuthorStatisticsFixture.cs index 3d9ab06f5..c18af5110 100644 --- a/src/NzbDrone.Core.Test/ArtistStatsTests/ArtistStatisticsFixture.cs +++ b/src/NzbDrone.Core.Test/AuthorStatsTests/AuthorStatisticsFixture.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using FizzWare.NBuilder; using FluentAssertions; @@ -17,7 +18,7 @@ namespace NzbDrone.Core.Test.AuthorStatsTests private Author _author; private Book _book; private Edition _edition; - private BookFile _trackFile; + private List _bookFiles; [SetUp] public void Setup() @@ -39,17 +40,24 @@ namespace NzbDrone.Core.Test.AuthorStatsTests .BuildNew(); Db.Insert(_edition); - _trackFile = Builder.CreateNew() + _bookFiles = Builder.CreateListOfSize(2) + .All() + .With(x => x.Id = 0) .With(e => e.Author = _author) .With(e => e.Edition = _edition) - .With(e => e.EditionId == _edition.Id) + .With(e => e.EditionId = _edition.Id) .With(e => e.Quality = new QualityModel(Quality.MP3)) - .BuildNew(); + .BuildList(); + } + + private void GivenBookFile() + { + Db.Insert(_bookFiles[0]); } - private void GivenTrackFile() + private void GivenTwoBookFiles() { - Db.Insert(_trackFile); + Db.InsertMany(_bookFiles); } [Test] @@ -61,7 +69,7 @@ namespace NzbDrone.Core.Test.AuthorStatsTests } [Test] - public void should_not_include_unmonitored_track_in_track_count() + public void should_not_include_unmonitored_book_in_book_count() { var stats = Subject.AuthorStatistics(); @@ -70,9 +78,9 @@ namespace NzbDrone.Core.Test.AuthorStatsTests } [Test] - public void should_include_unmonitored_track_with_file_in_track_count() + public void should_include_unmonitored_book_with_file_in_book_count() { - GivenTrackFile(); + GivenBookFile(); var stats = Subject.AuthorStatistics(); @@ -81,7 +89,7 @@ namespace NzbDrone.Core.Test.AuthorStatsTests } [Test] - public void should_have_size_on_disk_of_zero_when_no_track_file() + public void should_have_size_on_disk_of_zero_when_no_book_file() { var stats = Subject.AuthorStatistics(); @@ -90,14 +98,33 @@ namespace NzbDrone.Core.Test.AuthorStatsTests } [Test] - public void should_have_size_on_disk_when_track_file_exists() + public void should_have_size_on_disk_when_book_file_exists() + { + GivenBookFile(); + + var stats = Subject.AuthorStatistics(); + + stats.Should().HaveCount(1); + stats.First().SizeOnDisk.Should().Be(_bookFiles[0].Size); + } + + [Test] + public void should_count_book_with_two_files_as_one_book() { - GivenTrackFile(); + GivenTwoBookFiles(); var stats = Subject.AuthorStatistics(); + Db.All().Should().HaveCount(2); stats.Should().HaveCount(1); - stats.First().SizeOnDisk.Should().Be(_trackFile.Size); + + var bookStats = stats.First(); + + bookStats.TotalBookCount.Should().Be(1); + bookStats.BookCount.Should().Be(1); + bookStats.AvailableBookCount.Should().Be(1); + bookStats.SizeOnDisk.Should().Be(_bookFiles.Sum(x => x.Size)); + bookStats.BookFileCount.Should().Be(2); } } } diff --git a/src/NzbDrone.Core/AuthorStats/AuthorStatistics.cs b/src/NzbDrone.Core/AuthorStats/AuthorStatistics.cs index 26d9a80bc..e0ec1f9fa 100644 --- a/src/NzbDrone.Core/AuthorStats/AuthorStatistics.cs +++ b/src/NzbDrone.Core/AuthorStats/AuthorStatistics.cs @@ -6,9 +6,10 @@ namespace NzbDrone.Core.AuthorStats public class AuthorStatistics : ResultSet { public int AuthorId { get; set; } + public int BookFileCount { get; set; } public int BookCount { get; set; } + public int AvailableBookCount { get; set; } public int TotalBookCount { get; set; } - public int BookFileCount { get; set; } public long SizeOnDisk { get; set; } public List BookStatistics { get; set; } } diff --git a/src/NzbDrone.Core/AuthorStats/AuthorStatisticsRepository.cs b/src/NzbDrone.Core/AuthorStats/AuthorStatisticsRepository.cs index 027fd2766..dca9cb9aa 100644 --- a/src/NzbDrone.Core/AuthorStats/AuthorStatisticsRepository.cs +++ b/src/NzbDrone.Core/AuthorStats/AuthorStatisticsRepository.cs @@ -28,7 +28,6 @@ namespace NzbDrone.Core.AuthorStats public List AuthorStatistics() { var time = DateTime.UtcNow; - var stats = Query(Builder()); #pragma warning disable CS0472 return Query(Builder().OrWhere(x => x.ReleaseDate < time) @@ -60,10 +59,10 @@ namespace NzbDrone.Core.AuthorStats .Select(@"Authors.Id AS AuthorId, Books.Id AS BookId, SUM(COALESCE(BookFiles.Size, 0)) AS SizeOnDisk, - COUNT(Books.Id) AS TotalBookCount, - SUM(CASE WHEN BookFiles.Id IS NULL THEN 0 ELSE 1 END) AS AvailableBookCount, - SUM(CASE WHEN Books.Monitored = 1 OR BookFiles.Id IS NOT NULL THEN 1 ELSE 0 END) AS BookCount, - SUM(CASE WHEN BookFiles.Id IS NULL THEN 0 ELSE 1 END) AS BookFileCount") + 1 AS TotalBookCount, + CASE WHEN BookFiles.Id IS NULL THEN 0 ELSE 1 END AS AvailableBookCount, + CASE WHEN Books.Monitored = 1 OR BookFiles.Id IS NOT NULL THEN 1 ELSE 0 END AS BookCount, + CASE WHEN BookFiles.Id IS NULL THEN 0 ELSE COUNT(BookFiles.Id) END AS BookFileCount") .Join((e, b) => e.BookId == b.Id) .Join((book, author) => book.AuthorMetadataId == author.AuthorMetadataId) .LeftJoin((t, f) => t.Id == f.EditionId) diff --git a/src/NzbDrone.Core/AuthorStats/AuthorStatisticsService.cs b/src/NzbDrone.Core/AuthorStats/AuthorStatisticsService.cs index 50ee771a6..e1df59fcd 100644 --- a/src/NzbDrone.Core/AuthorStats/AuthorStatisticsService.cs +++ b/src/NzbDrone.Core/AuthorStats/AuthorStatisticsService.cs @@ -56,12 +56,13 @@ namespace NzbDrone.Core.AuthorStats { var authorStatistics = new AuthorStatistics { - BookStatistics = bookStatistics, - BookCount = bookStatistics.Sum(s => s.BookCount), - TotalBookCount = bookStatistics.Sum(s => s.TotalBookCount), AuthorId = bookStatistics.First().AuthorId, BookFileCount = bookStatistics.Sum(s => s.BookFileCount), - SizeOnDisk = bookStatistics.Sum(s => s.SizeOnDisk) + BookCount = bookStatistics.Sum(s => s.BookCount), + AvailableBookCount = bookStatistics.Sum(s => s.AvailableBookCount), + TotalBookCount = bookStatistics.Sum(s => s.TotalBookCount), + SizeOnDisk = bookStatistics.Sum(s => s.SizeOnDisk), + BookStatistics = bookStatistics }; return authorStatistics; diff --git a/src/NzbDrone.Core/AuthorStats/BookStatistics.cs b/src/NzbDrone.Core/AuthorStats/BookStatistics.cs index 313d9e69d..506294f0f 100644 --- a/src/NzbDrone.Core/AuthorStats/BookStatistics.cs +++ b/src/NzbDrone.Core/AuthorStats/BookStatistics.cs @@ -8,7 +8,8 @@ namespace NzbDrone.Core.AuthorStats public int BookId { get; set; } public int BookFileCount { get; set; } public int BookCount { get; set; } - public long SizeOnDisk { get; set; } + public int AvailableBookCount { get; set; } public int TotalBookCount { get; set; } + public long SizeOnDisk { get; set; } } } diff --git a/src/Readarr.Api.V1/Author/AuthorStatisticsResource.cs b/src/Readarr.Api.V1/Author/AuthorStatisticsResource.cs index 35d7db9a7..7af3c9d40 100644 --- a/src/Readarr.Api.V1/Author/AuthorStatisticsResource.cs +++ b/src/Readarr.Api.V1/Author/AuthorStatisticsResource.cs @@ -4,8 +4,9 @@ namespace Readarr.Api.V1.Author { public class AuthorStatisticsResource { - public int BookCount { get; set; } public int BookFileCount { get; set; } + public int BookCount { get; set; } + public int AvailableBookCount { get; set; } public int TotalBookCount { get; set; } public long SizeOnDisk { get; set; } @@ -18,7 +19,7 @@ namespace Readarr.Api.V1.Author return 0; } - return BookFileCount / (decimal)BookCount * 100; + return AvailableBookCount / (decimal)BookCount * 100; } } } @@ -34,9 +35,10 @@ namespace Readarr.Api.V1.Author return new AuthorStatisticsResource { + BookFileCount = model.BookFileCount, BookCount = model.BookCount, + AvailableBookCount = model.AvailableBookCount, TotalBookCount = model.TotalBookCount, - BookFileCount = model.BookFileCount, SizeOnDisk = model.SizeOnDisk }; }