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

View File

@@ -317,13 +317,15 @@ public class AgentSummaryRepository : IAgentSummaryRepository
entity.AgentName = agentName; entity.AgentName = agentName;
entity.UpdatedAt = DateTime.UtcNow; entity.UpdatedAt = DateTime.UtcNow;
_context.AgentSummaries.Update(entity);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
_logger.LogInformation("Agent name updated for user {UserId} to {AgentName}", userId, agentName); _logger.LogInformation("Agent name updated for user {UserId} to {AgentName}", userId, agentName);
} }
else 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); userId, agentName);
} }
} }
@@ -340,9 +342,11 @@ public class AgentSummaryRepository : IAgentSummaryRepository
if (entity != null) if (entity != null)
{ {
entity.BacktestCount++; var newCount = entity.BacktestCount + 1;
entity.BacktestCount = newCount;
entity.UpdatedAt = DateTime.UtcNow; entity.UpdatedAt = DateTime.UtcNow;
_context.AgentSummaries.Update(entity);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
_logger.LogInformation("Backtest count incremented for user {UserId} to {BacktestCount}", _logger.LogInformation("Backtest count incremented for user {UserId} to {BacktestCount}",