def _print_report(stats, now=None):
 if (now is None):
  now = datetime.utcnow()
 s = stats
 if (not s['clusters']):
  print('No clusters created in the past two months!')
  return
 print(('Total  # of Clusters: %d' % len(s['clusters'])))
 print()
 print('* All times are in UTC.')
 print()
 print(('Min create time: %s' % min((cs['created'] for cs in s['clusters']))))
 print(('Max create time: %s' % max((cs['created'] for cs in s['clusters']))))
 print(('   Current time: %s' % now.replace(microsecond=0)))
 print()
 print('* All usage is measured in Normalized Instance Hours, which are')
 print('  roughly equivalent to running an m1.medium instance for an hour.')
 print("  Billing is estimated, and may not match Amazon's system exactly.")
 print()
 def with_pct(usage):
  return (usage, _percent(usage, s['nih_billed']))
 print(('Total billed:  %9.2f  %5.1f%%' % with_pct(s['nih_billed'])))
 print(('  Total used:  %9.2f  %5.1f%%' % with_pct(s['nih_used'])))
 print(('    bootstrap: %9.2f  %5.1f%%' % with_pct(s['bootstrap_nih_used'])))
 print(('    jobs:      %9.2f  %5.1f%%' % with_pct(s['job_nih_used'])))
 print(('  Total waste: %9.2f  %5.1f%%' % with_pct(s['nih_bbnu'])))
 print(('    at end:    %9.2f  %5.1f%%' % with_pct(s['end_nih_bbnu'])))
 print(('    other:     %9.2f  %5.1f%%' % with_pct(s['other_nih_bbnu'])))
 print()
 if s['date_to_nih_billed']:
  print('Daily statistics:')
  print()
  print(' date          billed      used     waste   % waste')
  d = max(s['date_to_nih_billed'])
  while (d >= min(s['date_to_nih_billed'])):
   print((' %10s %9.2f %9.2f %9.2f     %5.1f' % (d, s['date_to_nih_billed'].get(d, 0.0), s['date_to_nih_used'].get(d, 0.0), s['date_to_nih_bbnu'].get(d, 0.0), _percent(s['date_to_nih_bbnu'].get(d, 0.0), s['date_to_nih_billed'].get(d, 0.0)))))
   d -= timedelta(days=1)
  print()
 if s['hour_to_nih_billed']:
  print('Hourly statistics:')
  print()
  print(' hour              billed      used     waste   % waste')
  h = max(s['hour_to_nih_billed'])
  while (h >= min(s['hour_to_nih_billed'])):
   print((' %13s  %9.2f %9.2f %9.2f     %5.1f' % (h.strftime('%Y-%m-%d %H'), s['hour_to_nih_billed'].get(h, 0.0), s['hour_to_nih_used'].get(h, 0.0), s['hour_to_nih_bbnu'].get(h, 0.0), _percent(s['hour_to_nih_bbnu'].get(h, 0.0), s['hour_to_nih_billed'].get(h, 0.0)))))
   h -= timedelta(hours=1)
  print()
 print('* clusters are considered to belong to the user and job that')
 print('  started them or last ran on them.')
 print()
 print('Top jobs, by total time used:')
 for (label, nih_used) in sorted(s['label_to_nih_used'].items(), key=(lambda lb_nih: ((- lb_nih[1]), lb_nih[0]))):
  print(('  %9.2f %s' % (nih_used, label)))
 print()
 print('Top jobs, by time billed but not used:')
 for (label, nih_bbnu) in sorted(s['label_to_nih_bbnu'].items(), key=(lambda lb_nih1: ((- lb_nih1[1]), lb_nih1[0]))):
  print(('  %9.2f %s' % (nih_bbnu, label)))
 print()
 print('Top users, by total time used:')
 for (owner, nih_used) in sorted(s['owner_to_nih_used'].items(), key=(lambda o_nih: ((- o_nih[1]), o_nih[0]))):
  print(('  %9.2f %s' % (nih_used, owner)))
 print()
 print('Top users, by time billed but not used:')
 for (owner, nih_bbnu) in sorted(s['owner_to_nih_bbnu'].items(), key=(lambda o_nih2: ((- o_nih2[1]), o_nih2[0]))):
  print(('  %9.2f %s' % (nih_bbnu, owner)))
 print()
 print('Top job steps, by total time used (step number first):')
 for ((label, step_num), nih_used) in sorted(s['job_step_to_nih_used'].items(), key=(lambda k_nih: ((- k_nih[1]), k_nih[0]))):
  if label:
   print(('  %9.2f %3d %s' % (nih_used, step_num, label)))
  else:
   print(('  %9.2f     (non-mrjob step)' % (nih_used,)))
 print()
 print('Top job steps, by total time billed but not used (un-pooled only):')
 for ((label, step_num), nih_bbnu) in sorted(s['job_step_to_nih_bbnu_no_pool'].items(), key=(lambda k_nih3: ((- k_nih3[1]), k_nih3[0]))):
  if label:
   print(('  %9.2f %3d %s' % (nih_bbnu, step_num, label)))
  else:
   print(('  %9.2f     (non-mrjob step)' % (nih_bbnu,)))
 print()
 print('All pools, by total time billed:')
 for (pool, nih_billed) in sorted(s['pool_to_nih_billed'].items(), key=(lambda p_nih: ((- p_nih[1]), p_nih[0]))):
  print(('  %9.2f %s' % (nih_billed, (pool or '(not pooled)'))))
 print()
 print('All pools, by total time billed but not used:')
 for (pool, nih_bbnu) in sorted(s['pool_to_nih_bbnu'].items(), key=(lambda p_nih4: ((- p_nih4[1]), p_nih4[0]))):
  print(('  %9.2f %s' % (nih_bbnu, (pool or '(not pooled)'))))
 print()
 print('All clusters, by total time billed:')
 top_clusters = sorted(s['clusters'], key=(lambda cs: ((- cs['nih_billed']), cs['name'])))
 for cs in top_clusters:
  print(('  %9.2f %-15s %s' % (cs['nih_billed'], cs['id'], cs['name'])))
 print()
 print('All clusters, by time billed but not used:')
 top_clusters_bbnu = sorted(s['clusters'], key=(lambda cs: ((- cs['nih_bbnu']), cs['name'])))
 for cs in top_clusters_bbnu:
  print(('  %9.2f %-15s %s' % (cs['nih_bbnu'], cs['id'], cs['name'])))
 print()
 print('Details for all clusters:')
 print()
 print(' id              state                  created             steps        time ran     billed    waste   user   name')
 all_clusters = sorted(s['clusters'], key=(lambda cs: cs['created']), reverse=True)
 for cs in all_clusters:
  print((' %-15s %-22s %19s %3d %17s %9.2f %9.2f %8s %s' % (cs['id'], cs['state'], cs['created'], cs['num_steps'], strip_microseconds(cs['ran']), cs['nih_used'], cs['nih_bbnu'], (cs['owner'] or ''), (cs['label'] or 'not started by mrjob'))))

