| | | 1 | | using Anichron.Core.Domain; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Core.Data.Repository; |
| | | 5 | | |
| | | 6 | | /// <summary>Bursts and the burst-checked mark, written only by the Worker's burst detection.</summary> |
| | | 7 | | public 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 | | |
| | 76 | 72 | | public sealed class EfBurstRepository(AnichronDbContext db) : IBurstRepository |
| | | 73 | | { |
| | | 74 | | public async Task<IReadOnlyList<Burst>> GetInConfigWithMembersAsync(Guid storageConfigId, CancellationToken ct) |
| | 59 | 75 | | => await WithMembers() |
| | 59 | 76 | | .Where(b => b.Assets.Any(a => a.StorageConfigId == storageConfigId)) |
| | 59 | 77 | | .ToListAsync(ct); |
| | | 78 | | |
| | | 79 | | public async Task<IReadOnlyList<Burst>> GetWithMembersAsync(IReadOnlyCollection<Guid> burstIds, CancellationToken ct |
| | 42 | 80 | | => await WithMembers() |
| | 42 | 81 | | .Where(b => burstIds.Contains(b.Id)) |
| | 42 | 82 | | .ToListAsync(ct); |
| | | 83 | | |
| | | 84 | | // Stryker disable all |
| | | 85 | | public Task<int> DeleteMemberlessAsync(CancellationToken ct) |
| | 0 | 86 | | => db.Bursts.IgnoreQueryFilters() |
| | 0 | 87 | | .Where(b => !db.MediaAssets.IgnoreQueryFilters().Any(a => a.BurstId == b.Id)) |
| | 0 | 88 | | .ExecuteDeleteAsync(ct); |
| | | 89 | | |
| | | 90 | | public Task<int> CountUncheckedPhotosAsync(Guid storageConfigId, CancellationToken ct) |
| | 59 | 91 | | => UncheckedPhotos(storageConfigId).CountAsync(ct); |
| | | 92 | | |
| | | 93 | | public async Task<IReadOnlyList<MediaAsset>> GetUncheckedPhotosAsync( |
| | | 94 | | Guid storageConfigId, MediaAsset? after, int count, CancellationToken ct) |
| | 117 | 95 | | => await UncheckedPhotosAfter(storageConfigId, after) |
| | 117 | 96 | | .Include(a => a.Metadata) |
| | 117 | 97 | | .Include(a => a.ProxyFiles) |
| | 117 | 98 | | .OrderBy(a => a.DateCaptured).ThenBy(a => a.Id) |
| | 117 | 99 | | .Take(count) |
| | 117 | 100 | | .ToListAsync(ct); |
| | | 101 | | |
| | | 102 | | public async Task<IReadOnlyList<MediaAsset>> GetCandidatesLabelledAsync( |
| | | 103 | | Guid storageConfigId, string cameraBurstId, CancellationToken ct) |
| | 41 | 104 | | => await CandidatesLabelled(storageConfigId, cameraBurstId).ToListAsync(ct); |
| | | 105 | | |
| | | 106 | | internal IQueryable<MediaAsset> CandidatesLabelled(Guid storageConfigId, string cameraBurstId) |
| | 42 | 107 | | => db.BurstChecks |
| | 42 | 108 | | .IgnoreQueryFilters() |
| | 42 | 109 | | .Where(c => c.IsCandidate && c.CameraBurstId == cameraBurstId |
| | 42 | 110 | | && c.Asset.StorageConfigId == storageConfigId && !c.Asset.IsSoftDeleted) |
| | 42 | 111 | | .Select(c => c.Asset); |
| | | 112 | | |
| | | 113 | | public async Task<IReadOnlyList<MediaAsset>> GetCandidatesNearAsync( |
| | | 114 | | Guid storageConfigId, string cameraMake, string cameraModel, LocalDateTime from, LocalDateTime to, CancellationT |
| | 298 | 115 | | => 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) |
| | 299 | 119 | | => db.MediaAssets |
| | 299 | 120 | | .IgnoreQueryFilters() |
| | 299 | 121 | | .Include(a => a.ProxyFiles) |
| | 299 | 122 | | .Where(a => a.StorageConfigId == storageConfigId && !a.IsSoftDeleted |
| | 299 | 123 | | && a.DateCaptured >= from && a.DateCaptured <= to |
| | 299 | 124 | | && a.Metadata!.Description.CameraMake == cameraMake && a.Metadata.Description.CameraModel == cameraModel |
| | 299 | 125 | | && a.ProxyFiles.Any(p => p.ProxyType == ProxyType.Thumbnail) |
| | 299 | 126 | | && db.BurstChecks.Any(c => c.AssetId == a.Id && c.IsCandidate)) |
| | 299 | 127 | | .OrderBy(a => a.DateCaptured).ThenBy(a => a.Id); |
| | | 128 | | |
| | | 129 | | public void Add(Burst burst) |
| | 25 | 130 | | => db.Bursts.Add(burst); |
| | | 131 | | |
| | | 132 | | public void Remove(Burst burst) |
| | 6 | 133 | | => db.Bursts.Remove(burst); |
| | | 134 | | |
| | | 135 | | public void AddCheck(BurstCheck check) |
| | 300 | 136 | | => db.BurstChecks.Add(check); |
| | | 137 | | |
| | | 138 | | public async Task RemoveCheckAsync(Guid assetId, CancellationToken ct) |
| | 3 | 139 | | { |
| | 3 | 140 | | if (await db.BurstChecks.IgnoreQueryFilters().FirstOrDefaultAsync(c => c.AssetId == assetId, ct) is { } check) |
| | 2 | 141 | | db.BurstChecks.Remove(check); |
| | 3 | 142 | | } |
| | | 143 | | |
| | | 144 | | public void ForgetLoaded() |
| | 55 | 145 | | => db.ChangeTracker.Clear(); |
| | | 146 | | |
| | | 147 | | private IQueryable<Burst> WithMembers() |
| | 101 | 148 | | => db.Bursts.IgnoreQueryFilters().Include(b => b.Assets); |
| | | 149 | | |
| | | 150 | | internal IQueryable<MediaAsset> UncheckedPhotosAfter(Guid storageConfigId, MediaAsset? after) |
| | 118 | 151 | | { |
| | 118 | 152 | | if (after is null) |
| | 61 | 153 | | return UncheckedPhotos(storageConfigId); |
| | | 154 | | |
| | 57 | 155 | | var (capturedAt, id) = (after.DateCaptured!.Value, after.Id); |
| | 57 | 156 | | return UncheckedPhotos(storageConfigId) |
| | 57 | 157 | | .Where(a => a.DateCaptured > capturedAt || (a.DateCaptured == capturedAt && a.Id.CompareTo(id) > 0)); |
| | 118 | 158 | | } |
| | | 159 | | |
| | | 160 | | internal IQueryable<MediaAsset> UncheckedPhotos(Guid storageConfigId) |
| | 179 | 161 | | => db.MediaAssets.IgnoreQueryFilters().Where(a => a.StorageConfigId == storageConfigId && !a.IsSoftDeleted |
| | 179 | 162 | | && (a.MediaType == MediaType.Image || a.MediaType == MediaType.LivePhoto) |
| | 179 | 163 | | && EF.Constant(CaptureSources.Timed).Contains(a.CaptureSource) |
| | 179 | 164 | | && !db.BurstChecks.Any(c => c.AssetId == a.Id)); |
| | | 165 | | } |