< Summary

Information
Class: RoleRadar.Application.Insights.InsightsStatsCalculator
Assembly: RoleRadar.Application
File(s): /home/runner/work/roleradar/roleradar/src/RoleRadar.Application/Insights/InsightsStatsCalculator.cs
Line coverage
100%
Covered lines: 44
Uncovered lines: 0
Coverable lines: 44
Total lines: 131
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Bounds(...)100%22100%
ResponseDays(...)100%11100%
MedianDays(...)100%44100%
ResponseTime(...)100%22100%
WeeklyApplications(...)100%88100%
RunYield(...)100%22100%
WeekStart(...)100%11100%
Midnight(...)100%11100%

File(s)

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

#LineLine coverage
 1namespace RoleRadar.Application.Insights;
 2
 3/// <summary>The instants that bound a window: <c>Start</c> inclusive (null for all time), <c>EndExclusive</c> exclusive
 4public readonly record struct InsightsWindowBounds(DateTimeOffset? Start, DateTimeOffset EndExclusive)
 5{
 6    public bool Contains(DateTimeOffset instant) =>
 7        instant < EndExclusive && (Start is null || instant >= Start.Value);
 8}
 9
 10/// <summary>
 11/// The pure calculations behind GET /api/v1/insights/stats (ADR 0030). No I/O. Window math is identical to the web lens
 12/// module: <c>last90Days</c> is <c>[asOf-89d 00:00Z, asOf+1d 00:00Z)</c>, <c>allTime</c> has no lower bound and the sam
 13/// upper bound, so the numbers agree with the dashboard pinned to the same as-of date.
 14/// </summary>
 15public static class InsightsStatsCalculator
 16{
 17    public const int MaxRunYield = 120;
 18
 19    public static InsightsWindowBounds Bounds(InsightsWindow window, DateOnly asOf)
 20    {
 26021        var end = Midnight(asOf.AddDays(1));
 26022        return window == InsightsWindow.Last90Days
 26023            ? new InsightsWindowBounds(Midnight(asOf.AddDays(-89)), end)
 26024            : new InsightsWindowBounds(null, end);
 25    }
 26
 27    /// <summary>
 28    /// Whole UTC calendar days from the applied date to the first-response date. A response dated before the applied da
 29    /// counts as 0 (it stays in the sample rather than being dropped, so the sample size matches the "responded" lens).
 30    /// </summary>
 31    public static int ResponseDays(DateTimeOffset appliedAt, DateTimeOffset firstResponseAt)
 32    {
 35633        var days = DateOnly.FromDateTime(firstResponseAt.UtcDateTime).DayNumber - DateOnly.FromDateTime(appliedAt.UtcDat
 35634        return Math.Max(0, days);
 35    }
 36
 37    /// <summary>The median: the middle value for an odd count, the mean of the two middle values for an even count, nul
 38    public static double? MedianDays(IEnumerable<int> days)
 39    {
 7640        ArgumentNullException.ThrowIfNull(days);
 7641        var sorted = days.Order().ToArray();
 7642        if (sorted.Length == 0)
 43        {
 1644            return null;
 45        }
 46
 6047        var middle = sorted.Length / 2;
 6048        return sorted.Length % 2 == 1 ? sorted[middle] : (sorted[middle - 1] + (double)sorted[middle]) / 2d;
 49    }
 50
 51    /// <summary>Median response time over the applications applied inside the window that have a first response.</summa
 52    public static ResponseTimeDto ResponseTime(
 53        IEnumerable<(DateTimeOffset AppliedAt, DateTimeOffset? FirstResponseAt)> applications,
 54        InsightsWindow window,
 55        DateOnly asOf)
 56    {
 6257        ArgumentNullException.ThrowIfNull(applications);
 6258        var bounds = Bounds(window, asOf);
 6259        var days = applications
 10061860            .Where(a => a.FirstResponseAt is not null && bounds.Contains(a.AppliedAt))
 34661            .Select(a => ResponseDays(a.AppliedAt, a.FirstResponseAt!.Value))
 6262            .ToArray();
 6263        return new ResponseTimeDto(MedianDays(days), days.Length);
 64    }
 65
 66    /// <summary>
 67    /// Applications per Monday-start UTC week, oldest first, zero-filled. Only applications applied inside the window a
 68    /// counted. <c>last90Days</c> lists every week that overlaps the window; <c>allTime</c> runs from the week of the
 69    /// earliest application to the week of the as-of date, and is empty when there is no application.
 70    /// </summary>
 71    public static IReadOnlyList<WeeklyApplicationsDto> WeeklyApplications(
 72        IEnumerable<DateTimeOffset> appliedAt,
 73        InsightsWindow window,
 74        DateOnly asOf)
 75    {
 7076        ArgumentNullException.ThrowIfNull(appliedAt);
 7077        var bounds = Bounds(window, asOf);
 7078        var counts = new Dictionary<DateOnly, int>();
 20138079        foreach (var instant in appliedAt.Where(bounds.Contains))
 80        {
 10062081            var week = WeekStart(DateOnly.FromDateTime(instant.UtcDateTime));
 10062082            counts[week] = counts.GetValueOrDefault(week) + 1;
 83        }
 84
 85        DateOnly first;
 7086        if (window == InsightsWindow.Last90Days)
 87        {
 4088            first = WeekStart(asOf.AddDays(-89));
 89        }
 3090        else if (counts.Count == 0)
 91        {
 892            return [];
 93        }
 94        else
 95        {
 2296            first = counts.Keys.Min();
 97        }
 98
 6299        var last = WeekStart(asOf);
 62100        var weeks = new List<WeeklyApplicationsDto>();
 1788101        for (var week = first; week <= last; week = week.AddDays(7))
 102        {
 832103            weeks.Add(new WeeklyApplicationsDto(week, counts.GetValueOrDefault(week)));
 104        }
 105
 62106        return weeks;
 107    }
 108
 109    /// <summary>The latest <see cref="MaxRunYield"/> runs inside the window, oldest first; <c>Truncated</c> when more f
 110    public static SearchRunYieldDto RunYield(
 111        IEnumerable<(DateTimeOffset RunDate, int NewPostings)> runs,
 112        InsightsWindow window,
 113        DateOnly asOf)
 114    {
 64115        ArgumentNullException.ThrowIfNull(runs);
 64116        var bounds = Bounds(window, asOf);
 1362117        var inWindow = runs.Where(r => bounds.Contains(r.RunDate)).OrderBy(r => r.RunDate).ToArray();
 64118        var truncated = inWindow.Length > MaxRunYield;
 64119        var kept = truncated ? inWindow[^MaxRunYield..] : inWindow;
 704120        return new SearchRunYieldDto(kept.Select(r => new SearchRunYieldPointDto(r.RunDate, r.NewPostings)).ToArray(), t
 121    }
 122
 123    private static DateOnly WeekStart(DateOnly date)
 124    {
 100722125        var sinceMonday = ((int)date.DayOfWeek + 6) % 7;
 100722126        return date.AddDays(-sinceMonday);
 127    }
 128
 129    private static DateTimeOffset Midnight(DateOnly date) =>
 410130        new(date.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc), TimeSpan.Zero);
 131}