Fix update agent Summary when new balance fetch
This commit is contained in:
@@ -47,4 +47,11 @@ public interface IAgentSummaryRepository
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">The user ID</param>
|
/// <param name="userId">The user ID</param>
|
||||||
Task IncrementBacktestCountAsync(int userId);
|
Task IncrementBacktestCountAsync(int userId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the total balance for a specific user's agent summary
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">The user ID</param>
|
||||||
|
/// <param name="totalBalance">The new total balance value</param>
|
||||||
|
Task UpdateTotalBalanceAsync(int userId, decimal totalBalance);
|
||||||
}
|
}
|
||||||
@@ -30,4 +30,11 @@ public interface IAgentService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="userId">The user ID</param>
|
/// <param name="userId">The user ID</param>
|
||||||
Task IncrementBacktestCountAsync(int userId);
|
Task IncrementBacktestCountAsync(int userId);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates the total balance for a specific user's agent summary
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="userId">The user ID</param>
|
||||||
|
/// <param name="totalBalance">The new total balance value</param>
|
||||||
|
Task UpdateAgentSummaryTotalBalanceAsync(int userId, decimal totalBalance);
|
||||||
}
|
}
|
||||||
@@ -1,13 +1,12 @@
|
|||||||
|
using Managing.Application.Abstractions;
|
||||||
using Managing.Application.Abstractions.Repositories;
|
using Managing.Application.Abstractions.Repositories;
|
||||||
using Managing.Application.Abstractions.Services;
|
using Managing.Application.Abstractions.Services;
|
||||||
using Managing.Application.Abstractions;
|
|
||||||
using Managing.Core;
|
using Managing.Core;
|
||||||
|
using Managing.Domain.Accounts;
|
||||||
|
using Managing.Domain.Bots;
|
||||||
using Managing.Domain.Statistics;
|
using Managing.Domain.Statistics;
|
||||||
using Managing.Domain.Trades;
|
using Managing.Domain.Trades;
|
||||||
using Managing.Domain.Users;
|
using Managing.Domain.Users;
|
||||||
using Managing.Domain.Accounts;
|
|
||||||
using Managing.Domain.Bots;
|
|
||||||
using Managing.Domain.Shared.Helpers;
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using static Managing.Common.Enums;
|
using static Managing.Common.Enums;
|
||||||
@@ -95,6 +94,9 @@ public class AgentService : IAgentService
|
|||||||
// Insert the fresh balance data into InfluxDB
|
// Insert the fresh balance data into InfluxDB
|
||||||
_agentBalanceRepository.InsertAgentBalance(freshBalance);
|
_agentBalanceRepository.InsertAgentBalance(freshBalance);
|
||||||
|
|
||||||
|
// Update the AgentSummary TotalBalance with the fresh balance data
|
||||||
|
await UpdateAgentSummaryTotalBalanceAsync(userId, freshBalance.TotalBalanceValue);
|
||||||
|
|
||||||
// Add the fresh balance to our results if it falls within the requested time range
|
// Add the fresh balance to our results if it falls within the requested time range
|
||||||
if (freshBalance.Time >= start && freshBalance.Time <= effectiveEnd)
|
if (freshBalance.Time >= start && freshBalance.Time <= effectiveEnd)
|
||||||
{
|
{
|
||||||
@@ -186,6 +188,21 @@ public class AgentService : IAgentService
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task UpdateAgentSummaryTotalBalanceAsync(int userId, decimal totalBalance)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _agentSummaryRepository.UpdateTotalBalanceAsync(userId, totalBalance);
|
||||||
|
|
||||||
|
_logger.LogInformation("Total balance updated for user {UserId} to {TotalBalance}", userId, totalBalance);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error updating total balance for user {UserId} to {TotalBalance}", userId, totalBalance);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Determines if fresh balance data should be calculated based on the last balance timestamp
|
/// Determines if fresh balance data should be calculated based on the last balance timestamp
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1969,6 +1969,7 @@ public class TradingBotBase : ITradingBot
|
|||||||
|
|
||||||
// Add the signal to our collection
|
// Add the signal to our collection
|
||||||
await AddSignal(signal);
|
await AddSignal(signal);
|
||||||
|
await ManagePositions();
|
||||||
|
|
||||||
return signal;
|
return signal;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -465,4 +465,84 @@ public class AgentSummaryRepository : IAgentSummaryRepository
|
|||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task UpdateTotalBalanceAsync(int userId, decimal totalBalance)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// First, check if there's already a tracked entity with this key
|
||||||
|
var trackedEntity = _context.ChangeTracker.Entries<AgentSummaryEntity>()
|
||||||
|
.FirstOrDefault(e => e.Entity.UserId == userId);
|
||||||
|
|
||||||
|
AgentSummaryEntity? entityToUpdate = null;
|
||||||
|
bool wasTracked = false;
|
||||||
|
|
||||||
|
if (trackedEntity != null)
|
||||||
|
{
|
||||||
|
// Entity is already tracked, update it directly
|
||||||
|
entityToUpdate = trackedEntity.Entity;
|
||||||
|
wasTracked = true;
|
||||||
|
|
||||||
|
_logger.LogInformation("Found tracked entity for user {UserId}. Current total balance: {CurrentBalance}, New total balance: {NewBalance}",
|
||||||
|
userId, entityToUpdate.TotalBalance, totalBalance);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Entity is not tracked, fetch it normally
|
||||||
|
entityToUpdate = await _context.AgentSummaries
|
||||||
|
.FirstOrDefaultAsync(a => a.UserId == userId);
|
||||||
|
|
||||||
|
if (entityToUpdate == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("No AgentSummary found for user {UserId} when trying to update total balance to {TotalBalance}",
|
||||||
|
userId, totalBalance);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Fetched entity for user {UserId}. Current total balance: {CurrentBalance}, New total balance: {NewBalance}",
|
||||||
|
userId, entityToUpdate.TotalBalance, totalBalance);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the total balance is actually different
|
||||||
|
if (entityToUpdate.TotalBalance == totalBalance)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Total balance for user {UserId} is already {TotalBalance}, no update needed", userId, totalBalance);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the entity
|
||||||
|
var oldBalance = entityToUpdate.TotalBalance;
|
||||||
|
entityToUpdate.TotalBalance = totalBalance;
|
||||||
|
entityToUpdate.UpdatedAt = DateTime.UtcNow;
|
||||||
|
|
||||||
|
// If it wasn't tracked, explicitly mark it as modified
|
||||||
|
if (!wasTracked)
|
||||||
|
{
|
||||||
|
_context.Entry(entityToUpdate).State = EntityState.Modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Log the change tracker state before saving
|
||||||
|
var modifiedEntries = _context.ChangeTracker.Entries()
|
||||||
|
.Where(e => e.State == EntityState.Modified)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
_logger.LogInformation("Change tracker has {Count} modified entries before save", modifiedEntries.Count);
|
||||||
|
|
||||||
|
// Save changes
|
||||||
|
var changesSaved = await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("Total balance updated for user {UserId} from {OldBalance} to {NewBalance}. Changes saved: {ChangesSaved}",
|
||||||
|
userId, oldBalance, totalBalance, changesSaved);
|
||||||
|
|
||||||
|
if (changesSaved == 0)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("No changes were saved for user {UserId}. This might indicate a tracking issue.", userId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error updating total balance for user {UserId} to {TotalBalance}", userId, totalBalance);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user