Fix BacktestCount

This commit is contained in:
2025-10-02 00:31:00 +07:00
parent 06850b57c4
commit a31f834a68
2 changed files with 21 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services;
using Managing.Application.Hubs;
using Managing.Core;
using Managing.Domain.Accounts;
using Managing.Domain.Backtests;
using Managing.Domain.Bots;
@@ -10,6 +11,7 @@ using Managing.Domain.Candles;
using Managing.Domain.Scenarios;
using Managing.Domain.Users;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
using LightBacktestResponse = Managing.Domain.Backtests.LightBacktest; // Use the domain model for notification
@@ -19,6 +21,7 @@ namespace Managing.Application.Backtests
public class Backtester : IBacktester
{
private readonly IBacktestRepository _backtestRepository;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ILogger<Backtester> _logger;
private readonly IExchangeService _exchangeService;
private readonly IScenarioService _scenarioService;
@@ -27,7 +30,6 @@ namespace Managing.Application.Backtests
private readonly IKaigenService _kaigenService;
private readonly IHubContext<BacktestHub> _hubContext;
private readonly IGrainFactory _grainFactory;
private readonly IAgentService _agentService;
public Backtester(
IExchangeService exchangeService,
@@ -39,7 +41,7 @@ namespace Managing.Application.Backtests
IKaigenService kaigenService,
IHubContext<BacktestHub> hubContext,
IGrainFactory grainFactory,
IAgentService agentService)
IServiceScopeFactory serviceScopeFactory)
{
_exchangeService = exchangeService;
_backtestRepository = backtestRepository;
@@ -50,7 +52,7 @@ namespace Managing.Application.Backtests
_kaigenService = kaigenService;
_hubContext = hubContext;
_grainFactory = grainFactory;
_agentService = agentService;
_serviceScopeFactory = serviceScopeFactory;
}
/// <summary>
@@ -199,7 +201,8 @@ namespace Managing.Application.Backtests
{
try
{
await _agentService.IncrementBacktestCountAsync(user.Id);
await ServiceScopeHelpers.WithScopedService<IAgentService>(_serviceScopeFactory,
async (agentService) => await agentService.IncrementBacktestCountAsync(user.Id));
}
catch (Exception ex)
{

View File

@@ -134,7 +134,7 @@ public class AgentSummaryRepository : IAgentSummaryRepository
// Check if an entity with this key is already being tracked
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
.FirstOrDefault(e => e.Entity.Id == existing.Id);
if (trackedEntity != null)
{
// Entity is already tracked, update its values
@@ -147,7 +147,7 @@ public class AgentSummaryRepository : IAgentSummaryRepository
// Entity is not tracked, use Update method which handles state properly
_context.AgentSummaries.Update(entityToUpdate);
}
await _context.SaveChangesAsync();
_logger.LogInformation("AgentSummary updated for user {UserId} with agent name {AgentName}",
@@ -316,14 +316,16 @@ public class AgentSummaryRepository : IAgentSummaryRepository
{
entity.AgentName = agentName;
entity.UpdatedAt = DateTime.UtcNow;
_context.AgentSummaries.Update(entity);
await _context.SaveChangesAsync();
_logger.LogInformation("Agent name updated for user {UserId} to {AgentName}", userId, agentName);
}
else
{
_logger.LogWarning("No AgentSummary found for user {UserId} when trying to update agent name to {AgentName}",
_logger.LogWarning(
"No AgentSummary found for user {UserId} when trying to update agent name to {AgentName}",
userId, agentName);
}
}
@@ -340,17 +342,19 @@ public class AgentSummaryRepository : IAgentSummaryRepository
if (entity != null)
{
entity.BacktestCount++;
var newCount = entity.BacktestCount + 1;
entity.BacktestCount = newCount;
entity.UpdatedAt = DateTime.UtcNow;
_context.AgentSummaries.Update(entity);
await _context.SaveChangesAsync();
_logger.LogInformation("Backtest count incremented for user {UserId} to {BacktestCount}",
_logger.LogInformation("Backtest count incremented for user {UserId} to {BacktestCount}",
userId, entity.BacktestCount);
}
else
{
_logger.LogWarning("No AgentSummary found for user {UserId} when trying to increment backtest count",
_logger.LogWarning("No AgentSummary found for user {UserId} when trying to increment backtest count",
userId);
}
}