-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesign.py
234 lines (207 loc) · 8.4 KB
/
design.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#
# Microlog. Copyright (c) 2023 laffra, dcharbon. All rights reserved.
#
import js # type: ignore
import json
from microlog.models import Call
from dashboard import profiler
from dashboard import colors
class Node():
def __init__(self):
self.index = -1
self.depth = -1
self.count = 1
def setName(self, moduleName, className):
self.moduleName = moduleName
self.className = className
self.name = f"{moduleName}.{className}" if className else moduleName
return self
def setIndex(self, index):
if self.index == -1:
self.index = index
return self
def setDepth(self, depth):
self.depth = depth
return self
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
return other.name == self.name
class Edge():
def __init__(self):
self.counts = {}
self.duration = {}
def connect(self, fromNode: Node, toNode: Node, function: str, duration: int):
self.fromNode = fromNode
self.toNode = toNode
self.counts[function] = self.counts.get(function, 0) + 1
self.duration[function] = self.duration.get(function, 0) + duration
return self
def __repr__(self):
return f"{self.fromNode.name} => {self.toNode.name}"
def __hash__(self):
return self.__repr__().__hash__()
def __eq__(self, other):
return other.__repr__() == self.__repr__()
class Design():
LEVEL1 = 0
LEVEL2 = 50
LEVEL3 = 100
def __init__(self, calls):
self.nodes = {}
self.edges = {}
self.calls = []
for call in calls:
self.addCall(call)
def getNode(self, name, depth):
parts = name.split(".")
cls = parts[-2]
module = ".".join(parts[:-2])
key = f"{module}.{cls}"
if not key in self.nodes:
self.nodes[key] = Node()
return self.nodes[key].setName(module, cls).setIndex(len(self.nodes)).setDepth(depth)
def getEdge(self, fromNode, toNode, function, duration):
if not fromNode or not toNode:
return None
key = f"{fromNode.name}=>{toNode.name}"
if not key in self.edges:
self.edges[key] = Edge()
return self.edges[key].connect(fromNode, toNode, function, duration)
def addCall(self, call: Call):
self.calls.append(call)
fromNode = self.getNode(call.callerSite.name, call.depth)
toNode = self.getNode(call.callSite.name, call.depth)
function = call.getShortName()
self.getEdge(fromNode, toNode, function, call.duration)
def draw(self):
nodes = {}
edges = {}
edgeCount = len(self.edges)
level = 3 if edgeCount > self.LEVEL3 else 2 if edgeCount > self.LEVEL2 else 1
def cluster(node):
if level == 1:
name, cls = node.name, node.className
elif level == 2:
name, cls = ".".join(node.name.split(".")[:-1]), ""
else:
name, cls = node.name.split(".")[0], ""
if name in nodes:
nodes[name].count += 1
return nodes[name]
node = Node().setName(name, cls).setIndex(len(nodes))
node.selfReference = 0
nodes[name] = node
return node
liveNodes = set()
calls = []
for edge in reversed(list(self.edges.values())):
fromNode = cluster(edge.fromNode)
toNode = cluster(edge.toNode)
if fromNode == toNode and level > 1:
continue
for function, count in edge.counts.items():
if level == 3 and count < 2:
continue
if function in ["__init__", "__getattr__", "__get__", "__str__", "__call__", "<module>"]:
continue
liveNodes.add(fromNode)
liveNodes.add(toNode)
if fromNode == toNode:
fromNode.selfReference += 1
calls.append(f"{fromNode.name} => {toNode.name}.{function} called {count} times, total duration: {edge.duration[function]}s")
edges[(fromNode.index, toNode.index, function)] = {
"from": fromNode.index,
"to": toNode.index,
"label": function,
"value": edge.duration[function],
"font": {
"face": "Courier",
"bold": "12pt courier white",
"color": "white",
"background": "#222",
"strokeWidth": 0,
},
"selfReferenceSize": fromNode.selfReference * 15,
"scaling": {
"max": 9,
"label": {
"max": 21,
"enabled": False,
},
},
"arrows": {
"to": {
"enabled": True,
"type": "arrow",
},
},
}
print("draw graph", "\n".join(calls))
js.drawGraph(json.dumps({
"nodes": [
{
"id": node.index,
"label": node.name,
"level": node.depth,
"shape": "circularImage" if self.getImage(node) else "box",
"borderWidth": 2,
"value": node.count * 4,
"size": 30,
"font": { "color": "white" },
"image": self.getImage(node),
"color": {
"border": colors.getColor(node.name, colors.DISCO),
"background": "darkgreen" if node.name == "__main__" else colors.getColor(node.name, colors.DISCO),
"highlight": { "border": "yellow", "background": "green" },
"hover": { "border": "orange", "background": "grey" },
},
"imagePadding": { "left": 2, "top": 2, "right": 2, "bottom": 8 },
} for node in liveNodes ],
"edges": list(edges.values()),
}))
def getImage(self, node):
if node.name in images:
return images[node.name]
topName = node.name.split(".")[0]
if topName in images:
return images[topName]
images = {
"runpy": "/microlog/images/icons/python.png",
"matplotlib": "/microlog/images/icons/matplotlib.png",
"matplotlib_inline": "/microlog/images/icons/matplotlib.png",
"squarify": "/microlog/images/icons/squarify.png",
"pandas": "/microlog/images/icons/pandas.png",
"numpy": "/microlog/images/icons/numpy.png",
"asyncio": "/microlog/images/icons/asyncio.png",
"pyparsing": "/microlog/images/icons/pyparsing.png",
"ssl": "/microlog/images/icons/ssl.png",
"http": "/microlog/images/icons/http.png",
"httplib2": "/microlog/images/icons/http.png",
"urllib3": "/microlog/images/icons/http.png",
"socket": "/microlog/images/icons/http.png",
"pluggy": "/microlog/images/icons/pluggy.webp",
"pytest": "/microlog/images/icons/pytest.png",
"testing_tools": "/microlog/images/icons/pytest.png",
"_pytest": "/microlog/images/icons/pytest.png",
"networkx": "/microlog/images/icons/networkx.png",
"re": "/microlog/images/icons/re.png",
"sre_compile": "/microlog/images/icons/re.png",
"sre_parse": "/microlog/images/icons/re.png",
"dataclasses": "/microlog/images/icons/dataclasses.png",
"tornado": "/microlog/images/icons/tornado.png",
"urllib": "/microlog/images/icons/urllib.png",
"guppy": "/microlog/images/icons/guppy.png",
"zmq": "/microlog/images/icons/zmq.png",
"email": "/microlog/images/icons/email.png",
"google_auth_oauthlib": "/microlog/images/icons/google.png",
"google_auth_httplib2": "/microlog/images/icons/google.png",
"googleapiclient": "/microlog/images/icons/google.png",
"google": "/microlog/images/icons/google.png",
"wsgiref": "/microlog/images/icons/wsgi.png",
"IPython": "/microlog/images/icons/jupyter.png",
"jupyter_client": "/microlog/images/icons/jupyter.png",
"ipykernel": "/microlog/images/icons/jupyter.png",
"Jupyter": "/microlog/images/icons/jupyter.png",
"microlog": "/microlog/images/icons/microlog.png",
}