PeoplePostScanTask.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 PeoplePostScanTask : ILibraryPostScanTask
  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 PeoplePostScanTask(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 allItems = _libraryManager.RootFolder.RecursiveChildren.ToList();
  40. var userLibraries = _userManager.Users
  41. .Select(i => new Tuple<Guid, List<BaseItem>>(i.Id, i.RootFolder.GetRecursiveChildren(i).ToList()))
  42. .ToList();
  43. var allLibraryItems = allItems;
  44. var masterDictionary = new Dictionary<string, Dictionary<Guid, Dictionary<string, int>>>(StringComparer.OrdinalIgnoreCase);
  45. // Populate counts of items
  46. SetItemCounts(null, allLibraryItems, masterDictionary);
  47. progress.Report(2);
  48. var numComplete = 0;
  49. foreach (var lib in userLibraries)
  50. {
  51. cancellationToken.ThrowIfCancellationRequested();
  52. SetItemCounts(lib.Item1, lib.Item2, masterDictionary);
  53. numComplete++;
  54. double percent = numComplete;
  55. percent /= userLibraries.Count;
  56. percent *= 8;
  57. progress.Report(percent);
  58. }
  59. progress.Report(10);
  60. var names = masterDictionary.Keys.ToList();
  61. numComplete = 0;
  62. foreach (var name in names)
  63. {
  64. cancellationToken.ThrowIfCancellationRequested();
  65. try
  66. {
  67. await UpdateItemByNameCounts(name, masterDictionary[name]).ConfigureAwait(false);
  68. }
  69. catch (Exception ex)
  70. {
  71. _logger.ErrorException("Error updating counts for {0}", ex, name);
  72. }
  73. numComplete++;
  74. double percent = numComplete;
  75. percent /= names.Count;
  76. percent *= 90;
  77. progress.Report(percent + 10);
  78. }
  79. progress.Report(100);
  80. }
  81. private async Task UpdateItemByNameCounts(string name, Dictionary<Guid, Dictionary<string, int>> counts)
  82. {
  83. var itemByName = await _libraryManager.GetPerson(name).ConfigureAwait(false);
  84. foreach (var libraryId in counts.Keys.ToList())
  85. {
  86. var itemCounts = CountHelpers.GetCounts(counts[libraryId]);
  87. if (libraryId == Guid.Empty)
  88. {
  89. itemByName.ItemCounts = itemCounts;
  90. }
  91. else
  92. {
  93. itemByName.UserItemCounts[libraryId] = itemCounts;
  94. }
  95. }
  96. }
  97. private void SetItemCounts(Guid? userId, IEnumerable<BaseItem> allItems, Dictionary<string, Dictionary<Guid, Dictionary<string, 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. }