From d49494476770b3c0a091841bd3bbd44862fb8137 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 9 Mar 2014 18:14:44 -0400 Subject: calculate item by name counts on the fly --- .../Dto/DtoService.cs | 62 ++++++----- .../Library/LibraryManager.cs | 9 +- .../Library/SearchEngine.cs | 4 +- .../Library/Validators/ArtistsValidator.cs | 45 +------- .../Library/Validators/GameGenresValidator.cs | 62 ++--------- .../Library/Validators/GenresValidator.cs | 62 ++--------- .../Library/Validators/MusicGenresValidator.cs | 73 +++---------- .../Library/Validators/PeoplePostScanTask.cs | 103 ++---------------- .../Library/Validators/PeopleValidator.cs | 6 +- .../Library/Validators/StudiosValidator.cs | 65 ++---------- .../LiveTv/LiveTvManager.cs | 118 ++++++++++++++------- .../MediaBrowser.Server.Implementations.csproj | 7 -- .../Sorting/AlbumCountComparer.cs | 71 ------------- .../Sorting/EpisodeCountComparer.cs | 71 ------------- .../Sorting/MovieCountComparer.cs | 71 ------------- .../Sorting/MusicVideoCountComparer.cs | 71 ------------- .../Sorting/SeriesCountComparer.cs | 71 ------------- .../Sorting/SongCountComparer.cs | 71 ------------- .../Sorting/TrailerCountComparer.cs | 71 ------------- 19 files changed, 181 insertions(+), 932 deletions(-) delete mode 100644 MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs delete mode 100644 MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs delete mode 100644 MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs delete mode 100644 MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs delete mode 100644 MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs delete mode 100644 MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs delete mode 100644 MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs (limited to 'MediaBrowser.Server.Implementations') diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index 7bf87875e..641bf0a6a 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -121,41 +121,51 @@ namespace MediaBrowser.Server.Implementations.Dto } } - var itemByName = item as IItemByName; - if (itemByName != null) - { - AttachItemByNameCounts(dto, itemByName, user); - } - return dto; } - /// - /// Attaches the item by name counts. - /// - /// The dto. - /// The item. - /// The user. - private void AttachItemByNameCounts(BaseItemDto dto, IItemByName item, User user) + public BaseItemDto GetItemByNameDto(T item, List fields, User user = null) + where T : BaseItem, IItemByName { - if (user == null) + var libraryItems = user != null ? user.RootFolder.GetRecursiveChildren(user) : + _libraryManager.RootFolder.RecursiveChildren; + + return GetItemByNameDto(item, fields, item.GetTaggedItems(libraryItems).ToList(), user); + } + + public BaseItemDto GetItemByNameDto(T item, List fields, List taggedItems, User user = null) + where T : BaseItem, IItemByName + { + var dto = GetBaseItemDto(item, fields, user); + + if (item is MusicArtist || item is MusicGenre) { - return; + dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum); + dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo); + dto.SongCount = taggedItems.Count(i => i is Audio); + } + else if (item is GameGenre) + { + dto.GameCount = taggedItems.Count(i => i is Game); } + else + { + // This populates them all and covers Genre, Person, Studio, Year - var counts = item.GetItemByNameCounts(user.Id) ?? new ItemByNameCounts(); + dto.AdultVideoCount = taggedItems.Count(i => i is AdultVideo); + dto.AlbumCount = taggedItems.Count(i => i is MusicAlbum); + dto.EpisodeCount = taggedItems.Count(i => i is Episode); + dto.GameCount = taggedItems.Count(i => i is Game); + dto.MovieCount = taggedItems.Count(i => i is Movie); + dto.MusicVideoCount = taggedItems.Count(i => i is MusicVideo); + dto.SeriesCount = taggedItems.Count(i => i is Series); + dto.SongCount = taggedItems.Count(i => i is Audio); + dto.TrailerCount = taggedItems.Count(i => i is Trailer); + } - dto.ChildCount = counts.TotalCount; + dto.ChildCount = taggedItems.Count; - dto.AdultVideoCount = counts.AdultVideoCount; - dto.AlbumCount = counts.AlbumCount; - dto.EpisodeCount = counts.EpisodeCount; - dto.GameCount = counts.GameCount; - dto.MovieCount = counts.MovieCount; - dto.MusicVideoCount = counts.MusicVideoCount; - dto.SeriesCount = counts.SeriesCount; - dto.SongCount = counts.SongCount; - dto.TrailerCount = counts.TrailerCount; + return dto; } /// diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 6e9d803bf..b190b8947 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -484,6 +484,9 @@ namespace MediaBrowser.Server.Implementations.Library await ItemRepository.DeleteItem(child.Id, CancellationToken.None).ConfigureAwait(false); } + BaseItem removed; + _libraryItemsCache.TryRemove(item.Id, out removed); + ReportItemRemoved(item); } @@ -922,10 +925,10 @@ namespace MediaBrowser.Server.Implementations.Library /// Task. public Task ValidatePeople(CancellationToken cancellationToken, IProgress progress) { - // Ensure the location is unavailable. + // Ensure the location is available. Directory.CreateDirectory(ConfigurationManager.ApplicationPaths.PeoplePath); - return new PeopleValidator(this, _logger).ValidatePeople(cancellationToken, progress); + return new PeopleValidator(this, _logger).ValidatePeople(cancellationToken, new MetadataRefreshOptions(), progress); } /// @@ -953,7 +956,7 @@ namespace MediaBrowser.Server.Implementations.Library // Ensure the location is unavailable. Directory.CreateDirectory(ConfigurationManager.ApplicationPaths.MusicGenrePath); - return new MusicGenresValidator(this, _userManager, _logger).Run(progress, cancellationToken); + return new MusicGenresValidator(this, _logger).Run(progress, cancellationToken); } /// diff --git a/MediaBrowser.Server.Implementations/Library/SearchEngine.cs b/MediaBrowser.Server.Implementations/Library/SearchEngine.cs index b22fd343b..aaafd35a9 100644 --- a/MediaBrowser.Server.Implementations/Library/SearchEngine.cs +++ b/MediaBrowser.Server.Implementations/Library/SearchEngine.cs @@ -102,7 +102,9 @@ namespace MediaBrowser.Server.Implementations.Library if (query.IncludeArtists) { // Find artists - var artists = _libraryManager.GetAllArtists(items) + var artists = items.OfType private readonly ILibraryManager _libraryManager; - /// - /// The _user manager - /// - private readonly IUserManager _userManager; - /// /// The _logger /// private readonly ILogger _logger; - public MusicGenresValidator(ILibraryManager libraryManager, IUserManager userManager, ILogger logger) + public MusicGenresValidator(ILibraryManager libraryManager, ILogger logger) { _libraryManager = libraryManager; - _userManager = userManager; _logger = logger; } @@ -42,38 +34,24 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var userLibraries = _userManager.Users - .Select(i => new Tuple>(i.Id, i.RootFolder.GetRecursiveChildren(i, m => m is IHasMusicGenres))) + var items = _libraryManager.RootFolder.RecursiveChildren.Where(i => (i is IHasMusicGenres)) + .SelectMany(i => i.Genres) + .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); - var masterDictionary = new Dictionary>>(StringComparer.OrdinalIgnoreCase); - progress.Report(2); - var numComplete = 0; + var count = items.Count; - foreach (var lib in userLibraries) + foreach (var name in items) { - SetItemCounts(lib.Item1, lib.Item2, masterDictionary); - - numComplete++; - double percent = numComplete; - percent /= userLibraries.Count; - percent *= 8; - - progress.Report(percent); - } - - progress.Report(10); + cancellationToken.ThrowIfCancellationRequested(); - var count = masterDictionary.Count; - numComplete = 0; - - foreach (var name in masterDictionary.Keys) - { try { - await UpdateItemByNameCounts(name, cancellationToken, masterDictionary[name]).ConfigureAwait(false); + var itemByName = _libraryManager.GetMusicGenre(name); + + await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { @@ -82,7 +60,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators } catch (Exception ex) { - _logger.ErrorException("Error updating counts for {0}", ex, name); + _logger.ErrorException("Error refreshing {0}", ex, name); } numComplete++; @@ -95,32 +73,5 @@ namespace MediaBrowser.Server.Implementations.Library.Validators progress.Report(100); } - - private Task UpdateItemByNameCounts(string name, CancellationToken cancellationToken, Dictionary> counts) - { - var itemByName = _libraryManager.GetMusicGenre(name); - - foreach (var libraryId in counts.Keys) - { - var itemCounts = CountHelpers.GetCounts(counts[libraryId]); - - itemByName.SetItemByNameCounts(libraryId, itemCounts); - } - - return itemByName.RefreshMetadata(cancellationToken); - } - - private void SetItemCounts(Guid userId, IEnumerable allItems, Dictionary>> masterDictionary) - { - foreach (var media in allItems) - { - var names = media - .Genres - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - - CountHelpers.SetItemCounts(userId, media, names, masterDictionary); - } - } } } diff --git a/MediaBrowser.Server.Implementations/Library/Validators/PeoplePostScanTask.cs b/MediaBrowser.Server.Implementations/Library/Validators/PeoplePostScanTask.cs index 86c5dbc4c..d11e62a1a 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/PeoplePostScanTask.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/PeoplePostScanTask.cs @@ -1,10 +1,7 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Logging; using System; -using System.Collections.Generic; -using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -17,20 +14,14 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// private readonly ILibraryManager _libraryManager; - /// - /// The _user manager - /// - private readonly IUserManager _userManager; - /// /// The _logger /// private readonly ILogger _logger; - public PeoplePostScanTask(ILibraryManager libraryManager, IUserManager userManager, ILogger logger) + public PeoplePostScanTask(ILibraryManager libraryManager, ILogger logger) { _libraryManager = libraryManager; - _userManager = userManager; _logger = logger; } @@ -42,94 +33,12 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public Task Run(IProgress progress, CancellationToken cancellationToken) { - return RunInternal(progress, cancellationToken); - } - - private async Task RunInternal(IProgress progress, CancellationToken cancellationToken) - { - var userLibraries = _userManager.Users - .Select(i => new Tuple>(i.Id, i.RootFolder.GetRecursiveChildren(i, null))) - .ToList(); - - var masterDictionary = new Dictionary>>(StringComparer.OrdinalIgnoreCase); - - progress.Report(2); - - var numComplete = 0; - - foreach (var lib in userLibraries) + return new PeopleValidator(_libraryManager, _logger).ValidatePeople(cancellationToken, new MetadataRefreshOptions { - cancellationToken.ThrowIfCancellationRequested(); - - SetItemCounts(lib.Item1, lib.Item2, masterDictionary); - - numComplete++; - double percent = numComplete; - percent /= userLibraries.Count; - percent *= 8; + ImageRefreshMode = ImageRefreshMode.ValidationOnly, + MetadataRefreshMode = MetadataRefreshMode.None - progress.Report(percent); - } - - progress.Report(10); - - var count = masterDictionary.Count; - numComplete = 0; - - foreach (var name in masterDictionary.Keys) - { - cancellationToken.ThrowIfCancellationRequested(); - - try - { - var counts = masterDictionary[name]; - - var itemByName = _libraryManager.GetPerson(name); - - // The only purpose here is to be able to react to image changes without running the people task. - // All other metadata can wait for that. - await itemByName.RefreshMetadata(new MetadataRefreshOptions - { - ImageRefreshMode = ImageRefreshMode.ValidationOnly, - MetadataRefreshMode = MetadataRefreshMode.None - - }, cancellationToken).ConfigureAwait(false); - - foreach (var libraryId in counts.Keys) - { - var itemCounts = CountHelpers.GetCounts(counts[libraryId]); - - itemByName.SetItemByNameCounts(libraryId, itemCounts); - } - } - catch (Exception ex) - { - _logger.ErrorException("Error updating counts for {0}", ex, name); - } - - numComplete++; - double percent = numComplete; - percent /= count; - percent *= 90; - - progress.Report(percent + 10); - } - - progress.Report(100); + }, progress); } - - private void SetItemCounts(Guid userId, IEnumerable allItems, Dictionary>> masterDictionary) - { - foreach (var media in allItems) - { - var names = media - .People.Select(i => i.Name) - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - - CountHelpers.SetItemCounts(userId, media, names, masterDictionary); - } - } - } } diff --git a/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs index 268bccd7a..722c24a10 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/PeopleValidator.cs @@ -1,5 +1,6 @@ using MediaBrowser.Common.Progress; using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Logging; using MoreLinq; using System; @@ -38,9 +39,10 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Validates the people. /// /// The cancellation token. + /// The options. /// The progress. /// Task. - public async Task ValidatePeople(CancellationToken cancellationToken, IProgress progress) + public async Task ValidatePeople(CancellationToken cancellationToken, MetadataRefreshOptions options, IProgress progress) { var innerProgress = new ActionableProgress(); @@ -61,7 +63,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators { var item = _libraryManager.GetPerson(person.Name); - await item.RefreshMetadata(cancellationToken).ConfigureAwait(false); + await item.RefreshMetadata(options, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { diff --git a/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs b/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs index 54fadfb77..a2ec9788c 100644 --- a/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs +++ b/MediaBrowser.Server.Implementations/Library/Validators/StudiosValidator.cs @@ -1,8 +1,6 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Library; using MediaBrowser.Model.Logging; using System; -using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -41,38 +39,24 @@ namespace MediaBrowser.Server.Implementations.Library.Validators /// Task. public async Task Run(IProgress progress, CancellationToken cancellationToken) { - var userLibraries = _userManager.Users - .Select(i => new Tuple>(i.Id, i.RootFolder.GetRecursiveChildren(i, null))) + var items = _libraryManager.RootFolder.RecursiveChildren + .SelectMany(i => i.Studios) + .Distinct(StringComparer.OrdinalIgnoreCase) .ToList(); - var masterDictionary = new Dictionary>>(StringComparer.OrdinalIgnoreCase); - progress.Report(2); - var numComplete = 0; + var count = items.Count; - foreach (var lib in userLibraries) + foreach (var name in items) { - SetItemCounts(lib.Item1, lib.Item2, masterDictionary); - - numComplete++; - double percent = numComplete; - percent /= userLibraries.Count; - percent *= 8; - - progress.Report(percent); - } + cancellationToken.ThrowIfCancellationRequested(); - progress.Report(10); - - var count = masterDictionary.Count; - numComplete = 0; - - foreach (var name in masterDictionary.Keys) - { try { - await UpdateItemByNameCounts(name, cancellationToken, masterDictionary[name]).ConfigureAwait(false); + var itemByName = _libraryManager.GetStudio(name); + + await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { @@ -81,7 +65,7 @@ namespace MediaBrowser.Server.Implementations.Library.Validators } catch (Exception ex) { - _logger.ErrorException("Error updating counts for {0}", ex, name); + _logger.ErrorException("Error refreshing {0}", ex, name); } numComplete++; @@ -94,32 +78,5 @@ namespace MediaBrowser.Server.Implementations.Library.Validators progress.Report(100); } - - private Task UpdateItemByNameCounts(string name, CancellationToken cancellationToken, Dictionary> counts) - { - var itemByName = _libraryManager.GetStudio(name); - - foreach (var libraryId in counts.Keys) - { - var itemCounts = CountHelpers.GetCounts(counts[libraryId]); - - itemByName.SetItemByNameCounts(libraryId, itemCounts); - } - - return itemByName.RefreshMetadata(cancellationToken); - } - - private void SetItemCounts(Guid userId, IEnumerable allItems, Dictionary>> masterDictionary) - { - foreach (var media in allItems) - { - var names = media - .Studios - .Distinct(StringComparer.OrdinalIgnoreCase) - .ToList(); - - CountHelpers.SetItemCounts(userId, media, names, masterDictionary); - } - } } } diff --git a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs index 7c0361a1f..6cd782140 100644 --- a/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs +++ b/MediaBrowser.Server.Implementations/LiveTv/LiveTvManager.cs @@ -47,6 +47,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv private List _channelIdList = new List(); private Dictionary _programs = new Dictionary(); + private readonly ConcurrentDictionary _refreshedPrograms = new ConcurrentDictionary(); private readonly SemaphoreSlim _refreshSemaphore = new SemaphoreSlim(1, 1); @@ -106,7 +107,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv _taskManager.CancelIfRunningAndQueue(); } - public Task> GetChannels(ChannelQuery query, CancellationToken cancellationToken) + public async Task> GetChannels(ChannelQuery query, CancellationToken cancellationToken) { var user = string.IsNullOrEmpty(query.UserId) ? null : _userManager.GetUserById(new Guid(query.UserId)); @@ -161,17 +162,22 @@ namespace MediaBrowser.Server.Implementations.LiveTv allEnumerable = allEnumerable.Take(query.Limit.Value); } - var returnChannels = allEnumerable - .Select(i => _tvDtoService.GetChannelInfoDto(i, GetCurrentProgram(i.ExternalId), user)) - .ToArray(); + var returnList = new List(); + + foreach (var channel in allEnumerable) + { + var currentProgram = await GetCurrentProgram(channel.ExternalId, cancellationToken).ConfigureAwait(false); + + returnList.Add(_tvDtoService.GetChannelInfoDto(channel, currentProgram, user)); + } var result = new QueryResult { - Items = returnChannels, - TotalRecordCount = allChannels.Count + Items = returnList.ToArray(), + TotalRecordCount = returnList.Count }; - return Task.FromResult(result); + return result; } public LiveTvChannel GetInternalChannel(string id) @@ -184,16 +190,41 @@ namespace MediaBrowser.Server.Implementations.LiveTv return _libraryManager.GetItemById(id) as LiveTvChannel; } - public LiveTvProgram GetInternalProgram(string id) + public async Task GetInternalProgram(string id, CancellationToken cancellationToken) { var guid = new Guid(id); LiveTvProgram obj = null; _programs.TryGetValue(guid, out obj); + + if (obj != null) + { + await RefreshIfNeeded(obj, cancellationToken).ConfigureAwait(false); + } return obj; } + private async Task RefreshIfNeeded(IEnumerable programs, CancellationToken cancellationToken) + { + foreach (var program in programs) + { + await RefreshIfNeeded(program, cancellationToken).ConfigureAwait(false); + } + } + + private async Task RefreshIfNeeded(LiveTvProgram program, CancellationToken cancellationToken) + { + if (_refreshedPrograms.ContainsKey(program.Id)) + { + return; + } + + await program.RefreshMetadata(CancellationToken.None).ConfigureAwait(false); + + _refreshedPrograms.TryAdd(program.Id, true); + } + public async Task GetInternalRecording(string id, CancellationToken cancellationToken) { var service = ActiveService; @@ -336,10 +367,8 @@ namespace MediaBrowser.Server.Implementations.LiveTv return item; } - private async Task GetProgram(ProgramInfo info, ChannelType channelType, string serviceName, CancellationToken cancellationToken) + private LiveTvProgram GetProgram(ProgramInfo info, ChannelType channelType, string serviceName, CancellationToken cancellationToken) { - var isNew = false; - var id = _tvDtoService.GetInternalProgramId(serviceName, info.Id); var item = _itemRepo.RetrieveItem(id) as LiveTvProgram; @@ -353,8 +382,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv DateCreated = DateTime.UtcNow, DateModified = DateTime.UtcNow }; - - isNew = true; } item.ChannelType = channelType; @@ -386,12 +413,6 @@ namespace MediaBrowser.Server.Implementations.LiveTv item.RunTimeTicks = (info.EndDate - info.StartDate).Ticks; item.StartDate = info.StartDate; - await item.RefreshMetadata(new MetadataRefreshOptions - { - ForceSave = isNew - - }, cancellationToken); - return item; } @@ -464,7 +485,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv public async Task GetProgram(string id, CancellationToken cancellationToken, User user = null) { - var program = GetInternalProgram(id); + var program = await GetInternalProgram(id, cancellationToken).ConfigureAwait(false); var channel = GetChannel(program); @@ -531,7 +552,9 @@ namespace MediaBrowser.Server.Implementations.LiveTv programs = programs.Where(i => i.IsParentalAllowed(currentUser)); } - var returnArray = programs + var programList = programs.ToList(); + + var returnArray = programList .Select(i => { var channel = GetChannel(i); @@ -540,6 +563,8 @@ namespace MediaBrowser.Server.Implementations.LiveTv }) .ToArray(); + await RefreshIfNeeded(programList, cancellationToken).ConfigureAwait(false); + await AddRecordingInfo(returnArray, cancellationToken).ConfigureAwait(false); var result = new QueryResult @@ -592,7 +617,11 @@ namespace MediaBrowser.Server.Implementations.LiveTv .OrderBy(i => i.StartDate); } - var returnArray = programs + programList = programs.ToList(); + + await RefreshIfNeeded(programList, cancellationToken).ConfigureAwait(false); + + var returnArray = programList .Select(i => { var channel = GetChannel(i); @@ -791,8 +820,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv var channelPrograms = await service.GetProgramsAsync(currentChannel.ExternalId, start, end, cancellationToken).ConfigureAwait(false); - var programTasks = channelPrograms.Select(program => GetProgram(program, currentChannel.ChannelType, service.Name, cancellationToken)); - var programEntities = await Task.WhenAll(programTasks).ConfigureAwait(false); + var programEntities = channelPrograms.Select(program => GetProgram(program, currentChannel.ChannelType, service.Name, cancellationToken)); programs.AddRange(programEntities); } @@ -813,6 +841,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv } _programs = programs.ToDictionary(i => i.Id); + _refreshedPrograms.Clear(); progress.Report(90); // Load these now which will prefetch metadata @@ -1031,14 +1060,20 @@ namespace MediaBrowser.Server.Implementations.LiveTv .Where(i => _tvDtoService.GetInternalSeriesTimerId(currentServiceName, i.SeriesTimerId) == guid); } - var returnArray = timers - .Select(i => - { - var program = string.IsNullOrEmpty(i.ProgramId) ? null : GetInternalProgram(_tvDtoService.GetInternalProgramId(service.Name, i.ProgramId).ToString("N")); - var channel = string.IsNullOrEmpty(i.ChannelId) ? null : GetInternalChannel(_tvDtoService.GetInternalChannelId(service.Name, i.ChannelId)); + var returnList = new List(); - return _tvDtoService.GetTimerInfoDto(i, service, program, channel); - }) + foreach (var i in timers) + { + var program = string.IsNullOrEmpty(i.ProgramId) ? + null : + await GetInternalProgram(_tvDtoService.GetInternalProgramId(service.Name, i.ProgramId).ToString("N"), cancellationToken).ConfigureAwait(false); + + var channel = string.IsNullOrEmpty(i.ChannelId) ? null : GetInternalChannel(_tvDtoService.GetInternalChannelId(service.Name, i.ChannelId)); + + returnList.Add(_tvDtoService.GetTimerInfoDto(i, service, program, channel)); + } + + var returnArray = returnList .OrderBy(i => i.StartDate) .ToArray(); @@ -1163,24 +1198,33 @@ namespace MediaBrowser.Server.Implementations.LiveTv }; } - public Task GetChannel(string id, CancellationToken cancellationToken, User user = null) + public async Task GetChannel(string id, CancellationToken cancellationToken, User user = null) { var channel = GetInternalChannel(id); - var dto = _tvDtoService.GetChannelInfoDto(channel, GetCurrentProgram(channel.ExternalId), user); + var currentProgram = await GetCurrentProgram(channel.ExternalId, cancellationToken).ConfigureAwait(false); + + var dto = _tvDtoService.GetChannelInfoDto(channel, currentProgram, user); - return Task.FromResult(dto); + return dto; } - private LiveTvProgram GetCurrentProgram(string externalChannelId) + private async Task GetCurrentProgram(string externalChannelId, CancellationToken cancellationToken) { var now = DateTime.UtcNow; - return _programs.Values + var program = _programs.Values .Where(i => string.Equals(externalChannelId, i.ExternalChannelId, StringComparison.OrdinalIgnoreCase)) .OrderBy(i => i.StartDate) .SkipWhile(i => now >= (i.EndDate ?? DateTime.MinValue)) .FirstOrDefault(); + + if (program != null) + { + await RefreshIfNeeded(program, cancellationToken).ConfigureAwait(false); + } + + return program; } private async Task GetNewTimerDefaultsInternal(CancellationToken cancellationToken, LiveTvProgram program = null) @@ -1236,7 +1280,7 @@ namespace MediaBrowser.Server.Implementations.LiveTv public async Task GetNewTimerDefaults(string programId, CancellationToken cancellationToken) { - var program = GetInternalProgram(programId); + var program = await GetInternalProgram(programId, cancellationToken).ConfigureAwait(false); var programDto = await GetProgram(programId, cancellationToken).ConfigureAwait(false); var defaults = await GetNewTimerDefaultsInternal(cancellationToken, program).ConfigureAwait(false); diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj index 78902f0dc..c44b60845 100644 --- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj +++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj @@ -216,7 +216,6 @@ - @@ -224,13 +223,10 @@ - - - @@ -240,16 +236,13 @@ - - - diff --git a/MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs deleted file mode 100644 index e35ba00f2..000000000 --- a/MediaBrowser.Server.Implementations/Sorting/AlbumCountComparer.cs +++ /dev/null @@ -1,71 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Querying; - -namespace MediaBrowser.Server.Implementations.Sorting -{ - class AlbumCountComparer : IUserBaseItemComparer - { - /// - /// Gets or sets the user. - /// - /// The user. - public User User { get; set; } - - /// - /// Gets or sets the user manager. - /// - /// The user manager. - public IUserManager UserManager { get; set; } - - /// - /// Gets or sets the user data repository. - /// - /// The user data repository. - public IUserDataManager UserDataRepository { get; set; } - - /// - /// Compares the specified x. - /// - /// The x. - /// The y. - /// System.Int32. - public int Compare(BaseItem x, BaseItem y) - { - return GetValue(x).CompareTo(GetValue(y)); - } - - /// - /// Gets the date. - /// - /// The x. - /// DateTime. - private int GetValue(BaseItem x) - { - var itemByName = x as IItemByName; - - if (itemByName != null) - { - var counts = itemByName.GetItemByNameCounts(User.Id); - - if (counts != null) - { - return counts.AlbumCount; - } - } - - return 0; - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ItemSortBy.AlbumCount; } - } - } -} diff --git a/MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs deleted file mode 100644 index b3fd8a023..000000000 --- a/MediaBrowser.Server.Implementations/Sorting/EpisodeCountComparer.cs +++ /dev/null @@ -1,71 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Querying; - -namespace MediaBrowser.Server.Implementations.Sorting -{ - class EpisodeCountComparer : IUserBaseItemComparer - { - /// - /// Gets or sets the user. - /// - /// The user. - public User User { get; set; } - - /// - /// Gets or sets the user manager. - /// - /// The user manager. - public IUserManager UserManager { get; set; } - - /// - /// Gets or sets the user data repository. - /// - /// The user data repository. - public IUserDataManager UserDataRepository { get; set; } - - /// - /// Compares the specified x. - /// - /// The x. - /// The y. - /// System.Int32. - public int Compare(BaseItem x, BaseItem y) - { - return GetValue(x).CompareTo(GetValue(y)); - } - - /// - /// Gets the date. - /// - /// The x. - /// DateTime. - private int GetValue(BaseItem x) - { - var itemByName = x as IItemByName; - - if (itemByName != null) - { - var counts = itemByName.GetItemByNameCounts(User.Id); - - if (counts != null) - { - return counts.EpisodeCount; - } - } - - return 0; - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ItemSortBy.EpisodeCount; } - } - } -} diff --git a/MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs deleted file mode 100644 index 605f4d1af..000000000 --- a/MediaBrowser.Server.Implementations/Sorting/MovieCountComparer.cs +++ /dev/null @@ -1,71 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Querying; - -namespace MediaBrowser.Server.Implementations.Sorting -{ - public class MovieCountComparer : IUserBaseItemComparer - { - /// - /// Gets or sets the user. - /// - /// The user. - public User User { get; set; } - - /// - /// Gets or sets the user manager. - /// - /// The user manager. - public IUserManager UserManager { get; set; } - - /// - /// Gets or sets the user data repository. - /// - /// The user data repository. - public IUserDataManager UserDataRepository { get; set; } - - /// - /// Compares the specified x. - /// - /// The x. - /// The y. - /// System.Int32. - public int Compare(BaseItem x, BaseItem y) - { - return GetValue(x).CompareTo(GetValue(y)); - } - - /// - /// Gets the date. - /// - /// The x. - /// DateTime. - private int GetValue(BaseItem x) - { - var itemByName = x as IItemByName; - - if (itemByName != null) - { - var counts = itemByName.GetItemByNameCounts(User.Id); - - if (counts != null) - { - return counts.MovieCount; - } - } - - return 0; - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ItemSortBy.MovieCount; } - } - } -} diff --git a/MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs deleted file mode 100644 index 6c9c5534d..000000000 --- a/MediaBrowser.Server.Implementations/Sorting/MusicVideoCountComparer.cs +++ /dev/null @@ -1,71 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Querying; - -namespace MediaBrowser.Server.Implementations.Sorting -{ - class MusicVideoCountComparer : IUserBaseItemComparer - { - /// - /// Gets or sets the user. - /// - /// The user. - public User User { get; set; } - - /// - /// Gets or sets the user manager. - /// - /// The user manager. - public IUserManager UserManager { get; set; } - - /// - /// Gets or sets the user data repository. - /// - /// The user data repository. - public IUserDataManager UserDataRepository { get; set; } - - /// - /// Compares the specified x. - /// - /// The x. - /// The y. - /// System.Int32. - public int Compare(BaseItem x, BaseItem y) - { - return GetValue(x).CompareTo(GetValue(y)); - } - - /// - /// Gets the date. - /// - /// The x. - /// DateTime. - private int GetValue(BaseItem x) - { - var itemByName = x as IItemByName; - - if (itemByName != null) - { - var counts = itemByName.GetItemByNameCounts(User.Id); - - if (counts != null) - { - return counts.MusicVideoCount; - } - } - - return 0; - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ItemSortBy.MusicVideoCount; } - } - } -} diff --git a/MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs deleted file mode 100644 index 8567e400c..000000000 --- a/MediaBrowser.Server.Implementations/Sorting/SeriesCountComparer.cs +++ /dev/null @@ -1,71 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Querying; - -namespace MediaBrowser.Server.Implementations.Sorting -{ - class SeriesCountComparer : IUserBaseItemComparer - { - /// - /// Gets or sets the user. - /// - /// The user. - public User User { get; set; } - - /// - /// Gets or sets the user manager. - /// - /// The user manager. - public IUserManager UserManager { get; set; } - - /// - /// Gets or sets the user data repository. - /// - /// The user data repository. - public IUserDataManager UserDataRepository { get; set; } - - /// - /// Compares the specified x. - /// - /// The x. - /// The y. - /// System.Int32. - public int Compare(BaseItem x, BaseItem y) - { - return GetValue(x).CompareTo(GetValue(y)); - } - - /// - /// Gets the date. - /// - /// The x. - /// DateTime. - private int GetValue(BaseItem x) - { - var itemByName = x as IItemByName; - - if (itemByName != null) - { - var counts = itemByName.GetItemByNameCounts(User.Id); - - if (counts != null) - { - return counts.SeriesCount; - } - } - - return 0; - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ItemSortBy.SeriesCount; } - } - } -} diff --git a/MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs deleted file mode 100644 index 85b849a21..000000000 --- a/MediaBrowser.Server.Implementations/Sorting/SongCountComparer.cs +++ /dev/null @@ -1,71 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Querying; - -namespace MediaBrowser.Server.Implementations.Sorting -{ - class SongCountComparer : IUserBaseItemComparer - { - /// - /// Gets or sets the user. - /// - /// The user. - public User User { get; set; } - - /// - /// Gets or sets the user manager. - /// - /// The user manager. - public IUserManager UserManager { get; set; } - - /// - /// Gets or sets the user data repository. - /// - /// The user data repository. - public IUserDataManager UserDataRepository { get; set; } - - /// - /// Compares the specified x. - /// - /// The x. - /// The y. - /// System.Int32. - public int Compare(BaseItem x, BaseItem y) - { - return GetValue(x).CompareTo(GetValue(y)); - } - - /// - /// Gets the date. - /// - /// The x. - /// DateTime. - private int GetValue(BaseItem x) - { - var itemByName = x as IItemByName; - - if (itemByName != null) - { - var counts = itemByName.GetItemByNameCounts(User.Id); - - if (counts != null) - { - return counts.SongCount; - } - } - - return 0; - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ItemSortBy.SongCount; } - } - } -} diff --git a/MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs b/MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs deleted file mode 100644 index a13875674..000000000 --- a/MediaBrowser.Server.Implementations/Sorting/TrailerCountComparer.cs +++ /dev/null @@ -1,71 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Library; -using MediaBrowser.Controller.Persistence; -using MediaBrowser.Controller.Sorting; -using MediaBrowser.Model.Querying; - -namespace MediaBrowser.Server.Implementations.Sorting -{ - public class TrailerCountComparer : IUserBaseItemComparer - { - /// - /// Gets or sets the user. - /// - /// The user. - public User User { get; set; } - - /// - /// Gets or sets the user manager. - /// - /// The user manager. - public IUserManager UserManager { get; set; } - - /// - /// Gets or sets the user data repository. - /// - /// The user data repository. - public IUserDataManager UserDataRepository { get; set; } - - /// - /// Compares the specified x. - /// - /// The x. - /// The y. - /// System.Int32. - public int Compare(BaseItem x, BaseItem y) - { - return GetValue(x).CompareTo(GetValue(y)); - } - - /// - /// Gets the date. - /// - /// The x. - /// DateTime. - private int GetValue(BaseItem x) - { - var itemByName = x as IItemByName; - - if (itemByName != null) - { - var counts = itemByName.GetItemByNameCounts(User.Id); - - if (counts != null) - { - return counts.TrailerCount; - } - } - - return 0; - } - - /// - /// Gets the name. - /// - /// The name. - public string Name - { - get { return ItemSortBy.TrailerCount; } - } - } -} -- cgit v1.2.3