1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-06-07 07:56:12 +00:00

Get the state of ApiClient, SocketConnection, and Subscription as a record (#195)

This commit is contained in:
Jonnern 2024-04-16 14:37:00 +02:00 committed by GitHub
parent 24c40d2dc6
commit 2dbd5be924
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 145 additions and 24 deletions

View File

@ -108,5 +108,19 @@ namespace CryptoExchange.Net.Clients
}
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;
}
}
}

View File

@ -622,32 +622,76 @@ namespace CryptoExchange.Net.Clients
/// </summary>
public string GetSubscriptionsState(bool includeSubDetails = true)
{
var sb = new StringBuilder();
sb.AppendLine($"{GetType().Name}");
sb.AppendLine($" Connections: {socketConnections.Count}");
sb.AppendLine($" Subscriptions: {CurrentSubscriptions}");
sb.AppendLine($" Download speed: {IncomingKbps} kbps");
foreach (var connection in socketConnections)
return GetState(includeSubDetails).ToString();
}
/// <summary>
/// Gets the state of the client
/// </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}");
sb.AppendLine($" Address: {connection.Value.ConnectionUri}");
sb.AppendLine($" Subscriptions: {connection.Value.UserSubscriptionCount}");
sb.AppendLine($" Status: {connection.Value.Status}");
sb.AppendLine($" Authenticated: {connection.Value.Authenticated}");
sb.AppendLine($" Download speed: {connection.Value.IncomingKbps} kbps");
sb.AppendLine($" Subscriptions:");
if (includeSubDetails)
{
foreach (var subscription in connection.Value.Subscriptions)
{
sb.AppendLine($" Id: {subscription.Id}");
sb.AppendLine($" Confirmed: {subscription.Confirmed}");
sb.AppendLine($" Invocations: {subscription.TotalInvocations}");
sb.AppendLine($" Identifiers: [{string.Join(", ", subscription.ListenerIdentifiers)}]");
}
}
SocketConnection connection = socketIdAndConnection.Value;
SocketConnection.SocketConnectionState connectionState = connection.GetState(includeSubDetails);
connectionStates.Add(connectionState);
}
return new SocketApiClientState(socketConnections.Count, CurrentSubscriptions, IncomingKbps, connectionStates);
}
/// <summary>
/// Get the current state of the client
/// </summary>
/// <param name="Connections">Number of sockets for this client</param>
/// <param name="Subscriptions">Total number of subscriptions</param>
/// <param name="DownloadSpeed">Total download speed</param>
/// <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>

View File

@ -19,6 +19,28 @@ namespace CryptoExchange.Net.Sockets
/// </summary>
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>
/// Connection lost event
/// </summary>
@ -620,6 +642,24 @@ namespace CryptoExchange.Net.Sockets
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>
/// Send a query request and wait for an answer
/// </summary>

View File

@ -156,6 +156,29 @@ namespace CryptoExchange.Net.Sockets
{
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 />