Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 6d4962d

Browse files
committed
feat: implement bitswap.stat and stats.bitswap according to SPEC
1 parent 3bca165 commit 6d4962d

File tree

9 files changed

+89
-18
lines changed

9 files changed

+89
-18
lines changed

src/core/components/bitswap.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
4+
const promisify = require('promisify-es6')
45

56
function formatWantlist (list) {
67
return Array.from(list).map((e) => e[1])
@@ -16,16 +17,25 @@ module.exports = function bitswap (self) {
1617
const list = self._bitswap.getWantlist()
1718
return formatWantlist(list)
1819
},
19-
stat: () => {
20+
stat: promisify((callback) => {
2021
if (!self.isOnline()) {
21-
throw new Error(OFFLINE_ERROR)
22+
callback(new Error(OFFLINE_ERROR))
2223
}
2324

24-
return Object.assign({}, self._bitswap.stat().snapshot, {
25+
const snapshot = self._bitswap.stat().snapshot
26+
27+
callback(null, {
28+
provideBufLen: snapshot.providesBufferLength,
29+
blocksReceived: snapshot.blocksReceived,
2530
wantlist: formatWantlist(self._bitswap.getWantlist()),
26-
peers: self._bitswap.peers().map((id) => id.toB58String())
31+
peers: self._bitswap.peers().map((id) => id.toB58String()),
32+
dupBlksReceived: snapshot.dupBlksReceived,
33+
dupDataReceived: snapshot.dupDataReceived,
34+
dataReceived: snapshot.dataReceived,
35+
blocksSent: snapshot.blocksSent,
36+
dataSent: snapshot.dataSent
2737
})
28-
},
38+
}),
2939
unwant: (key) => {
3040
if (!self.isOnline()) {
3141
throw new Error(OFFLINE_ERROR)

src/core/components/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ exports.bitswap = require('./bitswap')
2121
exports.pubsub = require('./pubsub')
2222
exports.dht = require('./dht')
2323
exports.dns = require('./dns')
24+
exports.stats = require('./stats')

src/core/components/stats.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict'
2+
3+
const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR
4+
5+
function formatWantlist (list) {
6+
return Array.from(list).map((e) => e[1])
7+
}
8+
9+
module.exports = function stats (self) {
10+
return {
11+
bitswap: require('./bitswap')(self).stat
12+
}
13+
}

src/core/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class IPFS extends EventEmitter {
9595
this.pubsub = components.pubsub(this)
9696
this.dht = components.dht(this)
9797
this.dns = components.dns(this)
98+
this.stats = components.stats(this)
9899

99100
if (this._options.EXPERIMENTAL.pubsub) {
100101
this.log('EXPERIMENTAL pubsub is enabled')

src/http/api/resources/bitswap.js

+21-13
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,27 @@ exports.wantlist = (request, reply) => {
2121
}
2222

2323
exports.stat = (request, reply) => {
24-
let stats
25-
try {
26-
stats = request.server.app.ipfs.bitswap.stat()
27-
} catch (err) {
28-
return reply(boom.badRequest(err))
29-
}
30-
31-
reply({
32-
BlocksReceived: stats.blocksReceived,
33-
Wantlist: stats.wantlist,
34-
Peers: stats.peers,
35-
DupBlksReceived: stats.dupBlksReceived,
36-
DupDataReceived: stats.dupDataReceived
24+
const ipfs = request.server.app.ipfs
25+
26+
ipfs.bitswap.stat((err, stats) => {
27+
if (err) {
28+
return reply({
29+
Message: err.toString(),
30+
Code: 0
31+
}).code(500)
32+
}
33+
34+
reply({
35+
ProvideBufLen: stats.provideBufLen,
36+
BlocksReceived: stats.blocksReceived,
37+
Wantlist: stats.wantlist,
38+
Peers: stats.peers,
39+
DupBlksReceived: stats.dupBlksReceived,
40+
DupDataReceived: stats.dupDataReceived,
41+
DataReceived: stats.dataReceived,
42+
BlocksSent: stats.blocksSent,
43+
DataSent: stats.dataSent
44+
})
3745
})
3846
}
3947

src/http/api/resources/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ exports.file = require('./file')
1313
exports.files = require('./files')
1414
exports.pubsub = require('./pubsub')
1515
exports.dns = require('./dns')
16+
exports.stats = require('./stats')

src/http/api/resources/stats.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
exports = module.exports
4+
5+
exports.bitswap = require('./bitswap').stat

src/http/api/routes/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ module.exports = (server) => {
1616
require('./debug')(server)
1717
require('./webui')(server)
1818
require('./dns')(server)
19+
require('./stats')(server)
1920
}

src/http/api/routes/stats.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const resources = require('./../resources')
4+
5+
module.exports = (server) => {
6+
const api = server.select('API')
7+
8+
api.route({
9+
method: '*',
10+
path: '/api/v0/stats/bitswap',
11+
config: {
12+
handler: resources.stats.bitswap
13+
}
14+
})
15+
16+
/* api.route({
17+
method: '*',
18+
path: '/api/v0/stats/bw',
19+
config: {
20+
handler: resources.stats.bw
21+
}
22+
}) */
23+
24+
/* api.route({
25+
method: '*',
26+
path: '/api/v0/stats/repo',
27+
config: {
28+
handler: resources.stats.repo
29+
}
30+
}) */
31+
}

0 commit comments

Comments
 (0)