forked from USC-NSL/ripe-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosest_IXP.py
More file actions
291 lines (223 loc) · 8.15 KB
/
closest_IXP.py
File metadata and controls
291 lines (223 loc) · 8.15 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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/python
import sys
import json
import multiprocessing
from multiprocessing import Pool
import numpy as np
from math import radians, sin, cos, asin, sqrt, pi, atan2
import heapq
import traceback
import logging
import itertools
#these nasty globals allow us to share data across processes
results = list()
haystack = None
values = None
ixp2locationFile = 'ixp2location.txt'
pfx2locationFile = 'prefix_lat_lon_country_asn_2014_04_14.txt'
ixp2eyeballsFile = 'ixp2eyeballs.txt'
ixp2participantsFile = 'ixp2participants.txt'
provider2custumerFile = '20131101.ppdc-ases.txt'
ixp2customersFile = 'ixp2customers.dat'
pfx2ixpFile = 'pfx2ixp.dat'
num_process = 6
loc2ixp = {}
loc2pfx = {}
ixp2eyeballs = {}
ixp2customers = {}
def dist_vector(needle, haystack):
"""
Calculate the distance from needle to all points in haystack
"""
lats = [float(x[0]) for x in haystack]
lons = [float(x[1]) for x in haystack]
#needle[0] = float(needle[0])
#needle[1] = float(needle[1])
dlat = np.radians(lats) - radians(float(needle[0]))
#print dlat
dlon = np.radians(lons) - radians(float(needle[1]))
a = np.square(np.sin(dlat/2.0)) + cos(radians(float(needle[0]))) * np.cos(np.radians(lats)) * np.square(np.sin(dlon/2.0))
great_circle_distance = 2 * np.arcsin(np.minimum(np.sqrt(a), np.repeat(1, len(a))))
d = 6367 * great_circle_distance #vector of distances
return d
def closest(needle, haystack, values):
d = dist_vector(needle, haystack)
#i = np.argmin(d) #index of minimum distance
ixp2distance = {}
i = 0
for elem in d:
if elem <= 500:
for ixp in values[i]:
ixp_id = ixp[0]
ixp_name = ixp[1]
k = ','.join([ixp_id,ixp_name])
ixp2distance[k] = int(elem)
i +=1
return ixp2distance
#return (values[i], d[i]) #return tuple with distance and matching ip
def log_result(result):
#print result
#results = dict(results.items() + result.items())
results.append(result)
sys.stderr.write('results: %d\n' % len(results))
sys.stderr.flush()
def process(needle_list, prefix_list):
try:
pfx2ixp_tmp = {}
#for needle in needle_list:
for i in range(0, len(needle_list)):
needle = needle_list[i]
prefixes = prefix_list[i]
ixp2distance = closest(needle, haystack, values)
if ixp2distance != {}:
for prefix in prefixes:
k = ','.join([prefix[0],prefix[4]])
pfx2ixp_tmp[k] = ixp2distance
#print "temp: ", pfx2ixp_tmp
return [pfx2ixp_tmp]
except:
traceback.print_exc(file=sys.stderr)
sys.stderr.flush()
def split(l, n):
""" Yield successive n-sized chunks from l.
"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def combine_result(l):
out = {}
for elem in l:
out = dict(elem.items() + out.items())
return out
def unique_ixpLocation():
with open(ixp2locationFile) as f:
count = 0
for line in f:
count += 1
chunks = line.split('\n')[0].split('|')
if chunks[2] not in loc2ixp:
loc2ixp[chunks[2]] = []
loc2ixp[chunks[2]].append((chunks[0], chunks[1]))
#if count == 10:
# break
print count, len(loc2ixp.keys())
#print loc2ixp
def unique_prefixes():
with open(pfx2locationFile) as f:
count = 0
for line in f:
count += 1
chunks = line.split('\n')[0].split(' ')
#print chunks
loc = chunks[1]+','+chunks[2]
if loc not in loc2pfx:
loc2pfx[loc] = []
loc2pfx[loc].append(tuple(chunks))
#if count==1000:
# break
print count, len(loc2pfx.keys())
#print loc2pfx
def update_ixp2eyeballs(pfx_location2ixp):
for elem in pfx_location2ixp:
chunks = elem.split('|')
ixps = chunks[1:]
for ixp in ixps:
tmp = ixp.split(',')
if len(tmp) > 2:
ixp_id = tmp[0]
ixp_name = tmp[1]
if (ixp_id, ixp_name) not in ixp2eyeballs:
ixp2eyeballs[(ixp_id, ixp_name)] = {}
pfx_loc = chunks[0]
for pfx_tuple in loc2pfx[pfx_loc]:
pfx_asn = pfx_tuple[4]
if pfx_asn == '*':
continue
if pfx_asn not in ixp2eyeballs[(ixp_id, ixp_name)]:
ixp2eyeballs[(ixp_id, ixp_name)][pfx_asn] = 1
else:
ixp2eyeballs[(ixp_id, ixp_name)][pfx_asn] += 1
print len(pfx_location2ixp), len(ixp2eyeballs.keys())
def store_ixp2eyeballs():
fout = open(ixp2eyeballsFile,'w')
for (ixp_id,ixp_name) in ixp2eyeballs:
line = ''+ixp_id+','+ixp_name
for k,v in ixp2eyeballs[(ixp_id,ixp_name)].iteritems():
line += '|'+k+','+str(v)
line += '\n'
fout.write(line)
fout.close()
def verify_ixp2eyeballs():
count = 0
with open(ixp2eyeballsFile) as f:
for line in f:
chunks = line.split('|')
for chunk in chunks[1:]:
n = chunk.split(',')[1]
if '}' not in n:
print n
count += int(n)
print count
def get_provide2customer():
provide2customer = {}
with open(provider2custumerFile) as f:
for line in f:
line = line.strip()
chunks = line.split()
provide2customer[chunks[0]] = chunks[1:]
return provide2customer
def get_ixp2customers():
provide2customer = get_provide2customer()
with open(ixp2participantsFile) as f:
for line in f:
chunks = line.strip().split('|')
#[ixp_id, ixp_name] = chunks[:2]
key = ','.join(chunks[:2])
ixp2customers [key] = {}
for elem in chunks[2:]:
if elem not in ixp2customers [key]:
ixp2customers [key][elem] = []
if elem in provide2customer:
customers = provide2customer[elem]
ixp2customers [key][elem] = customers
with open(ixp2customersFile, 'w') as outfile:
json.dump(ixp2customers, outfile, ensure_ascii=True, encoding="ascii")
if __name__ == '__main__':
#verify_ixp2eyeballs()
get_ixp2customers()
unique_ixpLocation()
unique_prefixes()
multiprocessing.log_to_stderr(logging.ERROR)
haystack = list()
values = list()
for loc_ixp in loc2ixp:
[lat,lon] = loc_ixp.split(',')
haystack.append((lat, lon))
values.append(loc2ixp[loc_ixp])
pool = Pool(num_process) #make this the number of cores to use!
needles = list()
prefixes = list()
for prefix_loc in loc2pfx:
[lat, lon] = prefix_loc.split(',')
needles.append((lat,lon))
prefixes.append(loc2pfx[prefix_loc])
needle_lists = list(split(needles, 60000)) #convert generator into list
prefix_lists = list(split(prefixes, 60000))
#these should be lists of lists
assert len(needle_lists) == len(prefix_lists)
for i in range(0, len(needle_lists)):
needle_list = needle_lists[i]
prefix_list = prefix_lists[i]
pool.apply_async(process, (needle_list, prefix_list), callback=log_result)
pool.close()
pool.join()
#print results
#merge list of lists into one list
pfx2ixp = combine_result(list(itertools.chain(*results)))
with open(pfx2ixpFile, 'w') as outfile:
json.dump(pfx2ixp, outfile, ensure_ascii=True, encoding="ascii")
#update_ixp2eyeballs(pfx_location2ixp)
#store_ixp2eyeballs()
#distances_str = map(str, distances)
#out = '\n'.join(pfx_location2ixp)
#print (pfx_location2ixp)
#print type(pfx2ixp)