PeoplePostScanTask.cs 4.5 KB

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