-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrace.py
71 lines (52 loc) · 1.76 KB
/
trace.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
70
71
from utils import dictget
class Trace:
"""
"""
def __init__(self):
self.buffer = []
self.size = 300 # 5min
def push(self, data):
"""
Push dataset to buffer. Length is limited to self.size
:param data: dictionary
"""
self.buffer.append(data) # append dataset
if self.size:
self.buffer = self.buffer[-self.size:] # limit to max size
def get_csv(self, key):
"""
Get trace data as CSV
[('value', ('device', 'val4')), ... ]
:param key: dest, source, tuple list with keys (nested, keys allowed)
:return:
"""
try:
# csv = ";".join(columns) + '\n'
csv = ";".join([dest for dest, src in key]) + '\n'
for d in self.buffer:
csv += ";".join(["{}".format(dictget(d, src)) for dest, src in key]) + '\n'
except:
csv = ''
return csv
def get_chart(self, key):
"""
Get elements from trace in single lists for charting
[('value', ('device', 'val4')), ... ]
:param key: dest, source, tuple list with keys (nested, keys allowed)
:return: dictionary
"""
chart = {dest: [] for dest, src in key}
for d in self.buffer:
for dest, src in key:
chart[dest].append(dictget(d, src))
return chart
if __name__ == "__main__":
trace = Trace()
trace.push({'a': 1, 'b': 10, 'c': {'q': 100}})
trace.push({'a': 2, 'b': None, 'c': {'q': 200}})
trace.push({'a': 3, 'b': 30})
trace.push({'a': 4, 'b': 40, 'c': {'q': 400}})
cfg = [('aaa', 'a'), ('bbb', 'b'), ('ccc', ('c', 'q'))]
print(trace.buffer)
print(trace.get_chart(cfg))
print(trace.get_csv(cfg))