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

Commit 5803d39

Browse files
hacdiasdaviddias
authored andcommittedJan 18, 2018
fix: normalize stats fields (#669)
1 parent 210dabb commit 5803d39

File tree

5 files changed

+80
-49
lines changed

5 files changed

+80
-49
lines changed
 

Diff for: ‎src/stats/bitswap.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,30 @@
22

33
const promisify = require('promisify-es6')
44

5+
const transform = function (res, callback) {
6+
callback(null, {
7+
provideBufLen: res.ProvideBufLen,
8+
wantlist: res.Wantlist,
9+
peers: res.Peers,
10+
blocksReceived: res.BlocksReceived,
11+
dataReceived: res.DataReceived,
12+
blocksSent: res.BlocksSent,
13+
dataSent: res.DataSent,
14+
dupBlksReceived: res.DupBlksReceived,
15+
dupDataReceived: res.DupDataReceived
16+
})
17+
}
18+
519
module.exports = (send) => {
620
return promisify((opts, callback) => {
721
if (typeof (opts) === 'function') {
822
callback = opts
923
opts = {}
1024
}
1125

12-
send({
26+
send.andTransform({
1327
path: 'stats/bitswap',
1428
qs: opts
15-
}, callback)
29+
}, transform, callback)
1630
})
1731
}

Diff for: ‎src/stats/bw.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@
33
const promisify = require('promisify-es6')
44
const streamToValue = require('../utils/stream-to-value')
55

6+
const transform = function (res, callback) {
7+
streamToValue(res, (err, data) => {
8+
if (err) {
9+
return callback(err)
10+
}
11+
12+
callback(null, {
13+
totalIn: data[0].TotalIn,
14+
totalOut: data[0].TotalOut,
15+
rateIn: data[0].RateIn,
16+
rateOut: data[0].RateOut
17+
})
18+
})
19+
}
20+
621
module.exports = (send) => {
722
return promisify((opts, callback) => {
823
if (typeof (opts) === 'function') {
@@ -13,14 +28,6 @@ module.exports = (send) => {
1328
send.andTransform({
1429
path: 'stats/bw',
1530
qs: opts
16-
}, streamToValue, (err, stats) => {
17-
if (err) {
18-
return callback(err)
19-
}
20-
21-
// streamToValue returns an array and we're only
22-
// interested in returning the object itself.
23-
callback(err, stats[0])
24-
})
31+
}, transform, callback)
2532
})
2633
}

Diff for: ‎src/stats/repo.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33

44
const promisify = require('promisify-es6')
55

6+
const transform = function (res, callback) {
7+
callback(null, {
8+
numObjects: res.NumObjects,
9+
repoSize: res.RepoSize,
10+
repoPath: res.RepoPath,
11+
version: res.Version,
12+
storageMax: res.StorageMax
13+
})
14+
}
15+
616
module.exports = (send) => {
717
return promisify((opts, callback) => {
818
if (typeof (opts) === 'function') {
919
callback = opts
1020
opts = {}
1121
}
1222

13-
send({
23+
send.andTransform({
1424
path: 'stats/repo',
1525
qs: opts
16-
}, callback)
26+
}, transform, callback)
1727
})
1828
}

Diff for: ‎test/fixtures/test-folder/hello-link

-1
This file was deleted.

Diff for: ‎test/fixtures/test-folder/hello-link

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
files/hello.txt

Diff for: ‎test/stats.spec.js

+36-36
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ describe('stats', function () {
3131
ipfs.stats.bitswap((err, res) => {
3232
expect(err).to.not.exist()
3333
expect(res).to.exist()
34-
expect(res).to.have.a.property('ProvideBufLen')
35-
expect(res).to.have.a.property('Wantlist')
36-
expect(res).to.have.a.property('Peers')
37-
expect(res).to.have.a.property('BlocksReceived')
38-
expect(res).to.have.a.property('DataReceived')
39-
expect(res).to.have.a.property('BlocksSent')
40-
expect(res).to.have.a.property('DataSent')
41-
expect(res).to.have.a.property('DupBlksReceived')
42-
expect(res).to.have.a.property('DupDataReceived')
34+
expect(res).to.have.a.property('provideBufLen')
35+
expect(res).to.have.a.property('wantlist')
36+
expect(res).to.have.a.property('peers')
37+
expect(res).to.have.a.property('blocksReceived')
38+
expect(res).to.have.a.property('dataReceived')
39+
expect(res).to.have.a.property('blocksSent')
40+
expect(res).to.have.a.property('dataSent')
41+
expect(res).to.have.a.property('dupBlksReceived')
42+
expect(res).to.have.a.property('dupDataReceived')
4343
done()
4444
})
4545
})
@@ -48,10 +48,10 @@ describe('stats', function () {
4848
ipfs.stats.bw((err, res) => {
4949
expect(err).to.not.exist()
5050
expect(res).to.exist()
51-
expect(res).to.have.a.property('TotalIn')
52-
expect(res).to.have.a.property('TotalOut')
53-
expect(res).to.have.a.property('RateIn')
54-
expect(res).to.have.a.property('RateOut')
51+
expect(res).to.have.a.property('totalIn')
52+
expect(res).to.have.a.property('totalOut')
53+
expect(res).to.have.a.property('rateIn')
54+
expect(res).to.have.a.property('rateOut')
5555
done()
5656
})
5757
})
@@ -60,11 +60,11 @@ describe('stats', function () {
6060
ipfs.stats.repo((err, res) => {
6161
expect(err).to.not.exist()
6262
expect(res).to.exist()
63-
expect(res).to.have.a.property('NumObjects')
64-
expect(res).to.have.a.property('RepoSize')
65-
expect(res).to.have.a.property('RepoPath')
66-
expect(res).to.have.a.property('Version')
67-
expect(res).to.have.a.property('StorageMax')
63+
expect(res).to.have.a.property('numObjects')
64+
expect(res).to.have.a.property('repoSize')
65+
expect(res).to.have.a.property('repoPath')
66+
expect(res).to.have.a.property('version')
67+
expect(res).to.have.a.property('storageMax')
6868
done()
6969
})
7070
})
@@ -75,38 +75,38 @@ describe('stats', function () {
7575
return ipfs.stats.bw()
7676
.then((res) => {
7777
expect(res).to.exist()
78-
expect(res).to.have.a.property('TotalIn')
79-
expect(res).to.have.a.property('TotalOut')
80-
expect(res).to.have.a.property('RateIn')
81-
expect(res).to.have.a.property('RateOut')
78+
expect(res).to.have.a.property('totalIn')
79+
expect(res).to.have.a.property('totalOut')
80+
expect(res).to.have.a.property('rateIn')
81+
expect(res).to.have.a.property('rateOut')
8282
})
8383
})
8484

8585
it('.stats.repo', () => {
8686
return ipfs.stats.repo()
8787
.then((res) => {
8888
expect(res).to.exist()
89-
expect(res).to.have.a.property('NumObjects')
90-
expect(res).to.have.a.property('RepoSize')
91-
expect(res).to.have.a.property('RepoPath')
92-
expect(res).to.have.a.property('Version')
93-
expect(res).to.have.a.property('StorageMax')
89+
expect(res).to.have.a.property('numObjects')
90+
expect(res).to.have.a.property('repoSize')
91+
expect(res).to.have.a.property('repoPath')
92+
expect(res).to.have.a.property('version')
93+
expect(res).to.have.a.property('storageMax')
9494
})
9595
})
9696

9797
it('.stats.bitswap', () => {
9898
return ipfs.stats.bitswap()
9999
.then((res) => {
100100
expect(res).to.exist()
101-
expect(res).to.have.a.property('ProvideBufLen')
102-
expect(res).to.have.a.property('Wantlist')
103-
expect(res).to.have.a.property('Peers')
104-
expect(res).to.have.a.property('BlocksReceived')
105-
expect(res).to.have.a.property('DataReceived')
106-
expect(res).to.have.a.property('BlocksSent')
107-
expect(res).to.have.a.property('DataSent')
108-
expect(res).to.have.a.property('DupBlksReceived')
109-
expect(res).to.have.a.property('DupDataReceived')
101+
expect(res).to.have.a.property('provideBufLen')
102+
expect(res).to.have.a.property('wantlist')
103+
expect(res).to.have.a.property('peers')
104+
expect(res).to.have.a.property('blocksReceived')
105+
expect(res).to.have.a.property('dataReceived')
106+
expect(res).to.have.a.property('blocksSent')
107+
expect(res).to.have.a.property('dataSent')
108+
expect(res).to.have.a.property('dupBlksReceived')
109+
expect(res).to.have.a.property('dupDataReceived')
110110
})
111111
})
112112
})

0 commit comments

Comments
 (0)
This repository has been archived.