Skip to content

Commit ca5af17

Browse files
committed
Skip bitfield for now
1 parent 5e17e15 commit ca5af17

File tree

2 files changed

+39
-86
lines changed

2 files changed

+39
-86
lines changed

lib/monitor.js

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const ReadyResource = require('ready-resource')
22
const debounce = require('debounceify')
3-
const safetyCatch = require('safety-catch')
43

54
module.exports = class Monitor extends ReadyResource {
65
constructor (drive, opts = {}) {
@@ -9,15 +8,13 @@ module.exports = class Monitor extends ReadyResource {
98
this.blobs = null
109
this.name = opts.name
1110
this.entry = opts.entry
12-
this.isDownload = opts.download === true
1311

1412
this._boundOnAppend = debounce(this._onAppend.bind(this))
1513
this._boundOnUpload = this._onUpload.bind(this)
1614
this._boundOnDownload = this._onDownload.bind(this)
1715
this.drive.on('close', () => this.close())
1816

19-
// Updated on each upload/download event
20-
this.stats = {
17+
const stats = {
2118
startTime: 0,
2219
percentage: 0,
2320
peersCount: 0,
@@ -28,6 +25,10 @@ module.exports = class Monitor extends ReadyResource {
2825
targetBytes: null,
2926
targetBlocks: null
3027
}
28+
29+
// Updated on each upload/download event
30+
this.uploadStats = { ...stats }
31+
this.downloadStats = { ...stats }
3132
}
3233

3334
async _open () {
@@ -36,15 +37,6 @@ module.exports = class Monitor extends ReadyResource {
3637
this.entry = await this.drive.entry(this.name)
3738
if (this.entry) this._setEntryInfo()
3839

39-
// load the local state of the file.
40-
// upload is a bit more tricky...
41-
if (this.entry && this.isDownload) {
42-
await this._loadLocalState().catch(safetyCatch).finally(() => {
43-
this._calculateStats()
44-
this.emit('update')
45-
})
46-
}
47-
4840
// Handlers
4941
this.blobs.core.on('append', this._boundOnAppend)
5042
this.blobs.core.on('upload', this._boundOnUpload)
@@ -65,53 +57,44 @@ module.exports = class Monitor extends ReadyResource {
6557
}
6658

6759
_setEntryInfo () {
68-
if (this.stats.targetBytes || this.stats.targetBlocks) return
69-
this.stats.targetBytes = this.entry.value.blob.byteLength
70-
this.stats.targetBlocks = this.entry.value.blob.blockLength
71-
this.stats.blockOffset = this.entry.value.blob.blockOffset
72-
this.stats.byteOffset = this.entry.value.blob.byteOffset
73-
}
60+
if (!this.downloadStats.targetBytes || !this.downloadStats.targetBlocks) {
61+
this.downloadStats.targetBytes = this.entry.value.blob.byteLength
62+
this.downloadStats.targetBlocks = this.entry.value.blob.blockLength
63+
}
7464

75-
async _onUpload (index, bytes, from) {
76-
this._updateStats(index, bytes, from)
65+
if (!this.uploadStats.targetBytes || !this.uploadStats.targetBlocks) {
66+
this.uploadStats.targetBytes = this.entry.value.blob.byteLength
67+
this.uploadStats.targetBlocks = this.entry.value.blob.blockLength
68+
}
7769
}
7870

79-
async _onDownload (index, bytes, from) {
80-
this._updateStats(index, bytes, from)
71+
_onUpload (index, bytes, from) {
72+
this._updateStats(this.uploadStats, index, bytes, from)
8173
}
8274

83-
async _loadLocalState () {
84-
// @TODO: There's a better way to do this?
85-
let blockIdx = this.stats.blockOffset
86-
while (blockIdx <= this.stats.targetBlocks) {
87-
if (await this.blobs.core.core.bitfield.get(blockIdx)) {
88-
const bytes = await this.blobs.core.core.blocks.get(blockIdx)
89-
this.stats.totalBytes += bytes.length
90-
this.stats.blocks++
91-
}
92-
blockIdx++
93-
}
75+
_onDownload (index, bytes, from) {
76+
this._updateStats(this.downloadStats, index, bytes, from)
9477
}
9578

96-
_updateStats (index, bytes, from) {
79+
_updateStats (stats, index, bytes, from) {
9780
if (!this.entry || this.closing) return
9881
if (!isWithinRange(index, this.entry)) return
9982

100-
this.stats.peersCount = from.replicator.peers.length
101-
this.stats.blocks++
102-
this.stats.monitoringBytes += bytes
103-
this.stats.totalBytes += bytes
83+
stats.peersCount = from.replicator.peers.length
84+
stats.blocks++
85+
stats.monitoringBytes += bytes
86+
stats.totalBytes += bytes
10487

105-
this._calculateStats()
88+
this._calculateStats(stats)
10689
this.emit('update')
10790
}
10891

109-
_calculateStats () {
110-
if (!this.stats.startTime) this.stats.startTime = Date.now()
111-
this.stats.percentage = Number(((this.stats.totalBytes / this.stats.targetBytes) * 100).toFixed(2))
112-
const timeElapsed = (Date.now() - this.stats.startTime) / 1000
92+
_calculateStats (stats) {
93+
if (!stats.startTime) stats.startTime = Date.now()
94+
stats.percentage = Number(((stats.totalBytes / stats.targetBytes) * 100).toFixed(2))
95+
const timeElapsed = (Date.now() - stats.startTime) / 1000
11396
if (timeElapsed > 0) {
114-
this.stats.speed = Math.floor(this.stats.monitoringBytes / timeElapsed) // Speed in bytes/sec
97+
stats.speed = Math.floor(stats.monitoringBytes / timeElapsed) // bytes/sec
11598
}
11699
}
117100
}

test.js

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,7 +1563,7 @@ test('drive.list (recursive false) ignore', async (t) => {
15631563
})
15641564

15651565
test('upload/download can be monitored', async (t) => {
1566-
t.plan(17)
1566+
t.plan(21)
15671567
const { corestore, drive, swarm, mirror } = await testenv(t.teardown)
15681568
swarm.on('connection', (conn) => corestore.replicate(conn))
15691569
swarm.join(drive.discoveryKey, { server: true, client: false })
@@ -1585,10 +1585,11 @@ test('upload/download can be monitored', async (t) => {
15851585
const expectedBlocks = [2, 1]
15861586
const expectedBytes = [bytes, 65536]
15871587
monitor.on('update', () => {
1588-
t.is(monitor.stats.blocks, expectedBlocks.pop())
1589-
t.is(monitor.stats.monitoringBytes, expectedBytes.pop())
1590-
t.is(monitor.stats.targetBlocks, 2)
1591-
t.is(monitor.stats.targetBytes, bytes)
1588+
t.is(monitor.uploadStats.blocks, expectedBlocks.pop())
1589+
t.is(monitor.uploadStats.monitoringBytes, expectedBytes.pop())
1590+
t.is(monitor.uploadStats.targetBlocks, 2)
1591+
t.is(monitor.uploadStats.targetBytes, bytes)
1592+
t.absent(monitor.downloadStats.blocks)
15921593
})
15931594
}
15941595

@@ -1601,48 +1602,17 @@ test('upload/download can be monitored', async (t) => {
16011602
const expectedBlocks = [2, 1]
16021603
const expectedBytes = [bytes, 65536]
16031604
monitor.on('update', () => {
1604-
t.is(monitor.stats.blocks, expectedBlocks.pop())
1605-
t.is(monitor.stats.monitoringBytes, expectedBytes.pop())
1606-
t.is(monitor.stats.targetBlocks, 2)
1607-
t.is(monitor.stats.targetBytes, bytes)
1605+
t.is(monitor.downloadStats.blocks, expectedBlocks.pop())
1606+
t.is(monitor.downloadStats.monitoringBytes, expectedBytes.pop())
1607+
t.is(monitor.downloadStats.targetBlocks, 2)
1608+
t.is(monitor.downloadStats.targetBytes, bytes)
1609+
t.absent(monitor.uploadStats.blocks)
16081610
})
16091611
}
16101612

16111613
await mirror.drive.get(file)
16121614
})
16131615

1614-
test('monitor loads the local state on download', async (t) => {
1615-
t.plan(3)
1616-
const { corestore, drive, swarm, mirror } = await testenv(t.teardown)
1617-
swarm.on('connection', (conn) => corestore.replicate(conn))
1618-
swarm.join(drive.discoveryKey, { server: true, client: false })
1619-
await swarm.flush()
1620-
1621-
mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn))
1622-
mirror.swarm.join(drive.discoveryKey, { server: false, client: true })
1623-
await mirror.swarm.flush()
1624-
1625-
const file = '/example.md'
1626-
const bytes = 1234
1627-
const buffer = Buffer.alloc(bytes, '0')
1628-
await drive.put(file, buffer)
1629-
1630-
observe()
1631-
async function observe () {
1632-
for await (const _ of mirror.drive.watch()) { /* eslint-disable-line */
1633-
await mirror.drive.get(file)
1634-
// Start monitoring after we've downloaded the file
1635-
const monitor = mirror.drive.monitor(file, { download: true })
1636-
monitor.on('update', () => {
1637-
t.is(monitor.stats.percentage, 100)
1638-
t.is(monitor.stats.totalBytes, bytes)
1639-
t.is(monitor.stats.targetBytes, bytes)
1640-
})
1641-
await monitor.ready()
1642-
}
1643-
}
1644-
})
1645-
16461616
async function testenv (teardown) {
16471617
const corestore = new Corestore(RAM)
16481618
await corestore.ready()

0 commit comments

Comments
 (0)