forked from gousiosg/java-callgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathto_xmind.py
More file actions
121 lines (93 loc) · 2.59 KB
/
Copy pathto_xmind.py
File metadata and controls
121 lines (93 loc) · 2.59 KB
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
import sys
import json
file = sys.argv[1]
def main():
# read traces
traces = list(read_file())
# separate threads
thread_traces = separate_threads(traces)
# make tree for every thread
root = TraceNode('<root>')
for thread_id, traces in thread_traces:
t = make_tree(str(thread_id), traces)
root.add(t)
# output as xmind
print(xmind(root))
def xmind(node):
return '\n'.join(xmind_lines(node))
def xmind_lines(node):
lines = []
lines.append(node.text)
for e in node.children:
for line in xmind_lines(e):
lines.append('\t' + line)
return lines
def make_tree(name, traces):
root = TraceNode(name)
if not traces: return root
target_depth = traces[0].depth
next_level = []
for e in traces:
if e.direction == 'in': continue
if e.depth != target_depth:
next_level.append(e)
continue
if e.depth == target_depth:
name = simple_method_name(e.clazz, e.method)
node = make_tree(name, next_level)
root.add(node)
next_level = []
continue
return root
def simple_method_name(full_class, method_name):
dot = full_class.rindex('.')
simple_class = full_class[dot + 1:]
if '$' not in simple_class: return simple_class + '.' + method_name
dollar = simple_class.rindex('$')
nest_class = simple_class[dollar + 1:]
return nest_class + '.' + method_name
def separate_threads(traces):
# all threads
threads = []
for e in traces:
if e.thread not in threads:
threads.append(e.thread)
# every thread:
result = []
for e in threads:
thread_traces = []
for e2 in traces:
if e2.thread == e:
thread_traces.append(e2)
result.append((e, thread_traces))
return result
def read_file():
with open(file) as f:
for e in f:
e = e.strip()
if not e: continue
yield parse_trace(e)
def parse_trace(trace):
trace = json.loads(trace)
result = Trace()
result.time = trace['@timestamp']
result.method = trace['method']
result.clazz = trace['class']
result.depth = trace['depth']
result.thread = trace['thread']
result.direction = trace['direction']
return result
class Trace:
direction = None
thread = None
depth = None
clazz = None
method = None
time = None
class TraceNode:
def __init__(self, text):
self.text = text
self.children = []
def add(self, node):
self.children.append(node)
main()