| | | 1 | | using Anichron.Core.Data; |
| | | 2 | | using Anichron.Core.Domain; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | using System.Linq.Expressions; |
| | | 5 | | |
| | | 6 | | namespace Anichron.Core.Work; |
| | | 7 | | |
| | | 8 | | public sealed record WorkItemView( |
| | | 9 | | Guid Id, |
| | | 10 | | WorkKind Kind, |
| | | 11 | | Guid? StorageConfigId, |
| | | 12 | | Guid? RequestedBy, |
| | | 13 | | Instant RequestedAt, |
| | | 14 | | WorkItemStatus Status, |
| | | 15 | | int Attempt, |
| | | 16 | | int Stalls, |
| | | 17 | | string? ClaimedBy, |
| | | 18 | | Instant? StartedAt, |
| | | 19 | | Instant? FinishedAt, |
| | | 20 | | Instant UpdatedAt, |
| | | 21 | | WorkProgress Progress, |
| | | 22 | | string? Result, |
| | | 23 | | string? Error, |
| | | 24 | | Instant? CancelRequestedAt); |
| | | 25 | | |
| | | 26 | | public sealed record WorkItemQuery(WorkItemStatus? Status, WorkKind? Kind, int Skip, int Take); |
| | | 27 | | |
| | | 28 | | public sealed record WorkItemPage(IReadOnlyList<WorkItemView> Items, int Total); |
| | | 29 | | |
| | | 30 | | public interface IWorkItemReader |
| | | 31 | | { |
| | | 32 | | /// <summary>A page of items, newest request first.</summary> |
| | | 33 | | /// <param name="query">The optional status and kind filters, and the page.</param> |
| | | 34 | | /// <param name="ct">Cancels the read.</param> |
| | | 35 | | /// <returns>The page, and the number of items matching the filters.</returns> |
| | | 36 | | Task<WorkItemPage> ListAsync(WorkItemQuery query, CancellationToken ct); |
| | | 37 | | |
| | | 38 | | Task<WorkItemView?> FindAsync(Guid id, CancellationToken ct); |
| | | 39 | | |
| | | 40 | | /// <summary>Every item that changed after a point in time, oldest change first.</summary> |
| | | 41 | | /// <param name="since">Only items whose <see cref="WorkItem.UpdatedAt"/> is later are returned.</param> |
| | | 42 | | /// <param name="ct">Cancels the read.</param> |
| | | 43 | | /// <returns>The changed items, ordered by <see cref="WorkItem.UpdatedAt"/> and then id.</returns> |
| | | 44 | | Task<IReadOnlyList<WorkItemView>> UpdatedSinceAsync(Instant since, CancellationToken ct); |
| | | 45 | | |
| | | 46 | | /// <summary>The newest <see cref="WorkItem.UpdatedAt"/> of any item.</summary> |
| | | 47 | | /// <param name="ct">Cancels the read.</param> |
| | | 48 | | /// <returns>The newest change time, or null when there are no items.</returns> |
| | | 49 | | Task<Instant?> LatestUpdateAsync(CancellationToken ct); |
| | | 50 | | } |
| | | 51 | | |
| | 13 | 52 | | public sealed class EfWorkItemReader(AnichronDbContext db) : IWorkItemReader |
| | | 53 | | { |
| | | 54 | | public async Task<WorkItemPage> ListAsync(WorkItemQuery query, CancellationToken ct) |
| | 7 | 55 | | { |
| | 7 | 56 | | var matching = db.WorkItems.AsQueryable(); |
| | 7 | 57 | | if (query.Status is { } status) |
| | 2 | 58 | | matching = matching.Where(w => w.Status == status); |
| | 7 | 59 | | if (query.Kind is { } kind) |
| | 2 | 60 | | matching = matching.Where(w => w.Kind == kind); |
| | | 61 | | |
| | 7 | 62 | | var items = await matching |
| | 7 | 63 | | .OrderByDescending(w => w.RequestedAt) |
| | 7 | 64 | | .ThenByDescending(w => w.Id) |
| | 7 | 65 | | .Skip(query.Skip) |
| | 7 | 66 | | .Take(query.Take) |
| | 7 | 67 | | .Select(ToView) |
| | 7 | 68 | | .ToListAsync(ct); |
| | 7 | 69 | | return new WorkItemPage(items, await matching.CountAsync(ct)); |
| | 7 | 70 | | } |
| | | 71 | | |
| | | 72 | | public Task<WorkItemView?> FindAsync(Guid id, CancellationToken ct) |
| | 2 | 73 | | => db.WorkItems.Where(w => w.Id == id).Select(ToView).FirstOrDefaultAsync(ct); |
| | | 74 | | |
| | | 75 | | public async Task<IReadOnlyList<WorkItemView>> UpdatedSinceAsync(Instant since, CancellationToken ct) |
| | 2 | 76 | | => await UpdatedSinceQuery(db, since).ToListAsync(ct); |
| | | 77 | | |
| | | 78 | | public Task<Instant?> LatestUpdateAsync(CancellationToken ct) |
| | 2 | 79 | | => db.WorkItems.MaxAsync(w => (Instant?)w.UpdatedAt, ct); |
| | | 80 | | |
| | | 81 | | internal static IQueryable<WorkItemView> UpdatedSinceQuery(AnichronDbContext db, Instant since) |
| | 3 | 82 | | => db.WorkItems |
| | 3 | 83 | | .Where(w => w.UpdatedAt > since) |
| | 3 | 84 | | .OrderBy(w => w.UpdatedAt) |
| | 3 | 85 | | .ThenBy(w => w.Id) |
| | 3 | 86 | | .Select(ToView); |
| | | 87 | | |
| | | 88 | | private static Expression<Func<WorkItem, WorkItemView>> ToView |
| | 12 | 89 | | => w => new WorkItemView( |
| | 12 | 90 | | w.Id, w.Kind, w.StorageConfigId, w.RequestedBy, w.RequestedAt, w.Status, w.Attempt, w.Stalls, |
| | 12 | 91 | | w.ClaimedBy, w.StartedAt, w.FinishedAt, w.UpdatedAt, |
| | 12 | 92 | | new WorkProgress(w.ProgressDone, w.ProgressTotal, w.ProgressNote), w.Result, w.Error, |
| | 12 | 93 | | w.CancelRequestedAt); |
| | | 94 | | } |