diff --git a/src/Managing.Api/Controllers/DataController.cs b/src/Managing.Api/Controllers/DataController.cs index 5e5964b..c67e949 100644 --- a/src/Managing.Api/Controllers/DataController.cs +++ b/src/Managing.Api/Controllers/DataController.cs @@ -702,7 +702,7 @@ public class DataController : ControllerBase /// /// An array of online agent names [HttpGet("GetOnlineAgent")] - public async Task>> GetOnlineAgent() + public async Task>> GetOnlineAgent() { const string cacheKey = "OnlineAgentNames"; diff --git a/src/Managing.Application.Abstractions/Repositories/IUserRepository.cs b/src/Managing.Application.Abstractions/Repositories/IUserRepository.cs index 6876d6f..2317325 100644 --- a/src/Managing.Application.Abstractions/Repositories/IUserRepository.cs +++ b/src/Managing.Application.Abstractions/Repositories/IUserRepository.cs @@ -6,6 +6,7 @@ public interface IUserRepository { Task GetUserByAgentNameAsync(string agentName); Task GetUserByNameAsync(string name); + Task> GetAllUsersAsync(); Task InsertUserAsync(User user); Task UpdateUser(User user); } \ No newline at end of file diff --git a/src/Managing.Application.Abstractions/Services/IUserService.cs b/src/Managing.Application.Abstractions/Services/IUserService.cs index 999adf4..520e0be 100644 --- a/src/Managing.Application.Abstractions/Services/IUserService.cs +++ b/src/Managing.Application.Abstractions/Services/IUserService.cs @@ -11,4 +11,5 @@ public interface IUserService Task UpdateTelegramChannel(User user, string telegramChannel); Task GetUserByName(string name); Task GetUserByAgentName(string agentName); + Task> GetAllUsersAsync(); } \ No newline at end of file diff --git a/src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs b/src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs index 8ffb6b8..49ecd8e 100644 --- a/src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs +++ b/src/Managing.Application/ManageBot/Commands/GetOnlineAgentNamesCommand.cs @@ -5,7 +5,7 @@ namespace Managing.Application.ManageBot.Commands /// /// Command to retrieve only online agent names /// - public class GetOnlineAgentNamesCommand : IRequest> + public class GetOnlineAgentNamesCommand : IRequest> { public GetOnlineAgentNamesCommand() { diff --git a/src/Managing.Application/ManageBot/Commands/GetUserStrategiesCommand.cs b/src/Managing.Application/ManageBot/Commands/GetUserStrategiesCommand.cs index 8bf70e3..9fe23d6 100644 --- a/src/Managing.Application/ManageBot/Commands/GetUserStrategiesCommand.cs +++ b/src/Managing.Application/ManageBot/Commands/GetUserStrategiesCommand.cs @@ -6,7 +6,7 @@ namespace Managing.Application.ManageBot.Commands /// /// Command to retrieve all strategies owned by a specific user /// - public class GetUserStrategiesCommand : IRequest> + public class GetUserStrategiesCommand : IRequest> { public string AgentName { get; } diff --git a/src/Managing.Application/ManageBot/GetAllAgentsCommandHandler.cs b/src/Managing.Application/ManageBot/GetAllAgentsCommandHandler.cs new file mode 100644 index 0000000..0f4f876 --- /dev/null +++ b/src/Managing.Application/ManageBot/GetAllAgentsCommandHandler.cs @@ -0,0 +1,69 @@ +using Managing.Application.Abstractions; +using Managing.Application.Abstractions.Services; +using Managing.Application.ManageBot.Commands; +using Managing.Domain.Bots; +using Managing.Domain.Users; +using MediatR; + +namespace Managing.Application.ManageBot +{ + /// + /// Handler for retrieving all agents and their strategies + /// + public class GetAllAgentsCommandHandler : IRequestHandler>> + { + private readonly IBotService _botService; + private readonly IUserService _userService; + + public GetAllAgentsCommandHandler(IBotService botService, IUserService userService) + { + _botService = botService; + _userService = userService; + } + + public async Task>> Handle(GetAllAgentsCommand request, CancellationToken cancellationToken) + { + // Get all users + var allUsers = await _userService.GetAllUsersAsync(); + + // Create result dictionary + var result = new Dictionary>(); + + // For each user, get their bots + foreach (var user in allUsers) + { + var userBots = await _botService.GetBotsByUser(user.Id); + var botList = userBots.ToList(); + + // Apply time filter if specified + if (request.TimeFilter != "Total") + { + var cutoffDate = GetCutoffDate(request.TimeFilter); + botList = botList.Where(bot => + bot.StartupTime >= cutoffDate || + bot.CreateDate >= cutoffDate).ToList(); + } + + result[user] = botList; + } + + return result; + } + + /// + /// Gets the cutoff date based on the time filter + /// + private DateTime GetCutoffDate(string timeFilter) + { + return timeFilter switch + { + "24H" => DateTime.UtcNow.AddHours(-24), + "3D" => DateTime.UtcNow.AddDays(-3), + "1W" => DateTime.UtcNow.AddDays(-7), + "1M" => DateTime.UtcNow.AddMonths(-1), + "1Y" => DateTime.UtcNow.AddYears(-1), + _ => DateTime.MinValue // Default to include all data + }; + } + } +} \ No newline at end of file diff --git a/src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs b/src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs index 33e1c06..4664e1c 100644 --- a/src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs +++ b/src/Managing.Application/ManageBot/GetOnlineAgentNamesCommandHandler.cs @@ -7,7 +7,7 @@ namespace Managing.Application.ManageBot /// /// Handler for retrieving only online agent names /// - public class GetOnlineAgentNamesCommandHandler : IRequestHandler> + public class GetOnlineAgentNamesCommandHandler : IRequestHandler> { private readonly IBotService _botService; @@ -16,11 +16,11 @@ namespace Managing.Application.ManageBot _botService = botService; } - public async Task> Handle(GetOnlineAgentNamesCommand request, + public async Task> Handle(GetOnlineAgentNamesCommand request, CancellationToken cancellationToken) { var activeBots = await _botService.GetActiveBotsNamesAsync(); - return activeBots.ToList(); + return activeBots; } } } \ No newline at end of file diff --git a/src/Managing.Application/Users/UserService.cs b/src/Managing.Application/Users/UserService.cs index 8e2f17b..c9a6ca8 100644 --- a/src/Managing.Application/Users/UserService.cs +++ b/src/Managing.Application/Users/UserService.cs @@ -247,6 +247,11 @@ public class UserService : IUserService return user; } + public async Task> GetAllUsersAsync() + { + return await _userRepository.GetAllUsersAsync(); + } + public async Task SaveOrUpdateAgentSummary(AgentSummary agentSummary) { try diff --git a/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlUserRepository.cs b/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlUserRepository.cs index 204a3c6..1dd54b4 100644 --- a/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlUserRepository.cs +++ b/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlUserRepository.cs @@ -86,6 +86,27 @@ public class PostgreSqlUserRepository : IUserRepository await _context.SaveChangesAsync().ConfigureAwait(false); } + public async Task> GetAllUsersAsync() + { + try + { + await EnsureConnectionOpenAsync(); + + var userEntities = await _context.Users + .AsNoTracking() + .ToListAsync() + .ConfigureAwait(false); + + return userEntities.Select(PostgreSqlMappers.Map); + } + catch (Exception) + { + // If there's an error, try to reset the connection + await SafeCloseConnectionAsync(); + throw; + } + } + public async Task UpdateUser(User user) { try