< Summary - Jellyfin

Information
Class: Emby.Server.Implementations.Library.Validators.ArtistsValidator
Assembly: Emby.Server.Implementations
File(s): /srv/git/jellyfin/Emby.Server.Implementations/Library/Validators/ArtistsValidator.cs
Line coverage
100%
Covered lines: 4
Uncovered lines: 0
Coverable lines: 4
Total lines: 110
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.Linq;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Jellyfin.Data.Enums;
 7using MediaBrowser.Controller.Entities;
 8using MediaBrowser.Controller.Entities.Audio;
 9using MediaBrowser.Controller.Library;
 10using MediaBrowser.Controller.Persistence;
 11using Microsoft.Extensions.Logging;
 12
 13namespace Emby.Server.Implementations.Library.Validators;
 14
 15/// <summary>
 16/// Class ArtistsValidator.
 17/// </summary>
 18public class ArtistsValidator
 19{
 20    /// <summary>
 21    /// The library manager.
 22    /// </summary>
 23    private readonly ILibraryManager _libraryManager;
 24
 25    /// <summary>
 26    /// The logger.
 27    /// </summary>
 28    private readonly ILogger<ArtistsValidator> _logger;
 29    private readonly IItemRepository _itemRepo;
 30
 31    /// <summary>
 32    /// Initializes a new instance of the <see cref="ArtistsValidator" /> class.
 33    /// </summary>
 34    /// <param name="libraryManager">The library manager.</param>
 35    /// <param name="logger">The logger.</param>
 36    /// <param name="itemRepo">The item repository.</param>
 37    public ArtistsValidator(ILibraryManager libraryManager, ILogger<ArtistsValidator> logger, IItemRepository itemRepo)
 38    {
 1139        _libraryManager = libraryManager;
 1140        _logger = logger;
 1141        _itemRepo = itemRepo;
 1142    }
 43
 44    /// <summary>
 45    /// Runs the specified progress.
 46    /// </summary>
 47    /// <param name="progress">The progress.</param>
 48    /// <param name="cancellationToken">The cancellation token.</param>
 49    /// <returns>Task.</returns>
 50    public async Task Run(IProgress<double> progress, CancellationToken cancellationToken)
 51    {
 52        var names = _itemRepo.GetAllArtistNames();
 53
 54        var numComplete = 0;
 55        var count = names.Count;
 56
 57        foreach (var name in names)
 58        {
 59            try
 60            {
 61                var item = _libraryManager.GetArtist(name);
 62
 63                await item.RefreshMetadata(cancellationToken).ConfigureAwait(false);
 64            }
 65            catch (OperationCanceledException)
 66            {
 67                // Don't clutter the log
 68                throw;
 69            }
 70            catch (Exception ex)
 71            {
 72                _logger.LogError(ex, "Error refreshing {ArtistName}", name);
 73            }
 74
 75            numComplete++;
 76            double percent = numComplete;
 77            percent /= count;
 78            percent *= 100;
 79
 80            progress.Report(percent);
 81        }
 82
 83        var deadEntities = _libraryManager.GetItemList(new InternalItemsQuery
 84        {
 85            IncludeItemTypes = [BaseItemKind.MusicArtist],
 86            IsDeadArtist = true,
 87            IsLocked = false
 88        }).Cast<MusicArtist>().ToList();
 89
 90        foreach (var item in deadEntities)
 91        {
 92            if (!item.IsAccessedByName)
 93            {
 94                continue;
 95            }
 96
 97            _logger.LogInformation("Deleting dead {ItemType} {ItemId} {ItemName}", item.GetType().Name, item.Id.ToString
 98
 99            _libraryManager.DeleteItem(
 100                item,
 101                new DeleteOptions
 102                {
 103                    DeleteFileLocation = false
 104                },
 105                false);
 106        }
 107
 108        progress.Report(100);
 109    }
 110}