update music queries

This commit is contained in:
Luke Pulverenti 2016-03-24 23:53:42 -04:00
parent 71b6d5d439
commit 3c43e85d87
6 changed files with 46 additions and 150 deletions

View File

@ -192,7 +192,7 @@ namespace MediaBrowser.Api
_userDataRepository, _userDataRepository,
_dtoService, _dtoService,
Logger, Logger,
request, item => item is Game, request, new[] { typeof(Game) },
SimilarItemsHelper.GetSimiliarityScore); SimilarItemsHelper.GetSimiliarityScore);
return ToOptimizedSerializedResultUsingCache(result); return ToOptimizedSerializedResultUsingCache(result);

View File

@ -659,87 +659,38 @@ namespace MediaBrowser.Api.Library
/// <returns>System.Object.</returns> /// <returns>System.Object.</returns>
public object Get(GetItemCounts request) public object Get(GetItemCounts request)
{ {
var filteredItems = GetAllLibraryItems(request.UserId, _userManager, _libraryManager, null, i => i.LocationType != LocationType.Virtual && FilterItem(i, request, request.UserId)); var user = string.IsNullOrWhiteSpace(request.UserId) ? null : _userManager.GetUserById(request.UserId);
var counts = new ItemCounts var counts = new ItemCounts
{ {
AlbumCount = filteredItems.Count(i => i is MusicAlbum), AlbumCount = GetCount(typeof(MusicAlbum), user, request),
EpisodeCount = filteredItems.Count(i => i is Episode), EpisodeCount = GetCount(typeof(Episode), user, request),
GameCount = filteredItems.Count(i => i is Game), GameCount = GetCount(typeof(Game), user, request),
GameSystemCount = filteredItems.Count(i => i is GameSystem), GameSystemCount = GetCount(typeof(GameSystem), user, request),
MovieCount = filteredItems.Count(i => i is Movie), MovieCount = GetCount(typeof(Movie), user, request),
SeriesCount = filteredItems.Count(i => i is Series), SeriesCount = GetCount(typeof(Series), user, request),
SongCount = filteredItems.Count(i => i is Audio), SongCount = GetCount(typeof(Audio), user, request),
MusicVideoCount = filteredItems.Count(i => i is MusicVideo), MusicVideoCount = GetCount(typeof(MusicVideo), user, request),
BoxSetCount = filteredItems.Count(i => i is BoxSet), BoxSetCount = GetCount(typeof(BoxSet), user, request),
BookCount = filteredItems.Count(i => i is Book), BookCount = GetCount(typeof(Book), user, request)
UniqueTypes = filteredItems.Select(i => i.GetClientTypeName()).Distinct().ToList()
}; };
return ToOptimizedSerializedResultUsingCache(counts); return ToOptimizedSerializedResultUsingCache(counts);
} }
private IList<BaseItem> GetAllLibraryItems(string userId, IUserManager userManager, ILibraryManager libraryManager, string parentId, Func<BaseItem, bool> filter) private int GetCount(Type type, User user, GetItemCounts request)
{ {
if (!string.IsNullOrEmpty(parentId)) var query = new InternalItemsQuery(user)
{ {
var folder = (Folder)libraryManager.GetItemById(new Guid(parentId)); IncludeItemTypes = new[] { type.Name },
Limit = 0,
Recursive = true,
ExcludeLocationTypes = new[] { LocationType.Virtual },
SourceTypes = new[] { SourceType.Library },
IsFavorite = request.IsFavorite
};
if (!string.IsNullOrWhiteSpace(userId)) return _libraryManager.GetItemsResult(query).TotalRecordCount;
{
var user = userManager.GetUserById(userId);
if (user == null)
{
throw new ArgumentException("User not found");
}
return folder
.GetRecursiveChildren(user, filter)
.ToList();
}
return folder
.GetRecursiveChildren(filter);
}
if (!string.IsNullOrWhiteSpace(userId))
{
var user = userManager.GetUserById(userId);
if (user == null)
{
throw new ArgumentException("User not found");
}
return userManager
.GetUserById(userId)
.RootFolder
.GetRecursiveChildren(user, filter)
.ToList();
}
return libraryManager
.RootFolder
.GetRecursiveChildren(filter);
}
private bool FilterItem(BaseItem item, GetItemCounts request, string userId)
{
if (!string.IsNullOrWhiteSpace(userId))
{
if (request.IsFavorite.HasValue)
{
var val = request.IsFavorite.Value;
if (_userDataManager.GetUserData(userId, item.GetUserDataKey()).IsFavorite != val)
{
return false;
}
}
}
return true;
} }
/// <summary> /// <summary>
@ -980,20 +931,15 @@ namespace MediaBrowser.Api.Library
? new string[] { } ? new string[] { }
: request.IncludeItemTypes.Split(','); : request.IncludeItemTypes.Split(',');
Func<BaseItem, bool> filter = i => var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
{
if (includeTypes.Length > 0)
{
if (!includeTypes.Contains(i.GetType().Name, StringComparer.OrdinalIgnoreCase))
{
return false;
}
}
return true; var query = new InternalItemsQuery(user)
{
IncludeItemTypes = includeTypes,
Recursive = true
}; };
IEnumerable<BaseItem> items = GetAllLibraryItems(request.UserId, _userManager, _libraryManager, null, filter); var items = _libraryManager.GetItemList(query);
var lookup = items var lookup = items
.ToLookup(i => i.ProductionYear ?? -1) .ToLookup(i => i.ProductionYear ?? -1)

View File

