From a31f834a68f86feb430ed05a87aada18d7595e7b Mon Sep 17 00:00:00 2001 From: cryptooda Date: Thu, 2 Oct 2025 00:31:00 +0700 Subject: [PATCH] Fix BacktestCount --- .../Backtests/Backtester.cs | 11 +++++---- .../PostgreSql/AgentSummaryRepository.cs | 24 +++++++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Managing.Application/Backtests/Backtester.cs b/src/Managing.Application/Backtests/Backtester.cs index abe3f48e..ec8e52fe 100644 --- a/src/Managing.Application/Backtests/Backtester.cs +++ b/src/Managing.Application/Backtests/Backtester.cs @@ -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 _logger; private readonly IExchangeService _exchangeService; private readonly IScenarioService _scenarioService; @@ -27,7 +30,6 @@ namespace Managing.Application.Backtests private readonly IKaigenService _kaigenService; private readonly IHubContext _hubContext; private readonly IGrainFactory _grainFactory; - private readonly IAgentService _agentService; public Backtester( IExchangeService exchangeService, @@ -39,7 +41,7 @@ namespace Managing.Application.Backtests IKaigenService kaigenService, IHubContext 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; } /// @@ -199,7 +201,8 @@ namespace Managing.Application.Backtests { try { - await _agentService.IncrementBacktestCountAsync(user.Id); + await ServiceScopeHelpers.WithScopedService(_serviceScopeFactory, + async (agentService) => await agentService.IncrementBacktestCountAsync(user.Id)); } catch (Exception ex) { diff --git a/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs b/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs index b12a7271..fe092e89 100644 --- a/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs +++ b/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs @@ -134,7 +134,7 @@ public class AgentSummaryRepository : IAgentSummaryRepository // Check if an entity with this key is already being tracked var trackedEntity = _context.ChangeTracker.Entries() .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); } }