Fix update AgentName

This commit is contained in:
2025-09-24 11:35:40 +07:00
parent 68350e3c24
commit 44846a1817
6 changed files with 65 additions and 0 deletions

View File

@@ -28,4 +28,11 @@ public interface IAgentSummaryRepository
string sortOrder, string sortOrder,
IEnumerable<string>? agentNames = null); IEnumerable<string>? agentNames = null);
Task<IEnumerable<AgentSummary>> GetAllAgentWithRunningBots(); Task<IEnumerable<AgentSummary>> GetAllAgentWithRunningBots();
/// <summary>
/// Updates only the agent name for a specific user's agent summary
/// </summary>
/// <param name="userId">The user ID</param>
/// <param name="agentName">The new agent name</param>
Task UpdateAgentNameAsync(int userId, string agentName);
} }

View File

@@ -14,4 +14,11 @@ public interface IAgentService
Task<IEnumerable<AgentSummary>> GetAllAgentSummaries(); Task<IEnumerable<AgentSummary>> GetAllAgentSummaries();
Task<IEnumerable<string>> GetAllOnlineAgents(); Task<IEnumerable<string>> GetAllOnlineAgents();
/// <summary>
/// Updates only the agent name for a specific user's agent summary
/// </summary>
/// <param name="userId">The user ID</param>
/// <param name="agentName">The new agent name</param>
Task UpdateAgentSummaryNameAsync(int userId, string agentName);
} }

View File

@@ -17,11 +17,13 @@ namespace Managing.Application.Abstractions.Grains
/// Updates only the agent name without recalculating summary. /// Updates only the agent name without recalculating summary.
/// </summary> /// </summary>
/// <param name="agentName">The new agent name.</param> /// <param name="agentName">The new agent name.</param>
[OneWay]
Task UpdateAgentNameAsync(string agentName); Task UpdateAgentNameAsync(string agentName);
/// <summary> /// <summary>
/// Generates a summary of the agent's stats for the AgentRegistryGrain. /// Generates a summary of the agent's stats for the AgentRegistryGrain.
/// </summary> /// </summary>
[OneWay]
Task UpdateSummary(); Task UpdateSummary();
/// <summary> /// <summary>

View File

@@ -1,6 +1,8 @@
using Managing.Application.Abstractions.Repositories; using Managing.Application.Abstractions.Repositories;
using Managing.Application.Abstractions.Services; using Managing.Application.Abstractions.Services;
using Managing.Core;
using Managing.Domain.Statistics; using Managing.Domain.Statistics;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
namespace Managing.Application.Agents; namespace Managing.Application.Agents;
@@ -9,17 +11,20 @@ public class AgentService : IAgentService
{ {
private readonly IAgentBalanceRepository _agentBalanceRepository; private readonly IAgentBalanceRepository _agentBalanceRepository;
private readonly IAgentSummaryRepository _agentSummaryRepository; private readonly IAgentSummaryRepository _agentSummaryRepository;
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ICacheService _cacheService; private readonly ICacheService _cacheService;
private readonly ILogger<AgentService> _logger; private readonly ILogger<AgentService> _logger;
public AgentService( public AgentService(
IAgentBalanceRepository agentBalanceRepository, IAgentBalanceRepository agentBalanceRepository,
IAgentSummaryRepository agentSummaryRepository, IAgentSummaryRepository agentSummaryRepository,
IServiceScopeFactory serviceScopeFactory,
ICacheService cacheService, ICacheService cacheService,
ILogger<AgentService> logger) ILogger<AgentService> logger)
{ {
_agentBalanceRepository = agentBalanceRepository; _agentBalanceRepository = agentBalanceRepository;
_agentSummaryRepository = agentSummaryRepository; _agentSummaryRepository = agentSummaryRepository;
_serviceScopeFactory = serviceScopeFactory;
_cacheService = cacheService; _cacheService = cacheService;
_logger = logger; _logger = logger;
} }
@@ -96,6 +101,11 @@ public class AgentService : IAgentService
{ {
try try
{ {
if (string.IsNullOrEmpty(agentSummary.AgentName))
{
agentSummary.AgentName = await ServiceScopeHelpers.WithScopedService<IUserService, string>(_serviceScopeFactory,
async (userService) => (await userService.GetUserByIdAsync(agentSummary.UserId)).AgentName);
}
// Use the injected AgentSummaryRepository to save or update // Use the injected AgentSummaryRepository to save or update
await _agentSummaryRepository.SaveOrUpdateAsync(agentSummary); await _agentSummaryRepository.SaveOrUpdateAsync(agentSummary);
@@ -120,4 +130,19 @@ public class AgentService : IAgentService
var agentSummaries = await _agentSummaryRepository.GetAllAgentWithRunningBots(); var agentSummaries = await _agentSummaryRepository.GetAllAgentWithRunningBots();
return agentSummaries.Select(a => a.AgentName); 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;
}
}
} }

View File

@@ -85,6 +85,9 @@ public class AgentGrain : Grain, IAgentGrain
{ {
_state.State.AgentName = agentName; _state.State.AgentName = agentName;
await _state.WriteStateAsync(); 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); _logger.LogInformation("Agent {UserId} updated with name {AgentName}", this.GetPrimaryKeyLong(), agentName);
} }

View File

@@ -297,4 +297,25 @@ public class AgentSummaryRepository : IAgentSummaryRepository
return agentSummaries.Select(MapToDomain); 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);
}
}
} }