From 2a354bd7d21308f07884b5786fe5f7dbb3c362ee Mon Sep 17 00:00:00 2001 From: cryptooda Date: Sat, 22 Nov 2025 14:02:29 +0700 Subject: [PATCH] Implement profitable bots filtering in BotController and DataController - Added IConfiguration dependency to BotController for accessing environment variables. - Updated GetBotsPaginatedAsync method in BotService and IBotService to include a flag for filtering profitable bots. - Modified DataController to utilize the new filtering option for agent summaries and bot retrieval. - Enhanced PostgreSqlBotRepository to apply filtering based on profitability when querying bots. --- src/Managing.Api/Controllers/BotController.cs | 12 ++++++++++-- .../Controllers/DataController.cs | 10 +++++++--- .../Repositories/IBotRepository.cs | 19 ++++++++++--------- .../Abstractions/IBotService.cs | 4 +++- .../ManageBot/BotService.cs | 6 ++++-- .../PostgreSql/PostgreSqlBotRepository.cs | 9 ++++++++- 6 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/Managing.Api/Controllers/BotController.cs b/src/Managing.Api/Controllers/BotController.cs index 753191e9..f9d0256f 100644 --- a/src/Managing.Api/Controllers/BotController.cs +++ b/src/Managing.Api/Controllers/BotController.cs @@ -20,6 +20,7 @@ using MediatR; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Configuration; using static Managing.Common.Enums; namespace Managing.Api.Controllers; @@ -43,6 +44,7 @@ public class BotController : BaseController private readonly IMoneyManagementService _moneyManagementService; private readonly IServiceScopeFactory _scopeFactory; private readonly IAdminConfigurationService _adminService; + private readonly IConfiguration _configuration; /// /// Initializes a new instance of the class. @@ -56,10 +58,11 @@ public class BotController : BaseController /// /// /// + /// Configuration for accessing environment variables. public BotController(ILogger logger, IMediator mediator, IHubContext hubContext, IBacktester backtester, IBotService botService, IUserService userService, IAccountService accountService, IMoneyManagementService moneyManagementService, - IServiceScopeFactory scopeFactory, IAdminConfigurationService adminService) : base(userService) + IServiceScopeFactory scopeFactory, IAdminConfigurationService adminService, IConfiguration configuration) : base(userService) { _logger = logger; _mediator = mediator; @@ -70,6 +73,7 @@ public class BotController : BaseController _moneyManagementService = moneyManagementService; _scopeFactory = scopeFactory; _adminService = adminService; + _configuration = configuration; } /// @@ -431,6 +435,9 @@ public class BotController : BaseController pageSize = Math.Min(Math.Max(pageSize, 1), 100); } + // Check environment variable for filtering profitable bots only + var showOnlyProfitable = _configuration.GetValue("showOnlyProfitable", false); + // Get paginated bots from service var (bots, totalCount) = await _botService.GetBotsPaginatedAsync( pageNumber, @@ -440,7 +447,8 @@ public class BotController : BaseController ticker, agentName, sortBy, - sortDirection); + sortDirection, + showOnlyProfitable); // Map to response objects var tradingBotResponses = MapBotsToTradingBotResponse(bots); diff --git a/src/Managing.Api/Controllers/DataController.cs b/src/Managing.Api/Controllers/DataController.cs index ab1d7f46..4d66d7e3 100644 --- a/src/Managing.Api/Controllers/DataController.cs +++ b/src/Managing.Api/Controllers/DataController.cs @@ -711,10 +711,10 @@ public class DataController : ControllerBase } // Check environment variable for filtering profitable agents only - var showOnlyProfitableAgent = _configuration.GetValue("showOnlyProfitableAgent", false); + var showOnlyProfitable = _configuration.GetValue("showOnlyProfitable", false); // Get paginated results from database - var command = new GetPaginatedAgentSummariesCommand(page, pageSize, sortBy, sortOrder, agentNamesList, showOnlyProfitableAgent); + var command = new GetPaginatedAgentSummariesCommand(page, pageSize, sortBy, sortOrder, agentNamesList, showOnlyProfitable); var result = await _mediator.Send(command); var agentSummaries = result.Results; var totalCount = result.TotalCount; @@ -928,6 +928,9 @@ public class DataController : ControllerBase try { + // Check environment variable for filtering profitable strategies only + var showOnlyProfitable = _configuration.GetValue("showOnlyProfitable", false); + // Get paginated bots excluding Saved status var (bots, totalCount) = await _botService.GetBotsPaginatedAsync( pageNumber, @@ -937,7 +940,8 @@ public class DataController : ControllerBase ticker, agentName, sortBy, - sortDirection); + sortDirection, + showOnlyProfitable); // Filter out Saved status bots var filteredBots = bots.Where(bot => bot.Status != BotStatus.Saved).ToList(); diff --git a/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs b/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs index ed81f9a0..54c31134 100644 --- a/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs +++ b/src/Managing.Application.Abstractions/Repositories/IBotRepository.cs @@ -22,22 +22,23 @@ public interface IBotRepository /// Page number (1-based) /// Number of items per page /// Filter by status (optional) - /// Filter by user ID (optional) /// Filter by name (partial match, case-insensitive) /// Filter by ticker (partial match, case-insensitive) /// Filter by agent name (partial match, case-insensitive) /// Sort field /// Sort direction ("Asc" or "Desc") + /// Whether to show only profitable bots (ROI > 0) /// Tuple containing the bots for the current page and total count Task<(IEnumerable Bots, int TotalCount)> GetBotsPaginatedAsync( - int pageNumber, - int pageSize, - BotStatus? status = null, - string? name = null, - string? ticker = null, - string? agentName = null, - BotSortableColumn sortBy = BotSortableColumn.CreateDate, - string sortDirection = "Desc"); + int pageNumber, + int pageSize, + BotStatus? status = null, + string? name = null, + string? ticker = null, + string? agentName = null, + BotSortableColumn sortBy = BotSortableColumn.CreateDate, + string sortDirection = "Desc", + bool showOnlyProfitable = false); /// /// Gets the top performing bots by PnL from the specified statuses diff --git a/src/Managing.Application/Abstractions/IBotService.cs b/src/Managing.Application/Abstractions/IBotService.cs index 9c10217f..92999b2f 100644 --- a/src/Managing.Application/Abstractions/IBotService.cs +++ b/src/Managing.Application/Abstractions/IBotService.cs @@ -48,6 +48,7 @@ public interface IBotService /// Filter by agent name (partial match, case-insensitive) /// Sort field /// Sort direction ("Asc" or "Desc") + /// Whether to show only profitable bots (ROI > 0) /// Tuple containing the bots for the current page and total count Task<(IEnumerable Bots, int TotalCount)> GetBotsPaginatedAsync( int pageNumber, @@ -57,7 +58,8 @@ public interface IBotService string? ticker = null, string? agentName = null, BotSortableColumn sortBy = BotSortableColumn.CreateDate, - string sortDirection = "Desc"); + string sortDirection = "Desc", + bool showOnlyProfitable = false); /// /// Checks USDC and ETH balances for EVM/GMX V2 accounts diff --git a/src/Managing.Application/ManageBot/BotService.cs b/src/Managing.Application/ManageBot/BotService.cs index 17225769..9918c69e 100644 --- a/src/Managing.Application/ManageBot/BotService.cs +++ b/src/Managing.Application/ManageBot/BotService.cs @@ -460,7 +460,8 @@ namespace Managing.Application.ManageBot string? ticker = null, string? agentName = null, BotSortableColumn sortBy = BotSortableColumn.CreateDate, - string sortDirection = "Desc") + string sortDirection = "Desc", + bool showOnlyProfitable = false) { return await ServiceScopeHelpers.WithScopedService Bots, int TotalCount)>( _scopeFactory, @@ -474,7 +475,8 @@ namespace Managing.Application.ManageBot ticker, agentName, sortBy, - sortDirection); + sortDirection, + showOnlyProfitable); }); } diff --git a/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs b/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs index 15e11c8a..a7960d0f 100644 --- a/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs +++ b/src/Managing.Infrastructure.Database/PostgreSql/PostgreSqlBotRepository.cs @@ -204,7 +204,8 @@ public class PostgreSqlBotRepository : IBotRepository string? ticker = null, string? agentName = null, BotSortableColumn sortBy = BotSortableColumn.CreateDate, - string sortDirection = "Desc") + string sortDirection = "Desc", + bool showOnlyProfitable = false) { // Build the query with filters var query = _context.Bots @@ -234,6 +235,12 @@ public class PostgreSqlBotRepository : IBotRepository query = query.Where(b => b.User != null && EF.Functions.ILike(b.User.AgentName, $"%{agentName}%")); } + // Apply profitable bots filtering if specified + if (showOnlyProfitable) + { + query = query.Where(b => b.Roi > 0); + } + // Get total count before applying pagination var totalCount = await query.CountAsync().ConfigureAwait(false);