<s> from metrics . models import * <EOL> from settings import FISCAL_YEARS <EOL> from cfda . models import * <EOL> from django . db . models import Sum <EOL> from helpers . charts import Line <EOL> from utils import pretty_money <EOL> import csv <EOL> def find_possible_mistakes ( ) : <EOL> fins = Program . objects . filter ( types_of_assistance__financial = True ) . distinct ( ) . order_by ( '<STR_LIT>' ) <EOL> csv_out = csv . writer ( open ( '<STR_LIT>' , '<STR_LIT:w>' ) ) <EOL> for f in fins : <EOL> obs = ProgramObligation . objects . filter ( type = <NUM_LIT:1> , program = f ) . order_by ( '<STR_LIT>' ) <EOL> if obs : <EOL> current = obs [ <NUM_LIT:0> ] <EOL> for o in obs : <EOL> if current == o : continue <EOL> if not current . obligation : continue <EOL> pct_change = float ( o . obligation - current . obligation ) / float ( current . obligation ) <EOL> if pct_change < - <NUM_LIT> or pct_change > <NUM_LIT> : <EOL> csv_out . writerow ( ( o . program . program_number , "<STR_LIT:%s>" % o . program . program_title . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) , current . fiscal_year , current . obligation , o . fiscal_year , o . obligation ) ) <EOL> current = o <EOL> def add_to_agency ( agency_totals , agency_name , fy , obligation ) : <EOL> if agency_totals . has_key ( agency_name ) : <EOL> if agency_totals [ agency_name ] . has_key ( fy ) : <EOL> agency_totals [ agency_name ] [ fy ] += obligation <EOL> else : <EOL> agency_totals [ agency_name ] [ fy ] = obligation <EOL> else : <EOL> agency_totals [ agency_name ] = { fy : obligation } <EOL> return agency_totals <EOL> TYPE = <NUM_LIT:1> <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> TYPE = int ( sys . argv [ <NUM_LIT:1> ] ) <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> metric = sys . argv [ <NUM_LIT:2> ] <EOL> else : <EOL> metric = '<STR_LIT:all>' <EOL> print "<STR_LIT>" % metric <EOL> fins = Program . objects . filter ( types_of_assistance__financial = True ) . distinct ( ) . order_by ( '<STR_LIT>' ) <EOL> count = <NUM_LIT:0> <EOL> types = [ None , '<STR_LIT>' , '<STR_LIT>' ] <EOL> agency_totals = { } <EOL> for fy in FISCAL_YEARS : <EOL> writer = csv . writer ( open ( '<STR_LIT>' % ( types [ TYPE ] , fy ) , '<STR_LIT:w>' ) , delimiter = '<STR_LIT:U+002C>' , quotechar = '<STR_LIT:">' ) <EOL> writer . writerow ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> total = <NUM_LIT:0> <EOL> for program in fins : <EOL> ob = ProgramObligation . objects . filter ( program = program , fiscal_year = fy , type = TYPE ) <EOL> if len ( ob ) > <NUM_LIT:0> : <EOL> ob = ob [ <NUM_LIT:0> ] <EOL> if metric == '<STR_LIT:all>' or metric == '<STR_LIT>' : <EOL> cons = ProgramConsistency . objects . filter ( program = program , fiscal_year = fy , type = TYPE ) <EOL> if len ( cons ) > <NUM_LIT:0> : <EOL> cons_single = cons [ <NUM_LIT:0> ] <EOL> if cons_single . non_reported_dollars > <NUM_LIT:0> or cons_single . under_reported_pct > <NUM_LIT:50> or cons_single . over_reported_pct > <NUM_LIT:50> : <EOL> total += ob . obligation <EOL> agency_totals = add_to_agency ( agency_totals , program . agency . name , fy , ob . obligation ) <EOL> writer . writerow ( ( program . program_number , "<STR_LIT:%s>" % program . program_title . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) , '<STR_LIT>' , ob . obligation , ob . usaspending_obligation ) ) <EOL> continue <EOL> if metric == '<STR_LIT:all>' or metric == '<STR_LIT>' : <EOL> time = ProgramTimeliness . objects . filter ( program = program , fiscal_year = fy ) <EOL> if len ( time ) > <NUM_LIT:0> : <EOL> time = time [ <NUM_LIT:0> ] <EOL> if time . total_dollars and ( float ( time . late_dollars / time . total_dollars ) > <NUM_LIT> ) : <EOL> total += ob . obligation <EOL> agency_totals = add_to_agency ( agency_totals , program . agency . name , fy , ob . obligation ) <EOL> writer . writerow ( ( program . program_number , "<STR_LIT:%s>" % program . program_title . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) , '<STR_LIT>' , ob . obligation ) ) <EOL> continue <EOL> if metric == '<STR_LIT:all>' or metric == '<STR_LIT>' : <EOL> comp = ProgramCompleteness . objects . filter ( program = program , fiscal_year = fy ) <EOL> if len ( comp ) > <NUM_LIT:0> : <EOL> comp = comp [ <NUM_LIT:0> ] <EOL> if comp . completeness_total_dollars and ( float ( comp . completeness_failed_dollars / comp . completeness_total_dollars ) > <NUM_LIT> ) : <EOL> total += ob . obligation <EOL> agency_totals = add_to_agency ( agency_totals , program . agency . name , fy , ob . obligation ) <EOL> writer . writerow ( ( program . program_number , "<STR_LIT:%s>" % program . program_title . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) , '<STR_LIT>' , ob . obligation ) ) <EOL> continue <EOL> count += <NUM_LIT:1> <EOL> all_obs = ProgramObligation . objects . filter ( program__in = fins , fiscal_year = fy , type = TYPE ) . aggregate ( summ = Sum ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> if all_obs : <EOL> print "<STR_LIT>" % ( fy , pretty_money ( total ) , <EOL> pretty_money ( all_obs ) , <EOL> round ( total * <NUM_LIT:100> / all_obs , <NUM_LIT:2> ) ) <EOL> else : <EOL> print "<STR_LIT>" % fy <EOL> data = [ ] <EOL> agency_names = [ ] <EOL> for k in agency_totals . keys ( ) : <EOL> points = [ ] <EOL> for fy in FISCAL_YEARS : <EOL> if agency_totals [ k ] . has_key ( fy ) : <EOL> points . append ( ( fy , float ( agency_totals [ k ] [ fy ] ) ) ) <EOL> else : <EOL> points . append ( ( fy , <NUM_LIT:0> ) ) <EOL> data . append ( points ) <EOL> agency_names . append ( k ) </s>
