def on_button(self): self.client = TSDBClient() self.client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') self.client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) if self.master2: self.master2.destroy() else: self.master.destroy() _, results = self.client.select() if not len(results): self.master1 = tk.Tk() self.label = tk.Label(self.master1,text=""" It appears you have not chosen to load any time series into the database so the database will be automatically populated for the search. """,justify= 'left') self.label2 = tk.Label(self.master1,text= "How many time series would you like in the database?") self.entry = tk.Entry(self.master1) self.button = tk.Button(self.master1, text="continue", command=self.on_button1) self.label.pack() self.label2.pack() self.entry.pack() self.button.pack() else: self.switch = 1 self.on_button1()
def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() n_add = 50 mus = np.random.uniform(low=0.0, high=1.0, size=n_add) sigs = np.random.uniform(low=0.05, high=0.4, size=n_add) jits = np.random.uniform(low=0.05, high=0.2, size=n_add) for i, m, s, j in zip(range(n_add), mus, sigs, jits): client.insert_ts("ts-{}".format(i), tsmaker(m, s, j)) for i in range(5): client.add_vp() _, results = client.select(fields=[]) print("RESULT TYPE: ", type(results)) for k in results: print(k, results[k]) m = np.random.uniform(low=0.0, high=1.0) s = np.random.uniform(low=0.05, high=0.4) j = np.random.uniform(low=0.05, high=0.2) query = tsmaker(m, s, j) _, closest = client.simsearch(query) print("CLOSEST MATCH: ", closest) _, closest = client.sim_search_SAX(query) print("CLOSEST MATCH: ", closest)
async def test_complex_run(self): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() # Add a trigger. notice the argument. It does not do anything here but # it could be used to save a shlep of data from client to server. await client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # Our stats trigger await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) # Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=50) sigs = np.random.uniform(low=0.05, high=0.4, size=50) jits = np.random.uniform(low=0.05, high=0.2, size=50) # Dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(50), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta[ 'vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # Choose 5 distinct vantage point time series vpkeys = [ "ts-{}".format(i) for i in np.random.choice(range(50), size=5, replace=False) ] for i in range(5): # add 5 triggers to upsert distances to these vantage points await client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp'] = True # Having set up the triggers, now insert the time series, and upsert the metadata for k in tsdict: await client.insert_ts(k, tsdict[k]) await client.upsert_meta(k, metadict[k]) select = await client.select() # Assert we received all pks assert (len(select[1]) == 50) # Assert the primary keys have empty dicts assert (select[1]['ts-1'] == {}) # In this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. select = await client.select(fields=['order'], additional={'sort_by': '-order'}) print(select) # TODO: DO NOT APPEAR TO BE IN ORDER for x in range(1, len(select[1])): assert (select[1][list(select[1].keys())[x]]['order'] <= select[1][list(select[1].keys())[x - 1]]['order'])
async def test_simple_run(self): # Data t = [0, 1, 2, 3, 4] v = [1.0, 2.0, 3.0, 2.0, 1.0] ats = ts.TimeSeries(t, v) # Setup Client client = TSDBClient() # Add Trigger await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) # Insert await client.insert_ts(1, ats) # Select status, payload = await client.select({'pk': { '==': 1 }}, ['ts', 'mean', 'std'], None) assert (status == 0) assert (ts.TimeSeries(payload['1']['ts'][0], payload['1']['ts'][1]) == ats) assert (payload['1']['std'] == 1.4142135623730951) assert (payload['1']['mean'] == 2.0) # FINALLY WORKING!!! YAY!!! # Upsert await client.upsert_meta(1, {'order': 1}) status, payload = await client.select({'order': { '==': 1 }}, ['pk', 'order'], None) assert (status == 0) assert (payload['1']['order'] == 1) # Remove Trigger await client.remove_trigger('stats', 'insert_ts') # Insert (No Trigger) await client.insert_ts(2, ats) status, payload = await client.select({'pk': { '==': 2 }}, ['ts', 'mean', 'std'], None) assert (ts.TimeSeries(payload['2']['ts'][0], payload['2']['ts'][1]) == ats) assert ('std' not in payload['2']) assert ('mean' not in payload['2']) # Delete await client.delete_ts(1) status, payload = await client.select({'pk': { '==': 1 }}, ['ts', 'mean', 'std'], None) assert (status == 0) assert (payload == {})
async def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() await client.add_trigger('period', 'insert_ts', ['period'], None) #['mean', 'std'], None)# sigeta = np.random.normal(0, 1, 2000) sigeps = np.random.normal(0, 10, 2000) mus = np.cumsum(sigeta) + 20 y = mus + sigeps await client.insert_ts('one', ts.TimeSeries(y, np.arange(2000))) await client.upsert_meta('one', {'order': 1, 'blarg': 1}) print('---------------------') await client.select(fields=[])
def __init__(self): self.client = TSDBClient()
async def run(): np.random.seed(12345) print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() N_ts = 50 N_vp = 5 # add a trigger. notice the argument. It does not do anything here but # could be used to save a shlep of data from client to server. await client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # our stats trigger await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) #Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=N_ts) sigs = np.random.uniform(low=0.05, high=0.4, size=N_ts) jits = np.random.uniform(low=0.05, high=0.2, size=N_ts) # dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(N_ts), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta[ 'vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # choose 5 distinct vantage point time series vpkeys = [ "ts-{}".format(i) for i in np.random.choice(range(N_ts), size=N_vp, replace=False) ] for i in range(N_vp): # add 5 triggers to upsert distances to these vantage points await client.add_trigger('junk', 'insert_ts', None, 23) await client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp'] = True # Having set up the triggers, now inser the time series, and upsert the metadata for k in tsdict: print(tsdict[k]) await client.insert_ts(k, tsdict[k]) await client.upsert_meta(k, metadict[k]) print("UPSERTS FINISHED") print('---------------------') print("STARTING SELECTS") print('---------DEFAULT------------') await client.select() #in this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. print('---------ADDITIONAL------------') await client.select(additional={'sort_by': '-order'}) print('----------ORDER FIELD-----------') _, results = await client.select(fields=['order']) for k in results: print(k, results[k]) print('---------ALL FILEDS------------') await client.select(fields=[]) print('------------TS with order 1---------') await client.select({'order': 1}, fields=['ts']) print('------------All fields, blarg 1 ---------') await client.select({'blarg': 1}, fields=[]) print('------------order 1 blarg 2 no fields---------') _, bla = await client.select({'order': 1, 'blarg': 2}) print(bla) print( '------------order >= 4 order, blarg and mean sent back, also sorted---------' ) _, results = await client.select({'order': { '>=': 4 }}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) for k in results: print(k, results[k]) print('------------order 1 blarg >= 1 fields blarg and std---------') _, results = await client.select({ 'blarg': { '>=': 1 }, 'order': 1 }, fields=['blarg', 'std', 'order']) for k in results: print(k, results[k]) print('------------pk = ts-1 ---------') _, results = await client.select({'pk': 'ts-1'}) print(len(results)) print('------now computing vantage point stuff---------------------') print("VPS", vpkeys) #we first create a query time series. _, query = tsmaker(0.5, 0.2, 0.1) nearestwanted = await findNearest(client, query, vpkeys) print("Nearest :", nearestwanted) await client.delete_ts(nearestwanted) nearestwanted = await findNearest(client, query, vpkeys) print("Nearest :", nearestwanted) # # plot the timeseries to see visually how we did. import matplotlib.pyplot as plt plt.plot(query) plt.plot(tsdict[nearestwanted]) plt.show() ## ASK: CHECK to make sure you found the closest one (red) _, results = await client.augmented_select('corr', 'd', query) dists = np.array([results[k]['d'] for k in results]) ys = np.array([1] * len(dists)) + np.random.randn(len(dists)) / 100 labels = results.keys() colors = [] for l in labels: c = 'b' if l in vpkeys: c = 'g' if l == nearestwanted: c = 'r' colors.append(c) plt.scatter(dists, ys, c=colors, s=100) for x, y, l in zip(dists, ys, labels): plt.annotate(l, xy=(x, y), xytext=(0, 20), textcoords='offset points', rotation=90) plt.show()
def test_db_init(): client = TSDBClient(25000, test=True) assert client.port == 25000 return client
async def test_run(self): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() # add a trigger. notice the argument. It does not do anything here but # could be used to save a shlep of data from client to server. await client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # our stats trigger await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) #Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=self.N_ts) sigs = np.random.uniform(low=0.05, high=0.4, size=self.N_ts) jits = np.random.uniform(low=0.05, high=0.2, size=self.N_ts) # dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(self.N_ts), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta[ 'vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta for i in range(self.N_vp): # add 5 triggers to upsert distances to these vantage points await client.add_trigger('junk', 'insert_ts', None, 23) await client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[self.vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[self.vpkeys[i]]['vp'] = True metadict[self.vpkeys[i]]['vp_num'] = i # Having set up the triggers, now inser the time series, and upsert the metadata for k in tsdict: print(tsdict[k]) await client.insert_ts(k, tsdict[k]) await client.upsert_meta(k, metadict[k]) print("UPSERTS FINISHED") print('---------------------') print("STARTING SELECTS") print('---------DEFAULT------------') await client.select() #in this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. print('---------ADDITIONAL------------') await client.select(additional={'sort_by': '-order'}) print('----------ORDER FIELD-----------') _, results = await client.select(fields=['order']) for k in results: print(k, results[k]) print('---------ALL FILEDS------------') await client.select(fields=[]) print('------------TS with order 1---------') await client.select({'order': 1}, fields=['ts']) print('------------All fields, blarg 1 ---------') await client.select({'blarg': 1}, fields=[]) print('------------order 1 blarg 2 no fields---------') _, bla = await client.select({'order': 1, 'blarg': 2}) print(bla) print( '------------order >= 4 order, blarg and mean sent back, also sorted---------' ) _, results = await client.select({'order': { '>=': 4 }}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) for k in results: print(k, results[k]) print('------------order 1 blarg >= 1 fields blarg and std---------') _, results = await client.select({ 'blarg': { '>=': 1 }, 'order': 1 }, fields=['blarg', 'std', 'order']) for k in results: print(k, results[k]) print('------------pk = ts-1 ---------') _, results = await client.select({'pk': 'ts-1'}) print(len(results)) print('------now computing vantage point stuff---------------------') print("VPS", self.vpkeys) #we first create a query time series. _, query = tsmaker(0.5, 0.2, 0.1) # find nearestwanted directly _, results = await client.find_similar(query) nearestwanted = list(results)[0] print("Nearest :", nearestwanted) self.assertEqual(nearestwanted, 'ts-0') # make the VP Tree await client.make_vp_tree() # find nearestwanted directly w/ VP Tree _, results = await client.find_similar(query) nearestwanted = list(results)[0] print("Nearest :", nearestwanted) self.assertEqual(nearestwanted, 'ts-0') # find the nearest nearestwanted = await self._findNearest(client, query) print("Nearest :", nearestwanted) self.assertEqual(nearestwanted, 'ts-0') # delete the nearest await client.delete_ts(nearestwanted) # find the nearest nearestwanted = await self._findNearest(client, query) print("Nearest :", nearestwanted) self.assertEqual(nearestwanted, 'ts-12')
def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient(30000) # add a trigger. notice the argument. It does not do anything here but # could be used to save a shlep of data from client to server. client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # our stats trigger client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) # Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=50) sigs = np.random.uniform(low=0.05, high=0.4, size=50) jits = np.random.uniform(low=0.05, high=0.2, size=50) # dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(50), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta[ 'vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # choose 5 distinct vantage point time series vpkeys = [ "ts-{}".format(i) for i in np.random.choice(range(50), size=5, replace=False) ] for i in range(5): # add 5 triggers to upsert distances to these vantage points client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp'] = True # Having set up the triggers, now insert the time series, and upsert the metadata for k in tsdict: client.insert_ts(k, tsdict[k]) time.sleep(1) client.upsert_meta(k, metadict[k]) print("UPSERTS FINISHED") time.sleep(5) print('---------------------') print("STARTING SELECTS") print('---------DEFAULT------------') client.select() # in this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. print('---------ADDITIONAL------------') client.select(additional={'sort_by': '-order'}) print('----------ORDER FIELD-----------') _, results = client.select(fields=['order']) for k in results: print(k, results[k]) print('---------ALL FIELDS------------') client.select(fields=[]) print('------------TS with order 1---------') client.select({'order': 1}, fields=['ts']) print('------------All fields, blarg 1 ---------') client.select({'blarg': 1}, fields=[]) print('------------order 1 blarg 2 no fields---------') _, bla = client.select({'order': 1, 'blarg': 2}) print(bla) print( '------------order >= 4 order, blarg and mean sent back, also sorted---------' ) _, results = client.select({'order': { '>=': 4 }}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) for k in results: print(k, results[k]) print('------------order 1 blarg >= 1 fields blarg and std---------') _, results = client.select({ 'blarg': { '>=': 1 }, 'order': 1 }, fields=['blarg', 'std']) for k in results: print(k, results[k]) print('------now computing vantage point stuff---------------------') print("VPS", vpkeys) # # # we first create a query time series. _, query = tsmaker(0.5, 0.2, 0.1) # query = tsdict['ts-6'] # # # your code here begins # # # Step 1: in the vpdist key, get distances from query to vantage points # # this is an augmented select res = client.augmented_select('corr', ['dist'], query, {'vp': True}) print(res) # # 1b: choose the lowest distance vantage point # # you can do this in local code d_res = res[1] sorted_res = [] for k, v in d_res.items(): sorted_res.append((v['dist'], k)) sorted_res.sort() D = sorted_res[0][0] D_vp = vpkeys.index(sorted_res[0][1]) print(sorted_res, 'D = ', D) # # Step 2: find all time series within 2*d(query, nearest_vp_to_query) # # this is an augmented select to the same proc in correlation res2 = client.augmented_select('corr', ['dist'], query, {'d_vp-{}'.format(D_vp): { '<=': 2 * D }}) print(res2) # # 2b: find the smallest distance amongst this ( or k smallest) d_res = res2[1] sorted_res = [] for k, v in d_res.items(): sorted_res.append((v['dist'], k)) sorted_res.sort() D = sorted_res[0][0] D_k = sorted_res[0][1] print(sorted_res, 'D = ', D, 'D_k = ', D_k) nearestwanted = D_k # # you can do this in local code # # your code here ends # # plot the timeseries to see visually how we did. import matplotlib matplotlib.use('qt4agg') import matplotlib.pyplot as plt plt.plot(query, label='') plt.plot(tsdict[nearestwanted]) plt.show()
write_resp(self, res) except BaseException as e: json_error(self, 400, reason=str(e)) class TSRollbackHandler(tornado.web.RequestHandler): def post(self): try: res = client.rollback() write_resp(self, res) except BaseException as e: json_error(self, 400, reason=str(e)) class Application(tornado.web.Application): def __init__(self): handlers = [(r"/api/timeseries", TSHandler), (r"/api/timeseries/upsert", TSUpsertHandler), (r"/api/timeseries/augmented", TSAugmentHandler), (r"/api/timeseries/similarity", TSSimilarityHandler), (r"/api/commit", TSCommitHandler), (r"/api/rollback", TSRollbackHandler)] tornado.web.Application.__init__(self, handlers) app = Application() client = TSDBClient() if __name__ == '__main__': app.listen(5000) IOLoop.current().start()
async def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() #ppl = open('standardize.ppl').read() await client.add_trigger('junk', 'insert_ts', None, 23) #client.add_trigger('junk', 'select', None, 23) await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) await client.insert_ts('one', ts.TimeSeries([1, 2, 3], [1, 4, 9])) await client.insert_ts('two', ts.TimeSeries([2, 3, 4], [4, 9, 16])) await client.insert_ts('three', ts.TimeSeries([9, 3, 4], [4, 0, 16])) await client.remove_trigger('junk', 'insert_ts') await client.insert_ts('four', ts.TimeSeries([0, 0, 4], [1, 0, 4])) await client.upsert_meta('one', {'order': 1, 'blarg': 1}) await client.upsert_meta('two', {'order': 2}) await client.upsert_meta('three', {'order': 1, 'blarg': 2}) await client.upsert_meta('four', {'order': 2, 'blarg': 2}) print("UPSERTS FINISHED") print('---------------------') await client.select() print('---------------------') await client.select(fields=['order']) print('---------------------') await client.select(fields=[]) print('---------------------') print('---------------------') await client.select({'order': 1}, fields=['ts']) print('{{{{{{{{{{{{{{}}}}}}}}}}}}}}') await client.select({'blarg': 1}, fields=[]) print('{{{{{{{{{{{{{{}}}}}}}}}}}}}}') bla = client.select({'order': 1, 'blarg': 2}) print("END", bla) await client.select({'blarg': {'>=': 2}}, fields=['blarg', 'mean']) await client.select({ 'blarg': { '>=': 2 }, 'order': 1 }, fields=['blarg', 'std']) await client.select({'order': { '>=': 2 }}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) await client.select({'order': { '>=': 2 }}, fields=['order', 'blarg', 'mean'], additional={ 'sort_by': '-order', 'limit': 2 }) select = await client.select(fields=['order'], additional={'sort_by': '-order'}) for x in range(1, len(select[1])): print(list(select[1].keys())[x]) assert (select[1][list(select[1].keys())[x]]['order'] <= select[1][list(select[1].keys())[x - 1]]['order'])
async def client_op(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient(port=9999) # add a trigger. notice the argument. It does not do anything here but # could be used to save a shlep of data from client to server. await client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # our stats trigger await client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) #Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=50) sigs = np.random.uniform(low=0.05, high=0.4, size=50) jits = np.random.uniform(low=0.05, high=0.2, size=50) # dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(50), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta[ 'vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # choose 5 distinct vantage point time series vpkeys = [ "ts-{}".format(i) for i in np.random.choice(range(50), size=5, replace=False) ] for i in range(5): # add 5 triggers to upsert distances to these vantage points await client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp'] = True # Having set up the triggers, now insert the time series, and upsert the metadata for k in tsdict: await client.insert_ts(k, tsdict[k]) await client.upsert_meta(k, metadict[k]) print("UPSERTS FINISHED") print('---------------------') print("STARTING SELECTS") print('---------DEFAULT------------') await client.select() #in this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. print('---------ADDITIONAL------------') await client.select(additional={'sort_by': '-order'}) print('----------ORDER FIELD-----------') _, results = await client.select(fields=['order']) for k in results: print(k, results[k]) print('---------ALL FILEDS------------') await client.select(fields=[]) print('------------TS with order 1---------') await client.select({'order': 1}, fields=['ts']) print('------------All fields, blarg 1 ---------') await client.select({'blarg': 1}, fields=[]) print('------------order 1 blarg 2 no fields---------') _, bla = await client.select({'order': 1, 'blarg': 2}) print(bla) print( '------------order >= 4 order, blarg and mean sent back, also sorted---------' ) _, results = await client.select({'order': { '>=': 4 }}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) for k in results: print(k, results[k]) print('------------order 1 blarg >= 1 fields blarg and std---------') _, results = await client.select({ 'blarg': { '>=': 1 }, 'order': 1 }, fields=['blarg', 'std']) for k in results: print(k, results[k]) # print('------now computing vantage point stuff---------------------') print("VPS", vpkeys) # # #we first create a query time series. _, query = tsmaker(0.5, 0.2, 0.1) # # your code here begins # # Step 1: in the vpdist key, get distances from query to vantage points # this is an augmented select vpdists = {} for v in vpkeys: _, results = await client.augmented_select('corr', 'd', query, {'pk': v}) vpdists[v] = results[v]['d'] # # 1b: choose the lowest distance vantage point # you can do this in local code lowest_dist_vp = min(vpkeys, key=lambda v: vpdists[v]) # # # Step 2: find all time series within 2*d(query, nearest_vp_to_query) # this is an augmented select to the same proc in correlation _, results = await client.augmented_select( 'corr', 'd', query, { 'd_vp-' + str(vpkeys.index(lowest_dist_vp)): { '<=': 2 * vpdists[lowest_dist_vp] } }) #2b: find the smallest distance amongst this ( or k smallest) #you can do this in local code nearestwanted = min(results.keys(), key=lambda p: results[p]['d']) #your code here ends # plot the timeseries to see visually how we did. import matplotlib.pyplot as plt plt.plot(query) plt.plot(tsdict[nearestwanted]) plt.show('Agg')
}, 'mean': { 'convert': float, 'index': 1 }, 'std': { 'convert': float, 'index': 1 }, 'vp': { 'convert': bool, 'index': 1 } } client = TSDBClient(30000) @app.route('/') def index(): pass @app.route('/select') def select(): md = request.args.get('md', '') fields = request.args.get('fields', '') additional = request.args.get('additional', '') split_md = md.split(',') md = dict(zip(split_md[::2], split_md[1::2])) if md else None
def main(): print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') client = TSDBClient() # add a trigger. notice the argument. It does not do anything here but # could be used to save a shlep of data from client to server. client.add_trigger('junk', 'insert_ts', None, 'db:one:ts') # our stats trigger client.add_trigger('stats', 'insert_ts', ['mean', 'std'], None) #Set up 50 time series mus = np.random.uniform(low=0.0, high=1.0, size=50) sigs = np.random.uniform(low=0.05, high=0.4, size=50) jits = np.random.uniform(low=0.05, high=0.2, size=50) # dictionaries for time series and their metadata tsdict = {} metadict = {} for i, m, s, j in zip(range(50), mus, sigs, jits): meta, tsrs = tsmaker(m, s, j) # the primary key format is ts-1, ts-2, etc pk = "ts-{}".format(i) tsdict[pk] = tsrs meta[ 'vp'] = False # augment metadata with a boolean asking if this is a VP. metadict[pk] = meta # choose 5 distinct vantage point time series vpkeys = [ "ts-{}".format(i) for i in np.random.choice(range(50), size=5, replace=False) ] for i in range(5): # add 5 triggers to upsert distances to these vantage points client.add_trigger('corr', 'insert_ts', ["d_vp-{}".format(i)], tsdict[vpkeys[i]]) print('lalala:', i, *tsdict[vpkeys[i]]) # change the metadata for the vantage points to have meta['vp']=True metadict[vpkeys[i]]['vp'] = True # Having set up the triggers, now inser the time series, and upsert the metadata for k in tsdict: client.insert_ts(k, tsdict[k]) client.upsert_meta(k, metadict[k]) print("UPSERTS FINISHED") print('---------------------') print("STARTING SELECTS") print('---------DEFAULT------------') client.select() #in this version, select has sprouted an additional keyword argument # to allow for sorting. Limits could also be enforced through this. print('---------ADDITIONAL------------') print(client.select(additional={'sort_by': '-order'})) print('----------ORDER FIELD-----------') _, results = client.select(fields=['order']) print(results) for k in results: print(k, results[k]) print('---------ALL FIELDS------------') client.select(fields=[]) print('------------TS with order 1---------') client.select({'order': 1}, fields=['ts']) print('------------All fields, blarg 1 ---------') client.select({'blarg': 1}, fields=[]) print('------------order 1 blarg 2 no fields---------') _, bla = client.select({'order': 1, 'blarg': 2}) print(bla) print( '------------order >= 4 order, blarg and mean sent back, also sorted---------' ) _, results = client.select({'order': { '>=': 4 }}, fields=['order', 'blarg', 'mean'], additional={'sort_by': '-order'}) for k in results: print(k, results[k]) print('------------order 1 blarg >= 1 fields blarg and std---------') _, results = client.select({ 'blarg': { '>=': 1 }, 'order': 1 }, fields=['blarg', 'std']) for k in results: print(k, results[k]) print('------now computing vantage point stuff---------------------') #we first create a query time series. _, query = tsmaker(0.5, 0.2, 0.1) # Step 1: in the vpdist key, get distances from query to vantage points # this is an augmented select client.insert_ts('ts-query', query) dists = client.select({'pk': 'ts-query'}, fields=['d_vp-{}'.format(i) for i in range(5)])[1]['ts-query'] min_dist = np.inf nearest_vp_to_query = None for vp in dists: if dists[vp] < min_dist: min_dist = dists[vp] nearest_vp_to_query = vp print(nearest_vp_to_query, min_dist) #1b: choose the lowest distance vantage point # you can do this in local code # Step 2: find all time series within 2*d(query, nearest_vp_to_query) #this is an augmented select to the same proc in correlation # nearestwanted = client.augmented_select('corr', ['d_vp-1'], arg=query, # metadata_dict={'d_vp-1': {'<=': 2.0*min_dist}}, additional={'sort_by': '+d_vp-1', 'limit': 10}) #print(client.select(fields=['d_vp-1'])) #print(2*min_dist) nearestwanted = client.select( fields=['d_vp-1'], metadata_dict={'d_vp-1': { '<=': 2.0 * min_dist }}, additional={ 'sort_by': '+d_vp-1', 'limit': 10 }) print(nearestwanted) nearestwanted = list(nearestwanted[1].keys())[0] #2b: find the smallest distance amongst this ( or k smallest) #you can do this in local code #your code here ends # plot the timeseries to see visually how we did. import matplotlib.pyplot as plt for k in tsdict: plt.plot(tsdict[k], alpha=0.15) plt.plot(query) plt.plot(tsdict[nearestwanted]) plt.show()