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()
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()