Return only online agent name
This commit is contained in:
@@ -903,29 +903,29 @@ public class DataController : ControllerBase
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves an array of agent names and their statuses
|
||||
/// Retrieves an array of online agent names
|
||||
/// </summary>
|
||||
/// <returns>An array of agent status information</returns>
|
||||
/// <returns>An array of online agent names</returns>
|
||||
[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
|
||||
var cachedStatuses = _cacheService.GetValue<List<AgentStatusResponse>>(cacheKey);
|
||||
// Check if the online agent names are already cached
|
||||
var cachedAgentNames = _cacheService.GetValue<List<string>>(cacheKey);
|
||||
|
||||
if (cachedStatuses != null)
|
||||
if (cachedAgentNames != null)
|
||||
{
|
||||
return Ok(cachedStatuses);
|
||||
return Ok(cachedAgentNames);
|
||||
}
|
||||
|
||||
// Get all agent statuses
|
||||
var agentStatuses = await _mediator.Send(new GetAgentStatusesCommand());
|
||||
// Get only online agent names
|
||||
var onlineAgentNames = await _mediator.Send(new GetOnlineAgentNamesCommand());
|
||||
|
||||
// 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>
|
||||
|
||||
@@ -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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -310,7 +310,7 @@ function AgentIndex({ index }: { index: number }) {
|
||||
</button>
|
||||
<button
|
||||
className="btn btn-sm"
|
||||
onClick={() => setCurrentPage(data.totalPages)}
|
||||
onClick={() => setCurrentPage(data.totalPages || 1)}
|
||||
disabled={currentPage === data.totalPages}
|
||||
>
|
||||
{'>>'}
|
||||
|
||||
Reference in New Issue
Block a user