-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.py
55 lines (47 loc) · 1.56 KB
/
stats.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
import collections
import logging
import time
import util
class Stats:
def __init__(self):
self.printing = True
self.thread = util.start_daemon(self.run)
self.trackers = self.create_trackers()
def create_trackers(self):
return (
collections.Counter(),
collections.defaultdict(list),
)
def count(self, key, delta=1):
self.trackers[0][str(key)] += delta
def meter(self, key, value):
self.trackers[1][key].append(value)
def run(self):
prev_cols = None
while True:
time.sleep(3)
(counters, meters) = self.trackers
self.trackers = self.create_trackers()
record = {}
for key, count in counters.items():
record['# ' + key] = str(count)
for key, values in meters.items():
value = sum(values) / len(values)
if value < 1000:
record['avg ' + key] = '{:#.3g}'.format(value)
else:
record['avg ' + key] = str(value).split('.')[0]
if not record:
continue
cols = sorted(record.keys())
if self.printing:
if cols != prev_cols:
logging.info('{}'.format(' | '.join(cols)))
logging.info(' | '.join(
' '*(len(col) - len(record[col])) + record[col]
for col in cols
))
prev_cols = cols
INSTANCE = Stats()
COUNT = INSTANCE.count
METER = INSTANCE.meter