Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/commands/app/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ final class AppBackupCommand extends Command<void> with Disposable {
@override
Future<void> dispose() async {
_client?.close();
await _file?.safeDelete();
_monitor?.dispose();
await _file?.safeDelete();
}
}
2 changes: 1 addition & 1 deletion lib/commands/app/commit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class AppCommitCommand extends Command<void> with Disposable {

@override
Future<void> dispose() async {
await _file?.safeDelete();
_monitor?.dispose();
await _file?.safeDelete();
}
}
2 changes: 1 addition & 1 deletion lib/commands/app/upload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ final class AppUploadCommand extends Command<void> with Disposable {

@override
Future<void> dispose() async {
await _file?.safeDelete();
_monitor?.dispose();
await _file?.safeDelete();
}
}
2 changes: 1 addition & 1 deletion lib/commands/team/backup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ final class TeamBackupCommand extends Command<void> with Disposable {
@override
Future<void> dispose() async {
_client?.close();
await _file?.safeDelete();
_monitor?.dispose();
await _file?.safeDelete();
}
}
2 changes: 1 addition & 1 deletion lib/commands/team/commit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class TeamCommitCommand extends Command<void> with Disposable {

@override
FutureOr<void> dispose() async {
await _file?.safeDelete();
_monitor?.dispose();
await _file?.safeDelete();
}
}
35 changes: 12 additions & 23 deletions lib/utils/speed_monitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "dart:collection";

import "package:discloud/cli/disposable.dart";

final class _SpeedSample with LinkedListEntry<_SpeedSample> {
final class _SpeedSample {
_SpeedSample(this.units) : time = DateTime.now().microsecondsSinceEpoch;

final int units;
Expand All @@ -14,46 +14,35 @@ class SpeedMonitor implements Disposable {
static const double _zero = 0;

/// Creates a [SpeedMonitor]
factory SpeedMonitor() => ._(samples: .new());
factory SpeedMonitor() => ._(queue: .new());

const SpeedMonitor._({
required this._samples,
required this._queue,
this._windowDuration = const .new(seconds: 1),
});

/// The duration of the sliding window used for speed calculation.
final Queue<_SpeedSample> _queue;
final Duration _windowDuration;

/// A linked list of samples currently within the time window.
final LinkedList<_SpeedSample> _samples;

@override
void dispose() {
_samples.clear();
_queue.clear();
}

/// Adds a sample of the [totalProcessedUnits] and returns the current speed.
double add(int totalProcessedUnits) {
final _SpeedSample last = .new(totalProcessedUnits);

_samples.add(last);
_queue
..removeWhere((s) => last.time - s.time > _windowDuration.inMicroseconds)
..add(last);

_SpeedSample first = _samples.first;
final first = _queue.first;

if (first == last) return _zero;

int baseDeltaTime = last.time - first.time;

while (baseDeltaTime > _windowDuration.inMicroseconds) {
first.unlink();
first = _samples.first;
baseDeltaTime = last.time - first.time;
}

final deltaTime = baseDeltaTime / _windowDuration.inMicroseconds;

final deltaUnits = last.units - first.units;

return deltaUnits / deltaTime;
return last.units -
first.units / last.time -
first.time / _windowDuration.inMicroseconds;
}
}
Loading