-
Notifications
You must be signed in to change notification settings - Fork 4
/
ResultSet.py
151 lines (119 loc) · 4.12 KB
/
ResultSet.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
"""
This file is subject to the terms and conditions of the GNU General
Public License. See the file COPYING in the main directory of this
archive for more details.
"""
import math
import datetime
from copy import copy
from socket import gethostname
from time import sleep, localtime
import MySQLdb
#
# Constructor
#
# Input: None
#
# Output: None
#
class ResultSet:
def __init__(self):
# initialize a new dictionary to hold the data
self.data = {}
self.verboseLevel = 1
self.hoplist = None
#self.plot = Gnuplot.Gnuplot(debug=1)
def updateHoplist(self,hoplist):
self.hoplist = hoplist
# check if the hoplist matches with the dataset we got
c = self.conn.cursor()
c.execute("SELECT COUNT(*) FROM hoplist")
r = c.fetchone()
print "new",len(hoplist),"old",r[0]
# if there are no hop stats in our dataset,
# then we're good. + we insert them while we're here
if r[0] == 0:
print "hoplist matches"
hopCounter = 1
for t in self.hoplist:
print type (t), t
c.execute("INSERT INTO hoplist VALUES (%s,%s)",(hopCounter,t))
hopCounter += 1
return
elif r[0] != len(hoplist):
print "ERROR: length of the hoplist doesn't match our dataset"
sys.exit(1)
#
# DB related functions
#
def dbCommit (self):
""" Commit changes to the DB file
As committing each recieved packet would kill both performance and
some HDDs are changes commited (written) to file in batches
"""
self.conn.commit()
def dbConnect (self, hostname, username, password, dbName):
"""Connecting to a database
Attaching to a file which holds the sqlite3 database
Keyword arguments:
dbfile -- relative path to the db file
"""
# this block could probably have been done a bit neater,
# for ex move this to a config file - but it's OKAY for now.
try:
self.conn = MySQLdb.connect(
host=hostname,
user=username,
passwd=password,
db=dbName)
except ValueError:
raise Exception( "I crashed horribly, probably because the database {0} does not exist.".format(dbName))
# initializing tables if needed
c = self.conn.cursor ()
c.execute ("CREATE TABLE IF NOT EXISTS data (hop INTEGER, time TEXT, packetsize INTEGER, rtt FLOAT)")
c.execute ("CREATE TABLE IF NOT EXISTS hoplist (hop INTEGER PRIMARY KEY, ip TEXT)")
def dbClose (self):
self.conn.close()
#
# General methods
#
def add (self, hop, size, rtt, time):
"""Saves a new observation to the database
Keyword arguments:
hop -- hop number (int)
size -- packetsize
rtt -- observed rtt
time -- timestamp of when the round was started
"""
query = "INSERT INTO data VALUES ({0}, {1}, {2}, {3})".format(hop, time, size, rtt)
c = self.conn.cursor ()
c.execute (query)
# Find the percentile of a list of values.
# @parameter N - is a list of values. Note N MUST BE already sorted.
# @parameter percent - a float value from 0.0 to 1.0.
# @parameter key - optional key function to compute value from each element of N.
# @return - the percentile of the values
def percentile (self, N, percent, key=lambda x:x):
if not N:
return None
k = (len(N)-1)*percent
f = math.floor(k)
c = math.ceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) * (c-k)
d1 = key(N[int(c)]) * (k-f)
return d0+d1
#
# ResultTable::median
#
# Input:
#
# Output: Median value
#
# Compute the median of an array of doubles.
# As a side effect, the input array is sorted
#
def median (self, values, numValues=0):
values = values.sort()
return self.percentile(values,0.5)