< Summary

Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 424
Line coverage: 100%
Branch coverage
100%
Covered branches: 16
Total branches: 16
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .cctor()100%11100%
File 2: .ctor(...)100%11100%
File 2: SweepAsync()100%1616100%

File(s)

/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs

File '/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs' does not exist (any more).

/home/runner/work/anichron/anichron/src/Anichron.Worker/Crawling/MissingFileSweep.cs

#LineLine coverage
 1using Anichron.Core.Data.Repository;
 2using Anichron.Core.Domain;
 3using System.Text.Json.Serialization;
 4
 5namespace Anichron.Worker.Crawling;
 6
 7/// <summary>Why a crawl soft-deleted nothing, though rows may be missing.</summary>
 8[JsonConverter(typeof(JsonStringEnumConverter<MissingFileSkip>))]
 9public enum MissingFileSkip
 10{
 11    /// <summary>The walk skipped a folder it could not read.</summary>
 12    IncompleteWalk = 0,
 13
 14    /// <summary>The walk found no media files while the config has active rows, as when a share is not mounted.</summar
 15    EmptyRoot = 1,
 16}
 17
 18/// <summary>What a crawl's missing-file sweep did.</summary>
 19/// <param name="SoftDeleted">Rows the sweep soft-deleted.</param>
 20/// <param name="SkippedBecause">Why the sweep soft-deleted nothing, or null when it ran.</param>
 21internal sealed record MissingFiles(int SoftDeleted, MissingFileSkip? SkippedBecause);
 22
 23internal interface IMissingFileSweep
 24{
 25    Task<MissingFiles> SweepAsync(UserStorageConfig config, CrawlRun run, CancellationToken ct);
 26}
 27
 1528internal sealed partial class MissingFileSweep(
 1529    IMediaAssetRepository repository,
 1530    ILogger<MissingFileSweep> logger) : IMissingFileSweep
 31{
 32    public async Task<MissingFiles> SweepAsync(UserStorageConfig config, CrawlRun run, CancellationToken ct)
 1433    {
 1434        if (!run.WalkComplete)
 135            return new MissingFiles(0, MissingFileSkip.IncompleteWalk);
 36
 1337        var active = await repository.GetActivePathsAsync(config.Id, ct);
 1338        if (run.FoundPaths.Count == 0 && active.Count > 0)
 139            return new MissingFiles(0, MissingFileSkip.EmptyRoot);
 40
 1241        var missing = active.Where(row => !run.FoundPaths.Contains(row.FilePath)).ToList();
 1242        var missingIds = missing.Select(row => row.Id).ToHashSet();
 1243        var videosOfMissingPhotos = missing.Select(row => row.PairedAssetId).OfType<Guid>().ToHashSet();
 1244        var videosLeftBehind = active
 1245            .Where(row => !missingIds.Contains(row.Id) && videosOfMissingPhotos.Contains(row.Id))
 1246            .ToList();
 1247        if (missing.Count + videosLeftBehind.Count == 0)
 348            return new MissingFiles(0, null);
 49
 5350        foreach (var row in missing)
 1351            Log.SoftDeleting(logger, row.Id, row.FilePath);
 2952        foreach (var row in videosLeftBehind)
 153            Log.SoftDeletingLeftBehindVideo(logger, row.Id, row.FilePath);
 2054        foreach (var (photo, video) in
 955            from photo in active
 956            where !missingIds.Contains(photo.Id)
 957            join video in missing on photo.PairedAssetId equals video.Id
 958            select (photo, video))
 159        {
 160            Log.NowAStillPhoto(logger, photo.Id, video.Id, video.FilePath);
 161        }
 62
 963        return new MissingFiles(
 964            await repository.SoftDeleteAsync([.. missing.Concat(videosLeftBehind).Select(row => row.Id)], ct), null);
 1465    }
 66
 67    private static partial class Log
 68    {
 69        [LoggerMessage(Level = LogLevel.Debug, Message = "Soft-deleting asset {AssetId} at {FilePath}: a complete crawl 
 70        public static partial void SoftDeleting(ILogger logger, Guid assetId, string filePath);
 71
 72        [LoggerMessage(
 73            Level = LogLevel.Debug,
 74            Message = "Soft-deleting asset {AssetId} at {FilePath}: it is the video of a Live Photo whose photo is missi
 75        public static partial void SoftDeletingLeftBehindVideo(ILogger logger, Guid assetId, string filePath);
 76
 77        [LoggerMessage(
 78            Level = LogLevel.Information,
 79            Message = "Asset {PhotoId} is now a still photo: its video, asset {VideoId} at {FilePath}, is missing.")]
 80        public static partial void NowAStillPhoto(ILogger logger, Guid photoId, Guid videoId, string filePath);
 81    }
 82}