Fix update AgentName
This commit is contained in:
@@ -28,4 +28,11 @@ public interface IAgentSummaryRepository
|
||||
string sortOrder,
|
||||
IEnumerable<string>? agentNames = null);
|
||||
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);
|
||||
}
|
||||
@@ -14,4 +14,11 @@ public interface IAgentService
|
||||
|
||||
Task<IEnumerable<AgentSummary>> GetAllAgentSummaries();
|
||||
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);
|
||||
}
|
||||
@@ -17,11 +17,13 @@ namespace Managing.Application.Abstractions.Grains
|
||||
/// Updates only the agent name without recalculating summary.
|
||||
/// </summary>
|
||||
/// <param name="agentName">The new agent name.</param>
|
||||
[OneWay]
|
||||
Task UpdateAgentNameAsync(string agentName);
|
||||
|
||||
/// <summary>
|
||||
/// Generates a summary of the agent's stats for the AgentRegistryGrain.
|
||||
/// </summary>
|
||||
[OneWay]
|
||||
Task UpdateSummary();
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -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<AgentService> _logger;
|
||||
|
||||
public AgentService(
|
||||
IAgentBalanceRepository agentBalanceRepository,
|
||||
IAgentSummaryRepository agentSummaryRepository,
|
||||
IServiceScopeFactory serviceScopeFactory,
|
||||
ICacheService cacheService,
|
||||
ILogger<AgentService> 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<IUserService, string>(_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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user