-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology-vis.py
88 lines (77 loc) · 2.07 KB
/
topology-vis.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
import json
import networkx as nx
import matplotlib.pyplot as plt
import urllib2
import sys, getopt
try:
opts, args = getopt.getopt(sys.argv[1:],"u:f:",["url=","file="])
except getopt.GetoptError:
print 'topo.py -u <url> or topo.py -f <file>'
sys.exit(2)
url = None
inputfile = ""
for opt, arg in opts:
if opt == '-u':
url = arg
elif opt == '-f':
inputfile = arg
## Load data from json
api="/api/v1/topology/"
summary_api="summary"
topologies=""
##Get list of topologies
if url is not None:
rest_url = url + api + summary_api
topologies=json.load(urllib2.urlopen(rest_url))['topologies']
else:
topologies=json_load(open(inputfile))
topo_id=[]
for topology in topologies:
topo_id.append(topology['id'])
##Get details of topology
visualization_api="/visualization"
for id in topo_id:
rest_url = url + api + id + visualization_api
json_data=json.load(urllib2.urlopen(rest_url))
labels={}
## initialize a directed graph
G = nx.DiGraph()
edge_labels=dict()
nodelist=[]
for key in json_data.keys():
## filter acker and system nodes
if key =='__acker' or key =='__system':
continue
storm_component=json_data[key]
inputs=storm_component[':inputs']
G.add_node(key)
labels[key]=key
nodelist.append(key)
for input in inputs:
node=input[':component']
##Filter edges having acker and system nodes
if node =='__acker' or node =='__system':
continue
stream = input[':stream']
edge_labels[(key, input[':component'])] = stream
G.add_edge(input[':component'], key)
pos = nx.circular_layout(G, dim=2, scale=2)
number_of_nodes= len(G.nodes())
for edge in G.edges():
node=edge[0]
try:
ind=nodelist.index(node)
del nodelist[ind]
except:
pass
spout=nodelist[0]
##Create graph
nx.draw(G, pos=pos)
##Add node labels
nx.draw_networkx_labels(G, pos,labels, font_size=15)
##Add edge labels
nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels)
##Change edge color
nx.draw_networkx_edges(G,pos=pos, edge_color='b')
##show graph
plt.show()