feat: prioritize trader analytics via LastAnalyzedAt and add force-analyze endpoint
This commit is contained in:
@@ -40,21 +40,23 @@ public class TraderAnalyticsWorker : BackgroundService
|
||||
|
||||
private async Task RunAnalyticsAsync(CancellationToken ct)
|
||||
{
|
||||
var cutoff30d = DateTime.UtcNow.AddDays(-30);
|
||||
List<int> traderIds;
|
||||
|
||||
using (var scope = _services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
// Find traders active in the last 30 days
|
||||
traderIds = await db.Trades
|
||||
.Where(t => t.ExecutedAt >= cutoff30d)
|
||||
.Select(t => t.TraderId)
|
||||
.Distinct()
|
||||
// Find traders who have never been analyzed, or whose last analysis was before their latest trade.
|
||||
// Prioritize never-analyzed traders.
|
||||
traderIds = await db.Traders
|
||||
.Where(t => t.LastAnalyzedAt == null || t.Trades.Any(tr => tr.ExecutedAt > t.LastAnalyzedAt))
|
||||
.OrderBy(t => t.LastAnalyzedAt == null ? 0 : 1)
|
||||
.ThenBy(t => t.LastAnalyzedAt)
|
||||
.Select(t => t.Id)
|
||||
.Take(500) // Limit batch size to prevent long-running loops without save
|
||||
.ToListAsync(ct);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Found {Count} active traders to analyze", traderIds.Count);
|
||||
_logger.LogInformation("Found {Count} active or unanalyzed traders to update", traderIds.Count);
|
||||
|
||||
foreach (var id in traderIds)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user