< Summary

Information
Class: Anichron.Core.Data.Repository.EfBurstRepository
Assembly: Anichron.Core
File(s): /home/runner/work/anichron/anichron/src/Anichron.Core/Data/Repository/BurstRepository.cs
Tag: 19_45211875487
Line coverage
94%
Covered lines: 50
Uncovered lines: 3
Coverable lines: 53
Total lines: 165
Line coverage: 94.3%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetInConfigWithMembersAsync()100%11100%
GetWithMembersAsync()100%11100%
DeleteMemberlessAsync(...)100%210%
CountUncheckedPhotosAsync(...)100%11100%
GetUncheckedPhotosAsync()100%11100%
GetCandidatesLabelledAsync()100%11100%
CandidatesLabelled(...)100%11100%
GetCandidatesNearAsync()100%11100%
CandidatesNear(...)100%11100%
Add(...)100%11100%
Remove(...)100%11100%
AddCheck(...)100%11100%
RemoveCheckAsync()100%22100%
ForgetLoaded()100%11100%
WithMembers()100%11100%
UncheckedPhotosAfter(...)100%22100%
UncheckedPhotos(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Core/Data/Repository/BurstRepository.cs

#LineLine coverage
 1using Anichron.Core.Domain;
 2using Microsoft.EntityFrameworkCore;
 3
 4namespace Anichron.Core.Data.Repository;
 5
 6/// <summary>Bursts and the burst-checked mark, written only by the Worker's burst detection.</summary>
 7public interface IBurstRepository
 8{
 9    /// <summary>The config's bursts, each with every member, soft-deleted ones included.</summary>
 10    /// <param name="storageConfigId">The config whose bursts are loaded.</param>
 11    /// <param name="ct">Cancels the query.</param>
 12    Task<IReadOnlyList<Burst>> GetInConfigWithMembersAsync(Guid storageConfigId, CancellationToken ct);
 13
 14    /// <summary>The bursts with every member.</summary>
 15    /// <param name="burstIds">The bursts to load.</param>
 16    /// <param name="ct">Cancels the query.</param>
 17    Task<IReadOnlyList<Burst>> GetWithMembersAsync(IReadOnlyCollection<Guid> burstIds, CancellationToken ct);
 18
 19    /// <summary>
 20    /// Deletes every burst without a member row, in any config (left behind when a storage config
 21    /// was deleted), in one statement, so two Workers repairing at once cannot conflict.
 22    /// </summary>
 23    /// <param name="ct">Cancels the statement.</param>
 24    /// <returns>How many bursts were deleted.</returns>
 25    Task<int> DeleteMemberlessAsync(CancellationToken ct);
 26
 27    Task<int> CountUncheckedPhotosAsync(Guid storageConfigId, CancellationToken ct);
 28
 29    /// <summary>
 30    /// The config's oldest active dated photos without a burst-checked mark, with their metadata and
 31    /// proxy rows, by capture time and then id.
 32    /// </summary>
 33    /// <param name="storageConfigId">The config whose photos are loaded.</param>
 34    /// <param name="after">Only photos that come after this one in that order; null starts at the oldest.</param>
 35    /// <param name="count">At most this many.</param>
 36    /// <param name="ct">Cancels the query.</param>
 37    Task<IReadOnlyList<MediaAsset>> GetUncheckedPhotosAsync(
 38        Guid storageConfigId, MediaAsset? after, int count, CancellationToken ct);
 39
 40    /// <summary>The config's checked candidates the camera labelled with this burst id.</summary>
 41    /// <param name="storageConfigId">The config searched.</param>
 42    /// <param name="cameraBurstId">Apple's BurstUUID.</param>
 43    /// <param name="ct">Cancels the query.</param>
 44    Task<IReadOnlyList<MediaAsset>> GetCandidatesLabelledAsync(Guid storageConfigId, string cameraBurstId, CancellationT
 45
 46    /// <summary>
 47    /// The config's checked candidates from this camera captured within the window that still have
 48    /// a thumbnail, each with its proxy rows, oldest first (then by id).
 49    /// </summary>
 50    /// <param name="storageConfigId">The config searched.</param>
 51    /// <param name="cameraMake">The camera make the candidates share.</param>
 52    /// <param name="cameraModel">The camera model the candidates share.</param>
 53    /// <param name="from">The earliest capture time, inclusive.</param>
 54    /// <param name="to">The latest capture time, inclusive.</param>
 55    /// <param name="ct">Cancels the query.</param>
 56    Task<IReadOnlyList<MediaAsset>> GetCandidatesNearAsync(
 57        Guid storageConfigId, string cameraMake, string cameraModel, LocalDateTime from, LocalDateTime to, CancellationT
 58
 59    void Add(Burst burst);
 60    void Remove(Burst burst);
 61    void AddCheck(BurstCheck check);
 62
 63    /// <summary>Clears the asset's burst-checked mark, if it has one, at the next save.</summary>
 64    /// <param name="assetId">The asset whose mark is cleared.</param>
 65    /// <param name="ct">Cancels the lookup.</param>
 66    Task RemoveCheckAsync(Guid assetId, CancellationToken ct);
 67
 68    /// <summary>Stops tracking everything loaded so far, so a long run does not hold it all.</summary>
 69    void ForgetLoaded();
 70}
 71
 7672public sealed class EfBurstRepository(AnichronDbContext db) : IBurstRepository
 73{
 74    public async Task<IReadOnlyList<Burst>> GetInConfigWithMembersAsync(Guid storageConfigId, CancellationToken ct)
 5975        => await WithMembers()
 5976            .Where(b => b.Assets.Any(a => a.StorageConfigId == storageConfigId))
 5977            .ToListAsync(ct);
 78
 79    public async Task<IReadOnlyList<Burst>> GetWithMembersAsync(IReadOnlyCollection<Guid> burstIds, CancellationToken ct
 4280        => await WithMembers()
 4281            .Where(b => burstIds.Contains(b.Id))
 4282            .ToListAsync(ct);
 83
 84    // Stryker disable all
 85    public Task<int> DeleteMemberlessAsync(CancellationToken ct)
 086        => db.Bursts.IgnoreQueryFilters()
 087            .Where(b => !db.MediaAssets.IgnoreQueryFilters().Any(a => a.BurstId == b.Id))
 088            .ExecuteDeleteAsync(ct);
 89
 90    public Task<int> CountUncheckedPhotosAsync(Guid storageConfigId, CancellationToken ct)
 5991        => UncheckedPhotos(storageConfigId).CountAsync(ct);
 92
 93    public async Task<IReadOnlyList<MediaAsset>> GetUncheckedPhotosAsync(
 94        Guid storageConfigId, MediaAsset? after, int count, CancellationToken ct)
 11795        => await UncheckedPhotosAfter(storageConfigId, after)
 11796            .Include(a => a.Metadata)
 11797            .Include(a => a.ProxyFiles)
 11798            .OrderBy(a => a.DateCaptured).ThenBy(a => a.Id)
 11799            .Take(count)
 117100            .ToListAsync(ct);
 101
 102    public async Task<IReadOnlyList<MediaAsset>> GetCandidatesLabelledAsync(
 103        Guid storageConfigId, string cameraBurstId, CancellationToken ct)
 41104        => await CandidatesLabelled(storageConfigId, cameraBurstId).ToListAsync(ct);
 105
 106    internal IQueryable<MediaAsset> CandidatesLabelled(Guid storageConfigId, string cameraBurstId)
 42107        => db.BurstChecks
 42108            .IgnoreQueryFilters()
 42109            .Where(c => c.IsCandidate && c.CameraBurstId == cameraBurstId
 42110                && c.Asset.StorageConfigId == storageConfigId && !c.Asset.IsSoftDeleted)
 42111            .Select(c => c.Asset);
 112
 113    public async Task<IReadOnlyList<MediaAsset>> GetCandidatesNearAsync(
 114        Guid storageConfigId, string cameraMake, string cameraModel, LocalDateTime from, LocalDateTime to, CancellationT
 298115        => await CandidatesNear(storageConfigId, cameraMake, cameraModel, from, to).ToListAsync(ct);
 116
 117    internal IQueryable<MediaAsset> CandidatesNear(
 118        Guid storageConfigId, string cameraMake, string cameraModel, LocalDateTime from, LocalDateTime to)
 299119        => db.MediaAssets
 299120            .IgnoreQueryFilters()
 299121            .Include(a => a.ProxyFiles)
 299122            .Where(a => a.StorageConfigId == storageConfigId && !a.IsSoftDeleted
 299123                && a.DateCaptured >= from && a.DateCaptured <= to
 299124                && a.Metadata!.Description.CameraMake == cameraMake && a.Metadata.Description.CameraModel == cameraModel
 299125                && a.ProxyFiles.Any(p => p.ProxyType == ProxyType.Thumbnail)
 299126                && db.BurstChecks.Any(c => c.AssetId == a.Id && c.IsCandidate))
 299127            .OrderBy(a => a.DateCaptured).ThenBy(a => a.Id);
 128
 129    public void Add(Burst burst)
 25130        => db.Bursts.Add(burst);
 131
 132    public void Remove(Burst burst)
 6133        => db.Bursts.Remove(burst);
 134
 135    public void AddCheck(BurstCheck check)
 300136        => db.BurstChecks.Add(check);
 137
 138    public async Task RemoveCheckAsync(Guid assetId, CancellationToken ct)
 3139    {
 3140        if (await db.BurstChecks.IgnoreQueryFilters().FirstOrDefaultAsync(c => c.AssetId == assetId, ct) is { } check)
 2141            db.BurstChecks.Remove(check);
 3142    }
 143
 144    public void ForgetLoaded()
 55145        => db.ChangeTracker.Clear();
 146
 147    private IQueryable<Burst> WithMembers()
 101148        => db.Bursts.IgnoreQueryFilters().Include(b => b.Assets);
 149
 150    internal IQueryable<MediaAsset> UncheckedPhotosAfter(Guid storageConfigId, MediaAsset? after)
 118151    {
 118152        if (after is null)
 61153            return UncheckedPhotos(storageConfigId);
 154
 57155        var (capturedAt, id) = (after.DateCaptured!.Value, after.Id);
 57156        return UncheckedPhotos(storageConfigId)
 57157            .Where(a => a.DateCaptured > capturedAt || (a.DateCaptured == capturedAt && a.Id.CompareTo(id) > 0));
 118158    }
 159
 160    internal IQueryable<MediaAsset> UncheckedPhotos(Guid storageConfigId)
 179161        => db.MediaAssets.IgnoreQueryFilters().Where(a => a.StorageConfigId == storageConfigId && !a.IsSoftDeleted
 179162            && (a.MediaType == MediaType.Image || a.MediaType == MediaType.LivePhoto)
 179163            && EF.Constant(CaptureSources.Timed).Contains(a.CaptureSource)
 179164            && !db.BurstChecks.Any(c => c.AssetId == a.Id));
 165}