| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | |
| | | 3 | | namespace 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> |
| | | 10 | | internal 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> |
| | | 13 | | internal 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) |
| | 54 | 31 | | { |
| | | 32 | | IEnumerable<MediaAsset> GroupOf(MediaAsset asset) |
| | | 33 | | => bursts.FirstOrDefault(b => b.Id == asset.BurstId)?.Assets ?? [asset]; |
| | | 34 | | |
| | 54 | 35 | | HashSet<MediaAsset> members = [photo, .. labelled.SelectMany(GroupOf)]; |
| | 240 | 36 | | foreach (var neighbour in similar) |
| | 39 | 37 | | { |
| | 39 | 38 | | HashSet<MediaAsset> grown = [.. members, .. GroupOf(neighbour)]; |
| | 39 | 39 | | if (WithinSpan(grown, spanSeconds)) |
| | 30 | 40 | | members = grown; |
| | 39 | 41 | | } |
| | | 42 | | |
| | 54 | 43 | | if (members.Count == 1) |
| | 5 | 44 | | return null; |
| | | 45 | | |
| | 49 | 46 | | var included = bursts |
| | 49 | 47 | | .Where(b => members.Any(m => m.BurstId == b.Id)) |
| | 49 | 48 | | .OrderBy(b => b.CreatedAt).ThenBy(b => b.Id) |
| | 49 | 49 | | .ToList(); |
| | 49 | 50 | | return new BurstJoin(members, included.FirstOrDefault(), [.. included.Skip(1)], Cover(members)); |
| | 54 | 51 | | } |
| | | 52 | | |
| | | 53 | | public static MediaAsset Cover(IEnumerable<MediaAsset> members) |
| | 66 | 54 | | => members.Where(a => !a.IsSoftDeleted).MinBy(a => (a.DateCaptured!.Value, a.Id))!; |
| | | 55 | | |
| | | 56 | | private static bool WithinSpan(IEnumerable<MediaAsset> members, int spanSeconds) |
| | 39 | 57 | | { |
| | 39 | 58 | | var captured = members.Where(a => !a.IsSoftDeleted).Select(a => a.DateCaptured!.Value).ToList(); |
| | 39 | 59 | | return captured.Max() <= captured.Min().PlusSeconds(spanSeconds); |
| | 39 | 60 | | } |
| | | 61 | | } |