Add new endpoint for the agent status
This commit is contained in:
@@ -902,6 +902,32 @@ public class DataController : ControllerBase
|
|||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieves an array of agent names and their statuses
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>An array of agent status information</returns>
|
||||||
|
[HttpGet("GetAgentStatuses")]
|
||||||
|
public async Task<ActionResult<List<AgentStatusResponse>>> GetAgentStatuses()
|
||||||
|
{
|
||||||
|
const string cacheKey = "AgentStatuses";
|
||||||
|
|
||||||
|
// Check if the agent statuses are already cached
|
||||||
|
var cachedStatuses = _cacheService.GetValue<List<AgentStatusResponse>>(cacheKey);
|
||||||
|
|
||||||
|
if (cachedStatuses != null)
|
||||||
|
{
|
||||||
|
return Ok(cachedStatuses);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all agent statuses
|
||||||
|
var agentStatuses = await _mediator.Send(new GetAgentStatusesCommand());
|
||||||
|
|
||||||
|
// Cache the results for 2 minutes
|
||||||
|
_cacheService.SaveValue(cacheKey, agentStatuses, TimeSpan.FromMinutes(2));
|
||||||
|
|
||||||
|
return Ok(agentStatuses);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Maps a ScenarioRequest to a domain Scenario object.
|
/// Maps a ScenarioRequest to a domain Scenario object.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using MediatR;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
|
namespace Managing.Application.ManageBot.Commands
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Command to retrieve all agent statuses
|
||||||
|
/// </summary>
|
||||||
|
public class GetAgentStatusesCommand : IRequest<List<AgentStatusResponse>>
|
||||||
|
{
|
||||||
|
public GetAgentStatusesCommand()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Response model for agent status information
|
||||||
|
/// </summary>
|
||||||
|
public class AgentStatusResponse
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The name of the agent
|
||||||
|
/// </summary>
|
||||||
|
public string AgentName { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The status of the agent (Online if at least one strategy is running, Offline otherwise)
|
||||||
|
/// </summary>
|
||||||
|
public AgentStatus Status { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
using Managing.Application.Abstractions;
|
||||||
|
using Managing.Application.Abstractions.Services;
|
||||||
|
using Managing.Application.ManageBot.Commands;
|
||||||
|
using MediatR;
|
||||||
|
using static Managing.Common.Enums;
|
||||||
|
|
||||||
|
namespace Managing.Application.ManageBot
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Handler for retrieving all agent statuses
|
||||||
|
/// </summary>
|
||||||
|
public class GetAgentStatusesCommandHandler : IRequestHandler<GetAgentStatusesCommand, List<AgentStatusResponse>>
|
||||||
|
{
|
||||||
|
private readonly IBotService _botService;
|
||||||
|
private readonly IAccountService _accountService;
|
||||||
|
|
||||||
|
public GetAgentStatusesCommandHandler(IBotService botService, IAccountService accountService)
|
||||||
|
{
|
||||||
|
_botService = botService;
|
||||||
|
_accountService = accountService;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<List<AgentStatusResponse>> Handle(GetAgentStatusesCommand request,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
var result = new List<AgentStatusResponse>();
|
||||||
|
var allActiveBots = _botService.GetActiveBots();
|
||||||
|
|
||||||
|
// Group bots by user and determine status
|
||||||
|
var agentGroups = allActiveBots
|
||||||
|
.Where(bot => bot.User != null)
|
||||||
|
.GroupBy(bot => bot.User)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
foreach (var agentGroup in agentGroups)
|
||||||
|
{
|
||||||
|
var user = agentGroup.Key;
|
||||||
|
var bots = agentGroup.ToList();
|
||||||
|
|
||||||
|
// Determine agent status: Online if at least one strategy is running, Offline otherwise
|
||||||
|
var agentStatus = bots.Any(bot => bot.GetStatus() == BotStatus.Up.ToString()) ? AgentStatus.Online : AgentStatus.Offline;
|
||||||
|
|
||||||
|
result.Add(new AgentStatusResponse
|
||||||
|
{
|
||||||
|
AgentName = user.AgentName,
|
||||||
|
Status = agentStatus
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -464,4 +464,13 @@ public static class Enums
|
|||||||
Twors,
|
Twors,
|
||||||
Uniform
|
Uniform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Agent status for indicating if an agent is active or not
|
||||||
|
/// </summary>
|
||||||
|
public enum AgentStatus
|
||||||
|
{
|
||||||
|
Offline,
|
||||||
|
Online
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -2059,6 +2059,41 @@ export class DataClient extends AuthorizedApiBase {
|
|||||||
}
|
}
|
||||||
return Promise.resolve<BestAgentsResponse>(null as any);
|
return Promise.resolve<BestAgentsResponse>(null as any);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data_GetAgentStatuses(): Promise<AgentStatusResponse[]> {
|
||||||
|
let url_ = this.baseUrl + "/Data/GetAgentStatuses";
|
||||||
|
url_ = url_.replace(/[?&]$/, "");
|
||||||
|
|
||||||
|
let options_: RequestInit = {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
"Accept": "application/json"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return this.transformOptions(options_).then(transformedOptions_ => {
|
||||||
|
return this.http.fetch(url_, transformedOptions_);
|
||||||
|
}).then((_response: Response) => {
|
||||||
|
return this.processData_GetAgentStatuses(_response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected processData_GetAgentStatuses(response: Response): Promise<AgentStatusResponse[]> {
|
||||||
|
const status = response.status;
|
||||||
|
let _headers: any = {}; if (response.headers && response.headers.forEach) { response.headers.forEach((v: any, k: any) => _headers[k] = v); };
|
||||||
|
if (status === 200) {
|
||||||
|
return response.text().then((_responseText) => {
|
||||||
|
let result200: any = null;
|
||||||
|
result200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver) as AgentStatusResponse[];
|
||||||
|
return result200;
|
||||||
|
});
|
||||||
|
} else if (status !== 200 && status !== 204) {
|
||||||
|
return response.text().then((_responseText) => {
|
||||||
|
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return Promise.resolve<AgentStatusResponse[]>(null as any);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MoneyManagementClient extends AuthorizedApiBase {
|
export class MoneyManagementClient extends AuthorizedApiBase {
|
||||||
@@ -4498,6 +4533,16 @@ export interface BestAgentsResponse {
|
|||||||
totalPages?: number;
|
totalPages?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AgentStatusResponse {
|
||||||
|
agentName?: string | null;
|
||||||
|
status?: AgentStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum AgentStatus {
|
||||||
|
Offline = "Offline",
|
||||||
|
Online = "Online",
|
||||||
|
}
|
||||||
|
|
||||||
export interface ScenarioViewModel {
|
export interface ScenarioViewModel {
|
||||||
name: string;
|
name: string;
|
||||||
indicators: IndicatorViewModel[];
|
indicators: IndicatorViewModel[];
|
||||||
|
|||||||
@@ -1023,6 +1023,16 @@ export interface BestAgentsResponse {
|
|||||||
totalPages?: number;
|
totalPages?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface AgentStatusResponse {
|
||||||
|
agentName?: string | null;
|
||||||
|
status?: AgentStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum AgentStatus {
|
||||||
|
Offline = "Offline",
|
||||||
|
Online = "Online",
|
||||||
|
}
|
||||||
|
|
||||||
export interface ScenarioViewModel {
|
export interface ScenarioViewModel {
|
||||||
name: string;
|
name: string;
|
||||||
indicators: IndicatorViewModel[];
|
indicators: IndicatorViewModel[];
|
||||||
|
|||||||
Reference in New Issue
Block a user