# Path: helpers/charts.py
# class Line(GridChart):
# 
#     def __init__(self, height, width, data, stylesheet=None, *args, **kwargs):
# 
#         super(Line, self).__init__(height, width, data, stylesheet, **kwargs)
#        
#         self.x_scale = self.set_scale()  #find the width of each point in each series
#         self.x_group_scale = self.x_scale * self.number_of_series  #width of each data point grouping over multiple series
#         self.setup_chart()
# 
#         self.data_series()  #Chart subclass should have this method to chart the data series
#         #self.labels.sort() # Yikes! sorting the labels independently from the data leads to problems... 
#         self.set_labels()
#          
#     def set_scale(self):
#         #pixels between data points
#         return float(self.grid_width - (self.x_padding * 2) ) / (len(self.labels) - 1) 
# 
#     def data_series(self):
#         
#         series_count = 0
#         left_offset = self.padding  
#         bottom_offset = self.padding
#         g_container = ET.Element('g')
#         
#         for series in self.data:
#             series_count += 1
#             if series != 'placeholder':
#                 #move path to initial data point
#                 data_point_count = self.labels.index(series[0][0])
#                 path_string = "M %s %s" % (self.x_padding + int(data_point_count * self.x_scale), self.grid_height - (series[0][1] * self.y_scale))
# 
#                 for point in series:
#                     if data_point_count == 0: 
#                         data_point_count += 1
#                         continue
# 
#                     data_point_count = self.labels.index(point[0]) 
#                     path_string += " L "
#                     x = self.x_padding + int(data_point_count * self.x_scale)
#                     point_height = self.y_scale * point[1]                
#                     y = self.grid_height - point_height
#                     path_string += "%s %s" % (x, y)
#                     data_point_count += 1
#                     #put point markers in here at some point?
# 
#                 line = ET.Element("path", d=path_string)
#                 line.attrib['class'] = 'series-%s-line' % series_count
#                 g_container.append(line)
#         self.grid.append(g_container)
#     
#     def set_labels(self):
# 
#         label_count = 0
# 
#         for l in self.labels:
#             if  (self.label_intervals and label_count % self.label_intervals == 0) or not self.label_intervals:
#                 text_item = ET.Element("text")
#                 x_position = self.x_padding + (label_count * self.x_scale)
#                 y_position = self.grid_height + self.x_label_padding
#                 text_item.attrib['x'] = "%s" % x_position
#                 text_item.attrib['y'] = "%s" % y_position
#                 text_item.text = "%s" % l
#                 text_item.attrib['class'] = 'x-axis-label'
#                 self.grid.append(text_item)
#                 
#                 #insert the notch between data point groups
#                 if l != self.labels[-1]:
#                     if self.label_intervals:
#                         skip_labels = self.label_intervals
#                     else: skip_labels = 1
# 
#                     notch_x_pos = x_position + (((self.x_padding + ((label_count + skip_labels) * self.x_scale)) - x_position) / 2)
#                     notch_y_pos = self.grid_height
#                     notch = ET.Element("path", d="M %s %s L %s %s" % (notch_x_pos, notch_y_pos, notch_x_pos, notch_y_pos + 5))
#                     notch.attrib['class'] = 'x-notch'
#                     self.grid.append(notch)
#             label_count += 1
# 
# Path: utils.py
# def pretty_money(m):
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
#     return locale.currency(m, grouping=True)


from metrics.models import *
from settings import FISCAL_YEARS
from cfda.models import *
from django.db.models import Sum
from helpers.charts import Line
from utils import pretty_money
import csv
                        agency_totals = add_to_agency(agency_totals, program.agency.name, fy, ob.obligation)
                        writer.writerow((program.program_number, "%s" % program.program_title.encode('ascii', 'ignore'), 'consistency', ob.obligation, ob.usaspending_obligation))
                        continue

        #fail timeliness?
            if metric == 'all' or metric == 'timeliness':
                time = ProgramTimeliness.objects.filter(program=program, fiscal_year=fy)
                if len(time) > 0:
                    time = time[0]
                    if time.total_dollars and (float(time.late_dollars / time.total_dollars)  > .5):
                        total += ob.obligation
                        agency_totals = add_to_agency(agency_totals, program.agency.name, fy, ob.obligation)
                        writer.writerow((program.program_number, "%s" % program.program_title.encode('ascii', 'ignore'), 'timeliness', ob.obligation))
                        continue

        # fail completeness?
            if metric == 'all' or metric == 'completeness':
                comp = ProgramCompleteness.objects.filter(program=program, fiscal_year=fy)
                if len(comp) > 0:
                    comp = comp[0]
                    if comp.completeness_total_dollars and (float(comp.completeness_failed_dollars / comp.completeness_total_dollars) > .5):
                        total += ob.obligation
                        agency_totals = add_to_agency(agency_totals, program.agency.name, fy, ob.obligation)
                        writer.writerow((program.program_number, "%s" % program.program_title.encode('ascii', 'ignore'), 'completeness', ob.obligation))
                        continue


    count += 1
    all_obs = ProgramObligation.objects.filter(program__in=fins, fiscal_year=fy, type=TYPE).aggregate(summ=Sum('obligation'))['summ']
    if all_obs:

