| | | 1 | | using RoleRadar.Application.Abstractions; |
| | | 2 | | using RoleRadar.Application.Reading; |
| | | 3 | | using RoleRadar.Domain.Common; |
| | | 4 | | |
| | | 5 | | namespace 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> |
| | 760 | 8 | | public 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> |
| | | 15 | | public 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 | | |
| | | 23 | | public GetInsightsStatsQueryHandler(IOwnerReadSeam seam, IUtcClock clock) |
| | | 24 | | { |
| | | 25 | | _seam = seam ?? throw new ArgumentNullException(nameof(seam)); |
| | | 26 | | _clock = clock ?? throw new ArgumentNullException(nameof(clock)); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public Task<Result<InsightsStatsDto>> HandleAsync(GetInsightsStatsQuery query, CancellationToken cancellationToken = |
| | | 30 | | { |
| | | 31 | | ArgumentNullException.ThrowIfNull(query); |
| | | 32 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 33 | | |
| | | 34 | | var today = DateOnly.FromDateTime(_clock.UtcNow); |
| | | 35 | | if (query.AsOf > today.AddDays(1)) |
| | | 36 | | { |
| | | 37 | | return Task.FromResult(Result<InsightsStatsDto>.Failure( |
| | | 38 | | ErrorCode.ValidationError, |
| | | 39 | | "asOf cannot be more than 1 day in the future.")); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | var owner = new OwnerId(query.OwnerId); |
| | | 43 | | 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 |
| | | 46 | | var applicationsInWindow = _seam.QueryApplications(owner).Where(a => a.AppliedAt < bounds.EndExclusive); |
| | | 47 | | if (bounds.Start is { } start) |
| | | 48 | | { |
| | | 49 | | applicationsInWindow = applicationsInWindow.Where(a => a.AppliedAt >= start); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | var applications = applicationsInWindow |
| | | 53 | | .Select(a => new { a.AppliedAt, a.FirstResponseAt }) |
| | | 54 | | .Take(RowCap + 1) |
| | | 55 | | .ToList(); |
| | | 56 | | if (applications.Count > RowCap) |
| | | 57 | | { |
| | | 58 | | return Task.FromResult(VolumeLimit("applications")); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | var runsInWindow = _seam.QuerySearchRuns(owner).Where(r => r.RunDate < bounds.EndExclusive); |
| | | 62 | | if (bounds.Start is { } runStart) |
| | | 63 | | { |
| | | 64 | | runsInWindow = runsInWindow.Where(r => r.RunDate >= runStart); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | var runs = runsInWindow |
| | | 68 | | .Select(r => new { r.RunDate, r.NewPostings }) |
| | | 69 | | .Take(RowCap + 1) |
| | | 70 | | .ToList(); |
| | | 71 | | if (runs.Count > RowCap) |
| | | 72 | | { |
| | | 73 | | return Task.FromResult(VolumeLimit("search runs")); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | var dto = new InsightsStatsDto( |
| | | 77 | | AsOf: query.AsOf, |
| | | 78 | | Window: InsightsWindows.ToWireName(query.Window), |
| | | 79 | | WindowStart: bounds.Start is null ? null : DateOnly.FromDateTime(bounds.Start.Value.UtcDateTime), |
| | | 80 | | ResponseTime: InsightsStatsCalculator.ResponseTime( |
| | | 81 | | applications.Select(a => (a.AppliedAt, a.FirstResponseAt)), query.Window, query.AsOf), |
| | | 82 | | WeeklyApplications: InsightsStatsCalculator.WeeklyApplications( |
| | | 83 | | applications.Select(a => a.AppliedAt), query.Window, query.AsOf), |
| | | 84 | | SearchRunYield: InsightsStatsCalculator.RunYield( |
| | | 85 | | runs.Select(r => (r.RunDate, r.NewPostings)), query.Window, query.AsOf)); |
| | | 86 | | |
| | | 87 | | return Task.FromResult(Result<InsightsStatsDto>.Success(dto)); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | private static Result<InsightsStatsDto> VolumeLimit(string source) => |
| | | 91 | | Result<InsightsStatsDto>.Failure( |
| | | 92 | | ErrorCode.ValidationError, |
| | | 93 | | $"The Insights stats read reached its volume limit of 50,000 {source} for this window. Narrow the window."); |
| | | 94 | | } |