# Path: metrics/models.py
# class ProgramCorrection(models.Model):
#     
#     program = models.ForeignKey('cfda.Program', null=False, blank=False)
#     program_obligation = models.ForeignKey('cfda.ProgramObligation', null=False, blank=False)
#     correction_date = models.DateTimeField(null=False, blank=False)
#     corrected_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     old_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     note = models.TextField("Notes on Correction", 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)))


from settings import *
from cfda.models import *
from metrics.models import ProgramCorrection, USASpendingAggregate
from datetime import datetime
import pg
import csv
import numpy as np
    except Exception:
        if program.obligation == 0:
            if not program.usaspending_obligation:
                program.weighted_delta = 0
            else:
                program.weighted_delta = 1
    program.corrected = True
    program.save()

    correction = ProgramCorrection(program=program.program, program_obligation=program, corrected_obligation=program.obligation, old_obligation=old_obligation, note="Corrected manually in program run",  correction_date=datetime.today())
    correction.save()

if __name__ == '__main__':


#    conn = MySQLdb.connect(host=MYSQL_HOST, user=MYSQL_USER, passwd=MYSQL_PASSWORD, db=MYSQL_DATABASE, port=MYSQL_PORT)
    conn = pg.connect(host=PG_HOST, user=PG_USER, passwd=PG_PASSWORD, dbname=PG_DATABASE, port=PG_PORT)
    bogus_cfda = open('csv/bogus_cfda_program_numbers', 'w')
    bogus_cfda_writer = csv.writer(bogus_cfda)

    programs = Program.objects.all().order_by('program_number')
    program_data = {}
    
    usaspending_total = dict.fromkeys(FISCAL_YEARS, 0)

    usa_query = "SELECT fiscal_year, SUM(fed_funding_amount) as fed_funding_amount FROM %s WHERE fiscal_year >= %s AND fiscal_year <= %s GROUP BY fiscal_year" % (PG_TABLE_NAME, min(FISCAL_YEARS), max(FISCAL_YEARS))
    print "Fetching aggregate spending totals from USASpending"
    rows = conn.query(usa_query).dictresult()
    
    for row in rows:

# Path: metrics/models.py
# class ProgramCorrection(models.Model):
#     
#     program = models.ForeignKey('cfda.Program', null=False, blank=False)
#     program_obligation = models.ForeignKey('cfda.ProgramObligation', null=False, blank=False)
#     correction_date = models.DateTimeField(null=False, blank=False)
#     corrected_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     old_obligation = models.DecimalField(max_digits=20, decimal_places=2, blank=False)
#     note = models.TextField("Notes on Correction", 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: consistency/import_usaspending.py
from settings import *
from cfda.models import *
from metrics.models import ProgramCorrection, USASpendingAggregate
from datetime import datetime
import pg
import csv
import numpy as np

    except Exception:
        if program.obligation == 0:
            if not program.usaspending_obligation:
                program.weighted_delta = 0
            else:
                program.weighted_delta = 1
    program.corrected = True
    program.save()

    correction = ProgramCorrection(program=program.program, program_obligation=program, corrected_obligation=program.obligation, old_obligation=old_obligation, note="Corrected manually in program run",  correction_date=datetime.today())
    correction.save()

if __name__ == '__main__':


#    conn = MySQLdb.connect(host=MYSQL_HOST, user=MYSQL_USER, passwd=MYSQL_PASSWORD, db=MYSQL_DATABASE, port=MYSQL_PORT)
    conn = pg.connect(host=PG_HOST, user=PG_USER, passwd=PG_PASSWORD, dbname=PG_DATABASE, port=PG_PORT)
    bogus_cfda = open('csv/bogus_cfda_program_numbers', 'w')
    bogus_cfda_writer = csv.writer(bogus_cfda)

    programs = Program.objects.all().order_by('program_number')
    program_data = {}
    
    usaspending_total = dict.fromkeys(FISCAL_YEARS, 0)

    usa_query = "SELECT fiscal_year, SUM(fed_funding_amount) as fed_funding_amount FROM %s WHERE fiscal_year >= %s AND fiscal_year <= %s GROUP BY fiscal_year" % (PG_TABLE_NAME, min(FISCAL_YEARS), max(FISCAL_YEARS))
    print "Fetching aggregate spending totals from USASpending"
    rows = conn.query(usa_query).dictresult()
    
    for row in rows:

        agg = USASpendingAggregate(fiscal_year=row['fiscal_year'])