-
Notifications
You must be signed in to change notification settings - Fork 2
/
telemetry.py
executable file
·69 lines (48 loc) · 1.93 KB
/
telemetry.py
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
#!/usr/bin/env python3
import requests
import json
import sys
import argparse
import common
def parse_args():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-b', '--beta', action='store_true', default=False,
help='use beta network')
group.add_argument('-t', '--test', action='store_true', default=False,
help='use test network')
parser.add_argument('--rpc',
help='RPC URL to contact')
parser.add_argument('-r', '--raw', action='store_true', default=False,
help='raw telemetry data')
parser.add_argument('-m', '--max_block_count', action='store_true', default=False,
help='print the maximum block count seen in the network')
return parser.parse_args(sys.argv[1:])
def average(lst):
if len(lst) == 0: return 0
return sum(lst) // len(lst)
args = parse_args()
rpc_url = common.get_rpc_url(args)
print('RPC URL = %s' % rpc_url)
if args.raw or args.max_block_count:
params = {'action' : 'telemetry', 'raw' : 'true'}
else:
params = {'action' : 'telemetry'}
session = requests.Session()
result = common.post(session, params, rpc_url)
if args.max_block_count:
metrics = result['metrics']
block_counts = [ int(x['block_count']) for x in metrics ]
# sort metrics by block_count
metrics.sort(key=lambda x: int(x['block_count']), reverse=True)
# drop the smallest 20% of block counts (outliers), keep 80% largest
limit = len(metrics) * 80 // 100
metrics_filtered = metrics[:limit]
# block counts of metrics filtered
block_counts_filtered = [ int(x['block_count']) for x in metrics_filtered ]
print('max:%s avg:%s min:%s' %
(max(block_counts), average(block_counts_filtered), min(block_counts_filtered)))
elif args.raw:
print(json.dumps(result, indent=4))
else:
print(json.dumps(result, indent=4))