Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit c76eab4

Browse files
committed
feat: use stream on stats.bw
1 parent 328726a commit c76eab4

File tree

7 files changed

+206
-81
lines changed

7 files changed

+206
-81
lines changed

Diff for: SPEC/BITSWAP.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ Bitswap API
1717

1818
`callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful. `stats` is an Object containing the following keys:
1919

20-
- `provideBufLen`
20+
- `provideBufLen` is an integer.
2121
- `wantlist` (array)
2222
- `peers` (array)
23-
- `blocksReceived`
24-
- `dataReceived`
25-
- `blocksSent`
26-
- `dataSent`
27-
- `dupBlksReceived`
28-
- `dupDataReceived`
23+
- `blocksReceived` is a [Big Int][1]
24+
- `dataReceived` is a [Big Int][1]
25+
- `blocksSent` is a [Big Int][1]
26+
- `dataSent` is a [Big Int][1]
27+
- `dupBlksReceived` is a [Big Int][1]
28+
- `dupDataReceived` is a [Big Int][1]
2929

3030
If no `callback` is passed, a promise is returned.
3131

@@ -47,3 +47,5 @@ ipfs.stats.bitswap((err, stats) => console.log(stats))
4747
// dupBlksReceived: 0,
4848
// dupDataReceived: 0 }
4949
```
50+
51+
[1]: https://github.com/MikeMcl/big.js/

Diff for: SPEC/REPO.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ Where:
4242

4343
`callback` must follow `function (err, stats) {}` signature, where `err` is an Error if the operation was not successful and `stats` is an object containing the following keys:
4444

45-
- `numObjects`
46-
- `repoSize`
47-
- `repoPath`
48-
- `version`
49-
- `storageMax`
45+
- `numObjects` is a [Big Int][1].
46+
- `repoSize` is a [Big Int][1], in bytes.
47+
- `repoPath` is a string.
48+
- `version` is a string.
49+
- `storageMax` is a [Big Int][1].
5050

5151
If no `callback` is passed, a promise is returned.
5252

@@ -81,3 +81,5 @@ ipfs.repo.version((err, version) => console.log(version))
8181

8282
// "6"
8383
```
84+
85+
[1]: https://github.com/MikeMcl/big.js/

Diff for: SPEC/STATS.md

+75-10
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Stats API
1111

1212
#### `bw`
1313

14-
> Adds an IPFS object to the pinset and also stores it to the IPFS repo. pinset is the set of hashes currently pinned (not gc'able).
14+
> Get IPFS bandwidth information as an object.
1515
1616
##### `Go` **WIP**
1717

@@ -25,12 +25,14 @@ Where:
2525
- `poll` is used to print bandwidth at an interval.
2626
- `interval` is the time interval to wait between updating output, if `poll` is true.
2727

28-
`callback` must follow `function (err, stats) {}` signature, where `err` is an error if the operation was not successful. `stats` is an Object containing the following keys:
28+
`callback` must follow `function (err, stat) {}` signature, where `err` is an Error if the operation was not successful.
2929

30-
- `totalIn`
31-
- `totalOut`
32-
- `rateIn`
33-
- `rateOut`
30+
`stat` is, in both cases, an Object containing the following keys:
31+
32+
- `totalIn` - is a [Big Int][big], in bytes.
33+
- `totalOut` - is a [Big Int][big], in bytes.
34+
- `rateIn` - is a [Big Int][big], in bytes.
35+
- `rateOut` - is a [Big Int][big], in bytes.
3436

3537
If no `callback` is passed, a promise is returned.
3638

@@ -39,8 +41,71 @@ If no `callback` is passed, a promise is returned.
3941
```JavaScript
4042
ipfs.stats.bw((err, stats) => console.log(stats))
4143

