|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.EntityFrameworkCore; |
| 5 | +using SW.Bitween.Domain; |
| 6 | +using SW.Bitween.Domain.Accounts; |
| 7 | +using SW.Bitween.Model; |
| 8 | +using SW.PrimitiveTypes; |
| 9 | + |
| 10 | +namespace SW.Bitween.Resources.RetryPolicies; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Reports how much of each group's total budget the integrations using this policy have spent, |
| 14 | +/// so an exhausted group is visible instead of just silently declining to retry. |
| 15 | +/// </summary> |
| 16 | +[HandlerName("usage")] |
| 17 | +public class Usage : ICommandHandler<int, RetryPolicyUsageRequest, object> |
| 18 | +{ |
| 19 | + private readonly BitweenDbContext _dbContext; |
| 20 | + private readonly RequestContext _requestContext; |
| 21 | + |
| 22 | + public Usage(BitweenDbContext dbContext, RequestContext requestContext) |
| 23 | + { |
| 24 | + _dbContext = dbContext; |
| 25 | + _requestContext = requestContext; |
| 26 | + } |
| 27 | + |
| 28 | + public async Task<object> Handle(int key, RetryPolicyUsageRequest request) |
| 29 | + { |
| 30 | + _requestContext.EnsureAccess(AccountRole.Admin, AccountRole.Member); |
| 31 | + |
| 32 | + var policy = await _dbContext.Set<RetryPolicy>().AsNoTracking() |
| 33 | + .FirstOrDefaultAsync(p => p.Id == key); |
| 34 | + if (policy == null) throw new SWNotFoundException(key.ToString()); |
| 35 | + |
| 36 | + var subscriptions = await _dbContext.Set<Subscription>().AsNoTracking() |
| 37 | + .Where(s => s.RetryPolicyId == key) |
| 38 | + .Select(s => new { s.Id, s.Name }) |
| 39 | + .ToListAsync(); |
| 40 | + |
| 41 | + var subscriptionIds = subscriptions.Select(s => s.Id).ToList(); |
| 42 | + |
| 43 | + var usages = await _dbContext.Set<RetryGroupUsage>().AsNoTracking() |
| 44 | + .Where(u => subscriptionIds.Contains(u.SubscriptionId)) |
| 45 | + .ToListAsync(); |
| 46 | + |
| 47 | + // Only groups that allow retries have a budget to spend. |
| 48 | + var budgets = policy.Groups |
| 49 | + .Where(g => g.Budget != null) |
| 50 | + .ToDictionary(g => g.Id, g => new { g.Name, g.Budget.MaxAttemptsTotal }); |
| 51 | + |
| 52 | + var names = subscriptions.ToDictionary(s => s.Id, s => s.Name); |
| 53 | + |
| 54 | + var rows = usages |
| 55 | + .Where(u => budgets.ContainsKey(u.GroupId)) |
| 56 | + .Select(u => new RetryGroupUsageRow |
| 57 | + { |
| 58 | + SubscriptionId = u.SubscriptionId, |
| 59 | + SubscriptionName = names.GetValueOrDefault(u.SubscriptionId), |
| 60 | + GroupId = u.GroupId, |
| 61 | + GroupName = budgets[u.GroupId].Name, |
| 62 | + AttemptsUsed = u.AttemptsUsed, |
| 63 | + MaxAttemptsTotal = budgets[u.GroupId].MaxAttemptsTotal, |
| 64 | + Exhausted = u.AttemptsUsed >= budgets[u.GroupId].MaxAttemptsTotal, |
| 65 | + LastAttemptOn = u.LastAttemptOn |
| 66 | + }) |
| 67 | + // Exhausted integrations first — those are the ones no longer being retried. |
| 68 | + .OrderByDescending(r => r.Exhausted) |
| 69 | + .ThenByDescending(r => r.AttemptsUsed) |
| 70 | + .ToList(); |
| 71 | + |
| 72 | + return new List<RetryGroupUsageRow>(rows); |
| 73 | + } |
| 74 | +} |
0 commit comments