< Summary

Information
Class: Anichron.API.Endpoints.WorkItemEvents
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/WorkItemEvents.cs
Tag: 19_45211875487
Line coverage
95%
Covered lines: 23
Uncovered lines: 1
Coverable lines: 24
Total lines: 50
Line coverage: 95.8%
Branch coverage
88%
Covered branches: 16
Total branches: 18
Branch coverage: 88.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Lookback()100%11100%
StreamAsync()91.66%121294.73%
Parse(...)83.33%66100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Endpoints/WorkItemEvents.cs

#LineLine coverage
 1using Anichron.API.Settings;
 2using Anichron.Core.Work;
 3using Microsoft.Extensions.Options;
 4using NodaTime.Text;
 5using System.Net.ServerSentEvents;
 6using System.Runtime.CompilerServices;
 7
 8namespace Anichron.API.Endpoints;
 9
 1410public sealed class WorkItemEvents(IWorkItemReader reader, TimeProvider time, IOptions<WorkItemEventsSettings> settings)
 11{
 12    internal const string EventType = "work-item";
 13
 1714    internal static Duration Lookback => Duration.FromSeconds(10);
 15
 16    /// <summary>
 17    /// Every work item that changes from the cursor on, re-queried at the configured interval. Each
 18    /// event's id is the cursor after it, so a reconnect that sends it as Last-Event-ID resumes there.
 19    /// </summary>
 20    /// <param name="lastEventId">The cursor to resume from; without a readable one, the latest change.</param>
 21    /// <param name="ct">Ends the stream.</param>
 22    /// <returns>One event per changed item; an item may arrive twice, never not at all.</returns>
 23    public async IAsyncEnumerable<SseItem<WorkItemDto>> StreamAsync(
 24        string? lastEventId, [EnumeratorCancellation] CancellationToken ct)
 1325    {
 1326        var cursor = Parse(lastEventId) ?? await reader.LatestUpdateAsync(ct) ?? NodaConstants.UnixEpoch;
 1327        var interval = TimeSpan.FromSeconds(settings.Value.IntervalSeconds);
 1328        var sent = new HashSet<(Guid, Instant)>();
 1729        while (!ct.IsCancellationRequested)
 1730        {
 1731            var changed = await reader.UpdatedSinceAsync(cursor - Lookback, ct);
 7332            foreach (var item in changed.Where(item => !sent.Contains((item.Id, item.UpdatedAt))))
 1133            {
 1134                cursor = Instant.Max(cursor, item.UpdatedAt);
 1135                yield return new SseItem<WorkItemDto>(WorkItemDtoMapper.ToDto(item), EventType)
 1136                {
 1137                    EventId = WorkItemDtoMapper.Format(cursor),
 1138                };
 1139            }
 40
 1741            sent = [.. changed.Select(item => (item.Id, item.UpdatedAt))];
 1742            await Task.Delay(interval, time, ct);
 443        }
 044    }
 45
 46    private static Instant? Parse(string? lastEventId)
 1347        => lastEventId is not null && InstantPattern.ExtendedIso.Parse(lastEventId) is { Success: true } parsed
 1348            ? parsed.Value
 1349            : null;
 50}