From 44846a1817a823dd87fef9140c0fb0464552cdc4 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Wed, 24 Sep 2025 11:35:40 +0700 Subject: [PATCH] Fix update AgentName --- .../Repositories/IAgentSummaryRepository.cs | 7 ++++++ .../Services/IAgentService.cs | 7 ++++++ .../Abstractions/Grains/IAgentGrain.cs | 2 ++ .../Agents/AgentService.cs | 25 +++++++++++++++++++ .../Bots/Grains/AgentGrain.cs | 3 +++ .../PostgreSql/AgentSummaryRepository.cs | 21 ++++++++++++++++ 6 files changed, 65 insertions(+) diff --git a/src/Managing.Application.Abstractions/Repositories/IAgentSummaryRepository.cs b/src/Managing.Application.Abstractions/Repositories/IAgentSummaryRepository.cs index b2f095ee..5e6c2461 100644 --- a/src/Managing.Application.Abstractions/Repositories/IAgentSummaryRepository.cs +++ b/src/Managing.Application.Abstractions/Repositories/IAgentSummaryRepository.cs @@ -28,4 +28,11 @@ public interface IAgentSummaryRepository string sortOrder, IEnumerable? agentNames = null); Task> GetAllAgentWithRunningBots(); + + /// + /// Updates only the agent name for a specific user's agent summary + /// + /// The user ID + /// The new agent name + Task UpdateAgentNameAsync(int userId, string agentName); } \ No newline at end of file diff --git a/src/Managing.Application.Abstractions/Services/IAgentService.cs b/src/Managing.Application.Abstractions/Services/IAgentService.cs index 18d6cfe2..7685336d 100644 --- a/src/Managing.Application.Abstractions/Services/IAgentService.cs +++ b/src/Managing.Application.Abstractions/Services/IAgentService.cs @@ -14,4 +14,11 @@ public interface IAgentService Task> GetAllAgentSummaries(); Task> GetAllOnlineAgents(); + + /// + /// Updates only the agent name for a specific user's agent summary + /// + /// The user ID + /// The new agent name + Task UpdateAgentSummaryNameAsync(int userId, string agentName); } \ No newline at end of file diff --git a/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs b/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs index cb5da695..d27b5e7f 100644 --- a/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs +++ b/src/Managing.Application/Abstractions/Grains/IAgentGrain.cs @@ -17,11 +17,13 @@ namespace Managing.Application.Abstractions.Grains /// Updates only the agent name without recalculating summary. /// /// The new agent name. + [OneWay] Task UpdateAgentNameAsync(string agentName); /// /// Generates a summary of the agent's stats for the AgentRegistryGrain. /// + [OneWay] Task UpdateSummary(); /// diff --git a/src/Managing.Application/Agents/AgentService.cs b/src/Managing.Application/Agents/AgentService.cs index bcc5d318..f4a7a46c 100644 --- a/src/Managing.Application/Agents/AgentService.cs +++ b/src/Managing.Application/Agents/AgentService.cs @@ -1,6 +1,8 @@ using Managing.Application.Abstractions.Repositories; using Managing.Application.Abstractions.Services; +using Managing.Core; using Managing.Domain.Statistics; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Managing.Application.Agents; @@ -9,17 +11,20 @@ public class AgentService : IAgentService { private readonly IAgentBalanceRepository _agentBalanceRepository; private readonly IAgentSummaryRepository _agentSummaryRepository; + private readonly IServiceScopeFactory _serviceScopeFactory; private readonly ICacheService _cacheService; private readonly ILogger _logger; public AgentService( IAgentBalanceRepository agentBalanceRepository, IAgentSummaryRepository agentSummaryRepository, + IServiceScopeFactory serviceScopeFactory, ICacheService cacheService, ILogger logger) { _agentBalanceRepository = agentBalanceRepository; _agentSummaryRepository = agentSummaryRepository; + _serviceScopeFactory = serviceScopeFactory; _cacheService = cacheService; _logger = logger; } @@ -96,6 +101,11 @@ public class AgentService : IAgentService { try { + if (string.IsNullOrEmpty(agentSummary.AgentName)) + { + agentSummary.AgentName = await ServiceScopeHelpers.WithScopedService(_serviceScopeFactory, + async (userService) => (await userService.GetUserByIdAsync(agentSummary.UserId)).AgentName); + } // Use the injected AgentSummaryRepository to save or update await _agentSummaryRepository.SaveOrUpdateAsync(agentSummary); @@ -120,4 +130,19 @@ public class AgentService : IAgentService var agentSummaries = await _agentSummaryRepository.GetAllAgentWithRunningBots(); return agentSummaries.Select(a => a.AgentName); } + + public async Task UpdateAgentSummaryNameAsync(int userId, string agentName) + { + try + { + await _agentSummaryRepository.UpdateAgentNameAsync(userId, agentName); + + _logger.LogInformation("Agent name updated for user {UserId} to {AgentName}", userId, agentName); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error updating agent name for user {UserId} to {AgentName}", userId, agentName); + throw; + } + } } \ No newline at end of file diff --git a/src/Managing.Application/Bots/Grains/AgentGrain.cs b/src/Managing.Application/Bots/Grains/AgentGrain.cs index 115cc172..b0ac92d6 100644 --- a/src/Managing.Application/Bots/Grains/AgentGrain.cs +++ b/src/Managing.Application/Bots/Grains/AgentGrain.cs @@ -85,6 +85,9 @@ public class AgentGrain : Grain, IAgentGrain { _state.State.AgentName = agentName; await _state.WriteStateAsync(); + + // Use the efficient method to update only the agent name in the summary + await _agentService.UpdateAgentSummaryNameAsync((int)this.GetPrimaryKeyLong(), agentName); _logger.LogInformation("Agent {UserId} updated with name {AgentName}", this.GetPrimaryKeyLong(), agentName); } diff --git a/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs b/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs index 58082014..44828cb1 100644 --- a/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs +++ b/src/Managing.Infrastructure.Database/PostgreSql/AgentSummaryRepository.cs @@ -297,4 +297,25 @@ public class AgentSummaryRepository : IAgentSummaryRepository return agentSummaries.Select(MapToDomain); } + + public async Task UpdateAgentNameAsync(int userId, string agentName) + { + var entity = await _context.AgentSummaries + .FirstOrDefaultAsync(a => a.UserId == userId); + + if (entity != null) + { + entity.AgentName = agentName; + entity.UpdatedAt = DateTime.UtcNow; + + 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}", + userId, agentName); + } + } } \ No newline at end of file