Skip to content

Commit

Permalink
Update metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
PlugFox committed Aug 4, 2023
1 parent 58b36b5 commit 453c432
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
23 changes: 15 additions & 8 deletions lib/src/client/spinify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -727,47 +727,54 @@ base mixin SpinifyMetricsMixin on SpinifyBase, SpinifyStateMixin {
int _connectsTotal = 0, _connectsSuccessful = 0, _disconnects = 0;
DateTime? _lastDisconnectTime, _lastConnectTime;
({int? code, String? reason})? _lastDisconnect;
String? _lastUrl;
late DateTime _initializedAt;

@override
void _initSpinify() {
_initializedAt = DateTime.now().toUtc();
super._initSpinify();
}

@override
Future<void> connect(String url) async {
_lastUrl = url;
_connectsTotal++;
return super.connect(url);
}

@override
void _onConnected(SpinifyState$Connected state) {
_lastConnectTime = DateTime.now().toUtc();
super._onConnected(state);
_connectsSuccessful++;
super._onConnected(state);
}

@override
void _onDisconnected(SpinifyState$Disconnected state) {
_lastDisconnectTime = DateTime.now().toUtc();
super._onDisconnected(state);
_lastDisconnect = (code: state.closeCode, reason: state.closeReason);
_disconnects = 0;
super._onDisconnected(state);
}

/// Get metrics of Spinify client.
@override
SpinifyMetrics get metrics {
final timestamp = DateTime.now().toUtc();
final wsMetrics = _transport.metrics;
return SpinifyMetrics(
timestamp: timestamp,
lastUrl: wsMetrics.lastUrl,
initializedAt: _initializedAt,
lastUrl: _lastUrl,
reconnects: (successful: _connectsSuccessful, total: _connectsTotal),
subscriptions: (
client: _clientSubscriptionManager.count,
server: _serverSubscriptionManager.count,
),
speed: _transport.speed,
state: state,
receivedCount: wsMetrics.receivedCount,
receivedSize: wsMetrics.receivedSize,
transferredCount: wsMetrics.transferredCount,
transferredSize: wsMetrics.transferredSize,
received: _transport.received,
transferred: _transport.transferred,
lastConnectTime: _lastConnectTime,
lastDisconnectTime: _lastDisconnectTime,
disconnects: _disconnects,
Expand Down
45 changes: 23 additions & 22 deletions lib/src/model/metrics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
/// {@macro metrics}
const SpinifyMetrics({
required this.timestamp,
required this.initializedAt,
required this.state,
required this.transferredSize,
required this.receivedSize,
required this.transferred,
required this.received,
required this.reconnects,
required this.subscriptions,
required this.speed,
required this.transferredCount,
required this.receivedCount,
required this.lastUrl,
required this.lastConnectTime,
required this.lastDisconnectTime,
Expand All @@ -42,14 +41,17 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
/// Timestamp of the metrics.
final DateTime timestamp;

/// The time when the client was initialized.
final DateTime initializedAt;

/// The current state of the client.
final SpinifyState state;

/// The total number of bytes sent.
final BigInt transferredSize;
/// The total number of messages & size of bytes sent.
final ({BigInt count, BigInt size}) transferred;

/// The total number of bytes received.
final BigInt receivedSize;
/// The total number of messages & size of bytes received.
final ({BigInt count, BigInt size}) received;

/// The total number of times the connection has been re-established.
final ({int successful, int total}) reconnects;
Expand All @@ -66,12 +68,6 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
/// - max - maximum speed
final ({int min, int avg, int max}) speed;

/// The total number of messages sent.
final BigInt transferredCount;

/// The total number of messages received.
final BigInt receivedCount;

/// The last URL used to connect.
final String? lastUrl;

Expand All @@ -96,7 +92,11 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
/// Convert metrics to JSON.
Map<String, Object?> toJson() => <String, Object?>{
'timestamp': timestamp.toIso8601String(),
'initializedAt': initializedAt.toIso8601String(),
'lastConnectTime': lastConnectTime?.toIso8601String(),
'lastDisconnectTime': lastDisconnectTime?.toIso8601String(),
'state': state.toJson(),
'lastUrl': lastUrl,
'reconnects': <String, int>{
'successful': reconnects.successful,
'total': reconnects.total,
Expand All @@ -120,13 +120,15 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
'avg': speed.avg,
'max': speed.max,
},
'transferredSize': transferredSize,
'receivedSize': receivedSize,
'transferredCount': transferredCount,
'receivedCount': receivedCount,
'lastUrl': lastUrl,
'lastConnectTime': lastConnectTime?.toIso8601String(),
'lastDisconnectTime': lastDisconnectTime?.toIso8601String(),
'transferred': <String, BigInt>{
'count': transferred.count,
'size': transferred.size,
},
'received': <String, BigInt>{
'count': received.count,
'size': received.size,
},
'isRefreshActive': isRefreshActive,
'disconnects': disconnects,
'lastDisconnect': switch (lastDisconnect) {
(:int? code, :String? reason) => <String, Object?>{
Expand All @@ -135,7 +137,6 @@ final class SpinifyMetrics implements Comparable<SpinifyMetrics> {
},
_ => null,
},
'isRefreshActive': isRefreshActive,
};

@override
Expand Down
9 changes: 6 additions & 3 deletions lib/src/transport/transport_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import 'package:spinify/src/subscription/server_subscription_manager.dart';
import 'package:spinify/src/subscription/subcibed_on_channel.dart';
import 'package:spinify/src/subscription/subscription_config.dart';
import 'package:spinify/src/util/notifier.dart';
import 'package:ws/ws.dart';

/// Class responsible for sending and receiving data from the server.
/// {@nodoc}
Expand All @@ -30,9 +29,13 @@ abstract interface class ISpinifyTransport {
/// {@nodoc}
abstract final SpinifyListenable<SpinifyEvent> events;

/// Get web socket metrics.
/// Received bytes count & size.
/// {@nodoc}
WebSocketMetrics get metrics;
({BigInt count, BigInt size}) get received;

/// Transferred bytes count & size.
/// {@nodoc}
({BigInt count, BigInt size}) get transferred;

/// Message response timeout in milliseconds.
/// {@nodoc}
Expand Down
23 changes: 18 additions & 5 deletions lib/src/transport/ws_protobuf_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,6 @@ abstract base class SpinifyWSPBTransportBase implements ISpinifyTransport {
final SpinifyChangeNotifier<SpinifyEvent> events =
SpinifyChangeNotifier<SpinifyEvent>();

@override
WebSocketMetrics get metrics => _webSocket.metrics;

/// Init transport, override this method to add custom logic.
/// {@nodoc}
@protected
Expand Down Expand Up @@ -329,10 +326,18 @@ base mixin SpinifyWSPBSenderMixin
return cmd;
}

Future<void> _sendCommand(pb.Command command) {
BigInt _transferredCount = BigInt.zero;
BigInt _transferredSize = BigInt.zero;
@override
({BigInt count, BigInt size}) get transferred =>
(count: _transferredCount, size: _transferredSize);

Future<void> _sendCommand(pb.Command command) async {
if (!_webSocket.state.readyState.isOpen) throw StateError('Not connected');
final data = _commandEncoder.convert(command);
return _webSocket.add(data);
await _webSocket.add(data);
_transferredCount += BigInt.one;
_transferredSize += BigInt.from(data.length);
}
}

Expand Down Expand Up @@ -577,6 +582,12 @@ base mixin SpinifyWSPBHandlerMixin
/// {@nodoc}
StreamSubscription<List<int>>? _webSocketMessageSubscription;

BigInt _receivedCount = BigInt.zero;
BigInt _receivedSize = BigInt.zero;
@override
({BigInt count, BigInt size}) get received =>
(count: _receivedCount, size: _receivedSize);

@override
Future<void> connect(
String url,
Expand All @@ -596,6 +607,8 @@ base mixin SpinifyWSPBHandlerMixin
@pragma('vm:prefer-inline')
@pragma('dart2js:tryInline')
void _handleWebSocketMessage(List<int> response) {
_receivedCount += BigInt.one;
_receivedSize += BigInt.from(response.length);
final replies = _replyDecoder.convert(response);
for (final reply in replies) {
if (reply.hasId() && reply.id > 0) {
Expand Down

0 comments on commit 453c432

Please sign in to comment.