# Path: locksmith/common.py
# def apicall(url, signkey, **params):
#     params['signature'] = get_signature(params, signkey)
#     data = sorted([(k,v) for k,v in params.iteritems()])
#     body = urllib.urlencode(data)
#     urllib2.urlopen(url, body)
# 
# Path: locksmith/mongoauth/db.py
# MONGO_HOST = getattr(settings, 'LOCKSMITH_MONGO_HOST', 'localhost')
# MONGO_PORT = getattr(settings, 'LOCKSMITH_MONGO_PORT', 27017)
# MONGO_DATABASE = getattr(settings, 'LOCKSMITH_MONGO_DATABASE', 'locksmith')


import datetime
from urlparse import urljoin
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from locksmith.common import apicall
from locksmith.mongoauth.db import db

class Command(BaseCommand):
    help = "Push a given day's logs up to the analytics hub"
    args = '[date:YYYY-MM-DD]'
    requires_model_validation = False

    def handle(self, date=None, *args, **options):
        if not date:
            # set date to yesterday if not passed in
            yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
            date = yesterday.strftime('%Y-%m-%d')

        print 'pushing logs for %s' % date

        dt_begin = datetime.datetime.strptime(date, '%Y-%m-%d')
        dt_end = dt_begin + datetime.timedelta(days=1)

        # construct database query

# Path: locksmith/common.py
# def apicall(url, signkey, **params):
#     params['signature'] = get_signature(params, signkey)
#     data = sorted([(k,v) for k,v in params.iteritems()])
#     body = urllib.urlencode(data)
#     urllib2.urlopen(url, body)
# 
# Path: locksmith/mongoauth/db.py
# MONGO_HOST = getattr(settings, 'LOCKSMITH_MONGO_HOST', 'localhost')
# MONGO_PORT = getattr(settings, 'LOCKSMITH_MONGO_PORT', 27017)
# MONGO_DATABASE = getattr(settings, 'LOCKSMITH_MONGO_DATABASE', 'locksmith')


# Path: locksmith/mongoauth/management/commands/apireport.py
import datetime
from urlparse import urljoin
from django.core.management.base import BaseCommand, CommandError
from django.conf import settings
from locksmith.common import apicall
from locksmith.mongoauth.db import db


class Command(BaseCommand):
    help = "Push a given day's logs up to the analytics hub"
    args = '[date:YYYY-MM-DD]'
    requires_model_validation = False

    def handle(self, date=None, *args, **options):
        if not date:
            # set date to yesterday if not passed in
            yesterday = datetime.datetime.now() - datetime.timedelta(days=1)
            date = yesterday.strftime('%Y-%m-%d')

        print 'pushing logs for %s' % date

        dt_begin = datetime.datetime.strptime(date, '%Y-%m-%d')
        dt_end = dt_begin + datetime.timedelta(days=1)

        # construct database query

        results = db.logs.group(['key', 'method'],