-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.ts
126 lines (91 loc) · 3.16 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2017-2019 @polkadot/client-telemetry authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Config } from '@polkadot/client/types';
import { ChainInterface } from '@polkadot/client-chains/types';
import { BlockDb } from '@polkadot/client-db/types';
import { SyncStatus } from '@polkadot/client-sync/types';
import { TelemetryInterface } from './types';
import './polyfill';
import { logger } from '@polkadot/util';
import Base from './messages/Base';
import BlockImport from './messages/BlockImport';
import Connected from './messages/Connected';
import Interval from './messages/Interval';
import Started from './messages/Started';
const l = logger('telemetry');
export default class Telemetry implements TelemetryInterface {
private blocks: BlockDb;
private isActive = false;
private chain: string;
private name: string;
private url: string;
private websocket: WebSocket | null = null;
public constructor ({ telemetry }: Config, { blocks, chain }: ChainInterface) {
const name = telemetry.name ? telemetry.name.trim() : '';
this.blocks = blocks;
this.isActive = !!name.length && !!telemetry.url.length;
this.chain = chain.name;
this.name = name;
this.url = telemetry.url;
}
// eslint-disable-next-line @typescript-eslint/require-await
public async start (): Promise<void> {
if (!this.isActive) {
return;
}
this.connect();
}
// eslint-disable-next-line @typescript-eslint/require-await
public async stop (): Promise<boolean> {
this.isActive = false;
if (this.websocket) {
this.websocket.close();
this.websocket = null;
}
return true;
}
private connect (): void {
if (!this.isActive) {
return;
}
l.log(`Connecting to telemetry, url=${this.url}, name=${this.name}`);
const websocket = new WebSocket(this.url);
websocket.onclose = (): void => {
l.debug((): string => 'Disconnected from telemetry');
this.websocket = null;
setTimeout((): void => {
this.connect();
}, 5000);
};
websocket.onopen = (): void => {
l.debug((): string => 'Connected to telemetry');
this.websocket = websocket;
this.sendInitial();
};
}
public blockImported (): void {
const bestHash = this.blocks.bestHash.get();
const bestNumber = this.blocks.bestNumber.get();
this.send(new BlockImport(bestHash, bestNumber));
}
public intervalInfo (peers: number, status: SyncStatus): void {
const bestHash = this.blocks.bestHash.get();
const bestNumber = this.blocks.bestNumber.get();
this.send(new Interval(bestHash, bestNumber, peers, status));
}
private sendInitial (): void {
const bestHash = this.blocks.bestHash.get();
const bestNumber = this.blocks.bestNumber.get();
this.send(new Started(bestHash, bestNumber));
this.send(new Connected(this.chain, this.name));
}
private send (message: Base): void {
if (!this.websocket) {
return;
}
const json = JSON.stringify(message);
l.debug((): string => `Sending ${json}`);
this.websocket.send(json);
}
}