import datetime
import json
import logging
import os
import sys

sys.path.append(os.environ.get('STACKTACH_INSTALL_DIR', '/stacktach'))

from stacktach import datetime_to_decimal as dt
from stacktach import models

logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

def __get_previous_period(time):
    last_period = time - datetime.timedelta(days=1)
    start = datetime.datetime(year=last_period.year,
                              month=last_period.month,
                              day=last_period.day)
    end = datetime.datetime(year=time.year,
                            month=time.month,
                            day=time.day)
    return start, end


def __get_instance_exists(beginning, ending):
    filters = {
        'audit_period_beginning__gte': beginning,
        'audit_period_ending__lte': ending,
    }
    return models.InstanceExists.objects.filter(**filters)


def __audit_for_instance_exists(beginning, ending):
    beginning_decimal = dt.dt_to_decimal(beginning)
    ending_decimal = dt.dt_to_decimal(ending)
    instance_exists = __get_instance_exists(beginning_decimal, ending_decimal)
    total_bw = reduce(lambda x, y: x + y.bandwidth_public_out, instance_exists,
                      0)
    report = {
        'total_public_outbound_bandwidth': total_bw,
    }

    return report


def __store_report_in_db(start, end, report):
    values = {
        'json': __make_json_report(report),
        'created': dt.dt_to_decimal(datetime.datetime.utcnow()),
        'period_start': start,
        'period_end': end,
        'version': 1,
        'name': 'public outbound bandwidth'
    }

    report = models.JsonReport(**values)
    report.save()


def __make_json_report(report):
    return json.dumps(report)


if __name__ == '__main__':
    start, end = __get_previous_period(datetime.datetime.utcnow())
    logger.debug("Aggregating bw usage for period: %s to %s" % (start, end))
    report = __audit_for_instance_exists(start, end)
    __store_report_in_db(start, end, report)