Add signalr
This commit is contained in:
@@ -27,7 +27,7 @@ namespace Managing.Api.Controllers;
|
||||
[Produces("application/json")]
|
||||
public class BacktestController : BaseController
|
||||
{
|
||||
private readonly IHubContext<BotHub> _hubContext;
|
||||
private readonly IHubContext<BacktestHub> _hubContext;
|
||||
private readonly IBacktester _backtester;
|
||||
private readonly IScenarioService _scenarioService;
|
||||
private readonly IAccountService _accountService;
|
||||
@@ -45,7 +45,7 @@ public class BacktestController : BaseController
|
||||
/// <param name="geneticService">The service for genetic algorithm operations.</param>
|
||||
/// <param name="backtestRepository">The repository for backtest operations.</param>
|
||||
public BacktestController(
|
||||
IHubContext<BotHub> hubContext,
|
||||
IHubContext<BacktestHub> hubContext,
|
||||
IBacktester backtester,
|
||||
IScenarioService scenarioService,
|
||||
IAccountService accountService,
|
||||
@@ -537,6 +537,47 @@ public class BacktestController : BaseController
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Subscribes the client to real-time updates for a bundle backtest request via SignalR.
|
||||
/// The client will receive LightBacktestResponse objects as new backtests are generated.
|
||||
/// </summary>
|
||||
/// <param name="requestId">The bundle request ID to subscribe to.</param>
|
||||
[HttpPost]
|
||||
[Route("Bundle/Subscribe")] // POST /Backtest/Bundle/Subscribe
|
||||
public async Task<IActionResult> SubscribeToBundle([FromQuery] string requestId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(requestId))
|
||||
return BadRequest("RequestId is required");
|
||||
|
||||
// Get the connection ID from the SignalR context (assume it's passed via header or query)
|
||||
var connectionId = HttpContext.Request.Headers["X-SignalR-ConnectionId"].ToString();
|
||||
if (string.IsNullOrEmpty(connectionId))
|
||||
return BadRequest("SignalR connection ID is required in X-SignalR-ConnectionId header");
|
||||
|
||||
// Add the connection to the SignalR group for this bundle
|
||||
await _hubContext.Groups.AddToGroupAsync(connectionId, $"bundle-{requestId}");
|
||||
return Ok(new { Subscribed = true, RequestId = requestId });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unsubscribes the client from real-time updates for a bundle backtest request via SignalR.
|
||||
/// </summary>
|
||||
/// <param name="requestId">The bundle request ID to unsubscribe from.</param>
|
||||
[HttpPost]
|
||||
[Route("Bundle/Unsubscribe")] // POST /Backtest/Bundle/Unsubscribe
|
||||
public async Task<IActionResult> UnsubscribeFromBundle([FromQuery] string requestId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(requestId))
|
||||
return BadRequest("RequestId is required");
|
||||
|
||||
var connectionId = HttpContext.Request.Headers["X-SignalR-ConnectionId"].ToString();
|
||||
if (string.IsNullOrEmpty(connectionId))
|
||||
return BadRequest("SignalR connection ID is required in X-SignalR-ConnectionId header");
|
||||
|
||||
await _hubContext.Groups.RemoveFromGroupAsync(connectionId, $"bundle-{requestId}");
|
||||
return Ok(new { Unsubscribed = true, RequestId = requestId });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Runs a genetic algorithm optimization with the specified configuration.
|
||||
/// This endpoint saves the genetic request to the database and returns the request ID.
|
||||
|
||||
Reference in New Issue
Block a user