| | | 1 | | namespace RoleRadar.Application.Insights; |
| | | 2 | | |
| | | 3 | | /// <summary>The instants that bound a window: <c>Start</c> inclusive (null for all time), <c>EndExclusive</c> exclusive |
| | 604162 | 4 | | public readonly record struct InsightsWindowBounds(DateTimeOffset? Start, DateTimeOffset EndExclusive) |
| | | 5 | | { |
| | | 6 | | public bool Contains(DateTimeOffset instant) => |
| | 101628 | 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> |
| | | 15 | | public static class InsightsStatsCalculator |
| | | 16 | | { |
| | | 17 | | public const int MaxRunYield = 120; |
| | | 18 | | |
| | | 19 | | public static InsightsWindowBounds Bounds(InsightsWindow window, DateOnly asOf) |
| | | 20 | | { |
| | | 21 | | var end = Midnight(asOf.AddDays(1)); |
| | | 22 | | return window == InsightsWindow.Last90Days |
| | | 23 | | ? new InsightsWindowBounds(Midnight(asOf.AddDays(-89)), end) |
| | | 24 | | : 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 | | { |
| | | 33 | | var days = DateOnly.FromDateTime(firstResponseAt.UtcDateTime).DayNumber - DateOnly.FromDateTime(appliedAt.UtcDat |
| | | 34 | | 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 | | { |
| | | 40 | | ArgumentNullException.ThrowIfNull(days); |
| | | 41 | | var sorted = days.Order().ToArray(); |
| | | 42 | | if (sorted.Length == 0) |
| | | 43 | | { |
| | | 44 | | return null; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | var middle = sorted.Length / 2; |
| | | 48 | | 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 | | { |
| | | 57 | | ArgumentNullException.ThrowIfNull(applications); |
| | | 58 | | var bounds = Bounds(window, asOf); |
| | | 59 | | var days = applications |
| | | 60 | | .Where(a => a.FirstResponseAt is not null && bounds.Contains(a.AppliedAt)) |
| | | 61 | | .Select(a => ResponseDays(a.AppliedAt, a.FirstResponseAt!.Value)) |
| | | 62 | | .ToArray(); |
| | | 63 | | 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 | | { |
| | | 76 | | ArgumentNullException.ThrowIfNull(appliedAt); |
| | | 77 | | var bounds = Bounds(window, asOf); |
| | | 78 | | var counts = new Dictionary<DateOnly, int>(); |
| | | 79 | | foreach (var instant in appliedAt.Where(bounds.Contains)) |
| | | 80 | | { |
| | | 81 | | var week = WeekStart(DateOnly.FromDateTime(instant.UtcDateTime)); |
| | | 82 | | counts[week] = counts.GetValueOrDefault(week) + 1; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | DateOnly first; |
| | | 86 | | if (window == InsightsWindow.Last90Days) |
| | | 87 | | { |
| | | 88 | | first = WeekStart(asOf.AddDays(-89)); |
| | | 89 | | } |
| | | 90 | | else if (counts.Count == 0) |
| | | 91 | | { |
| | | 92 | | return []; |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | | 96 | | first = counts.Keys.Min(); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | var last = WeekStart(asOf); |
| | | 100 | | var weeks = new List<WeeklyApplicationsDto>(); |
| | | 101 | | for (var week = first; week <= last; week = week.AddDays(7)) |
| | | 102 | | { |
| | | 103 | | weeks.Add(new WeeklyApplicationsDto(week, counts.GetValueOrDefault(week))); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | 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 | | { |
| | | 115 | | ArgumentNullException.ThrowIfNull(runs); |
| | | 116 | | var bounds = Bounds(window, asOf); |
| | | 117 | | var inWindow = runs.Where(r => bounds.Contains(r.RunDate)).OrderBy(r => r.RunDate).ToArray(); |
| | | 118 | | var truncated = inWindow.Length > MaxRunYield; |
| | | 119 | | var kept = truncated ? inWindow[^MaxRunYield..] : inWindow; |
| | | 120 | | return new SearchRunYieldDto(kept.Select(r => new SearchRunYieldPointDto(r.RunDate, r.NewPostings)).ToArray(), t |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private static DateOnly WeekStart(DateOnly date) |
| | | 124 | | { |
| | | 125 | | var sinceMonday = ((int)date.DayOfWeek + 6) % 7; |
| | | 126 | | return date.AddDays(-sinceMonday); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | private static DateTimeOffset Midnight(DateOnly date) => |
| | | 130 | | new(date.ToDateTime(TimeOnly.MinValue, DateTimeKind.Utc), TimeSpan.Zero); |
| | | 131 | | } |