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,
_dtoService,
Logger,
request, item => item is Game,
request, new[] { typeof(Game) },
SimilarItemsHelper.GetSimiliarityScore);
return ToOptimizedSerializedResultUsingCache(result);

View File

@ -397,7 +397,7 @@ namespace MediaBrowser.Api.Library
});
}
if (item is Series || (program != null && program.IsSeries) )
if (item is Series || (program != null && program.IsSeries))
{
return new TvShowsService(_userManager, _userDataManager, _libraryManager, _itemRepo, _dtoService, _tvManager)
{
@ -659,87 +659,38 @@ namespace MediaBrowser.Api.Library
/// <returns>System.Object.</returns>
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
{
AlbumCount = filteredItems.Count(i => i is MusicAlbum),
EpisodeCount = filteredItems.Count(i => i is Episode),
GameCount = filteredItems.Count(i => i is Game),
GameSystemCount = filteredItems.Count(i => i is GameSystem),
MovieCount = filteredItems.Count(i => i is Movie),
SeriesCount = filteredItems.Count(i => i is Series),
SongCount = filteredItems.Count(i => i is Audio),
MusicVideoCount = filteredItems.Count(i => i is MusicVideo),
BoxSetCount = filteredItems.Count(i => i is BoxSet),
BookCount = filteredItems.Count(i => i is Book),
UniqueTypes = filteredItems.Select(i => i.GetClientTypeName()).Distinct().ToList()
AlbumCount = GetCount(typeof(MusicAlbum), user, request),
EpisodeCount = GetCount(typeof(Episode), user, request),
GameCount = GetCount(typeof(Game), user, request),
GameSystemCount = GetCount(typeof(GameSystem), user, request),
MovieCount = GetCount(typeof(Movie), user, request),
SeriesCount = GetCount(typeof(Series), user, request),
SongCount = GetCount(typeof(Audio), user, request),
MusicVideoCount = GetCount(typeof(MusicVideo), user, request),
BoxSetCount = GetCount(typeof(BoxSet), user, request),
BookCount = GetCount(typeof(Book), user, request)
};
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))
{
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;
return _libraryManager.GetItemsResult(query).TotalRecordCount;
}
/// <summary>
@ -980,20 +931,15 @@ namespace MediaBrowser.Api.Library
? new string[] { }
: request.IncludeItemTypes.Split(',');
Func<BaseItem, bool> filter = i =>
{
if (includeTypes.Length > 0)
{
if (!includeTypes.Contains(i.GetType().Name, StringComparer.OrdinalIgnoreCase))
{
return false;
}
}
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
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
.ToLookup(i => i.ProductionYear ?? -1)

View File

@ -52,10 +52,15 @@ namespace MediaBrowser.Api.Music
public object Get(GetSimilarArtists request)
{
var result = GetSimilarItemsResult(
request,
var dtoOptions = GetDtoOptions(request);
var result = SimilarItemsHelper.GetSimilarItemsResult(dtoOptions, _userManager,
_itemRepo,
_libraryManager,
_userDataRepository,
_dtoService,
Logger,
request, new[] { typeof(MusicArtist) },
SimilarItemsHelper.GetSimiliarityScore);
return ToOptimizedSerializedResultUsingCache(result);
@ -76,45 +81,12 @@ namespace MediaBrowser.Api.Music
_userDataRepository,
_dtoService,
Logger,
request, item => item is MusicAlbum,
request, new[] { typeof(MusicAlbum) },
GetAlbumSimilarityScore);
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>
/// Gets the album similarity score.
/// </summary>

View File

@ -54,21 +54,7 @@ namespace MediaBrowser.Api
/// </summary>
public static class SimilarItemsHelper
{
/// <summary>
/// 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)
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)
{
var user = !string.IsNullOrWhiteSpace(request.UserId) ? userManager.GetUserById(request.UserId) : null;
@ -76,11 +62,13 @@ namespace MediaBrowser.Api
(!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder :
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
? libraryManager.RootFolder.GetRecursiveChildren(filter)
: user.RootFolder.GetRecursiveChildren(user, filter);
var inputItems = libraryManager.GetItemList(query);
var items = GetSimilaritems(item, libraryManager, inputItems, getSimilarityScore)
.ToList();

View File

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

View File

@ -62,15 +62,5 @@ namespace MediaBrowser.Model.Dto
/// </summary>
/// <value>The book count.</value>
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>();
}
}
}