Skip to content

Commit

Permalink
Resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
MKPLKN committed Sep 9, 2024
1 parent f2aa6b5 commit ca57bc6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 19 deletions.
26 changes: 11 additions & 15 deletions lib/monitor.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
const ReadyResource = require('ready-resource')
const safetyCatch = require('safety-catch')
const speedometer = require('speedometer')
const debounce = require('debounceify')

module.exports = class Monitor extends ReadyResource {
constructor (drive, opts = {}) {
super()
this.drive = drive
this.blobs = null
this.name = opts.name
this.entry = opts.entry
this.name = opts.name || null
this.entry = opts.entry || null

this._boundOnAppend = debounce(this._onAppend.bind(this))
this._boundOnUpload = this._onUpload.bind(this)
this._boundOnDownload = this._onDownload.bind(this)

Expand All @@ -32,34 +31,27 @@ module.exports = class Monitor extends ReadyResource {

this.uploadSpeedometer = null
this.downloadSpeedometer = null

this.ready().catch(safetyCatch)
}

async _open () {
await this.drive.ready()
this.blobs = await this.drive.getBlobs()
this.entry = await this.drive.entry(this.name)
if (!this.entry && this.name) this.entry = await this.drive.entry(this.name)
if (this.entry) this._setEntryInfo()

// Handlers
this.blobs.core.on('append', this._boundOnAppend)
this.blobs.core.on('upload', this._boundOnUpload)
this.blobs.core.on('download', this._boundOnDownload)
}

async _close () {
this.blobs.core.off('append', this._boundOnAppend)
this.blobs.core.off('upload', this._boundOnUpload)
this.blobs.core.off('download', this._boundOnDownload)
this.drive.monitors.delete(this)
}

async _onAppend () {
if (this.entry) return
await new Promise(resolve => setImmediate(resolve))
this.entry = await this.drive.entry(this.name)
if (this.entry) this._setEntryInfo()
}

_setEntryInfo () {
if (!this.downloadStats.targetBytes || !this.downloadStats.targetBlocks) {
this.downloadStats.targetBytes = this.entry.value.blob.byteLength
Expand Down Expand Up @@ -93,7 +85,7 @@ module.exports = class Monitor extends ReadyResource {
stats.blocks++
stats.monitoringBytes += bytes
stats.totalBytes += bytes
stats.percentage = Number(((stats.totalBytes / stats.targetBytes) * 100).toFixed(2))
stats.percentage = toFixed(stats.totalBytes / stats.targetBytes * 100)

this.emit('update')
}
Expand All @@ -112,3 +104,7 @@ function isWithinRange (index, entry) {
const { blockOffset, blockLength } = entry.value.blob
return index >= blockOffset && index < blockOffset + blockLength
}

function toFixed (n) {
return Math.round(n * 100) / 100
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
},
"homepage": "https://github.com/holepunchto/hyperdrive#readme",
"dependencies": {
"debounceify": "^1.1.0",
"hyperbee": "^2.11.1",
"hyperblobs": "^2.3.0",
"hypercore": "^10.33.0",
Expand Down
7 changes: 4 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ test('drive.list (recursive false) ignore', async (t) => {
})

test('upload/download can be monitored', async (t) => {
t.plan(25)
t.plan(27)
const { corestore, drive, swarm, mirror } = await testenv(t.teardown)
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
Expand All @@ -1576,6 +1576,7 @@ test('upload/download can be monitored', async (t) => {
const file = '/example.md'
const bytes = 1024 * 100 // big enough to trigger more than one update event
const buffer = Buffer.alloc(bytes, '0')
await drive.put(file, buffer)

{
// Start monitoring upload
Expand All @@ -1590,12 +1591,11 @@ test('upload/download can be monitored', async (t) => {
t.is(monitor.uploadStats.targetBlocks, 2)
t.is(monitor.uploadStats.targetBytes, bytes)
t.is(monitor.uploadSpeed(), monitor.uploadStats.speed)
if (!expectedBlocks.length) t.is(monitor.uploadStats.percentage, 100)
t.absent(monitor.downloadStats.blocks)
})
}

await drive.put(file, buffer)

{
// Start monitoring download
const monitor = mirror.drive.monitor(file)
Expand All @@ -1608,6 +1608,7 @@ test('upload/download can be monitored', async (t) => {
t.is(monitor.downloadStats.targetBlocks, 2)
t.is(monitor.downloadStats.targetBytes, bytes)
t.is(monitor.downloadSpeed(), monitor.downloadStats.speed)
if (!expectedBlocks.length) t.is(monitor.downloadStats.percentage, 100)
t.absent(monitor.uploadStats.blocks)
})
}
Expand Down

0 comments on commit ca57bc6

Please sign in to comment.