Ignore:
Timestamp:
07/09/15 19:01:23 (9 years ago)
Author:
tim
Message:

.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/nanownlib/storage.py

    r7 r8  
    66import threading
    77import sqlite3
     8
     9import numpy
    810
    911def _newid():
     
    1517    cursor = None
    1618    _population_sizes = None
     19    _population_cache = None
    1720   
    1821    def __init__(self, path):
     
    2225        self.conn.row_factory = sqlite3.Row
    2326        self._population_sizes = {}
     27        self._population_cache = {}
    2428       
    2529        if not exists:
     
    8993            self.conn.close()
    9094
     95   
    9196    def populationSize(self, probe_type):
    9297        if probe_type in self._population_sizes:
     
    101106            print(e)
    102107            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
    103142       
    104143    def _insert(self, table, row):
Note: See TracChangeset for help on using the changeset viewer.