mirror of
https://github.com/JKorf/CryptoExchange.Net
synced 2025-06-07 16:06:15 +00:00
Get the state of ApiClient, SocketConnection, and Subscription as a record (#195)
This commit is contained in:
parent
24c40d2dc6
commit
2dbd5be924
@ -108,5 +108,19 @@ namespace CryptoExchange.Net.Clients
|
|||||||
}
|
}
|
||||||
return result.ToString();
|
return result.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the state of all socket api clients
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public List<SocketApiClient.SocketApiClientState> GetSocketApiClientStates()
|
||||||
|
{
|
||||||
|
var result = new List<SocketApiClient.SocketApiClientState>();
|
||||||
|
foreach (var client in ApiClients.OfType<SocketApiClient>())
|
||||||
|
{
|
||||||
|
result.Add(client.GetState());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -622,32 +622,76 @@ namespace CryptoExchange.Net.Clients
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public string GetSubscriptionsState(bool includeSubDetails = true)
|
public string GetSubscriptionsState(bool includeSubDetails = true)
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
return GetState(includeSubDetails).ToString();
|
||||||
sb.AppendLine($"{GetType().Name}");
|
}
|
||||||
sb.AppendLine($" Connections: {socketConnections.Count}");
|
|
||||||
sb.AppendLine($" Subscriptions: {CurrentSubscriptions}");
|
/// <summary>
|
||||||
sb.AppendLine($" Download speed: {IncomingKbps} kbps");
|
/// Gets the state of the client
|
||||||
foreach (var connection in socketConnections)
|
/// </summary>
|
||||||
|
/// <param name="includeSubDetails">True to get details for each subscription</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public SocketApiClientState GetState(bool includeSubDetails = true)
|
||||||
|
{
|
||||||
|
var connectionStates = new List<SocketConnection.SocketConnectionState>();
|
||||||
|
foreach (var socketIdAndConnection in socketConnections)
|
||||||
{
|
{
|
||||||
sb.AppendLine($" Id: {connection.Key}");
|
SocketConnection connection = socketIdAndConnection.Value;
|
||||||
sb.AppendLine($" Address: {connection.Value.ConnectionUri}");
|
SocketConnection.SocketConnectionState connectionState = connection.GetState(includeSubDetails);
|
||||||
sb.AppendLine($" Subscriptions: {connection.Value.UserSubscriptionCount}");
|
connectionStates.Add(connectionState);
|
||||||
sb.AppendLine($" Status: {connection.Value.Status}");
|
}
|
||||||
sb.AppendLine($" Authenticated: {connection.Value.Authenticated}");
|
|
||||||
sb.AppendLine($" Download speed: {connection.Value.IncomingKbps} kbps");
|
return new SocketApiClientState(socketConnections.Count, CurrentSubscriptions, IncomingKbps, connectionStates);
|
||||||
sb.AppendLine($" Subscriptions:");
|
}
|
||||||
if (includeSubDetails)
|
|
||||||
{
|
/// <summary>
|
||||||
foreach (var subscription in connection.Value.Subscriptions)
|
/// Get the current state of the client
|
||||||
{
|
/// </summary>
|
||||||
sb.AppendLine($" Id: {subscription.Id}");
|
/// <param name="Connections">Number of sockets for this client</param>
|
||||||
sb.AppendLine($" Confirmed: {subscription.Confirmed}");
|
/// <param name="Subscriptions">Total number of subscriptions</param>
|
||||||
sb.AppendLine($" Invocations: {subscription.TotalInvocations}");
|
/// <param name="DownloadSpeed">Total download speed</param>
|
||||||
sb.AppendLine($" Identifiers: [{string.Join(", ", subscription.ListenerIdentifiers)}]");
|
/// <param name="ConnectionStates">State of each socket connection</param>
|
||||||
}
|
public record SocketApiClientState(
|
||||||
}
|
int Connections,
|
||||||
|
int Subscriptions,
|
||||||
|
double DownloadSpeed,
|
||||||
|
List<SocketConnection.SocketConnectionState> ConnectionStates)
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Print the state of the client
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sb"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
protected virtual bool PrintMembers(StringBuilder sb)
|
||||||
|
{
|
||||||
|
sb.AppendLine();
|
||||||
|
sb.AppendLine($"\tTotal connections: {Connections}");
|
||||||
|
sb.AppendLine($"\tTotal subscriptions: {Subscriptions}");
|
||||||
|
sb.AppendLine($"\tDownload speed: {DownloadSpeed} kbps");
|
||||||
|
sb.AppendLine($"\tConnections:");
|
||||||
|
ConnectionStates.ForEach(cs =>
|
||||||
|
{
|
||||||
|
sb.AppendLine($"\t\tId: {cs.Id}");
|
||||||
|
sb.AppendLine($"\t\tAddress: {cs.Address}");
|
||||||
|
sb.AppendLine($"\t\tTotal subscriptions: {cs.Subscriptions}");
|
||||||
|
sb.AppendLine($"\t\tStatus: {cs.Status}");
|
||||||
|
sb.AppendLine($"\t\tAuthenticated: {cs.Authenticated}");
|
||||||
|
sb.AppendLine($"\t\tDownload speed: {cs.DownloadSpeed} kbps");
|
||||||
|
sb.AppendLine($"\t\tPending queries: {cs.PendingQueries}");
|
||||||
|
if (cs.SubscriptionStates?.Count > 0)
|
||||||
|
{
|
||||||
|
sb.AppendLine($"\t\tSubscriptions:");
|
||||||
|
cs.SubscriptionStates.ForEach(subState =>
|
||||||
|
{
|
||||||
|
sb.AppendLine($"\t\t\tId: {subState.Id}");
|
||||||
|
sb.AppendLine($"\t\t\tConfirmed: {subState.Confirmed}");
|
||||||
|
sb.AppendLine($"\t\t\tInvocations: {subState.Invocations}");
|
||||||
|
sb.AppendLine($"\t\t\tIdentifiers: [{string.Join(",", subState.Identifiers)}]");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return sb.ToString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -19,6 +19,28 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class SocketConnection
|
public class SocketConnection
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// State of a the connection
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Id">The id of the socket connection</param>
|
||||||
|
/// <param name="Address">The connection URI</param>
|
||||||
|
/// <param name="Subscriptions">Number of subscriptions on this socket</param>
|
||||||
|
/// <param name="Status">Socket status</param>
|
||||||
|
/// <param name="Authenticated">If the connection is authenticated</param>
|
||||||
|
/// <param name="DownloadSpeed">Download speed over this socket</param>
|
||||||
|
/// <param name="PendingQueries">Number of non-completed queries</param>
|
||||||
|
/// <param name="SubscriptionStates">State for each subscription on this socket</param>
|
||||||
|
public record SocketConnectionState(
|
||||||
|
int Id,
|
||||||
|
string Address,
|
||||||
|
int Subscriptions,
|
||||||
|
SocketStatus Status,
|
||||||
|
bool Authenticated,
|
||||||
|
double DownloadSpeed,
|
||||||
|
int PendingQueries,
|
||||||
|
List<Subscription.SubscriptionState> SubscriptionStates
|
||||||
|
);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Connection lost event
|
/// Connection lost event
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -620,6 +642,24 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
return _listeners.OfType<Subscription>().SingleOrDefault(s => s.Id == id);
|
return _listeners.OfType<Subscription>().SingleOrDefault(s => s.Id == id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the state of the connection
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public SocketConnectionState GetState(bool includeSubDetails)
|
||||||
|
{
|
||||||
|
return new SocketConnectionState(
|
||||||
|
SocketId,
|
||||||
|
ConnectionUri.AbsoluteUri,
|
||||||
|
UserSubscriptionCount,
|
||||||
|
Status,
|
||||||
|
Authenticated,
|
||||||
|
IncomingKbps,
|
||||||
|
PendingQueries: _listeners.OfType<Query>().Count(x => !x.Completed),
|
||||||
|
includeSubDetails ? Subscriptions.Select(sub => sub.GetState()).ToList() : new List<Subscription.SubscriptionState>()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Send a query request and wait for an answer
|
/// Send a query request and wait for an answer
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -156,6 +156,29 @@ namespace CryptoExchange.Net.Sockets
|
|||||||
{
|
{
|
||||||
Exception?.Invoke(e);
|
Exception?.Invoke(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// State of this subscription
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="Id">The id of the subscription</param>
|
||||||
|
/// <param name="Confirmed">True when the subscription query is handled (either accepted or rejected)</param>
|
||||||
|
/// <param name="Invocations">Number of times this subscription got a message</param>
|
||||||
|
/// <param name="Identifiers">Identifiers the subscription is listening to</param>
|
||||||
|
public record SubscriptionState(
|
||||||
|
int Id,
|
||||||
|
bool Confirmed,
|
||||||
|
int Invocations,
|
||||||
|
HashSet<string> Identifiers
|
||||||
|
);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get the state of this subscription
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public SubscriptionState GetState()
|
||||||
|
{
|
||||||
|
return new SubscriptionState(Id, Confirmed, TotalInvocations, ListenerIdentifiers);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user