-
Notifications
You must be signed in to change notification settings - Fork 1
/
heatmap_to_dot.py
executable file
·219 lines (162 loc) · 5.91 KB
/
heatmap_to_dot.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
#!/usr/bin/python
# heatmap_to_dot - Generate dot graphs from heatmap output
# Copyright (C) 2014 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301 USA
from __future__ import print_function
import json
import math
import re
import sys
root = json.load(open(sys.argv[1], 'r'))
pending = open(sys.argv[2], 'w')
merged = open(sys.argv[3], 'w')
def walk_nodes(f, acc):
def _walk_node(node, _acc):
children = node.get('children')
if children is None:
return _acc
_acc = f(node, _acc)
for child in children.values():
_acc = _walk_node(child, _acc)
return _acc
return _walk_node(root, acc)
def get_nodeset(path):
nodeset = [root]
node = root
for tok in path.split('/')[1::]:
node = node['children'][tok]
nodeset.append(node)
return nodeset
def prune(path, expunge=True):
nodeset = get_nodeset(path)
node = nodeset[-1]
parent = nodeset[-2]
if expunge:
stats = node['stats']
merged = node['merged']
for i in nodeset:
for j in i['stats']:
i['stats'][j] -= stats[j]
for j in i['merged']:
i['merged'][j] -= merged[j]
del(parent['children'][node['name']])
def dot_name(node):
return re.sub('[-/.]', '_', node['path'])
def nextrank(rank):
prune_lines = root['stats']['lines'] * 2 / 100
n = []
for _, node in rank:
for child in node['children'].values():
descs = child.get('children')
if descs is None:
continue
# Filter out small branches
if child['stats']['lines'] < prune_lines:
continue
n.append((node, child))
return n
root = get_nodeset('/nova')[-1]
# Big and irrelevant
prune('/locale')
# Represented elsewhere
prune('/tests')
# Path is too long, makes the graphs ugly
prune('/api/openstack/compute', False)
def gen_pending():
def get_pending(node):
stats = node['stats']
if stats['lines'] == 0:
return 0
return float(stats['removed']) * 100 / float(stats['lines'])
def format_pending(node, scale):
stat = get_pending(node)
stats = node['stats']
if stats['removed'] == 0:
age = 0
else:
age = stats['age_removed'] / stats['removed']
return '{name}[label="{label}({pc}%)", width={width}]'.format(
name=dot_name(node), label=node['name'],
pc=int(stat), width=math.sqrt(stat / scale) * 5)
def max_pending_f(node, _max):
stat = get_pending(node)
if stat > _max:
return stat
return _max
max_pending = walk_nodes(max_pending_f, 0)
print('digraph "Percentage of changed code" {', file=pending)
print(' node[shape=circle, fixedsize=true];', file=pending)
print(' overlap=scale;', file=pending)
print(file=pending)
print(' {};'.format(format_pending(root, max_pending)), file=pending)
print(' {}[style=filled, fillcolor=lightgreen];'.format(dot_name(root)),
file=pending)
rank = nextrank([(None, root)])
while len(rank) > 0:
print(file=pending)
print(' subgraph {', file=pending)
for parent, child in rank:
print(' {parent} -> {child};'.format(parent=dot_name(parent),
child=dot_name(child)),
file=pending)
print(' {};'.format(format_pending(child, max_pending)),
file=pending)
print(' }', file=pending)
rank = nextrank(rank)
print('}', file=pending)
def gen_merged():
def get_merged(node):
merged = node['merged']
if merged['touched'] == 0:
return 0
return float(merged['age']) / float(merged['touched'])
def format_merged(node, scale):
age = get_merged(node)
merged = node['merged']
return '{name}[label="{label}({age}d)", width={width}]'.format(
name=dot_name(node), label=node['name'],
age=int(age/60/60/24),
width=math.sqrt(age / scale) * 3)
def max_merged_f(node, _max):
stat = get_merged(node)
if stat > _max:
return stat
return _max
max_merged = walk_nodes(max_merged_f, 0)
print('digraph "Average Approval Time" {', file=merged)
print(' node[shape=circle, fixedsize=true];', file=merged)
print(' overlap=scale;', file=merged)
print(file=merged)
print(' {};'.format(format_merged(root, max_merged)), file=merged)
print(' {}[style=filled, fillcolor=lightgreen];'.format(dot_name(root)),
file=merged)
rank = nextrank([(None, root)])
while len(rank) > 0:
print(file=merged)
print(' subgraph {', file=merged)
for parent, child in rank:
print(' {parent} -> {child};'.format(parent=dot_name(parent),
child=dot_name(child)),
file=merged)
print(' {};'.format(format_merged(child, max_merged)),
file=merged)
print(' }', file=merged)
rank = nextrank(rank)
print('}', file=merged)
gen_pending()
gen_merged()
pending.close()
merged.close()