Skip to content

Commit

Permalink
update peers immediately and also make speed per file not global
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Nov 27, 2024
1 parent a3e0afc commit 3b1d5a7
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3b1d5a7

Please sign in to comment.