
import datetime
from dateutil.relativedelta import relativedelta
import env
import logging
import mongoengine
from openrunlog import models
import setproctitle
import time
from tornado import ioloop, gen
import tornadotinyfeedback


def timestamp_to_oid(timestamp):
    return '{0:x}0000000000000000'.format(int(timestamp))


def _daily_active_query():
    yesterday = timestamp_to_oid(time.time() - 60*60*24)
    today_runs = models.Run.objects(id__gte=yesterday)
    users = set()

    for run in today_runs:
        users.add(run.user)
    num = len(users)
    logging.info("daily active users: {}".format(num))
    return num


def _monthly_active_query():
    monthly = timestamp_to_oid(time.time() - 60*60*24*30)
    runs = models.Run.objects(id__gte=monthly)
    users = set()

    for run in runs:
        users.add(run.user)
    num = len(users)
    logging.info("monthly active users: {}".format(num))
    return num

@gen.coroutine
def active_users():
    logging.info('{} sending users.active'.format(datetime.datetime.today()))
    yield gen.Task(tf.send, {'users.active.daily': _daily_active_query()})
    yield gen.Task(tf.send, {'users.active.monthly': _monthly_active_query()})


def main():
    logging.info('starting active user counter')
    ioloop.PeriodicCallback(active_users, 60*1000).start()


if __name__ == '__main__':
    config = env.prefix('ORL_')
    if config['debug'] == 'True':
        config['debug'] = True
    else:
        config['debug'] = False

    mongoengine.connect(
        config['db_name'],
        host=config['db_uri'])
    tf = tornadotinyfeedback.Client('openrunlog')
    logging.basicConfig(level=logging.INFO)
    setproctitle.setproctitle('orl.workers.metrics')

    logging.info('starting workers.metrics')
    main()

    ioloop.IOLoop.instance().start()

@@ -331,28 +331,25 @@
  _daily_
-active_query
+timestamp
 ():%0A    
@@ -409,207 +409,106 @@
     
-today_runs = models.Run.objects(id__gte=yesterday)%0A    users = set()%0A%0A    for run in today_runs:%0A        users.add(run.user)%0A    num = len(users)%0A    logging.info(%22daily active users: %7B%7D%22.format(num)
+return yesterday%0A%0A%0Adef _weekly_timestamp():%0A    weekly = timestamp_to_oid(time.time() - 60*60*24*7
 )%0A  
@@ -516,19 +516,23 @@
  return 
-num
+weekly 
 %0A%0A%0Adef _
@@ -539,28 +539,25 @@
 monthly_
-active_query
+timestamp
 ():%0A    
@@ -610,16 +610,73 @@
 *24*30)%0A
+    return monthly%0A%0A%0Adef _active_users_query(timestamp):%0A
     runs
@@ -705,23 +705,25 @@
 id__gte=
-monthly
+timestamp
 )%0A    us
@@ -734,16 +734,20 @@
 = set()%0A
+    
 %0A    for
@@ -813,65 +813,8 @@
 rs)%0A
-    logging.info(%22monthly active users: %7B%7D%22.format(num))%0A
     
@@ -942,81 +942,449 @@
 )))%0A
-    yield gen.Task(tf.send, %7B'users.active.daily': _daily_active_query()%7D
+%0A    daily_users = _active_users_query(_daily_timestamp())%0A    yield gen.Task(tf.send, %7B'users.active.daily': daily_users%7D)%0A    logging.info(%22daily active users: %7B%7D%22.format(daily_users))%0A%0A    weekly_users = _active_users_query(_weekly_timestamp())%0A    yield gen.Task(tf.send, %7B'users.active.weekly': weekly_users%7D)%0A    logging.info(%22weekly active users: %7B%7D%22.format(weekly_users))%0A%0A    monthly_users = _active_users_query(_monthly_timestamp()
 )%0A  
@@ -1434,25 +1434,24 @@
 nthly': 
-_
 monthly_
 active_q
@@ -1446,23 +1446,81 @@
 hly_
-active_query()%7D
+users%7D)%0A    logging.info(%22monthly active users: %7B%7D%22.format(monthly_users)
 )%0A%0A%0A

refactor workers.metrics and add weekly active