< Summary

Information
Class: Anichron.Core.Work.EfWorkItemReader
Assembly: Anichron.Core
File(s): /home/runner/work/anichron/anichron/src/Anichron.Core/Work/WorkItemReader.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 94
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
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%
ListAsync()100%88100%
FindAsync(...)100%11100%
UpdatedSinceAsync()100%11100%
LatestUpdateAsync(...)100%11100%
UpdatedSinceQuery(...)100%11100%
get_ToView()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
 8public 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
 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
 1352public sealed class EfWorkItemReader(AnichronDbContext db) : IWorkItemReader
 53{
 54    public async Task<WorkItemPage> ListAsync(WorkItemQuery query, CancellationToken ct)
 755    {
 756        var matching = db.WorkItems.AsQueryable();
 757        if (query.Status is { } status)
 258            matching = matching.Where(w => w.Status == status);
 759        if (query.Kind is { } kind)
 260            matching = matching.Where(w => w.Kind == kind);
 61
 762        var items = await matching
 763            .OrderByDescending(w => w.RequestedAt)
 764            .ThenByDescending(w => w.Id)
 765            .Skip(query.Skip)
 766            .Take(query.Take)
 767            .Select(ToView)
 768            .ToListAsync(ct);
 769        return new WorkItemPage(items, await matching.CountAsync(ct));
 770    }
 71
 72    public Task<WorkItemView?> FindAsync(Guid id, CancellationToken ct)
 273        => db.WorkItems.Where(w => w.Id == id).Select(ToView).FirstOrDefaultAsync(ct);
 74
 75    public async Task<IReadOnlyList<WorkItemView>> UpdatedSinceAsync(Instant since, CancellationToken ct)
 276        => await UpdatedSinceQuery(db, since).ToListAsync(ct);
 77
 78    public Task<Instant?> LatestUpdateAsync(CancellationToken ct)
 279        => db.WorkItems.MaxAsync(w => (Instant?)w.UpdatedAt, ct);
 80
 81    internal static IQueryable<WorkItemView> UpdatedSinceQuery(AnichronDbContext db, Instant since)
 382        => db.WorkItems
 383            .Where(w => w.UpdatedAt > since)
 384            .OrderBy(w => w.UpdatedAt)
 385            .ThenBy(w => w.Id)
 386            .Select(ToView);
 87
 88    private static Expression<Func<WorkItem, WorkItemView>> ToView
 1289        => w => new WorkItemView(
 1290            w.Id, w.Kind, w.StorageConfigId, w.RequestedBy, w.RequestedAt, w.Status, w.Attempt, w.Stalls,
 1291            w.ClaimedBy, w.StartedAt, w.FinishedAt, w.UpdatedAt,
 1292            new WorkProgress(w.ProgressDone, w.ProgressTotal, w.ProgressNote), w.Result, w.Error,
 1293            w.CancelRequestedAt);
 94}