StudiosValidator.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Model.Logging;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.Server.Implementations.Library.Validators
  10. {
  11. class StudiosValidator
  12. {
  13. /// <summary>
  14. /// The _library manager
  15. /// </summary>
  16. private readonly ILibraryManager _libraryManager;
  17. /// <summary>
  18. /// The _user manager
  19. /// </summary>
  20. private readonly IUserManager _userManager;
  21. /// <summary>
  22. /// The _logger
  23. /// </summary>
  24. private readonly ILogger _logger;
  25. public StudiosValidator(ILibraryManager libraryManager, IUserManager userManager, ILogger logger)
  26. {
  27. _libraryManager = libraryManager;
  28. _userManager = userManager;
  29. _logger = logger;
  30. }
  31. /// <summary>
  32. /// Runs the specified progress.
  33. /// </summary>
  34. /// <param name="progress">The progress.</param>
  35. /// <param name="cancellationToken">The cancellation token.</param>
  36. /// <returns>Task.</returns>
  37. public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
  38. {
  39. var userLibraries = _userManager.Users
  40. .Select(i => new Tuple<Guid, IList<BaseItem>>(i.Id, i.RootFolder.GetRecursiveChildren(i, null)))
  41. .ToList();
  42. var masterDictionary = new Dictionary<string, Dictionary<Guid, Dictionary<CountType, int>>>(StringComparer.OrdinalIgnoreCase);
  43. // Populate counts of items
  44. //SetItemCounts(null, allLibraryItems, masterDictionary);
  45. progress.Report(2);
  46. var numComplete = 0;
  47. foreach (var lib in userLibraries)
  48. {
  49. SetItemCounts(lib.Item1, lib.Item2, masterDictionary);
  50. numComplete++;
  51. double percent = numComplete;
  52. percent /= userLibraries.Count;
  53. percent *= 8;
  54. progress.Report(percent);
  55. }
  56. progress.Report(10);
  57. var count = masterDictionary.Count;
  58. numComplete = 0;
  59. foreach (var name in masterDictionary.Keys)
  60. {
  61. try
  62. {
  63. await UpdateItemByNameCounts(name, cancellationToken, masterDictionary[name]).ConfigureAwait(false);
  64. }
  65. catch (OperationCanceledException)
  66. {
  67. // Don't clutter the log
  68. break;
  69. }
  70. catch (Exception ex)
  71. {
  72. _logger.ErrorException("Error updating counts for {0}", ex, name);
  73. }
  74. numComplete++;
  75. double percent = numComplete;
  76. percent /= count;
  77. percent *= 90;
  78. progress.Report(percent + 10);
  79. }
  80. progress.Report(100);
  81. }
  82. private async Task UpdateItemByNameCounts(string name, CancellationToken cancellationToken, Dictionary<Guid, Dictionary<CountType, int>> counts)
  83. {
  84. var itemByName = _libraryManager.GetStudio(name);
  85. foreach (var libraryId in counts.Keys)
  86. {
  87. var itemCounts = CountHelpers.GetCounts(counts[libraryId]);
  88. itemByName.UserItemCounts[libraryId] = itemCounts;
  89. }
  90. await itemByName.RefreshMetadata(cancellationToken).ConfigureAwait(false);
  91. }
  92. private void SetItemCounts(Guid userId, IEnumerable<BaseItem> allItems, Dictionary<string, Dictionary<Guid, Dictionary<CountType, int>>> masterDictionary)
  93. {
  94. foreach (var media in allItems)
  95. {
  96. var names = media
  97. .Studios
  98. .Distinct(StringComparer.OrdinalIgnoreCase)
  99. .ToList();
  100. CountHelpers.SetItemCounts(userId, media, names, masterDictionary);
  101. }
  102. }
  103. }
  104. }