# Path: metrics/models.py
# class ProgramTimeliness(ProgramMetric):
#         
#     late_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     late_rows = models.IntegerField(blank=True, null=True)
#     
#     total_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     total_rows = models.IntegerField(blank=True, null=True)
#     
#     late_pct =  models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
# 
# 
#     avg_lag_rows = models.IntegerField(blank=True, null=True)
#     avg_lag_dollars = models.IntegerField(blank=True, null=True)
# 
# class AgencyTimeliness(Metric):
#         
#     late_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     late_rows = models.IntegerField(blank=False, null=False)
#     
#     total_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     total_rows = models.IntegerField(blank=True, null=True)
# 
#     avg_lag_rows = models.IntegerField(blank=True, null=True)
#     avg_lag_dollars = models.IntegerField(blank=True, null=True)
# 
# class USASpendingAggregate(models.Model):
#     fiscal_year = models.IntegerField(blank=False, null=False, primary_key=True)
#     total_federal_funding = models.DecimalField("SUM(fed_funding_amount)", max_digits=21, decimal_places=2, null=False)
# 
#     def __unicode__(self):
#         return "<USASpendingAggregate(%d, %s)>" % (self.fiscal_year, unicode(pretty_money(self.total_federal_funding)))
# 
# Path: utils.py
# def pretty_money(m):
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
#     return locale.currency(m, grouping=True)


from django.db.models import Sum
from numpy import mean, std
from metrics.models import ProgramTimeliness, AgencyTimeliness, USASpendingAggregate
from utils import pretty_money
from settings import FISCAL_YEARS


print "----- Sample Size -----"
for fy in FISCAL_YEARS:
    timeliness_sum = ProgramTimeliness.objects.filter(fiscal_year=fy).aggregate(Sum('total_dollars'))['total_dollars__sum']
    usaspending_sum = USASpendingAggregate.objects.get(fiscal_year=fy).total_federal_funding
    sample_pct = timeliness_sum * 100 / usaspending_sum
    print "%d: %.2f%% (%s / %s)" % (fy, round(sample_pct, 2),
                                    pretty_money(timeliness_sum),
                                    pretty_money(usaspending_sum))

print "----- Average Lag (program) -----"
for fy in FISCAL_YEARS:
    lag_values = [pt.avg_lag_rows for pt in ProgramTimeliness.objects.filter(fiscal_year=fy)]
    print "%d: %d mean (days), %.1f stdev" % (fy, mean(lag_values), std(lag_values))

print "----- Average Lag (agency) -----"
for fy in FISCAL_YEARS:

# Path: metrics/models.py
# class ProgramTimeliness(ProgramMetric):
#         
#     late_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     late_rows = models.IntegerField(blank=True, null=True)
#     
#     total_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     total_rows = models.IntegerField(blank=True, null=True)
#     
#     late_pct =  models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
# 
# 
#     avg_lag_rows = models.IntegerField(blank=True, null=True)
#     avg_lag_dollars = models.IntegerField(blank=True, null=True)
# 
# class AgencyTimeliness(Metric):
#         
#     late_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     late_rows = models.IntegerField(blank=False, null=False)
#     
#     total_dollars = models.DecimalField(max_digits=21, decimal_places=2, blank=True, null=True) 
#     total_rows = models.IntegerField(blank=True, null=True)
# 
#     avg_lag_rows = models.IntegerField(blank=True, null=True)
#     avg_lag_dollars = models.IntegerField(blank=True, null=True)
# 
# class USASpendingAggregate(models.Model):
#     fiscal_year = models.IntegerField(blank=False, null=False, primary_key=True)
#     total_federal_funding = models.DecimalField("SUM(fed_funding_amount)", max_digits=21, decimal_places=2, null=False)
# 
#     def __unicode__(self):
#         return "<USASpendingAggregate(%d, %s)>" % (self.fiscal_year, unicode(pretty_money(self.total_federal_funding)))
# 
# Path: utils.py
# def pretty_money(m):
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
#     return locale.currency(m, grouping=True)


# Path: timeliness_results.py
from django.db.models import Sum
from numpy import mean, std
from metrics.models import ProgramTimeliness, AgencyTimeliness, USASpendingAggregate
from utils import pretty_money
from settings import FISCAL_YEARS



print "----- Sample Size -----"
for fy in FISCAL_YEARS:
    timeliness_sum = ProgramTimeliness.objects.filter(fiscal_year=fy).aggregate(Sum('total_dollars'))['total_dollars__sum']
    usaspending_sum = USASpendingAggregate.objects.get(fiscal_year=fy).total_federal_funding
    sample_pct = timeliness_sum * 100 / usaspending_sum
    print "%d: %.2f%% (%s / %s)" % (fy, round(sample_pct, 2),
                                    pretty_money(timeliness_sum),
                                    pretty_money(usaspending_sum))

print "----- Average Lag (program) -----"
for fy in FISCAL_YEARS:
    lag_values = [pt.avg_lag_rows for pt in ProgramTimeliness.objects.filter(fiscal_year=fy)]
    print "%d: %d mean (days), %.1f stdev" % (fy, mean(lag_values), std(lag_values))

print "----- Average Lag (agency) -----"
for fy in FISCAL_YEARS:

    lag_values = [at.avg_lag_rows for at in AgencyTimeliness.objects.filter(fiscal_year=fy)]