Add Agent tracking balance

This commit is contained in:
2025-05-16 22:30:18 +07:00
parent b34e3aa886
commit 1cfb83f0b1
34 changed files with 764 additions and 115 deletions

View File

@@ -1,6 +1,5 @@
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Application.Workers.Abstractions;
using Managing.Domain.Accounts;
using Managing.Domain.MoneyManagements;
using Managing.Domain.Scenarios;
@@ -24,6 +23,7 @@ public class StatisticService : IStatisticService
private readonly ITradaoService _tradaoService;
private readonly IMessengerService _messengerService;
private readonly ICacheService _cacheService;
private readonly IAgentBalanceRepository _agentBalanceRepository;
private readonly ILogger<StatisticService> _logger;
public StatisticService(
@@ -36,7 +36,8 @@ public class StatisticService : IStatisticService
IBacktester backtester,
ITradaoService tradaoService,
IMessengerService messengerService,
ICacheService cacheService)
ICacheService cacheService,
IAgentBalanceRepository agentBalanceRepository)
{
_exchangeService = exchangeService;
_accountService = accountService;
@@ -48,6 +49,7 @@ public class StatisticService : IStatisticService
_tradaoService = tradaoService;
_messengerService = messengerService;
_cacheService = cacheService;
_agentBalanceRepository = agentBalanceRepository;
}
public async Task UpdateTopVolumeTicker(TradingExchanges exchange, int top)
@@ -375,4 +377,74 @@ public class StatisticService : IStatisticService
await _messengerService.SendBadTraders(lastBadTrader);
}
public async Task<IList<AgentBalanceHistory>> GetAgentBalances(string agentName, DateTime start,
DateTime? end = null)
{
var effectiveEnd = end ?? DateTime.UtcNow;
string cacheKey = $"AgentBalances_{agentName}_{start:yyyyMMdd}_{effectiveEnd:yyyyMMdd}";
// Check if the balances are already cached
var cachedBalances = _cacheService.GetValue<IList<AgentBalanceHistory>>(cacheKey);
if (cachedBalances != null)
{
return cachedBalances;
}
var balances = await _agentBalanceRepository.GetAgentBalances(agentName, start, end);
// Create a single AgentBalanceHistory with all balances
var result = new List<AgentBalanceHistory>
{
new AgentBalanceHistory
{
AgentName = agentName,
AgentBalances = balances.OrderBy(b => b.Time).ToList()
}
};
// Cache the results for 5 minutes
_cacheService.SaveValue(cacheKey, result, TimeSpan.FromMinutes(5));
return result;
}
public async Task<(IList<AgentBalanceHistory> Agents, int TotalCount)> GetBestAgents(
DateTime start,
DateTime? end = null,
int page = 1,
int pageSize = 10)
{
var effectiveEnd = end ?? DateTime.UtcNow;
string cacheKey = $"BestAgents_{start:yyyyMMdd}_{effectiveEnd:yyyyMMdd}";
// Check if the results are already cached
var cachedResult = _cacheService.GetValue<(IList<AgentBalanceHistory>, int)>(cacheKey);
if (cachedResult != default)
{
// Apply pagination to cached results
var (cachedAgents, cachedTotalCount) = cachedResult;
var paginatedAgents = cachedAgents
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToList();
return (paginatedAgents, cachedTotalCount);
}
// Get all agents with their balance history
var (fetchedAgents, fetchedTotalCount) = await _agentBalanceRepository.GetAllAgentBalancesWithHistory(start, end);
// Cache all results for 5 minutes
_cacheService.SaveValue(cacheKey, (fetchedAgents, fetchedTotalCount), TimeSpan.FromMinutes(5));
// Apply pagination
var result = fetchedAgents
.Skip((page - 1) * pageSize)
.Take(pageSize)
.ToList();
return (result, fetchedTotalCount);
}
}