Changeset 8 for trunk/lib/nanownlib/storage.py
- Timestamp:
- 07/09/15 19:01:23 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/nanownlib/storage.py
r7 r8 6 6 import threading 7 7 import sqlite3 8 9 import numpy 8 10 9 11 def _newid(): … … 15 17 cursor = None 16 18 _population_sizes = None 19 _population_cache = None 17 20 18 21 def __init__(self, path): … … 22 25 self.conn.row_factory = sqlite3.Row 23 26 self._population_sizes = {} 27 self._population_cache = {} 24 28 25 29 if not exists: … … 89 93 self.conn.close() 90 94 95 91 96 def populationSize(self, probe_type): 92 97 if probe_type in self._population_sizes: … … 101 106 print(e) 102 107 return 0 108 109 110 def subseries(self, probe_type, unusual_case, size=None, offset=None, field='packet_rtt'): 111 if (probe_type,unusual_case,field) not in self._population_cache: 112 query=""" 113 SELECT %(field)s AS unusual_case, 114 (SELECT avg(%(field)s) FROM probes,analysis 115 WHERE analysis.probe_id=probes.id AND probes.test_case!=:unusual_case AND probes.type=:probe_type AND sample=u.sample) AS other_cases 116 FROM (SELECT probes.sample,%(field)s FROM probes,analysis 117 WHERE analysis.probe_id=probes.id AND probes.test_case =:unusual_case AND probes.type=:probe_type) u 118 """ % {"field":field} 119 120 params = {"probe_type":probe_type, "unusual_case":unusual_case} 121 cursor = self.conn.cursor() 122 cursor.execute(query, params) 123 self._population_cache[(probe_type,unusual_case,field)] = [dict(row) for row in cursor.fetchall()] 124 125 population = self._population_cache[(probe_type,unusual_case,field)] 126 127 if size == None or size > len(population): 128 size = len(population) 129 if offset == None or offset >= len(population) or offset < 0: 130 offset = numpy.random.random_integers(0,len(population)-1) 131 132 ret_val = population[offset:offset+size] 133 if len(ret_val) < size: 134 ret_val += population[0:size-len(ret_val)] 135 136 return ret_val 137 138 139 def clearCache(self): 140 self._population_cache = {} 141 103 142 104 143 def _insert(self, table, row):
Note: See TracChangeset
for help on using the changeset viewer.