@ -52,10 +52,15 @@ namespace MediaBrowser.Api.Music
public object Get(GetSimilarArtists request) public object Get(GetSimilarArtists request)
{ {
var result = GetSimilarItemsResult( var dtoOptions = GetDtoOptions(request);
request,
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
_itemRepo,
_libraryManager,
_userDataRepository,
_dtoService,
Logger,
request, new[] { typeof(MusicArtist) },
SimilarItemsHelper.GetSimiliarityScore); SimilarItemsHelper.GetSimiliarityScore);
return ToOptimizedSerializedResultUsingCache(result); return ToOptimizedSerializedResultUsingCache(result);
@ -76,45 +81,12 @@ namespace MediaBrowser.Api.Music
_userDataRepository, _userDataRepository,
_dtoService, _dtoService,
Logger, Logger,
request, item => item is MusicAlbum, request, new[] { typeof(MusicAlbum) },
GetAlbumSimilarityScore); GetAlbumSimilarityScore);
return ToOptimizedSerializedResultUsingCache(result); return ToOptimizedSerializedResultUsingCache(result);
} }
private ItemsResult GetSimilarItemsResult(BaseGetSimilarItemsFromItem request, Func<BaseItem, List<PersonInfo>, List<PersonInfo>, BaseItem, int> getSimilarityScore)
{
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
var item = string.IsNullOrEmpty(request.Id) ?
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
_libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id);
var inputItems = _libraryManager.GetArtists(user.RootFolder.GetRecursiveChildren(user, i => i is IHasArtist).OfType<IHasArtist>());
var list = inputItems.ToList();
var items = SimilarItemsHelper.GetSimilaritems(item, _libraryManager, list, getSimilarityScore).ToList();
IEnumerable<BaseItem> returnItems = items;
if (request.Limit.HasValue)
{
returnItems = returnItems.Take(request.Limit.Value);
}
var dtoOptions = GetDtoOptions(request);
var result = new ItemsResult
{
Items = _dtoService.GetBaseItemDtos(returnItems, dtoOptions, user).ToArray(),
TotalRecordCount = items.Count
};
return result;
}
/// <summary> /// <summary>
/// Gets the album similarity score. /// Gets the album similarity score.
/// </summary> /// </summary>

View File

@ -54,21 +54,7 @@ namespace MediaBrowser.Api
/// </summary> /// </summary>
public static class SimilarItemsHelper public static class SimilarItemsHelper
{ {
/// <summary> internal static ItemsResult GetSimilarItemsResult(DtoOptions dtoOptions, IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataManager userDataRepository, IDtoService dtoService, ILogger logger, BaseGetSimilarItemsFromItem request, Type[] includeTypes, Func<BaseItem, List<PersonInfo>, List<PersonInfo>, BaseItem, int> getSimilarityScore)
/// Gets the similar items.
/// </summary>
/// <param name="dtoOptions">The dto options.</param>
/// <param name="userManager">The user manager.</param>
/// <param name="itemRepository">The item repository.</param>
/// <param name="libraryManager">The library manager.</param>
/// <param name="userDataRepository">The user data repository.</param>
/// <param name="dtoService">The dto service.</param>
/// <param name="logger">The logger.</param>
/// <param name="request">The request.</param>
/// <param name="includeInSearch">The include in search.</param>
/// <param name="getSimilarityScore">The get similarity score.</param>
/// <returns>ItemsResult.</returns>
internal static ItemsResult GetSimilarItemsResult(DtoOptions dtoOptions, IUserManager userManager, IItemRepository itemRepository, ILibraryManager libraryManager, IUserDataManager userDataRepository, IDtoService dtoService, ILogger logger, BaseGetSimilarItemsFromItem request, Func<BaseItem, bool> includeInSearch, Func<BaseItem, List<PersonInfo>, List<PersonInfo>, BaseItem, int> getSimilarityScore)
{ {
var user = !string.IsNullOrWhiteSpace(request.UserId) ? userManager.GetUserById(request.UserId) : null; var user = !string.IsNullOrWhiteSpace(request.UserId) ? userManager.GetUserById(request.UserId) : null;
@ -76,11 +62,13 @@ namespace MediaBrowser.Api
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder : (!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
libraryManager.RootFolder) : libraryManager.GetItemById(request.Id); libraryManager.RootFolder) : libraryManager.GetItemById(request.Id);
Func<BaseItem, bool> filter = i => i.Id != item.Id && includeInSearch(i); var query = new InternalItemsQuery(user)
{
IncludeItemTypes = includeTypes.Select(i => i.Name).ToArray(),
Recursive = true
};
var inputItems = user == null var inputItems = libraryManager.GetItemList(query);
? libraryManager.RootFolder.GetRecursiveChildren(filter)
: user.RootFolder.GetRecursiveChildren(user, filter);
var items = GetSimilaritems(item, libraryManager, inputItems, getSimilarityScore) var items = GetSimilaritems(item, libraryManager, inputItems, getSimilarityScore)
.ToList(); .ToList();

View File

@ -263,7 +263,7 @@ namespace MediaBrowser.Api
_userDataManager, _userDataManager,
_dtoService, _dtoService,
Logger, Logger,
request, item => item is Series, request, new[] { typeof(Series) },
SimilarItemsHelper.GetSimiliarityScore); SimilarItemsHelper.GetSimiliarityScore);
return ToOptimizedSerializedResultUsingCache(result); return ToOptimizedSerializedResultUsingCache(result);

View File

@ -62,15 +62,5 @@ namespace MediaBrowser.Model.Dto
/// </summary> /// </summary>
/// <value>The book count.</value> /// <value>The book count.</value>
public int BookCount { get; set; } public int BookCount { get; set; }
/// <summary>
/// Gets or sets the unique types.
/// </summary>
/// <value>The unique types.</value>
public List<string> UniqueTypes { get; set; }
public ItemCounts()
{
UniqueTypes = new List<string>();
}
} }
} }