def _print_report ( stats , now = None ) : if ( now is None ) : now = datetime . utcnow ( ) s = stats if ( not s [ 'clusters' ] ) : print ( 'No clusters created in the past two months!' ) return print ( ( 'Total  # of Clusters: %d' % len ( s [ 'clusters' ] ) ) ) print ( ) print ( '* All times are in UTC.' ) print ( ) print ( ( 'Min create time: %s' % min ( ( cs [ 'created' ] for cs in s [ 'clusters' ] ) ) ) ) print ( ( 'Max create time: %s' % max ( ( cs [ 'created' ] for cs in s [ 'clusters' ] ) ) ) ) print ( ( '   Current time: %s' % now . replace ( microsecond = 0 ) ) ) print ( ) print ( '* All usage is measured in Normalized Instance Hours, which are' ) print ( '  roughly equivalent to running an m1.medium instance for an hour.' ) print ( "  Billing is estimated, and may not match Amazon's system exactly." ) print ( ) def with_pct ( usage ) : return ( usage , _percent ( usage , s [ 'nih_billed' ] ) ) print ( ( 'Total billed:  %9.2f  %5.1f%%' % with_pct ( s [ 'nih_billed' ] ) ) ) print ( ( '  Total used:  %9.2f  %5.1f%%' % with_pct ( s [ 'nih_used' ] ) ) ) print ( ( '    bootstrap: %9.2f  %5.1f%%' % with_pct ( s [ 'bootstrap_nih_used' ] ) ) ) print ( ( '    jobs:      %9.2f  %5.1f%%' % with_pct ( s [ 'job_nih_used' ] ) ) ) print ( ( '  Total waste: %9.2f  %5.1f%%' % with_pct ( s [ 'nih_bbnu' ] ) ) ) print ( ( '    at end:    %9.2f  %5.1f%%' % with_pct ( s [ 'end_nih_bbnu' ] ) ) ) print ( ( '    other:     %9.2f  %5.1f%%' % with_pct ( s [ 'other_nih_bbnu' ] ) ) ) print ( ) if s [ 'date_to_nih_billed' ] : print ( 'Daily statistics:' ) print ( ) print ( ' date          billed      used     waste   % waste' ) d = max ( s [ 'date_to_nih_billed' ] ) while ( d >= min ( s [ 'date_to_nih_billed' ] ) ) : print ( ( ' %10s %9.2f %9.2f %9.2f     %5.1f' % ( d , s [ 'date_to_nih_billed' ] . get ( d , 0.0 ) , s [ 'date_to_nih_used' ] . get ( d , 0.0 ) , s [ 'date_to_nih_bbnu' ] . get ( d , 0.0 ) , _percent ( s [ 'date_to_nih_bbnu' ] . get ( d , 0.0 ) , s [ 'date_to_nih_billed' ] . get ( d , 0.0 ) ) ) ) ) d -= timedelta ( days = 1 ) print ( ) if s [ 'hour_to_nih_billed' ] : print ( 'Hourly statistics:' ) print ( ) print ( ' hour              billed      used     waste   % waste' ) h = max ( s [ 'hour_to_nih_billed' ] ) while ( h >= min ( s [ 'hour_to_nih_billed' ] ) ) : print ( ( ' %13s  %9.2f %9.2f %9.2f     %5.1f' % ( h . strftime ( '%Y-%m-%d %H' ) , s [ 'hour_to_nih_billed' ] . get ( h , 0.0 ) , s [ 'hour_to_nih_used' ] . get ( h , 0.0 ) , s [ 'hour_to_nih_bbnu' ] . get ( h , 0.0 ) , _percent ( s [ 'hour_to_nih_bbnu' ] . get ( h , 0.0 ) , s [ 'hour_to_nih_billed' ] . get ( h , 0.0 ) ) ) ) ) h -= timedelta ( hours = 1 ) print ( ) print ( '* clusters are considered to belong to the user and job that' ) print ( '  started them or last ran on them.' ) print ( ) print ( 'Top jobs, by total time used:' ) for ( label , nih_used ) in sorted ( s [ 'label_to_nih_used' ] . items ( ) , key = ( lambda lb_nih : ( ( - lb_nih [ 1 ] ) , lb_nih [ 0 ] ) ) ) : print ( ( '  %9.2f %s' % ( nih_used , label ) ) ) print ( ) print ( 'Top jobs, by time billed but not used:' ) for ( label , nih_bbnu ) in sorted ( s [ 'label_to_nih_bbnu' ] . items ( ) , key = ( lambda lb_nih1 : ( ( - lb_nih1 [ 1 ] ) , lb_nih1 [ 0 ] ) ) ) : print ( ( '  %9.2f %s' % ( nih_bbnu , label ) ) ) print ( ) print ( 'Top users, by total time used:' ) for ( owner , nih_used ) in sorted ( s [ 'owner_to_nih_used' ] . items ( ) , key = ( lambda o_nih : ( ( - o_nih [ 1 ] ) , o_nih [ 0 ] ) ) ) : print ( ( '  %9.2f %s' % ( nih_used , owner ) ) ) print ( ) print ( 'Top users, by time billed but not used:' ) for ( owner , nih_bbnu ) in sorted ( s [ 'owner_to_nih_bbnu' ] . items ( ) , key = ( lambda o_nih2 : ( ( - o_nih2 [ 1 ] ) , o_nih2 [ 0 ] ) ) ) : print ( ( '  %9.2f %s' % ( nih_bbnu , owner ) ) ) print ( ) print ( 'Top job steps, by total time used (step number first):' ) for ( ( label , step_num ) , nih_used ) in sorted ( s [ 'job_step_to_nih_used' ] . items ( ) , key = ( lambda k_nih : ( ( - k_nih [ 1 ] ) , k_nih [ 0 ] ) ) ) : if label : print ( ( '  %9.2f %3d %s' % ( nih_used , step_num , label ) ) ) else : print ( ( '  %9.2f     (non-mrjob step)' % ( nih_used , ) ) ) print ( ) print ( 'Top job steps, by total time billed but not used (un-pooled only):' ) for ( ( label , step_num ) , nih_bbnu ) in sorted ( s [ 'job_step_to_nih_bbnu_no_pool' ] . items ( ) , key = ( lambda k_nih3 : ( ( - k_nih3 [ 1 ] ) , k_nih3 [ 0 ] ) ) ) : if label : print ( ( '  %9.2f %3d %s' % ( nih_bbnu , step_num , label ) ) ) else : print ( ( '  %9.2f     (non-mrjob step)' % ( nih_bbnu , ) ) ) print ( ) print ( 'All pools, by total time billed:' ) for ( pool , nih_billed ) in sorted ( s [ 'pool_to_nih_billed' ] . items ( ) , key = ( lambda p_nih : ( ( - p_nih [ 1 ] ) , p_nih [ 0 ] ) ) ) : print ( ( '  %9.2f %s' % ( nih_billed , ( pool or '(not pooled)' ) ) ) ) print ( ) print ( 'All pools, by total time billed but not used:' ) for ( pool , nih_bbnu ) in sorted ( s [ 'pool_to_nih_bbnu' ] . items ( ) , key = ( lambda p_nih4 : ( ( - p_nih4 [ 1 ] ) , p_nih4 [ 0 ] ) ) ) : print ( ( '  %9.2f %s' % ( nih_bbnu , ( pool or '(not pooled)' ) ) ) ) print ( ) print ( 'All clusters, by total time billed:' ) top_clusters = sorted ( s [ 'clusters' ] , key = ( lambda cs : ( ( - cs [ 'nih_billed' ] ) , cs [ 'name' ] ) ) ) for cs in top_clusters : print ( ( '  %9.2f %-15s %s' % ( cs [ 'nih_billed' ] , cs [ 'id' ] , cs [ 'name' ] ) ) ) print ( ) print ( 'All clusters, by time billed but not used:' ) top_clusters_bbnu = sorted ( s [ 'clusters' ] , key = ( lambda cs : ( ( - cs [ 'nih_bbnu' ] ) , cs [ 'name' ] ) ) ) for cs in top_clusters_bbnu : print ( ( '  %9.2f %-15s %s' % ( cs [ 'nih_bbnu' ] , cs [ 'id' ] , cs [ 'name' ] ) ) ) print ( ) print ( 'Details for all clusters:' ) print ( ) print ( ' id              state                  created             steps        time ran     billed    waste   user   name' ) all_clusters = sorted ( s [ 'clusters' ] , key = ( lambda cs : cs [ 'created' ] ) , reverse = True ) for cs in all_clusters : print ( ( ' %-15s %-22s %19s %3d %17s %9.2f %9.2f %8s %s' % ( cs [ 'id' ] , cs [ 'state' ] , cs [ 'created' ] , cs [ 'num_steps' ] , strip_microseconds ( cs [ 'ran' ] ) , cs [ 'nih_used' ] , cs [ 'nih_bbnu' ] , ( cs [ 'owner' ] or '' ) , ( cs [ 'label' ] or 'not started by mrjob' ) ) ) )
takes in a dictionary of info about a long-running job .