-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetflowscore.py
166 lines (137 loc) · 5.13 KB
/
netflowscore.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
import os
import urllib
import webapp2
import random
import uuid
import logging
import bisect
from datetime import datetime, timedelta
from google.appengine.ext import ndb
TEST_DEADLINE_IN_SECS = 10.0
def create_test_point(netnode_idx, calibration = False):
token = str(uuid.uuid4())
tp = TestPoint(id=token)
tp.iteration = 0
tp.freeze_size_iteration = 0
tp.calibration = calibration
tp.netnode_idx = netnode_idx
tp.score = -1
tp.start_time = None
tp.put()
return token
# indexed by ip_addr + device_type OR, by ip_addr alone, if device type not
# available
class NetworkNodeModel(ndb.Model):
reference_score = ndb.IntegerProperty()
ip_addr = ndb.StringProperty()
device_type = ndb.StringProperty()
class TestPoint(ndb.Model):
calibration = ndb.BooleanProperty()
netnode_idx = ndb.StringProperty()
start_time = ndb.DateTimeProperty(auto_now_add=False)
iteration = ndb.IntegerProperty()
freeze_size_iteration = ndb.IntegerProperty()
score = ndb.FloatProperty()
class StartHandler(webapp2.RequestHandler):
def get(self):
ip = self.request.remote_addr
device_type = self.request.get('device_type')
version = self.request.get('version')
netnode_idx = ip + device_type
nn = NetworkNodeModel.get_by_id(netnode_idx)
if nn == None:
self.error(403)
self.status_message = "This device is not calibrated on this network. /calibrate first."
return
logging.info("Starting test for device idx: " + netnode_idx)
token = create_test_point(netnode_idx)
if version == '2':
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write(token)
else:
self.response.set_status(303)
self.response.headers['Location'] = '/test?token=' + token
class CalibrateHandler(webapp2.RequestHandler):
def get(self):
ip = self.request.remote_addr
device_type = self.request.get('device_type')
version = self.request.get('version')
netnode_idx = ip + device_type
# look up or create the NetworkNode
nn = NetworkNodeModel.get_by_id(netnode_idx)
if nn == None:
logging.info("Created new network node with id: " + netnode_idx)
nn = NetworkNodeModel(id=netnode_idx)
nn.device_type = device_type
nn.put()
else:
logging.info("Recalibrated network node with id: " + netnode_idx)
# launch a test with calibration flag
token = create_test_point(netnode_idx, calibration=True)
if version == '2':
self.response.headers['Content-Type'] = 'text/plain'
self.response.out.write(token)
else:
self.response.set_status(303)
self.response.headers['Location'] = '/test?token=' + token
class TestHandler(webapp2.RequestHandler):
def get(self):
token = str(self.request.get('token'))
tp = TestPoint.get_by_id(token)
if not tp:
self.error(500)
self.status_message = "Test is not in progress."
logging.error("Test is not in progress.")
# If this is the first time we see this token, and therefore the start of the
# test proper, then record the start time.
if tp.start_time == None:
tp.start_time = datetime.now()
# All requests redirected. All but the last go back to this handler.
# The last one goes to the result page.
self.response.set_status(303)
delta = datetime.now() - tp.start_time
if (delta.total_seconds() > TEST_DEADLINE_IN_SECS):
self.response.headers['Location'] = '/result?token=' + token
else:
self.response.headers['Location'] = '/test?token=' + token
for i in range(1000 * (1 << tp.freeze_size_iteration)):
self.response.out.write("%04x" % i)
tp.iteration += 1;
if (delta.total_seconds() < TEST_DEADLINE_IN_SECS / 4):
tp.freeze_size_iteration += 1
tp.put()
class ResultHandler(webapp2.RequestHandler):
def get(self):
token = self.request.get('token')
tp = TestPoint.get_by_id(token)
if not tp:
logging.error("Test is not in progress.")
self.response.headers['Content-Type'] = 'text/plain'
# A score for this testpoint has already been computed.
# Return just that.
if tp.score != -1:
self.response.out.write("%.2f" % tp.score)
return
# try to look up the NetworkNode
nn = NetworkNodeModel.get_by_id(tp.netnode_idx)
if nn == None:
logging.error("Could not find network node for calibration test for idx "
+ tp.netnode_idx)
return
if tp.calibration:
nn.reference_score = tp.iteration
tp.calibration = False
nn.put()
tp.score = float(min(tp.iteration, nn.reference_score)) / nn.reference_score
logging.info("score = %.2f" % tp.score)
self.response.out.write("%.2f" % tp.score)
tp.put()
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write(random.getrandbits(1000))
application = webapp2.WSGIApplication([('/', MainHandler),
('/start', StartHandler),
('/test', TestHandler),
('/result', ResultHandler),
('/calibrate', CalibrateHandler)],
debug=True)