From 3b1d5a745019ffe40b72d94d37197508bb012e73 Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Wed, 27 Nov 2024 10:15:57 +0100 Subject: [PATCH] update peers immediately and also make speed per file not global --- lib/monitor.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/monitor.js b/lib/monitor.js index 63c46e7..c96a541 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -43,6 +43,11 @@ module.exports = class Monitor extends ReadyResource { if (!this.entry && this.name) this.entry = await this.drive.entry(this.name) if (this.entry) this._setEntryInfo() + this.uploadSpeedometer = speedometer() + this.downloadSpeedometer = speedometer() + + this._updatePeers() + // Handlers this.blobs.core.on('peer-add', this._boundPeerUpdate) this.blobs.core.on('peer-remove', this._boundPeerUpdate) @@ -71,26 +76,24 @@ module.exports = class Monitor extends ReadyResource { } _onUpload (index, bytes, from) { - if (!this.uploadSpeedometer) this.uploadSpeedometer = speedometer() - this.uploadStats.speed = this.uploadSpeedometer(bytes) - this._updateStats(this.uploadStats, index, bytes, from) + this._updateStats(this.uploadSpeedometer, this.uploadStats, index, bytes, from) } _onDownload (index, bytes, from) { - if (!this.downloadSpeedometer) this.downloadSpeedometer = speedometer() - this.downloadStats.speed = this.downloadSpeedometer(bytes) - this._updateStats(this.downloadStats, index, bytes, from) + this._updateStats(this.downloadSpeedometer, this.downloadStats, index, bytes, from) } _updatePeers () { this.uploadStats.peers = this.downloadStats.peers = this.peers = this.blobs.core.peers.length } - _updateStats (stats, index, bytes, from) { + _updateStats (speed, stats, index, bytes, from) { if (!this.entry || this.closing) return if (!isWithinRange(index, this.entry)) return if (!stats.startTime) stats.startTime = Date.now() + + stats.speed = speed(bytes) stats.blocks++ stats.monitoringBytes += bytes stats.totalBytes += bytes