# Path: helpers/charts.py
# class Line(GridChart):
# 
#     def __init__(self, height, width, data, stylesheet=None, *args, **kwargs):
# 
#         super(Line, self).__init__(height, width, data, stylesheet, **kwargs)
#        
#         self.x_scale = self.set_scale()  #find the width of each point in each series
#         self.x_group_scale = self.x_scale * self.number_of_series  #width of each data point grouping over multiple series
#         self.setup_chart()
# 
#         self.data_series()  #Chart subclass should have this method to chart the data series
#         #self.labels.sort() # Yikes! sorting the labels independently from the data leads to problems... 
#         self.set_labels()
#          
#     def set_scale(self):
#         #pixels between data points
#         return float(self.grid_width - (self.x_padding * 2) ) / (len(self.labels) - 1) 
# 
#     def data_series(self):
#         
#         series_count = 0
#         left_offset = self.padding  
#         bottom_offset = self.padding
#         g_container = ET.Element('g')
#         
#         for series in self.data:
#             series_count += 1
#             if series != 'placeholder':
#                 #move path to initial data point
#                 data_point_count = self.labels.index(series[0][0])
#                 path_string = "M %s %s" % (self.x_padding + int(data_point_count * self.x_scale), self.grid_height - (series[0][1] * self.y_scale))
# 
#                 for point in series:
#                     if data_point_count == 0: 
#                         data_point_count += 1
#                         continue
# 
#                     data_point_count = self.labels.index(point[0]) 
#                     path_string += " L "
#                     x = self.x_padding + int(data_point_count * self.x_scale)
#                     point_height = self.y_scale * point[1]                
#                     y = self.grid_height - point_height
#                     path_string += "%s %s" % (x, y)
#                     data_point_count += 1
#                     #put point markers in here at some point?
# 
#                 line = ET.Element("path", d=path_string)
#                 line.attrib['class'] = 'series-%s-line' % series_count
#                 g_container.append(line)
#         self.grid.append(g_container)
#     
#     def set_labels(self):
# 
#         label_count = 0
# 
#         for l in self.labels:
#             if  (self.label_intervals and label_count % self.label_intervals == 0) or not self.label_intervals:
#                 text_item = ET.Element("text")
#                 x_position = self.x_padding + (label_count * self.x_scale)
#                 y_position = self.grid_height + self.x_label_padding
#                 text_item.attrib['x'] = "%s" % x_position
#                 text_item.attrib['y'] = "%s" % y_position
#                 text_item.text = "%s" % l
#                 text_item.attrib['class'] = 'x-axis-label'
#                 self.grid.append(text_item)
#                 
#                 #insert the notch between data point groups
#                 if l != self.labels[-1]:
#                     if self.label_intervals:
#                         skip_labels = self.label_intervals
#                     else: skip_labels = 1
# 
#                     notch_x_pos = x_position + (((self.x_padding + ((label_count + skip_labels) * self.x_scale)) - x_position) / 2)
#                     notch_y_pos = self.grid_height
#                     notch = ET.Element("path", d="M %s %s L %s %s" % (notch_x_pos, notch_y_pos, notch_x_pos, notch_y_pos + 5))
#                     notch.attrib['class'] = 'x-notch'
#                     self.grid.append(notch)
#             label_count += 1
# 
# Path: utils.py
# def pretty_money(m):
#     locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
#     return locale.currency(m, grouping=True)


# Path: failed_dollars.py
from metrics.models import *
from settings import FISCAL_YEARS
from cfda.models import *
from django.db.models import Sum
from helpers.charts import Line
from utils import pretty_money
import csv

                        agency_totals = add_to_agency(agency_totals, program.agency.name, fy, ob.obligation)
                        writer.writerow((program.program_number, "%s" % program.program_title.encode('ascii', 'ignore'), 'consistency', ob.obligation, ob.usaspending_obligation))
                        continue

        #fail timeliness?
            if metric == 'all' or metric == 'timeliness':
                time = ProgramTimeliness.objects.filter(program=program, fiscal_year=fy)
                if len(time) > 0:
                    time = time[0]
                    if time.total_dollars and (float(time.late_dollars / time.total_dollars)  > .5):
                        total += ob.obligation
                        agency_totals = add_to_agency(agency_totals, program.agency.name, fy, ob.obligation)
                        writer.writerow((program.program_number, "%s" % program.program_title.encode('ascii', 'ignore'), 'timeliness', ob.obligation))
                        continue

        # fail completeness?
            if metric == 'all' or metric == 'completeness':
                comp = ProgramCompleteness.objects.filter(program=program, fiscal_year=fy)
                if len(comp) > 0:
                    comp = comp[0]
                    if comp.completeness_total_dollars and (float(comp.completeness_failed_dollars / comp.completeness_total_dollars) > .5):
                        total += ob.obligation
                        agency_totals = add_to_agency(agency_totals, program.agency.name, fy, ob.obligation)
                        writer.writerow((program.program_number, "%s" % program.program_title.encode('ascii', 'ignore'), 'completeness', ob.obligation))
                        continue


    count += 1
    all_obs = ProgramObligation.objects.filter(program__in=fins, fiscal_year=fy, type=TYPE).aggregate(summ=Sum('obligation'))['summ']
    if all_obs:

        print "%s - Total failed obligations: %s out of total obligations: %s (%s%%)" % (fy, pretty_money(total), 