< Summary

Information
Class: Anichron.Core.Work.WorkItemView
Assembly: Anichron.Core
File(s): /home/runner/work/anichron/anichron/src/Anichron.Core/Work/WorkItemReader.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 17
Uncovered lines: 0
Coverable lines: 17
Total lines: 94
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Core/Work/WorkItemReader.cs

#LineLine coverage
 1using Anichron.Core.Data;
 2using Anichron.Core.Domain;
 3using Microsoft.EntityFrameworkCore;
 4using System.Linq.Expressions;
 5
 6namespace Anichron.Core.Work;
 7
 1108public sealed record WorkItemView(
 1109    Guid Id,
 11010    WorkKind Kind,
 11011    Guid? StorageConfigId,
 11012    Guid? RequestedBy,
 11013    Instant RequestedAt,
 11014    WorkItemStatus Status,
 11015    int Attempt,
 11016    int Stalls,
 11017    string? ClaimedBy,
 11018    Instant? StartedAt,
 11019    Instant? FinishedAt,
 11020    Instant UpdatedAt,
 11021    WorkProgress Progress,
 11022    string? Result,
 11023    string? Error,
 11024    Instant? CancelRequestedAt);
 25
 26public sealed record WorkItemQuery(WorkItemStatus? Status, WorkKind? Kind, int Skip, int Take);
 27
 28public sealed record WorkItemPage(IReadOnlyList<WorkItemView> Items, int Total);
 29
 30public 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
 52public sealed class EfWorkItemReader(AnichronDbContext db) : IWorkItemReader
 53{
 54    public async Task<WorkItemPage> ListAsync(WorkItemQuery query, CancellationToken ct)
 55    {
 56        var matching = db.WorkItems.AsQueryable();
 57        if (query.Status is { } status)
 58            matching = matching.Where(w => w.Status == status);
 59        if (query.Kind is { } kind)
 60            matching = matching.Where(w => w.Kind == kind);
 61
 62        var items = await matching
 63            .OrderByDescending(w => w.RequestedAt)
 64            .ThenByDescending(w => w.Id)
 65            .Skip(query.Skip)
 66            .Take(query.Take)
 67            .Select(ToView)
 68            .ToListAsync(ct);
 69        return new WorkItemPage(items, await matching.CountAsync(ct));
 70    }
 71
 72    public Task<WorkItemView?> FindAsync(Guid id, CancellationToken ct)
 73        => db.WorkItems.Where(w => w.Id == id).Select(ToView).FirstOrDefaultAsync(ct);
 74
 75    public async Task<IReadOnlyList<WorkItemView>> UpdatedSinceAsync(Instant since, CancellationToken ct)
 76        => await UpdatedSinceQuery(db, since).ToListAsync(ct);
 77
 78    public Task<Instant?> LatestUpdateAsync(CancellationToken ct)
 79        => db.WorkItems.MaxAsync(w => (Instant?)w.UpdatedAt, ct);
 80
 81    internal static IQueryable<WorkItemView> UpdatedSinceQuery(AnichronDbContext db, Instant since)
 82        => db.WorkItems
 83            .Where(w => w.UpdatedAt > since)
 84            .OrderBy(w => w.UpdatedAt)
 85            .ThenBy(w => w.Id)
 86            .Select(ToView);
 87
 88    private static Expression<Func<WorkItem, WorkItemView>> ToView
 89        => w => new WorkItemView(
 90            w.Id, w.Kind, w.StorageConfigId, w.RequestedBy, w.RequestedAt, w.Status, w.Attempt, w.Stalls,
 91            w.ClaimedBy, w.StartedAt, w.FinishedAt, w.UpdatedAt,
 92            new WorkProgress(w.ProgressDone, w.ProgressTotal, w.ProgressNote), w.Result, w.Error,
 93            w.CancelRequestedAt);
 94}