import json
from redis import Redis
from dorina.run import analyse

def run_analyse(datadir, query_key, query_pending_key, query, uuid):
    from webdorina import RESULT_TTL, SESSION_TTL
    redis_store = Redis()
    # get rid of the now-unused genes parameter
    query.pop('genes', None)
    result = analyse(datadir=datadir, **query)
    result.sort(key=lambda x: x['score'], reverse=True)


    num_results = len(result)
    print "returning %s rows" % num_results

    if num_results < 1:
        result.append({
            'data_source': 'no results found',
            'score': -1,
            'track': '',
            'gene': '',
            'site': '',
            'strand': '',
            'location': ''
        })
        num_results += 1

    json_results = map(json.dumps, result)

    for i in xrange(0, num_results, 1000):
        res = json_results[i:i+1000]
        redis_store.rpush(query_key, *res)


    redis_store.expire(query_key, RESULT_TTL)
    redis_store.setex('sessions:{0}'.format(uuid), json.dumps(dict(state='done', uuid=uuid)), SESSION_TTL)
    redis_store.setex('results:sessions:{0}'.format(uuid), json.dumps(dict(redirect=query_key)), SESSION_TTL)

    redis_store.delete(query_pending_key)


def filter(genes, full_query_key, query_key, query_pending_key, uuid):
    """Filter for a given set of gene names"""
    from webdorina import RESULT_TTL, SESSION_TTL
    redis_store = Redis()

    full_results = redis_store.lrange(full_query_key, 0, -1)
    for res_string in full_results:
        res = json.loads(res_string)
        if res['gene'] in genes:
            redis_store.rpush(query_key, json.dumps(res))

    redis_store.expire(query_key, RESULT_TTL)
    redis_store.delete(query_pending_key)

    redis_store.setex('sessions:{0}'.format(uuid), json.dumps(dict(state='done', uuid=uuid)), SESSION_TTL)
    redis_store.setex('results:sessions:{0}'.format(uuid), json.dumps(dict(redirect=query_key)), SESSION_TTL)

@@ -59,16 +59,28 @@
  analyse
+%0Aimport time
 %0A%0Adef ru
@@ -135,24 +135,38 @@
  query, uuid
+, timeit=False
 ):%0A    from 
@@ -313,111 +313,392 @@
 ne)%0A
-    result = analyse(datadir=datadir, **query)%0A    result.sort(key=lambda x: x%5B'score'%5D, reverse=True)%0A
+%0A    if timeit:%0A        started = time.time()%0A%0A    result = analyse(datadir=datadir, **query)%0A%0A    if timeit:%0A        analysed = time.time()%0A        print %22analyse() ran %7B%7D seconds%22.format(analysed - started)%0A%0A    result.sort(key=lambda x: x%5B'score'%5D, reverse=True)%0A%0A    if timeit:%0A        sorted_time = time.time()%0A        print %22sort() ran %7B%7D seconds%22.format(sorted_time - analysed)
 %0A%0A  
@@ -1097,16 +1097,138 @@
 esult)%0A%0A
+    if timeit:%0A        json_time = time.time()%0A        print %22json.dumps ran %7B%7D seconds%22.format(json_time - sorted_time)%0A%0A
     for 
@@ -1650,16 +1650,195 @@
 ng_key)%0A
+    if timeit:%0A        pushed = time.time()%0A        print %22push() ran %7B%7D seconds%22.format(pushed - json_time)%0A        print %22total time taken: %7B%7D seconds%22.format(pushed - started)%0A
 %0A%0Adef fi

Add hook for run-time reporting