Return only online agent name

This commit is contained in:
2025-07-31 15:55:03 +07:00
parent c454e87d7a
commit 6cd28a4edb
4 changed files with 79 additions and 13 deletions

View File

@@ -903,29 +903,29 @@ public class DataController : ControllerBase
} }
/// <summary> /// <summary>
/// Retrieves an array of agent names and their statuses /// Retrieves an array of online agent names
/// </summary> /// </summary>
/// <returns>An array of agent status information</returns> /// <returns>An array of online agent names</returns>
[HttpGet("GetAgentStatuses")] [HttpGet("GetAgentStatuses")]
public async Task<ActionResult<List<AgentStatusResponse>>> GetAgentStatuses() public async Task<ActionResult<List<string>>> GetAgentStatuses()
{ {
const string cacheKey = "AgentStatuses"; const string cacheKey = "OnlineAgentNames";
// Check if the agent statuses are already cached // Check if the online agent names are already cached
var cachedStatuses = _cacheService.GetValue<List<AgentStatusResponse>>(cacheKey); var cachedAgentNames = _cacheService.GetValue<List<string>>(cacheKey);
if (cachedStatuses != null) if (cachedAgentNames != null)
{ {
return Ok(cachedStatuses); return Ok(cachedAgentNames);
} }
// Get all agent statuses // Get only online agent names
var agentStatuses = await _mediator.Send(new GetAgentStatusesCommand()); var onlineAgentNames = await _mediator.Send(new GetOnlineAgentNamesCommand());
// Cache the results for 2 minutes // Cache the results for 2 minutes
_cacheService.SaveValue(cacheKey, agentStatuses, TimeSpan.FromMinutes(2)); _cacheService.SaveValue(cacheKey, onlineAgentNames, TimeSpan.FromMinutes(2));
return Ok(agentStatuses); return Ok(onlineAgentNames);
} }
/// <summary> /// <summary>

View File

@@ -0,0 +1,14 @@
using MediatR;
namespace Managing.Application.ManageBot.Commands
{
/// <summary>
/// Command to retrieve only online agent names
/// </summary>
public class GetOnlineAgentNamesCommand : IRequest<List<string>>
{
public GetOnlineAgentNamesCommand()
{
}
}
}

View File

@@ -0,0 +1,52 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Services;
using Managing.Application.ManageBot.Commands;
using MediatR;
using static Managing.Common.Enums;
namespace Managing.Application.ManageBot
{
/// <summary>
/// Handler for retrieving only online agent names
/// </summary>
public class GetOnlineAgentNamesCommandHandler : IRequestHandler<GetOnlineAgentNamesCommand, List<string>>
{
private readonly IBotService _botService;
private readonly IAccountService _accountService;
public GetOnlineAgentNamesCommandHandler(IBotService botService, IAccountService accountService)
{
_botService = botService;
_accountService = accountService;
}
public Task<List<string>> Handle(GetOnlineAgentNamesCommand request,
CancellationToken cancellationToken)
{
var onlineAgentNames = new List<string>();
var allActiveBots = _botService.GetActiveBots();
// Group bots by user and determine status
var agentGroups = allActiveBots
.Where(bot => bot.User != null)
.GroupBy(bot => bot.User)
.ToList();
foreach (var agentGroup in agentGroups)
{
var user = agentGroup.Key;
var bots = agentGroup.ToList();
// Only include agents that have at least one strategy running (Online status)
var isOnline = bots.Any(bot => bot.GetStatus() == BotStatus.Up.ToString());
if (isOnline)
{
onlineAgentNames.Add(user.AgentName);
}
}
return Task.FromResult(onlineAgentNames);
}
}
}

View File

@@ -310,7 +310,7 @@ function AgentIndex({ index }: { index: number }) {
</button> </button>
<button <button
className="btn btn-sm" className="btn btn-sm"
onClick={() => setCurrentPage(data.totalPages)} onClick={() => setCurrentPage(data.totalPages || 1)}
disabled={currentPage === data.totalPages} disabled={currentPage === data.totalPages}
> >
{'>>'} {'>>'}