< Summary

Information
Class: RoleRadar.Application.Insights.GetInsightsStatsQueryHandler
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Insights/GetInsightsStatsQuery.cs
Line coverage
100%
Covered lines: 45
Uncovered lines: 0
Coverable lines: 45
Total lines: 94
Line coverage: 100%
Branch coverage
87%
Covered branches: 14
Total branches: 16
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
HandleAsync(...)100%1212100%
VolumeLimit(...)100%11100%

File(s)

/home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Insights/GetInsightsStatsQuery.cs

#LineLine coverage
 1using RoleRadar.Application.Abstractions;
 2using RoleRadar.Application.Reading;
 3using RoleRadar.Domain.Common;
 4
 5namespace RoleRadar.Application.Insights;
 6
 7/// <summary>Asks for the Insights stats of one owner for a window pinned to an as-of date (UTC).</summary>
 8public sealed record GetInsightsStatsQuery(string OwnerId, InsightsWindow Window, DateOnly AsOf) : IQuery<InsightsStatsD
 9
 10/// <summary>
 11/// Read-only handler for GET /api/v1/insights/stats (ADR 0030). It projects two date columns from applications and two
 12/// from search runs through <see cref="IOwnerReadSeam"/> (never the collection) and computes in memory. It is a query, 
 13/// it is not wrapped by the transaction and audit decorator: it writes nothing.
 14/// </summary>
 15public sealed class GetInsightsStatsQueryHandler : IQueryHandler<GetInsightsStatsQuery, InsightsStatsDto>
 16{
 17    /// <summary>Hard cap on projected rows per source. Above it the handler fails with a typed result rather than trunc
 18    public const int RowCap = 50_000;
 19
 20    private readonly IOwnerReadSeam _seam;
 21    private readonly IUtcClock _clock;
 22
 9823    public GetInsightsStatsQueryHandler(IOwnerReadSeam seam, IUtcClock clock)
 24    {
 9825        _seam = seam ?? throw new ArgumentNullException(nameof(seam));
 9826        _clock = clock ?? throw new ArgumentNullException(nameof(clock));
 9827    }
 28
 29    public Task<Result<InsightsStatsDto>> HandleAsync(GetInsightsStatsQuery query, CancellationToken cancellationToken =
 30    {
 6631        ArgumentNullException.ThrowIfNull(query);
 6632        cancellationToken.ThrowIfCancellationRequested();
 33
 6634        var today = DateOnly.FromDateTime(_clock.UtcNow);
 6635        if (query.AsOf > today.AddDays(1))
 36        {
 637            return Task.FromResult(Result<InsightsStatsDto>.Failure(
 638                ErrorCode.ValidationError,
 639                "asOf cannot be more than 1 day in the future."));
 40        }
 41
 6042        var owner = new OwnerId(query.OwnerId);
 6043        var bounds = InsightsStatsCalculator.Bounds(query.Window, query.AsOf);
 44
 45        // The window is applied in the query, so the cap bounds what is actually read. One extra row detects an overflo
 6046        var applicationsInWindow = _seam.QueryApplications(owner).Where(a => a.AppliedAt < bounds.EndExclusive);
 6047        if (bounds.Start is { } start)
 48        {
 3649            applicationsInWindow = applicationsInWindow.Where(a => a.AppliedAt >= start);
 50        }
 51
 6052        var applications = applicationsInWindow
 6053            .Select(a => new { a.AppliedAt, a.FirstResponseAt })
 6054            .Take(RowCap + 1)
 6055            .ToList();
 6056        if (applications.Count > RowCap)
 57        {
 258            return Task.FromResult(VolumeLimit("applications"));
 59        }
 60
 5861        var runsInWindow = _seam.QuerySearchRuns(owner).Where(r => r.RunDate < bounds.EndExclusive);
 5862        if (bounds.Start is { } runStart)
 63        {
 3464            runsInWindow = runsInWindow.Where(r => r.RunDate >= runStart);
 65        }
 66
 5867        var runs = runsInWindow
 5868            .Select(r => new { r.RunDate, r.NewPostings })
 5869            .Take(RowCap + 1)
 5870            .ToList();
 5871        if (runs.Count > RowCap)
 72        {
 273            return Task.FromResult(VolumeLimit("search runs"));
 74        }
 75
 5676        var dto = new InsightsStatsDto(
 5677            AsOf: query.AsOf,
 5678            Window: InsightsWindows.ToWireName(query.Window),
 5679            WindowStart: bounds.Start is null ? null : DateOnly.FromDateTime(bounds.Start.Value.UtcDateTime),
 5680            ResponseTime: InsightsStatsCalculator.ResponseTime(
 10060281                applications.Select(a => (a.AppliedAt, a.FirstResponseAt)), query.Window, query.AsOf),
 5682            WeeklyApplications: InsightsStatsCalculator.WeeklyApplications(
 10060283                applications.Select(a => a.AppliedAt), query.Window, query.AsOf),
 5684            SearchRunYield: InsightsStatsCalculator.RunYield(
 21085                runs.Select(r => (r.RunDate, r.NewPostings)), query.Window, query.AsOf));
 86
 5687        return Task.FromResult(Result<InsightsStatsDto>.Success(dto));
 88    }
 89
 90    private static Result<InsightsStatsDto> VolumeLimit(string source) =>
 491        Result<InsightsStatsDto>.Failure(
 492            ErrorCode.ValidationError,
 493            $"The Insights stats read reached its volume limit of 50,000 {source} for this window. Narrow the window.");
 94}