diff --git a/src/Managing.Api/Controllers/DataController.cs b/src/Managing.Api/Controllers/DataController.cs index f7b80db..a425c17 100644 --- a/src/Managing.Api/Controllers/DataController.cs +++ b/src/Managing.Api/Controllers/DataController.cs @@ -493,10 +493,10 @@ public class DataController : ControllerBase } /// - /// Retrieves a summary of platform activity across all agents + /// Retrieves a summary of platform activity across all agents (platform-level data only) /// /// Time filter to apply (24H, 3D, 1W, 1M, 1Y, Total) - /// A summary of platform activity including per-agent statistics + /// A summary of platform activity without individual agent details [HttpGet("GetPlatformSummary")] public async Task> GetPlatformSummary(string timeFilter = "Total") { @@ -533,6 +533,75 @@ public class DataController : ControllerBase decimal totalPlatformVolume = 0; decimal totalPlatformVolumeLast24h = 0; + // Calculate totals from all agents + foreach (var agent in agentsWithStrategies) + { + var strategies = agent.Value; + + if (strategies.Count == 0) + { + continue; // Skip agents with no strategies + } + + // Combine all positions from all strategies + var allPositions = strategies.SelectMany(s => s.Positions).ToList(); + + // Calculate agent metrics for platform totals + decimal totalPnL = TradingBox.GetPnLInTimeRange(allPositions, timeFilter); + decimal totalVolume = TradingBox.GetTotalVolumeTraded(allPositions); + decimal volumeLast24h = TradingBox.GetLast24HVolumeTraded(allPositions); + + // Add to platform totals + totalPlatformPnL += totalPnL; + totalPlatformVolume += totalVolume; + totalPlatformVolumeLast24h += volumeLast24h; + } + + // Set the platform totals + summary.TotalPlatformPnL = totalPlatformPnL; + summary.TotalPlatformVolume = totalPlatformVolume; + summary.TotalPlatformVolumeLast24h = totalPlatformVolumeLast24h; + + // Cache the results for 5 minutes + _cacheService.SaveValue(cacheKey, summary, TimeSpan.FromMinutes(5)); + + return Ok(summary); + } + + /// + /// Retrieves a list of agent summaries for the agent index page + /// + /// Time filter to apply (24H, 3D, 1W, 1M, 1Y, Total) + /// A list of agent summaries sorted by performance + [HttpGet("GetAgentIndex")] + public async Task> GetAgentIndex(string timeFilter = "Total") + { + // Validate time filter + var validTimeFilters = new[] { "24H", "3D", "1W", "1M", "1Y", "Total" }; + if (!validTimeFilters.Contains(timeFilter)) + { + timeFilter = "Total"; // Default to Total if invalid + } + + string cacheKey = $"AgentIndex_{timeFilter}"; + + // Check if the agent index is already cached + var cachedIndex = _cacheService.GetValue(cacheKey); + + if (cachedIndex != null) + { + return Ok(cachedIndex); + } + + // Get all agents and their strategies + var agentsWithStrategies = await _mediator.Send(new GetAllAgentsCommand(timeFilter)); + + // Create the agent index response + var agentIndex = new AgentIndexViewModel + { + TimeFilter = timeFilter + }; + // Create summaries for each agent foreach (var agent in agentsWithStrategies) { @@ -583,26 +652,16 @@ public class DataController : ControllerBase VolumeLast24h = volumeLast24h }; - summary.AgentSummaries.Add(agentSummary); - - // Add to platform totals - totalPlatformPnL += totalPnL; - totalPlatformVolume += totalVolume; - totalPlatformVolumeLast24h += volumeLast24h; + agentIndex.AgentSummaries.Add(agentSummary); } - // Set the platform totals - summary.TotalPlatformPnL = totalPlatformPnL; - summary.TotalPlatformVolume = totalPlatformVolume; - summary.TotalPlatformVolumeLast24h = totalPlatformVolumeLast24h; - // Sort agent summaries by total PnL (highest first) - summary.AgentSummaries = summary.AgentSummaries.OrderByDescending(a => a.TotalPnL).ToList(); + agentIndex.AgentSummaries = agentIndex.AgentSummaries.OrderByDescending(a => a.TotalPnL).ToList(); // Cache the results for 5 minutes - _cacheService.SaveValue(cacheKey, summary, TimeSpan.FromMinutes(5)); + _cacheService.SaveValue(cacheKey, agentIndex, TimeSpan.FromMinutes(5)); - return Ok(summary); + return Ok(agentIndex); } /// diff --git a/src/Managing.Api/Models/Responses/AgentSummaryViewModel.cs b/src/Managing.Api/Models/Responses/AgentSummaryViewModel.cs index f40c09c..9e36f42 100644 --- a/src/Managing.Api/Models/Responses/AgentSummaryViewModel.cs +++ b/src/Managing.Api/Models/Responses/AgentSummaryViewModel.cs @@ -62,7 +62,7 @@ namespace Managing.Api.Models.Responses } /// - /// Platform-wide statistics including per-agent summaries + /// Platform-wide statistics without individual agent details /// public class PlatformSummaryViewModel { @@ -92,7 +92,18 @@ namespace Managing.Api.Models.Responses public decimal TotalPlatformVolumeLast24h { get; set; } /// - /// Summaries for each agent + /// Time filter applied to the data + /// + public string TimeFilter { get; set; } = "Total"; + } + + /// + /// Response model containing a list of agent summaries for the agent index + /// + public class AgentIndexViewModel + { + /// + /// List of agent summaries sorted by performance /// public List AgentSummaries { get; set; } = new List();