import time
import logging
import argparse
from dateutil.parser import parse
from datetime import datetime, timedelta

from keen.client import KeenClient

from website.app import init_app
from website.settings import KEEN as keen_settings
from scripts.analytics.user_count import count as user_count
from scripts.analytics.node_count import count as node_count
from scripts.analytics.node_log_count import get_node_log_events as node_log_count

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def get_events_for_day(day):
    """Get an event summary for a certain 24 hour period.
     00:00UTC - 23:59:59UTC on the day provided.
     """
    logger.info('<---- Keen Counts for {} ---->'.format(day.isoformat()))

    keen_events = {}
    keen_events.update({'user_count_analytics': user_count(day)})
    keen_events.update({'node_analytics': [node_count(day)]})
    keen_events.update({'node_log_analytics': [node_log_count(day)]})

    return keen_events


def yield_chunked_events(events):
    """ The keen API likes events in chunks no bigger than 5000 -
    Only yield that many at a time.
    """
    for i in range(0, len(events), 5000):
        yield events[i:i + 5000]


def main(start_date, end_date):
    keen_project = keen_settings['private']['project_id']
    write_key = keen_settings['private']['write_key']
    if keen_project and write_key:
        client = KeenClient(
            project_id=keen_project,
            write_key=write_key,
        )
    assert(client)

    day = start_date
    while day < end_date:
        events_for_day = get_events_for_day(day)
        for key, value in events_for_day.iteritems():
            for chunk in yield_chunked_events(value):
                client.add_events({key: chunk})
                time.sleep(1)
        day = day + timedelta(1)

def parse_args():
    parser = argparse.ArgumentParser(description='Populate keen counts!')
    parser.add_argument('-e', '--end', dest='end_date', required=False)
    parser.add_argument('-s', '--start', dest='start_date', required=False)
    return parser.parse_args()

if __name__ == '__main__':
    now = datetime.utcnow()

    init_app()
    args = parse_args()
    end_date = parse(args.end_date) if args.end_date else now
    start_date = parse(args.start_date) if args.end_date else now - datetime.timedelta(1)

    main(start_date, end_date)

@@ -260,29 +260,31 @@
 cs.user_
-count
+summary
  import 
 count as
@@ -271,29 +271,34 @@
 mary import 
-cou
+get_eve
 nt
+s
  as user_cou
@@ -294,21 +294,22 @@
 as user_
-cou
+eve
 nt
+s
 %0Afrom sc
@@ -329,29 +329,31 @@
 cs.node_
-count
+summary
  import 
 count as
@@ -344,21 +344,26 @@
  import 
-cou
+get_eve
 nt
+s
  as node
@@ -363,21 +363,22 @@
 as node_
-cou
+eve
 nt
+s
 %0Afrom sc
@@ -419,25 +419,16 @@
 ort get_
-node_log_
 events a
@@ -438,21 +438,22 @@
 ode_log_
-cou
+eve
 nt
+s
 %0A%0Alogger
@@ -551,16 +551,30 @@
 _day(day
+, scripts=None
 ):%0A    %22
@@ -679,113 +679,566 @@
 ed.%0A
-     %22%22%22%0A    logger.info('%3C---- Keen Counts for %7B%7D ----%3E'.format(day.isoformat()))%0A%0A    keen_events = %7B%7D%0A
+%0A     Default to all known analytic summary scripts, but optionally%0A     pass in the names of specific scripts to run.%0A     %22%22%22%0A    logger.info('%3C---- Keen Counts for %7B%7D ----%3E'.format(day.isoformat()))%0A%0A    keen_events = %7B%7D%0A%0A    if scripts:%0A        # try to imprt the scripts given the string name%0A%0A        # raise a helpful error if the script named can't be found%0A%0A        # assume that script has a %22get_events%22 method and use that to add to keen_events%0A%0A        # use the name of the script file as the assumed collection name%0A        pass%0A    else:%0A    
     
@@ -1267,31 +1267,23 @@
 ser_
-count_analytics
+summary
 ': user_
 coun
@@ -1282,21 +1282,22 @@
 ser_
-cou
+eve
 nt
+s
 (day)%7D)%0A
     
@@ -1292,16 +1292,20 @@
 (day)%7D)%0A
+    
     keen
@@ -1326,25 +1326,23 @@
 (%7B'node_
-analytics
+summary
 ': %5Bnode
@@ -1342,21 +1342,22 @@
 : %5Bnode_
-cou
+eve
 nt
+s
 (day)%5D%7D)
@@ -1357,16 +1357,20 @@
 day)%5D%7D)%0A
+    
     keen
@@ -1395,24 +1395,21 @@
 ode_log_
-analytic
+event
 s': %5Bnod
@@ -1414,21 +1414,22 @@
 ode_log_
-cou
+eve
 nt
+s
 (day)%5D%7D)
@@ -1705,16 +1705,30 @@
 end_date
+, scripts=None
 ):%0A    k
@@ -2092,16 +2092,25 @@
 _day(day
+, scripts
 )%0A      
@@ -2559,24 +2559,131 @@
 ired=False)%0A
+    parser.add_argument('-as', '--analytics_scripts', nargs='+', dest='analytics_scripts', required=False)%0A
     return p
@@ -2948,16 +2948,53 @@
 delta(1)
+%0A    scripts = args.analytics_scripts
 %0A%0A    ma
@@ -3016,10 +3016,19 @@
 end_date
+, scripts
 )%0A

Add basis to also pass list of script names to run