Clear a bit more

This commit is contained in:
2025-08-05 19:34:42 +07:00
parent 0c8c3de807
commit 2dcbcc3ef2
9 changed files with 143 additions and 136 deletions

View File

@@ -24,8 +24,7 @@ public class StatisticService : IStatisticService
private readonly ITradaoService _tradaoService;
private readonly IMessengerService _messengerService;
private readonly ICacheService _cacheService;
private readonly IAgentBalanceRepository _agentBalanceRepository;
private readonly IAgentSummaryRepository _agentSummaryRepository;
private readonly ILogger<StatisticService> _logger;
public StatisticService(
@@ -38,9 +37,7 @@ public class StatisticService : IStatisticService
IBacktester backtester,
ITradaoService tradaoService,
IMessengerService messengerService,
ICacheService cacheService,
IAgentBalanceRepository agentBalanceRepository,
IAgentSummaryRepository agentSummaryRepository)
ICacheService cacheService)
{
_exchangeService = exchangeService;
_accountService = accountService;
@@ -52,8 +49,6 @@ public class StatisticService : IStatisticService
_tradaoService = tradaoService;
_messengerService = messengerService;
_cacheService = cacheService;
_agentBalanceRepository = agentBalanceRepository;
_agentSummaryRepository = agentSummaryRepository;
}
public async Task UpdateTopVolumeTicker(TradingExchanges exchange, int top)
@@ -433,95 +428,4 @@ public class StatisticService : IStatisticService
await _messengerService.SendBadTraders(lastBadTrader);
}
public async Task<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<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 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);
}
public async Task SaveOrUpdateAgentSummary(AgentSummary agentSummary)
{
try
{
// Use the injected AgentSummaryRepository to save or update
await _agentSummaryRepository.SaveOrUpdateAsync(agentSummary);
_logger.LogInformation("AgentSummary saved/updated for user {UserId} with agent name {AgentName}",
agentSummary.UserId, agentSummary.AgentName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error saving/updating AgentSummary for user {UserId} with agent name {AgentName}",
agentSummary.UserId, agentSummary.AgentName);
throw;
}
}
public async Task<IEnumerable<AgentSummary>> GetAllAgentSummaries()
{
return await _agentSummaryRepository.GetAllAsync();
}
}