hyperswarm-stats 1.3.0 → 1.3.2

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 (2) hide show
  1. package/index.js +51 -48
  2. package/package.json +17 -5
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const HyperDhtStats = require('hyperdht-stats')
2
2
 
3
3
  class HyperswarmStats {
4
- constructor (swarm) {
4
+ constructor(swarm) {
5
5
  this.swarm = swarm
6
6
  this.dhtStats = new HyperDhtStats(this.swarm.dht)
7
7
 
@@ -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,18 +23,21 @@ 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
- get connects () {
32
+ get connects() {
30
33
  return this.swarm.stats.connects
31
34
  }
32
35
 
33
- get updates () {
36
+ get updates() {
34
37
  return this.swarm.stats.updates
35
38
  }
36
39
 
37
- getAvgCongestionWindow () {
40
+ getAvgCongestionWindow() {
38
41
  let totalCongestionWindow = 0
39
42
  let count = 0
40
43
  for (const conn of this.swarm.connections) {
@@ -47,7 +50,7 @@ class HyperswarmStats {
47
50
  return totalCongestionWindow / count
48
51
  }
49
52
 
50
- getAvgMTU () {
53
+ getAvgMTU() {
51
54
  let totalMTU = 0
52
55
  let count = 0
53
56
  for (const conn of this.swarm.connections) {
@@ -60,7 +63,7 @@ class HyperswarmStats {
60
63
  return totalMTU / count
61
64
  }
62
65
 
63
- getRetransmitsAcrossAllStreams () {
66
+ getRetransmitsAcrossAllStreams() {
64
67
  let countFromCurrentConns = 0
65
68
  for (const conn of this.swarm.connections) {
66
69
  countFromCurrentConns += conn.rawStream?.retransmits || 0
@@ -69,7 +72,7 @@ class HyperswarmStats {
69
72
  return countFromCurrentConns + this._retransmitsOfClosedSwarmStreams
70
73
  }
71
74
 
72
- getFastRecoveriesAcrossAllStreams () {
75
+ getFastRecoveriesAcrossAllStreams() {
73
76
  let countFromCurrentConns = 0
74
77
  for (const conn of this.swarm.connections) {
75
78
  countFromCurrentConns += conn.rawStream?.fastRecoveries || 0
@@ -78,7 +81,7 @@ class HyperswarmStats {
78
81
  return countFromCurrentConns + this._fastRecoveriesOfClosedSwarmStreams
79
82
  }
80
83
 
81
- getRTOCountAcrossAllStreams () {
84
+ getRTOCountAcrossAllStreams() {
82
85
  let countFromCurrentConns = 0
83
86
  for (const conn of this.swarm.connections) {
84
87
  countFromCurrentConns += conn.rawStream?.rtoCount || 0
@@ -87,7 +90,7 @@ class HyperswarmStats {
87
90
  return countFromCurrentConns + this._rtoCountOfClosedSwarmStreams
88
91
  }
89
92
 
90
- getBytesTransmittedAcrossAllStreams () {
93
+ getBytesTransmittedAcrossAllStreams() {
91
94
  let bytesFromCurrentConns = 0
92
95
  for (const conn of this.swarm.connections) {
93
96
  bytesFromCurrentConns += conn.rawStream?.bytesTransmitted || 0
@@ -96,7 +99,7 @@ class HyperswarmStats {
96
99
  return bytesFromCurrentConns + this._bytesTransmittedOverClosedSwarmStreams
97
100
  }
98
101
 
99
- getBytesReceivedAcrossAllStreams () {
102
+ getBytesReceivedAcrossAllStreams() {
100
103
  let bytesFromCurrentConns = 0
101
104
  for (const conn of this.swarm.connections) {
102
105
  bytesFromCurrentConns += conn.rawStream?.bytesReceived || 0
@@ -105,7 +108,7 @@ class HyperswarmStats {
105
108
  return bytesFromCurrentConns + this._bytesReceivedOverClosedSwarmStreams
106
109
  }
107
110
 
108
- getPacketsTransmittedAcrossAllStreams () {
111
+ getPacketsTransmittedAcrossAllStreams() {
109
112
  let packetsFromCurrentConns = 0
110
113
  for (const conn of this.swarm.connections) {
111
114
  packetsFromCurrentConns += conn.rawStream?.packetsTransmitted || 0
@@ -114,7 +117,7 @@ class HyperswarmStats {
114
117
  return packetsFromCurrentConns + this._packetsTransmittedOverClosedSwarmStreams
115
118
  }
116
119
 
117
- getPacketsReceivedAcrossAllStreams () {
120
+ getPacketsReceivedAcrossAllStreams() {
118
121
  let packetsFromCurrentConns = 0
119
122
  for (const conn of this.swarm.connections) {
120
123
  packetsFromCurrentConns += conn.rawStream?.packetsReceived || 0
@@ -123,11 +126,11 @@ class HyperswarmStats {
123
126
  return packetsFromCurrentConns + this._packetsReceivedOverClosedSwarmStreams
124
127
  }
125
128
 
126
- get nrPeers () {
129
+ get nrPeers() {
127
130
  return this.swarm.peers.size
128
131
  }
129
132
 
130
- toJson () {
133
+ toJson() {
131
134
  return {
132
135
  dht: this.dhtStats.toJson(),
133
136
  nrPeers: this.nrPeers,
@@ -144,7 +147,7 @@ class HyperswarmStats {
144
147
  }
145
148
  }
146
149
 
147
- toString () {
150
+ toString() {
148
151
  return `Hyperswarm Stats
149
152
  - hyperswarm_nr_peers: ${this.nrPeers}
150
153
  - hyperswarm_client_connections_opened: ${this.connects.client.opened}
@@ -164,118 +167,118 @@ class HyperswarmStats {
164
167
  ${this.dhtStats.toString()}`
165
168
  }
166
169
 
167
- registerPrometheusMetrics (promClient) {
170
+ registerPrometheusMetrics(promClient) {
168
171
  this.dhtStats.registerPrometheusMetrics(promClient)
169
172
 
170
173
  const self = this
171
- new promClient.Gauge({ // eslint-disable-line no-new
174
+ new promClient.Gauge({
172
175
  name: 'hyperswarm_nr_peers',
173
176
  help: 'Number of peers this swarm is connected to',
174
- collect () {
177
+ collect() {
175
178
  this.set(self.nrPeers)
176
179
  }
177
180
  })
178
181
 
179
- new promClient.Gauge({ // eslint-disable-line no-new
182
+ new promClient.Gauge({
180
183
  name: 'hyperswarm_client_connections_opened',
181
184
  help: 'Total number of client connections opened by the swarm',
182
- collect () {
185
+ collect() {
183
186
  this.set(self.connects.client.opened)
184
187
  }
185
188
  })
186
- new promClient.Gauge({ // eslint-disable-line no-new
189
+ new promClient.Gauge({
187
190
  name: 'hyperswarm_client_connections_closed',
188
191
  help: 'Total number of client connections closed by the swarm',
189
- collect () {
192
+ collect() {
190
193
  this.set(self.connects.client.closed)
191
194
  }
192
195
  })
193
- new promClient.Gauge({ // eslint-disable-line no-new
196
+ new promClient.Gauge({
194
197
  name: 'hyperswarm_client_connections_attempted',
195
198
  help: 'Total number of client connections attempted by the swarm',
196
- collect () {
199
+ collect() {
197
200
  this.set(self.connects.client.attempted)
198
201
  }
199
202
  })
200
203
 
201
- new promClient.Gauge({ // eslint-disable-line no-new
204
+ new promClient.Gauge({
202
205
  name: 'hyperswarm_server_connections_opened',
203
206
  help: 'Total number of server connections opened by the swarm',
204
- collect () {
207
+ collect() {
205
208
  this.set(self.connects.server.opened)
206
209
  }
207
210
  })
208
- new promClient.Gauge({ // eslint-disable-line no-new
211
+ new promClient.Gauge({
209
212
  name: 'hyperswarm_server_connections_closed',
210
213
  help: 'Total number of server connections closed by the swarm',
211
- collect () {
214
+ collect() {
212
215
  this.set(self.connects.server.closed)
213
216
  }
214
217
  })
215
218
 
216
- new promClient.Gauge({ // eslint-disable-line no-new
219
+ new promClient.Gauge({
217
220
  name: 'hyperswarm_total_bytes_transmitted_over_swarm_streams',
218
221
  help: 'Total bytes transmitted over the streams exposed explicitly by hyperswarm connections',
219
- collect () {
222
+ collect() {
220
223
  this.set(self.getBytesTransmittedAcrossAllStreams())
221
224
  }
222
225
  })
223
- new promClient.Gauge({ // eslint-disable-line no-new
226
+ new promClient.Gauge({
224
227
  name: 'hyperswarm_total_bytes_received_over_swarm_streams',
225
228
  help: 'Total bytes received over the streams exposed explicitly by hyperswarm connections',
226
- collect () {
229
+ collect() {
227
230
  this.set(self.getBytesReceivedAcrossAllStreams())
228
231
  }
229
232
  })
230
- new promClient.Gauge({ // eslint-disable-line no-new
233
+ new promClient.Gauge({
231
234
  name: 'hyperswarm_total_packets_transmitted_over_swarm_streams',
232
235
  help: 'Total packets transmitted over the streams exposed explicitly by hyperswarm connections',
233
- collect () {
236
+ collect() {
234
237
  this.set(self.getPacketsTransmittedAcrossAllStreams())
235
238
  }
236
239
  })
237
- new promClient.Gauge({ // eslint-disable-line no-new
240
+ new promClient.Gauge({
238
241
  name: 'hyperswarm_total_packets_received_over_swarm_streams',
239
242
  help: 'Total packets received over the streams exposed explicitly by hyperswarm connections',
240
- collect () {
243
+ collect() {
241
244
  this.set(self.getPacketsReceivedAcrossAllStreams())
242
245
  }
243
246
  })
244
247
 
245
- new promClient.Gauge({ // eslint-disable-line no-new
248
+ new promClient.Gauge({
246
249
  name: 'hyperswarm_avg_congestion_window',
247
250
  help: 'Average size of the congestion window (over all hyperswarm connections)',
248
- collect () {
251
+ collect() {
249
252
  this.set(self.getAvgCongestionWindow())
250
253
  }
251
254
  })
252
255
 
253
- new promClient.Gauge({ // eslint-disable-line no-new
256
+ new promClient.Gauge({
254
257
  name: 'hyperswarm_avg_mtu',
255
258
  help: 'Average size of the Maximum Transmission Unit (over all hyperswarm connections)',
256
- collect () {
259
+ collect() {
257
260
  this.set(self.getAvgMTU())
258
261
  }
259
262
  })
260
263
 
261
- new promClient.Gauge({ // eslint-disable-line no-new
264
+ new promClient.Gauge({
262
265
  name: 'hyperswarm_total_retransmits_over_swarm_streams',
263
266
  help: 'Total UDX retransmits (after a lost packet), summed across the streams exposed explicitly by hyperswarm connections',
264
- collect () {
267
+ collect() {
265
268
  this.set(self.getRetransmitsAcrossAllStreams())
266
269
  }
267
270
  })
268
- new promClient.Gauge({ // eslint-disable-line no-new
271
+ new promClient.Gauge({
269
272
  name: 'hyperswarm_total_fast_recoveries_over_swarm_streams',
270
273
  help: 'Total UDX fast recoveries summed across the streams exposed explicitly by hyperswarm connections',
271
- collect () {
274
+ collect() {
272
275
  this.set(self.getFastRecoveriesAcrossAllStreams())
273
276
  }
274
277
  })
275
- new promClient.Gauge({ // eslint-disable-line no-new
278
+ new promClient.Gauge({
276
279
  name: 'hyperswarm_total_rto_count_over_swarm_streams',
277
280
  help: 'Total UDX retransmission time-outs, summed across the streams exposed explicitly by hyperswarm connections',
278
- collect () {
281
+ collect() {
279
282
  this.set(self.getRTOCountAcrossAllStreams())
280
283
  }
281
284
  })
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "hyperswarm-stats",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Hyperswarm stats with prometheus support",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "standard && brittle test.js"
7
+ "format": "prettier --write .",
8
+ "lint": "lunte && prettier --check .",
9
+ "test": "npm run lint && brittle-node test.js",
10
+ "test:bare": "brittle-bare test.js"
8
11
  },
9
12
  "repository": {
10
13
  "type": "git",
@@ -26,12 +29,21 @@
26
29
  ],
27
30
  "devDependencies": {
28
31
  "b4a": "^1.6.6",
29
- "brittle": "^3.6.1",
32
+ "bare-events": "^2.9.2",
33
+ "bare-prom-client": "^15.1.9",
34
+ "brittle": "^4.0.0",
30
35
  "hyperswarm": "^4.8.2",
31
- "prom-client": "^15.1.3",
32
- "standard": "^17.1.0"
36
+ "lunte": "^1.8.6",
37
+ "prettier": "^3.9.10",
38
+ "prettier-config-holepunch": "^2.0.0"
33
39
  },
34
40
  "dependencies": {
35
41
  "hyperdht-stats": "^1.7.0"
42
+ },
43
+ "imports": {
44
+ "events": {
45
+ "bare": "bare-events",
46
+ "default": "events"
47
+ }
36
48
  }
37
49
  }