Add platform grain
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using Managing.Api.Models.Requests;
|
||||
using Managing.Api.Extensions;
|
||||
using Managing.Api.Models.Requests;
|
||||
using Managing.Api.Models.Responses;
|
||||
using Managing.Application.Abstractions.Grains;
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Hubs;
|
||||
using Managing.Application.ManageBot.Commands;
|
||||
@@ -36,6 +38,7 @@ public class DataController : ControllerBase
|
||||
private readonly IHubContext<CandleHub> _hubContext;
|
||||
private readonly IMediator _mediator;
|
||||
private readonly ITradingService _tradingService;
|
||||
private readonly IGrainFactory _grainFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DataController"/> class.
|
||||
@@ -47,6 +50,7 @@ public class DataController : ControllerBase
|
||||
/// <param name="hubContext">SignalR hub context for real-time communication.</param>
|
||||
/// <param name="mediator">Mediator for handling commands and queries.</param>
|
||||
/// <param name="tradingService">Service for trading operations.</param>
|
||||
/// <param name="grainFactory">Orleans grain factory for accessing grains.</param>
|
||||
public DataController(
|
||||
IExchangeService exchangeService,
|
||||
IAccountService accountService,
|
||||
@@ -55,7 +59,8 @@ public class DataController : ControllerBase
|
||||
IAgentService agentService,
|
||||
IHubContext<CandleHub> hubContext,
|
||||
IMediator mediator,
|
||||
ITradingService tradingService)
|
||||
ITradingService tradingService,
|
||||
IGrainFactory grainFactory)
|
||||
{
|
||||
_exchangeService = exchangeService;
|
||||
_accountService = accountService;
|
||||
@@ -65,6 +70,7 @@ public class DataController : ControllerBase
|
||||
_hubContext = hubContext;
|
||||
_mediator = mediator;
|
||||
_tradingService = tradingService;
|
||||
_grainFactory = grainFactory;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -458,68 +464,31 @@ public class DataController : ControllerBase
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves a summary of platform activity across all agents (platform-level data only)
|
||||
/// Uses Orleans grain for efficient caching and real-time updates
|
||||
/// </summary>
|
||||
/// <returns>A summary of platform activity without individual agent details</returns>
|
||||
[HttpGet("GetPlatformSummary")]
|
||||
public async Task<ActionResult<PlatformSummaryViewModel>> GetPlatformSummary()
|
||||
{
|
||||
const string cacheKey = "PlatformSummary";
|
||||
|
||||
// Check if the platform summary is already cached
|
||||
var cachedSummary = _cacheService.GetValue<PlatformSummaryViewModel>(cacheKey);
|
||||
|
||||
if (cachedSummary != null)
|
||||
try
|
||||
{
|
||||
return Ok(cachedSummary);
|
||||
// Get the platform summary grain
|
||||
var platformSummaryGrain = _grainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
|
||||
|
||||
// Get the platform summary from the grain (handles caching and real-time updates)
|
||||
var abstractionsSummary = await platformSummaryGrain.GetPlatformSummaryAsync();
|
||||
|
||||
// Convert to API ViewModel
|
||||
var summary = abstractionsSummary.ToApiViewModel();
|
||||
|
||||
return Ok(summary);
|
||||
}
|
||||
|
||||
// Get all agents and their strategies (without time filter)
|
||||
var agentsWithStrategies = await _mediator.Send(new GetAllAgentsCommand());
|
||||
|
||||
// Create the platform summary
|
||||
var summary = new PlatformSummaryViewModel
|
||||
catch (Exception ex)
|
||||
{
|
||||
TotalAgents = agentsWithStrategies.Count,
|
||||
TotalActiveStrategies = agentsWithStrategies.Values.Sum(list => list.Count)
|
||||
};
|
||||
|
||||
// Calculate total platform metrics
|
||||
decimal totalPlatformPnL = 0;
|
||||
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
|
||||
}
|
||||
|
||||
// TODO: Add this calculation into repository for better performance
|
||||
|
||||
var globalPnL = strategies.Sum(s => s.Pnl);
|
||||
var globalVolume = strategies.Sum(s => s.Volume);
|
||||
var globalVolumeLast24h = strategies.Sum(s => s.Volume);
|
||||
|
||||
// Calculate agent metrics for platform totals
|
||||
// Add to platform totals
|
||||
totalPlatformPnL += globalPnL;
|
||||
totalPlatformVolume += globalVolume;
|
||||
totalPlatformVolumeLast24h += globalVolumeLast24h;
|
||||
// Log the error and return a fallback response
|
||||
// In production, you might want to return cached data or partial data
|
||||
return StatusCode(500, $"Error retrieving platform summary: {ex.Message}");
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user