From 6cd28a4edba354b20a5ca47df14321e140ded264 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Thu, 31 Jul 2025 15:55:03 +0700 Subject: [PATCH] Return only online agent name --- .../Controllers/DataController.cs | 24 ++++----- .../Commands/GetOnlineAgentNamesCommand.cs | 14 +++++ .../GetOnlineAgentNamesCommandHandler.cs | 52 +++++++++++++++++++ .../src/pages/dashboardPage/agentIndex.tsx | 2 +- 4 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs create mode 100644 src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs diff --git a/src/Managing.Api/Controllers/DataController.cs b/src/Managing.Api/Controllers/DataController.cs index 40e7eb0..d6d78a7 100644 --- a/src/Managing.Api/Controllers/DataController.cs +++ b/src/Managing.Api/Controllers/DataController.cs @@ -903,29 +903,29 @@ public class DataController : ControllerBase } /// - /// Retrieves an array of agent names and their statuses + /// Retrieves an array of online agent names /// - /// An array of agent status information + /// An array of online agent names [HttpGet("GetAgentStatuses")] - public async Task>> GetAgentStatuses() + public async Task>> GetAgentStatuses() { - const string cacheKey = "AgentStatuses"; + const string cacheKey = "OnlineAgentNames"; - // Check if the agent statuses are already cached - var cachedStatuses = _cacheService.GetValue>(cacheKey); + // Check if the online agent names are already cached + var cachedAgentNames = _cacheService.GetValue>(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); } /// diff --git a/src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs b/src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs new file mode 100644 index 0000000..8ffb6b8 --- /dev/null +++ b/src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs @@ -0,0 +1,14 @@ +using MediatR; + +namespace Managing.Application.ManageBot.Commands +{ + /// + /// Command to retrieve only online agent names + /// + public class GetOnlineAgentNamesCommand : IRequest> + { + public GetOnlineAgentNamesCommand() + { + } + } +} \ No newline at end of file diff --git a/src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs b/src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs new file mode 100644 index 0000000..b3ecbd4 --- /dev/null +++ b/src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs @@ -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 +{ + /// + /// Handler for retrieving only online agent names + /// + public class GetOnlineAgentNamesCommandHandler : IRequestHandler> + { + private readonly IBotService _botService; + private readonly IAccountService _accountService; + + public GetOnlineAgentNamesCommandHandler(IBotService botService, IAccountService accountService) + { + _botService = botService; + _accountService = accountService; + } + + public Task> Handle(GetOnlineAgentNamesCommand request, + CancellationToken cancellationToken) + { + var onlineAgentNames = new List(); + 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); + } + } +} \ No newline at end of file diff --git a/src/Managing.WebApp/src/pages/dashboardPage/agentIndex.tsx b/src/Managing.WebApp/src/pages/dashboardPage/agentIndex.tsx index 1d61b8c..d2f4f12 100644 --- a/src/Managing.WebApp/src/pages/dashboardPage/agentIndex.tsx +++ b/src/Managing.WebApp/src/pages/dashboardPage/agentIndex.tsx @@ -310,7 +310,7 @@ function AgentIndex({ index }: { index: number }) {