Fix BacktestCount
This commit is contained in:
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -134,7 +134,7 @@ public class AgentSummaryRepository : IAgentSummaryRepository
|
|||||||
// Check if an entity with this key is already being tracked
|
// Check if an entity with this key is already being tracked
|
||||||
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
|
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
|
||||||
.FirstOrDefault(e => e.Entity.Id == existing.Id);
|
.FirstOrDefault(e => e.Entity.Id == existing.Id);
|
||||||
|
|
||||||
if (trackedEntity != null)
|
if (trackedEntity != null)
|
||||||
{
|
{
|
||||||
// Entity is already tracked, update its values
|
// 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
|
// Entity is not tracked, use Update method which handles state properly
|
||||||
_context.AgentSummaries.Update(entityToUpdate);
|
_context.AgentSummaries.Update(entityToUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
_logger.LogInformation("AgentSummary updated for user {UserId} with agent name {AgentName}",
|
_logger.LogInformation("AgentSummary updated for user {UserId} with agent name {AgentName}",
|
||||||
@@ -316,14 +316,16 @@ 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,17 +342,19 @@ 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}",
|
||||||
userId, entity.BacktestCount);
|
userId, entity.BacktestCount);
|
||||||
}
|
}
|
||||||
else
|
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);
|
userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user