* New: Release Group for movie table index Co-authored-by: Qstick <qstick@gmail.com> * fixup! New: Release Group for movie table index --------- Co-authored-by: Qstick <qstick@gmail.com>pull/9632/head
parent
1932aec131
commit
35651ac59b
@ -0,0 +1,35 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Core.Datastore;
|
||||
|
||||
namespace NzbDrone.Core.MovieStats
|
||||
{
|
||||
public class MovieStatistics : ResultSet
|
||||
{
|
||||
public int MovieId { get; set; }
|
||||
public int MovieFileCount { get; set; }
|
||||
public long SizeOnDisk { get; set; }
|
||||
public string ReleaseGroupsString { get; set; }
|
||||
|
||||
public List<string> ReleaseGroups
|
||||
{
|
||||
get
|
||||
{
|
||||
var releaseGroups = new List<string>();
|
||||
|
||||
if (ReleaseGroupsString.IsNotNullOrWhiteSpace())
|
||||
{
|
||||
releaseGroups = ReleaseGroupsString
|
||||
.Split('|')
|
||||
.Distinct()
|
||||
.Where(rg => rg.IsNotNullOrWhiteSpace())
|
||||
.OrderBy(rg => rg)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
return releaseGroups;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Dapper;
|
||||
using NzbDrone.Core.Datastore;
|
||||
using NzbDrone.Core.MediaFiles;
|
||||
using NzbDrone.Core.Movies;
|
||||
|
||||
namespace NzbDrone.Core.MovieStats
|
||||
{
|
||||
public interface IMovieStatisticsRepository
|
||||
{
|
||||
List<MovieStatistics> MovieStatistics();
|
||||
List<MovieStatistics> MovieStatistics(int movieId);
|
||||
}
|
||||
|
||||
public class MovieStatisticsRepository : IMovieStatisticsRepository
|
||||
{
|
||||
private const string _selectMoviesTemplate = "SELECT /**select**/ FROM \"Movies\" /**join**/ /**innerjoin**/ /**leftjoin**/ /**where**/ /**groupby**/ /**having**/ /**orderby**/";
|
||||
private const string _selectMovieFilesTemplate = "SELECT /**select**/ FROM \"MovieFiles\" /**join**/ /**innerjoin**/ /**leftjoin**/ /**where**/ /**groupby**/ /**having**/ /**orderby**/";
|
||||
|
||||
private readonly IMainDatabase _database;
|
||||
|
||||
public MovieStatisticsRepository(IMainDatabase database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public List<MovieStatistics> MovieStatistics()
|
||||
{
|
||||
return MapResults(Query(MoviesBuilder(), _selectMoviesTemplate),
|
||||
Query(MovieFilesBuilder(), _selectMovieFilesTemplate));
|
||||
}
|
||||
|
||||
public List<MovieStatistics> MovieStatistics(int movieId)
|
||||
{
|
||||
return MapResults(Query(MoviesBuilder().Where<Movie>(x => x.Id == movieId), _selectMoviesTemplate),
|
||||
Query(MovieFilesBuilder().Where<MovieFile>(x => x.MovieId == movieId), _selectMovieFilesTemplate));
|
||||
}
|
||||
|
||||
private List<MovieStatistics> MapResults(List<MovieStatistics> moviesResult, List<MovieStatistics> filesResult)
|
||||
{
|
||||
moviesResult.ForEach(e =>
|
||||
{
|
||||
var file = filesResult.SingleOrDefault(f => f.MovieId == e.MovieId);
|
||||
|
||||
e.SizeOnDisk = file?.SizeOnDisk ?? 0;
|
||||
e.ReleaseGroupsString = file?.ReleaseGroupsString;
|
||||
});
|
||||
|
||||
return moviesResult;
|
||||
}
|
||||
|
||||
private List<MovieStatistics> Query(SqlBuilder builder, string template)
|
||||
{
|
||||
var sql = builder.AddTemplate(template).LogQuery();
|
||||
|
||||
using var conn = _database.OpenConnection();
|
||||
|
||||
return conn.Query<MovieStatistics>(sql.RawSql, sql.Parameters).ToList();
|
||||
}
|
||||
|
||||
private SqlBuilder MoviesBuilder()
|
||||
{
|
||||
return new SqlBuilder(_database.DatabaseType)
|
||||
.Select(@"""Movies"".""Id"" AS MovieId,
|
||||
SUM(CASE WHEN ""MovieFileId"" > 0 THEN 1 ELSE 0 END) AS MovieFileCount")
|
||||
.GroupBy<Movie>(x => x.Id);
|
||||
}
|
||||
|
||||
private SqlBuilder MovieFilesBuilder()
|
||||
{
|
||||
if (_database.DatabaseType == DatabaseType.SQLite)
|
||||
{
|
||||
return new SqlBuilder(_database.DatabaseType)
|
||||
.Select(@"""MovieId"",
|
||||
SUM(COALESCE(""Size"", 0)) AS SizeOnDisk,
|
||||
GROUP_CONCAT(""ReleaseGroup"", '|') AS ReleaseGroupsString")
|
||||
.GroupBy<MovieFile>(x => x.MovieId);
|
||||
}
|
||||
|
||||
return new SqlBuilder(_database.DatabaseType)
|
||||
.Select(@"""MovieId"",
|
||||
SUM(COALESCE(""Size"", 0)) AS SizeOnDisk,
|
||||
string_agg(""ReleaseGroup"", '|') AS ReleaseGroupsString")
|
||||
.GroupBy<MovieFile>(x => x.MovieId);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace NzbDrone.Core.MovieStats
|
||||
{
|
||||
public interface IMovieStatisticsService
|
||||
{
|
||||
List<MovieStatistics> MovieStatistics();
|
||||
MovieStatistics MovieStatistics(int movieId);
|
||||
}
|
||||
|
||||
public class MovieStatisticsService : IMovieStatisticsService
|
||||
{
|
||||
private readonly IMovieStatisticsRepository _movieStatisticsRepository;
|
||||
|
||||
public MovieStatisticsService(IMovieStatisticsRepository movieStatisticsRepository)
|
||||
{
|
||||
_movieStatisticsRepository = movieStatisticsRepository;
|
||||
}
|
||||
|
||||
public List<MovieStatistics> MovieStatistics()
|
||||
{
|
||||
var movieStatistics = _movieStatisticsRepository.MovieStatistics();
|
||||
|
||||
return movieStatistics.GroupBy(m => m.MovieId).Select(m => m.First()).ToList();
|
||||
}
|
||||
|
||||
public MovieStatistics MovieStatistics(int movieId)
|
||||
{
|
||||
var stats = _movieStatisticsRepository.MovieStatistics(movieId);
|
||||
|
||||
if (stats == null || stats.Count == 0)
|
||||
{
|
||||
return new MovieStatistics();
|
||||
}
|
||||
|
||||
return stats.First();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
using System.Collections.Generic;
|
||||
using NzbDrone.Core.MovieStats;
|
||||
|
||||
namespace Radarr.Api.V3.Movies
|
||||
{
|
||||
public class MovieStatisticsResource
|
||||
{
|
||||
public int MovieFileCount { get; set; }
|
||||
public long SizeOnDisk { get; set; }
|
||||
public List<string> ReleaseGroups { get; set; }
|
||||
}
|
||||
|
||||
public static class MovieStatisticsResourceMapper
|
||||
{
|
||||
public static MovieStatisticsResource ToResource(this MovieStatistics model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new MovieStatisticsResource
|
||||
{
|
||||
MovieFileCount = model.MovieFileCount,
|
||||
SizeOnDisk = model.SizeOnDisk,
|
||||
ReleaseGroups = model.ReleaseGroups
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue