mirror of
https://github.com/Readarr/Readarr
synced 2025-12-15 21:02:40 +01:00
parent
12c67891fb
commit
72ae466892
6 changed files with 60 additions and 29 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using FizzWare.NBuilder;
|
||||
using FluentAssertions;
|
||||
|
|
@ -17,7 +18,7 @@ public class AuthorStatisticsFixture : DbTest<AuthorStatisticsRepository, Author
|
|||
private Author _author;
|
||||
private Book _book;
|
||||
private Edition _edition;
|
||||
private BookFile _trackFile;
|
||||
private List<BookFile> _bookFiles;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
|
|
@ -39,17 +40,24 @@ public void Setup()
|
|||
.BuildNew();
|
||||
Db.Insert(_edition);
|
||||
|
||||
_trackFile = Builder<BookFile>.CreateNew()
|
||||
_bookFiles = Builder<BookFile>.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 GivenTrackFile()
|
||||
private void GivenBookFile()
|
||||
{
|
||||
Db.Insert(_trackFile);
|
||||
Db.Insert(_bookFiles[0]);
|
||||
}
|
||||
|
||||
private void GivenTwoBookFiles()
|
||||
{
|
||||
Db.InsertMany(_bookFiles);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
|
@ -61,7 +69,7 @@ public void should_get_stats_for_author()
|
|||
}
|
||||
|
||||
[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 @@ public void should_not_include_unmonitored_track_in_track_count()
|
|||
}
|
||||
|
||||
[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 @@ public void should_include_unmonitored_track_with_file_in_track_count()
|
|||
}
|
||||
|
||||
[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 @@ public void should_have_size_on_disk_of_zero_when_no_track_file()
|
|||
}
|
||||
|
||||
[Test]
|
||||
public void should_have_size_on_disk_when_track_file_exists()
|
||||
public void should_have_size_on_disk_when_book_file_exists()
|
||||
{
|
||||
GivenTrackFile();
|
||||
GivenBookFile();
|
||||
|
||||
var stats = Subject.AuthorStatistics();
|
||||
|
||||
stats.Should().HaveCount(1);
|
||||
stats.First().SizeOnDisk.Should().Be(_trackFile.Size);
|
||||
stats.First().SizeOnDisk.Should().Be(_bookFiles[0].Size);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void should_count_book_with_two_files_as_one_book()
|
||||
{
|
||||
GivenTwoBookFiles();
|
||||
|
||||
var stats = Subject.AuthorStatistics();
|
||||
|
||||
Db.All<BookFile>().Should().HaveCount(2);
|
||||
stats.Should().HaveCount(1);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -6,9 +6,10 @@ namespace NzbDrone.Core.AuthorStats
|
|||
public class AuthorStatistics : ResultSet
|
||||
{
|
||||
public int AuthorId { get; set; }
|
||||
public int BookCount { get; set; }
|
||||
public int TotalBookCount { 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; }
|
||||
public List<BookStatistics> BookStatistics { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ public AuthorStatisticsRepository(IMainDatabase database)
|
|||
public List<BookStatistics> AuthorStatistics()
|
||||
{
|
||||
var time = DateTime.UtcNow;
|
||||
var stats = Query(Builder());
|
||||
|
||||
#pragma warning disable CS0472
|
||||
return Query(Builder().OrWhere<Book>(x => x.ReleaseDate < time)
|
||||
|
|
@ -60,10 +59,10 @@ private List<BookStatistics> Query(SqlBuilder builder)
|
|||
.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<Edition, Book>((e, b) => e.BookId == b.Id)
|
||||
.Join<Book, Author>((book, author) => book.AuthorMetadataId == author.AuthorMetadataId)
|
||||
.LeftJoin<Edition, BookFile>((t, f) => t.Id == f.EditionId)
|
||||
|
|
|
|||
|
|
@ -56,12 +56,13 @@ private AuthorStatistics MapAuthorStatistics(List<BookStatistics> bookStatistics
|
|||
{
|
||||
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;
|
||||
|
|
|
|||
|
|
@ -8,7 +8,8 @@ public class BookStatistics : ResultSet
|
|||
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; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 @@ public decimal PercentOfBooks
|
|||
return 0;
|
||||
}
|
||||
|
||||
return BookFileCount / (decimal)BookCount * 100;
|
||||
return AvailableBookCount / (decimal)BookCount * 100;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -34,9 +35,10 @@ public static AuthorStatisticsResource ToResource(this AuthorStatistics model)
|
|||
|
||||
return new AuthorStatisticsResource
|
||||
{
|
||||
BookCount = model.BookCount,
|
||||
TotalBookCount = model.TotalBookCount,
|
||||
BookFileCount = model.BookFileCount,
|
||||
BookCount = model.BookCount,
|
||||
AvailableBookCount = model.AvailableBookCount,
|
||||
TotalBookCount = model.TotalBookCount,
|
||||
SizeOnDisk = model.SizeOnDisk
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue