Add new endpoint for the agent status
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
using MediatR;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.ManageBot.Commands
|
||||
{
|
||||
/// <summary>
|
||||
/// Command to retrieve all agent statuses
|
||||
/// </summary>
|
||||
public class GetAgentStatusesCommand : IRequest<List<AgentStatusResponse>>
|
||||
{
|
||||
public GetAgentStatusesCommand()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Response model for agent status information
|
||||
/// </summary>
|
||||
public class AgentStatusResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the agent
|
||||
/// </summary>
|
||||
public string AgentName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The status of the agent (Online if at least one strategy is running, Offline otherwise)
|
||||
/// </summary>
|
||||
public AgentStatus Status { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
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 all agent statuses
|
||||
/// </summary>
|
||||
public class GetAgentStatusesCommandHandler : IRequestHandler<GetAgentStatusesCommand, List<AgentStatusResponse>>
|
||||
{
|
||||
private readonly IBotService _botService;
|
||||
private readonly IAccountService _accountService;
|
||||
|
||||
public GetAgentStatusesCommandHandler(IBotService botService, IAccountService accountService)
|
||||
{
|
||||
_botService = botService;
|
||||
_accountService = accountService;
|
||||
}
|
||||
|
||||
public Task<List<AgentStatusResponse>> Handle(GetAgentStatusesCommand request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var result = new List<AgentStatusResponse>();
|
||||
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();
|
||||
|
||||
// Determine agent status: Online if at least one strategy is running, Offline otherwise
|
||||
var agentStatus = bots.Any(bot => bot.GetStatus() == BotStatus.Up.ToString()) ? AgentStatus.Online : AgentStatus.Offline;
|
||||
|
||||
result.Add(new AgentStatusResponse
|
||||
{
|
||||
AgentName = user.AgentName,
|
||||
Status = agentStatus
|
||||
});
|
||||
}
|
||||
|
||||
return Task.FromResult(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user