-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackend.py
129 lines (101 loc) · 3.32 KB
/
backend.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
import bottle
import json
from ripe.atlas.sagan import TracerouteResult
import networkx as nx
import requests
from collections import defaultdict
import math
stat_ripe = "https://stat.ripe.net/data/routing-status/data.json"
with open('metadata.json') as f:
exp_config = json.load(f)
# exp_config = {
# '0':{
# 'ipv4': '3806401',
# 'ipv6': '3806402',
# 'label': 'Denmark to Comcast'
# },
# '1':{
# 'ipv4': '3814200',
# 'ipv6': '3814201',
# 'label': 'Somewhere to Google'
# }
# }
addr2as = {}
with open('as_map.json') as f:
addr2as = json.load(f)
def get_addr_as(a):
return addr2as.get(a, None)
def transform_graph(g, origin_asn, dest_asn):
nl = []
n_idx = {}
idx = 0
for n in g.nodes_iter():
# idx = g.node[n]['index']
if n == origin_asn:
nl.append({'label': n, 'id': idx, 'value': g.node[n]['hops'],
'x': 10, 'y': 140, 'color': 'green'})
elif n == dest_asn:
nl.append({'label': n, 'id': idx, 'value': g.node[n]['hops'],
'x': 600, 'y': 140, 'color': 'yellow'})
else:
nl.append({'label': n, 'id': idx, 'value': g.node[n]['hops']})
n_idx[n] = idx
idx += 1
el = [{'to': n_idx[t],
'from': n_idx[s],
'value': d['rtt'],
'label': d['rtt']} for s, t, d in g.edges_iter(data=True)]
return nl, el
bottle.app().catchall = False
bottle.debug(True)
@bottle.hook('after_request')
def enable_cors():
bottle.response.headers['Access-Control-Allow-Origin'] = '*'
@bottle.route('/path/<family>')
def index(family):
exp_id = bottle.request.query.id
res_blob = exp_config[exp_id][family]
# Read the data
G = nx.DiGraph()
hop_cnt = defaultdict(int)
hop_idx = defaultdict(int)
origin_asn = None
seq = []
for res_set in res_blob:
sagan_res = TracerouteResult(res_set)
asn = get_addr_as(sagan_res.origin)
prev_asn = asn
origin_asn = asn
hop_cnt[asn] += 1
for hop in sagan_res.hops:
hop_addr = set()
for packet in hop.packets:
if packet.origin is None:
continue
hop_addr.add(packet.origin)
for a in hop_addr:
asn = get_addr_as(a)
if asn is not None:
hop_cnt[asn] += 1
hop_idx[asn] = hop.index
addr2as[a] = asn
else:
hop_cnt[prev_asn] += 1
hop_idx[prev_asn] = hop.index
addr2as[a] = prev_asn
if asn is not None and prev_asn != asn:
G.add_edge(prev_asn, asn, rtt=hop.median_rtt)
prev_asn = asn
for n in G.nodes_iter():
G.node[n]['hops'] = hop_cnt[n]
G.node[n]['index'] = hop_idx[n]
n, e = transform_graph(G, origin_asn, asn)
# Return an object
return {'nodes': n, 'edges': e}
@bottle.route('/experiment')
def list_experiment():
return {'items': [dict((('id', k),
('label', v['label']))) for k,
v in
exp_config.iteritems() if len(v['ipv4']+v['ipv6']) >= 2]}
bottle.run(host='localhost', port=8080)