1 | #-*- mode: Python;-*- |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import uuid |
---|
6 | import threading |
---|
7 | import sqlite3 |
---|
8 | |
---|
9 | def _newid(): |
---|
10 | return uuid.uuid4().hex |
---|
11 | |
---|
12 | |
---|
13 | class db(threading.local): |
---|
14 | conn = None |
---|
15 | cursor = None |
---|
16 | def __init__(self, path): |
---|
17 | exists = os.path.exists(path) |
---|
18 | self.conn = sqlite3.connect(path) |
---|
19 | self.conn.execute("PRAGMA foreign_keys = ON;") |
---|
20 | self.conn.row_factory = sqlite3.Row |
---|
21 | |
---|
22 | if not exists: |
---|
23 | self.conn.execute( |
---|
24 | """CREATE TABLE meta (id BLOB PRIMARY KEY, |
---|
25 | tcpts_mean REAL, |
---|
26 | tcpts_stddev REAL, |
---|
27 | tcpts_slopes TEXT) |
---|
28 | """) |
---|
29 | |
---|
30 | self.conn.execute( |
---|
31 | """CREATE TABLE probes (id BLOB PRIMARY KEY, |
---|
32 | sample INTEGER, |
---|
33 | test_case TEXT, |
---|
34 | type TEXT, |
---|
35 | tc_order INTEGER, |
---|
36 | time_of_day INTEGER, |
---|
37 | local_port INTEGER, |
---|
38 | reported INTEGER, |
---|
39 | userspace_rtt INTEGER, |
---|
40 | UNIQUE (sample, test_case)) |
---|
41 | """) |
---|
42 | |
---|
43 | self.conn.execute( |
---|
44 | """CREATE TABLE packets (id BLOB PRIMARY KEY, |
---|
45 | probe_id REFERENCES probes(id) ON DELETE CASCADE, |
---|
46 | sent INTEGER, |
---|
47 | observed INTEGER, |
---|
48 | tsval INTEGER, |
---|
49 | payload_len INTEGER, |
---|
50 | tcpseq INTEGER, |
---|
51 | tcpack INTEGER) |
---|
52 | """) |
---|
53 | |
---|
54 | self.conn.execute( |
---|
55 | """CREATE TABLE analysis (id BLOB PRIMARY KEY, |
---|
56 | probe_id UNIQUE REFERENCES probes(id) ON DELETE CASCADE, |
---|
57 | suspect TEXT, |
---|
58 | packet_rtt INTEGER, |
---|
59 | tsval_rtt INTEGER) |
---|
60 | """) |
---|
61 | |
---|
62 | self.conn.execute( |
---|
63 | """CREATE TABLE trim_analysis (id BLOB PRIMARY KEY, |
---|
64 | probe_id REFERENCES probes(id) ON DELETE CASCADE, |
---|
65 | suspect TEXT, |
---|
66 | packet_rtt INTEGER, |
---|
67 | tsval_rtt INTEGER, |
---|
68 | sent_trimmed INTEGER, |
---|
69 | rcvd_trimmed INTEGER) |
---|
70 | """) |
---|
71 | |
---|
72 | self.conn.execute( |
---|
73 | """CREATE TABLE classifier_results (id BLOB PRIMARY KEY, |
---|
74 | algorithm TEXT, |
---|
75 | params TEXT, |
---|
76 | sample_size INTEGER, |
---|
77 | num_trials INTEGER, |
---|
78 | trial_type TEXT, |
---|
79 | false_positives REAL, |
---|
80 | false_negatives REAL) |
---|
81 | """) |
---|
82 | |
---|
83 | def __del__(self): |
---|
84 | if self.conn: |
---|
85 | self.conn.commit() |
---|
86 | self.conn.close() |
---|
87 | |
---|
88 | def _insert(self, table, row): |
---|
89 | rid = _newid() |
---|
90 | keys = row.keys() |
---|
91 | columns = ','.join(keys) |
---|
92 | placeholders = ':'+', :'.join(keys) |
---|
93 | query = "INSERT INTO %s (id,%s) VALUES ('%s',%s)" % (table, columns, rid, placeholders) |
---|
94 | #print(row) |
---|
95 | self.conn.execute(query, row) |
---|
96 | return rid |
---|
97 | |
---|
98 | def addMeta(self, meta): |
---|
99 | ret_val = self._insert('meta', meta) |
---|
100 | self.conn.commit() |
---|
101 | return ret_val |
---|
102 | |
---|
103 | def addProbes(self, p): |
---|
104 | return [self._insert('probes', row) for row in p] |
---|
105 | |
---|
106 | def addPackets(self, pkts, window_size): |
---|
107 | query = ("INSERT INTO packets (id,probe_id,sent,observed,tsval,payload_len,tcpseq,tcpack)" |
---|
108 | " VALUES(randomblob(16)," |
---|
109 | "(SELECT id FROM probes WHERE local_port=:local_port AND :observed>time_of_day" |
---|
110 | " AND :observed<time_of_day+userspace_rtt+%d" |
---|
111 | " ORDER BY time_of_day ASC LIMIT 1)," |
---|
112 | ":sent,:observed,:tsval,:payload_len,:tcpseq,:tcpack)") % window_size |
---|
113 | self.conn.execute("PRAGMA foreign_keys = OFF;") |
---|
114 | self.conn.execute("CREATE INDEX IF NOT EXISTS probes_port ON probes (local_port)") |
---|
115 | cursor = self.conn.cursor() |
---|
116 | #print(query, list(pkts)[0:3]) |
---|
117 | cursor.executemany(query, pkts) |
---|
118 | self.conn.commit() |
---|
119 | self.conn.execute("PRAGMA foreign_keys = ON;") |
---|
120 | |
---|
121 | def addAnalyses(self, analyses): |
---|
122 | return [self._insert('analysis', row) for row in analyses] |
---|
123 | |
---|
124 | def addTrimAnalyses(self, analyses): |
---|
125 | return [self._insert('trim_analysis', row) for row in analyses] |
---|
126 | |
---|
127 | def addClassifierResults(self, results): |
---|
128 | ret_val = self._insert('classifier_results', results) |
---|
129 | self.conn.commit() |
---|
130 | return ret_val |
---|