< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.Validators.PeopleValidator
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/Validators/PeopleValidator.cs
Line coverage
18%
Covered lines: 11
Uncovered lines: 50
Coverable lines: 61
Total lines: 159
Line coverage: 18%
Branch coverage
42%
Covered branches: 6
Total branches: 14
Branch coverage: 42.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/31/2026 - 12:15:44 AM Line coverage: 0% (0/46) Branch coverage: 0% (0/6) Total lines: 12010/29/2026 - 12:17:11 AM Line coverage: 18% (11/61) Branch coverage: 42.8% (6/14) Total lines: 159 5/31/2026 - 12:15:44 AM Line coverage: 0% (0/46) Branch coverage: 0% (0/6) Total lines: 12010/29/2026 - 12:17:11 AM Line coverage: 18% (11/61) Branch coverage: 42.8% (6/14) Total lines: 159

Coverage delta

Coverage delta 43 -43

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
Run()0%7280%
PartitionCreditsByPersonId(...)100%66100%

File(s)

/srv/git/jellyfin/Emby.Server.Implementations/Library/Validators/PeopleValidator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Globalization;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Jellyfin.Data.Enums;
 8using MediaBrowser.Controller.Entities;
 9using MediaBrowser.Controller.Library;
 10using Microsoft.Extensions.Logging;
 11
 12namespace Emby.Server.Implementations.Library.Validators;
 13
 14/// <summary>
 15/// Class PeopleValidator.
 16/// </summary>
 17public class PeopleValidator
 18{
 19    /// <summary>
 20    /// The library manager.
 21    /// </summary>
 22    private readonly ILibraryManager _libraryManager;
 23
 24    /// <summary>
 25    /// The logger.
 26    /// </summary>
 27    private readonly ILogger<PeopleValidator> _logger;
 28
 29    /// <summary>
 30    /// Initializes a new instance of the <see cref="PeopleValidator" /> class.
 31    /// </summary>
 32    /// <param name="libraryManager">The library manager.</param>
 33    /// <param name="logger">The logger.</param>
 34    public PeopleValidator(ILibraryManager libraryManager, ILogger<PeopleValidator> logger)
 35    {
 036        _libraryManager = libraryManager;
 037        _logger = logger;
 038    }
 39
 40    /// <summary>
 41    /// Validates the people.
 42    /// </summary>
 43    /// <param name="progress">The progress.</param>
 44    /// <param name="cancellationToken">The cancellation token.</param>
 45    /// <returns>Task.</returns>
 46    public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
 47    {
 48        // Before the refresh below walks them: a credit no item maps to any more stands for nothing,
 49        // and while it is there the person it names cannot reach the dead-person sweep either.
 050        var numOrphaned = _libraryManager.DeleteOrphanedCredits();
 051        if (numOrphaned > 0)
 52        {
 053            _logger.LogInformation("Deleted {Amount} credits no item maps to", numOrphaned);
 54        }
 55
 056        var names = _libraryManager.GetPeopleNames(new InternalPeopleQuery());
 057        var existingPersonIds = _libraryManager.GetItemIds(new InternalItemsQuery
 058        {
 059            IncludeItemTypes = [BaseItemKind.Person]
 060        }).ToHashSet();
 61
 062        var (newNames, deadIds) = PartitionCreditsByPersonId(names, _libraryManager.GetPersonId, existingPersonIds);
 63
 064        var numComplete = 0;
 065        var count = names.Count;
 066        var refreshed = 0;
 67
 068        foreach (var name in names)
 69        {
 070            cancellationToken.ThrowIfCancellationRequested();
 71
 72            try
 73            {
 074                var item = _libraryManager.GetOrCreatePerson(name);
 075                var isNew = !existingPersonIds.Contains(item.Id);
 076                var neverRefreshed = item.DateLastRefreshed == default;
 77
 078                if (isNew || neverRefreshed)
 79                {
 080                    await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
 081                    refreshed++;
 82                }
 083            }
 084            catch (OperationCanceledException)
 85            {
 86                // Don't clutter the log
 087                throw;
 88            }
 089            catch (Exception ex)
 90            {
 091                _logger.LogError(ex, "Error refreshing {PersonName}", name);
 092            }
 93
 094            numComplete++;
 095            double percent = numComplete;
 096            percent /= count;
 097            percent *= 100;
 98
 099            progress.Report(percent);
 0100        }
 101
 0102        _logger.LogInformation(
 0103            "Refreshed metadata for {RefreshedCount} people out of {TotalCount} total, {NewCount} of which had no item y
 0104            refreshed,
 0105            count,
 0106            newNames.Count);
 107
 108        // A person somebody locked is theirs, not ours, however little the library still credits them.
 0109        var deadEntities = deadIds
 0110            .Select(_libraryManager.GetItemById)
 0111            .OfType<Person>()
 0112            .Where(item => !item.IsLocked)
 0113            .ToList();
 114
 0115        foreach (var item in deadEntities)
 116        {
 0117            _logger.LogInformation("Deleting dead {ItemType} {ItemId} {ItemName}", item.GetType().Name, item.Id.ToString
 118        }
 119
 0120        _libraryManager.DeleteItemsUnsafeFast(deadEntities, deleteSourceFiles: true);
 121
 0122        progress.Report(100);
 0123    }
 124
 125    /// <summary>
 126    /// Splits the person items into the ones a credit still calls for and the ones nothing does.
 127    /// </summary>
 128    /// <param name="creditNames">Every name credited on an item, from the people table.</param>
 129    /// <param name="getPersonId">Maps a credit name to the id its person item has.</param>
 130    /// <param name="existingPersonIds">The ids of the person items that exist.</param>
 131    /// <returns>The credits needing an item, and the ids of the items nothing credits.</returns>
 132    internal static (List<string> NewNames, List<Guid> DeadIds) PartitionCreditsByPersonId(
 133        IReadOnlyList<string> creditNames,
 134        Func<string, Guid> getPersonId,
 135        IReadOnlySet<Guid> existingPersonIds)
 136    {
 11137        ArgumentNullException.ThrowIfNull(creditNames);
 11138        ArgumentNullException.ThrowIfNull(getPersonId);
 11139        ArgumentNullException.ThrowIfNull(existingPersonIds);
 140
 11141        var newNames = new List<string>();
 11142        var liveIds = new HashSet<Guid>();
 143
 56144        foreach (var name in creditNames)
 145        {
 17146            var personId = getPersonId(name);
 147
 148            // Distinct credit names can normalize onto one id; only the first of them needs an item.
 17149            if (liveIds.Add(personId) && !existingPersonIds.Contains(personId))
 150            {
 5151                newNames.Add(name);
 152            }
 153        }
 154
 11155        var deadIds = existingPersonIds.Where(id => !liveIds.Contains(id)).ToList();
 156
 11157        return (newNames, deadIds);
 158    }
 159}