42-
// { totalIn: 15456,
43-
// totalOut: 15420,
44-
// rateIn: 905.0873512246716,
45-
// rateOut: 893.7400053359125 }
44+
// { totalIn: Big {...},
45+
// totalOut: Big {...},
46+
// rateIn: Big {...},
47+
// rateOut: Big {...} }
48+
```
49+
50+
#### `bwPullStream`
51+
52+
> Get IPFS bandwidth information as a [Pull Stream][ps].
53+
54+
##### `Go` **WIP**
55+
56+
##### `JavaScript` - ipfs.stats.bwPullStream([options]) -> [Pull Stream][ps]
57+
58+
Options are described on [`ipfs.stats.bw`](#bw).
59+
60+
**Example:**
61+
62+
```JavaScript
63+
const pull = require('pull-stream')
64+
const log = require('pull-stream/sinks/log')
65+
66+
const stream = ipfs.stats.bwReadableStream({ poll: true })
67+
68+
pull(
69+
stream,
70+
log()
71+
)
72+
73+
// { totalIn: Big {...},
74+
// totalOut: Big {...},
75+
// rateIn: Big {...},
76+
// rateOut: Big {...} }
77+
// ...
78+
// Ad infinitum
4679
```
80+
81+
#### `bwReadableStream`
82+
83+
> Get IPFS bandwidth information as a [Readable Stream][rs].
84+
85+
##### `Go` **WIP**
86+
87+
##### `JavaScript` - ipfs.stats.bwReadableStream([options]) -> [Readable Stream][rs]
88+
89+
Options are described on [`ipfs.stats.bw`](#bw).
90+
91+
**Examples:**
92+
93+
```JavaScript
94+
const stream = ipfs.stats.bwReadableStream({ poll: true })
95+
const bl = require('bl')
96+
97+
stream.pipe(bl((err, data) => {
98+
console.log(data)
99+
}))
100+
101+
// { totalIn: Big {...},
102+
// totalOut: Big {...},
103+
// rateIn: Big {...},
104+
// rateOut: Big {...} }
105+
// ...
106+
// Ad infinitum
107+
```
108+
109+
[big]: https://github.com/MikeMcl/big.js/
110+
[rs]: https://www.npmjs.com/package/readable-stream
111+
[ps]: https://www.npmjs.com/package/pull-stream

Diff for: js/src/repo.js

+5-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
const chai = require('chai')
77
const dirtyChai = require('dirty-chai')
8+
const statsTests = require('./utils/stats')
89
const expect = chai.expect
910
chai.use(dirtyChai)
1011

@@ -46,26 +47,15 @@ module.exports = (common) => {
4647
})
4748

4849
it('.stat', (done) => {
49-
ipfs.repo.stat((err, stat) => {
50-
expect(err).to.not.exist()
51-
expect(stat).to.exist()
52-
expect(stat).to.have.property('numObjects')
53-
expect(stat).to.have.property('repoSize')
54-
expect(stat).to.have.property('repoPath')
55-
expect(stat).to.have.property('version')
56-
expect(stat).to.have.property('storageMax')
50+
ipfs.repo.stat((err, res) => {
51+
statsTests.expectIsRepo(err, res)
5752
done()
5853
})
5954
})
6055

6156
it('.stat Promise', () => {
62-
return ipfs.repo.stat().then((stat) => {
63-
expect(stat).to.exist()
64-
expect(stat).to.have.property('numObjects')
65-
expect(stat).to.have.property('repoSize')
66-
expect(stat).to.have.property('repoPath')
67-
expect(stat).to.have.property('version')
68-
expect(stat).to.have.property('storageMax')
57+
return ipfs.repo.stat().then((res) => {
58+
statsTests.expectIsRepo(null, res)
6959
})
7060
})
7161

Diff for: js/src/stats.js

+41-44
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
const chai = require('chai')
77
const dirtyChai = require('dirty-chai')
8+
const statsTests = require('./utils/stats')
89
const expect = chai.expect
910
chai.use(dirtyChai)
1011

@@ -43,17 +44,7 @@ module.exports = (common) => {
4344
}
4445

4546
ipfs.stats.bitswap((err, res) => {
46-
expect(err).to.not.exist()
47-
expect(res).to.exist()
48-
expect(res).to.have.a.property('provideBufLen')
49-
expect(res).to.have.a.property('wantlist')
50-
expect(res).to.have.a.property('peers')
51-
expect(res).to.have.a.property('blocksReceived')
52-
expect(res).to.have.a.property('dataReceived')
53-
expect(res).to.have.a.property('blocksSent')
54-
expect(res).to.have.a.property('dataSent')
55-
expect(res).to.have.a.property('dupBlksReceived')
56-
expect(res).to.have.a.property('dupDataReceived')
47+
statsTests.expectIsBitswap(err, res)
5748
done()
5849
})
5950
})
@@ -65,16 +56,7 @@ module.exports = (common) => {
6556
}
6657

6758
return ipfs.stats.bitswap().then((res) => {
68-
expect(res).to.exist()
69-
expect(res).to.have.a.property('provideBufLen')
70-
expect(res).to.have.a.property('wantlist')
71-
expect(res).to.have.a.property('peers')
72-
expect(res).to.have.a.property('blocksReceived')
73-
expect(res).to.have.a.property('dataReceived')
74-
expect(res).to.have.a.property('blocksSent')
75-
expect(res).to.have.a.property('dataSent')
76-
expect(res).to.have.a.property('dupBlksReceived')
77-
expect(res).to.have.a.property('dupDataReceived')
59+
statsTests.expectIsBitswap(null, res)
7860
})
7961
})
8062

@@ -85,13 +67,26 @@ module.exports = (common) => {
8567
}
8668

8769
ipfs.stats.bw((err, res) => {
70+
statsTests.expectIsBandwidth(err, res)
71+
done()
72+
})
73+
})
74+
75+
it('.bw Poll', (done) => {
76+
if (!withGo) {
77+
console.log('Not supported in js-ipfs yet')
78+
return done()
79+
}
80+
81+
ipfs.stats.bw({poll: true}, (err, res) => {
8882
expect(err).to.not.exist()
8983
expect(res).to.exist()
90-
expect(res).to.have.a.property('totalIn')
91-
expect(res).to.have.a.property('totalOut')
92-
expect(res).to.have.a.property('rateIn')
93-
expect(res).to.have.a.property('rateOut')
94-
done()
84+
85+
res.once('data', (data) => {
86+
statsTests.expectIsBandwidth(null, data)
87+
done()
88+
res.destroy()
89+
})
9590
})
9691
})
9792

@@ -102,28 +97,35 @@ module.exports = (common) => {
10297
}
10398

10499
return ipfs.stats.bw().then((res) => {
105-
expect(res).to.exist()
106-
expect(res).to.have.a.property('totalIn')
107-
expect(res).to.have.a.property('totalOut')
108-
expect(res).to.have.a.property('rateIn')
109-
expect(res).to.have.a.property('rateOut')
100+
statsTests.expectIsBandwidth(null, res)
110101
})
111102
})
112103

104+
it('.bw Promise Poll', (done) => {
105+
if (!withGo) {
106+
console.log('Not supported in js-ipfs yet')
107+
return
108+
}
109+
110+
ipfs.stats.bw({poll: true}).then((res) => {
111+
expect(res).to.exist()
112+
113+
res.once('data', (data) => {
114+
statsTests.expectIsBandwidth(null, data)
115+
done()
116+
res.destroy()
117+
})
118+
}).catch(done)
119+
})
120+
113121
it('.repo', (done) => {
114122
if (!withGo) {
115123
console.log('Not supported in js-ipfs yet')
116124
return done()
117125
}
118126

119127
ipfs.stats.repo((err, res) => {
120-
expect(err).to.not.exist()
121-
expect(res).to.exist()
122-
expect(res).to.have.a.property('numObjects')
123-
expect(res).to.have.a.property('repoSize')
124-
expect(res).to.have.a.property('repoPath')
125-
expect(res).to.have.a.property('version')
126-
expect(res).to.have.a.property('storageMax')
128+
statsTests.expectIsRepo(err, res)
127129
done()
128130
})
129131
})
@@ -135,12 +137,7 @@ module.exports = (common) => {
135137
}
136138

137139
return ipfs.stats.repo().then((res) => {
138-
expect(res).to.exist()
139-
expect(res).to.have.a.property('numObjects')
140-
expect(res).to.have.a.property('repoSize')
141-
expect(res).to.have.a.property('repoPath')
142-
expect(res).to.have.a.property('version')
143-
expect(res).to.have.a.property('storageMax')
140+
statsTests.expectIsRepo(null, res)
144141
})
145142
})
146143
})

0 commit comments

Comments
 (0)