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

@@ -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);
}
}
}