hyperswarm-stats 1.2.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +2 -0
  2. package/index.js +22 -2
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -30,3 +30,5 @@ console.log(metrics)
30
30
  ## Usage Without Prometheus
31
31
 
32
32
  `swarmStats.toString()` returns a string overview of all hyperswarm and DHT stats.
33
+
34
+ `swarmStats.toJson()` returns a JSON overview of all hyperswarm and DHT stats.
package/index.js CHANGED
@@ -13,7 +13,7 @@ class HyperswarmStats {
13
13
  this._fastRecoveriesOfClosedSwarmStreams = 0
14
14
  this._rtoCountOfClosedSwarmStreams = 0
15
15
 
16
- swarm.on('connection', conn => {
16
+ const onconnection = conn => {
17
17
  conn.on('close', () => {
18
18
  this._bytesTransmittedOverClosedSwarmStreams += conn.rawStream?.bytesTransmitted || 0
19
19
  this._packetsTransmittedOverClosedSwarmStreams += conn.rawStream?.packetsTransmitted || 0
@@ -23,7 +23,10 @@ class HyperswarmStats {
23
23
  this._fastRecoveriesOfClosedSwarmStreams += conn.rawStream?.fastRecoveries
24
24
  this._rtoCountOfClosedSwarmStreams += conn.rawStream?.rtoCount
25
25
  })
26
- })
26
+ }
27
+
28
+ for (const conn of swarm.connections) onconnection(conn)
29
+ swarm.on('connection', onconnection)
27
30
  }
28
31
 
29
32
  get connects () {
@@ -127,6 +130,23 @@ class HyperswarmStats {
127
130
  return this.swarm.peers.size
128
131
  }
129
132
 
133
+ toJson () {
134
+ return {
135
+ dht: this.dhtStats.toJson(),
136
+ nrPeers: this.nrPeers,
137
+ connects: JSON.parse(JSON.stringify(this.connects)), // quick-and-dirty deep copy
138
+ bytesTransmittedOverSwarmStreams: this.getBytesTransmittedAcrossAllStreams(),
139
+ bytesReceivedOverSwarmStreams: this.getBytesReceivedAcrossAllStreams(),
140
+ packetsTransmittedOverSwarmStreams: this.getPacketsTransmittedAcrossAllStreams(),
141
+ packetsReceivedOverSwarmStreams: this.getPacketsReceivedAcrossAllStreams(),
142
+ avgCongestionWindow: this.getAvgCongestionWindow(),
143
+ avgMtu: this.getAvgMTU(),
144
+ retransmitsOverSwarmStreams: this.getRetransmitsAcrossAllStreams(),
145
+ fastRecoveriesOverSwarmStreams: this.getFastRecoveriesAcrossAllStreams(),
146
+ rtoCountOverSwarmStreams: this.getRTOCountAcrossAllStreams()
147
+ }
148
+ }
149
+
130
150
  toString () {
131
151
  return `Hyperswarm Stats
132
152
  - hyperswarm_nr_peers: ${this.nrPeers}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hyperswarm-stats",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "Hyperswarm stats with prometheus support",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -32,6 +32,6 @@
32
32
  "standard": "^17.1.0"
33
33
  },
34
34
  "dependencies": {
35
- "hyperdht-stats": "^1.5.0"
35
+ "hyperdht-stats": "^1.7.0"
36
36
  }
37
37
  }