< Summary

Information
Class: Anichron.Worker.Bursts.BurstGrouping
Assembly: Anichron.Worker
File(s): /home/runner/work/anichron/anichron/src/Anichron.Worker/Bursts/BurstGrouping.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 61
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Join(...)100%1212100%
Cover(...)100%11100%
WithinSpan(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Worker/Bursts/BurstGrouping.cs

#LineLine coverage
 1using Anichron.Core.Domain;
 2
 3namespace Anichron.Worker.Bursts;
 4
 5/// <summary>What a photo's join changes.</summary>
 6/// <param name="Members">Every row that ends up in the burst, the photo included.</param>
 7/// <param name="Target">The burst the members join; null when a new one is created.</param>
 8/// <param name="Merged">The other bursts, whose members move into the target.</param>
 9/// <param name="Cover">The earliest active member.</param>
 10internal sealed record BurstJoin(IReadOnlySet<MediaAsset> Members, Burst? Target, IReadOnlyList<Burst> Merged, MediaAsse
 11
 12/// <summary>Burst detection's grouping rule, on rows tracked by one context, so a row is one instance.</summary>
 13internal static class BurstGrouping
 14{
 15    /// <summary>
 16    /// Joins the photo with every labelled neighbour, then with each similar neighbour in order unless
 17    /// its burst would make the active members span more than <paramref name="spanSeconds"/>.
 18    /// </summary>
 19    /// <param name="photo">The photo being checked.</param>
 20    /// <param name="labelled">The candidates the camera gave the photo's burst label.</param>
 21    /// <param name="similar">The candidates whose thumbnails matched, oldest first.</param>
 22    /// <param name="bursts">The neighbours' bursts, with every member.</param>
 23    /// <param name="spanSeconds">The longest time, in seconds, the active members may span.</param>
 24    /// <returns>Null when the photo joins nothing.</returns>
 25    public static BurstJoin? Join(
 26        MediaAsset photo,
 27        IReadOnlyCollection<MediaAsset> labelled,
 28        IReadOnlyList<MediaAsset> similar,
 29        IReadOnlyCollection<Burst> bursts,
 30        int spanSeconds)
 5431    {
 32        IEnumerable<MediaAsset> GroupOf(MediaAsset asset)
 33            => bursts.FirstOrDefault(b => b.Id == asset.BurstId)?.Assets ?? [asset];
 34
 5435        HashSet<MediaAsset> members = [photo, .. labelled.SelectMany(GroupOf)];
 24036        foreach (var neighbour in similar)
 3937        {
 3938            HashSet<MediaAsset> grown = [.. members, .. GroupOf(neighbour)];
 3939            if (WithinSpan(grown, spanSeconds))
 3040                members = grown;
 3941        }
 42
 5443        if (members.Count == 1)
 544            return null;
 45
 4946        var included = bursts
 4947            .Where(b => members.Any(m => m.BurstId == b.Id))
 4948            .OrderBy(b => b.CreatedAt).ThenBy(b => b.Id)
 4949            .ToList();
 4950        return new BurstJoin(members, included.FirstOrDefault(), [.. included.Skip(1)], Cover(members));
 5451    }
 52
 53    public static MediaAsset Cover(IEnumerable<MediaAsset> members)
 6654        => members.Where(a => !a.IsSoftDeleted).MinBy(a => (a.DateCaptured!.Value, a.Id))!;
 55
 56    private static bool WithinSpan(IEnumerable<MediaAsset> members, int spanSeconds)
 3957    {
 3958        var captured = members.Where(a => !a.IsSoftDeleted).Select(a => a.DateCaptured!.Value).ToList();
 3959        return captured.Max() <= captured.Min().PlusSeconds(spanSeconds);
 3960    }
 61}