import re, json, time
from const import *
[docs]def stats( good, bad, timetaken, children, times, starttime ):
"""Prints out statistics for the current running pool.
This includes the following:
* Successful/failed jobs
* Total jobs done
* Remaining jobs
* Failure rate
* Number of children currently and at peak
* Average time per job
* Average jobs per minute, seconds, and more
:param good: The number of jobs completed successfully.
:param bad: The number of jobs that failed.
:param timetaken: An array of job completion times from all children.
:param children: An array of our pool's children. Counted and checked if alive for display.
:param times: The number of jobs initially given to our pool.
:param starttime: A timestamp for when our pool started.
:return: None
"""
print( ''.join( [ "\n", ( "=" * 40 ) ] ) )
print( ''.join( [ "Successful: ", str( good ), ( " " * 3 ), "Failed: ", str( bad ) ] ) )
print( ''.join( [ "Total: ", str( good + bad ), ( " " * 3 ), "Remaining: ", str( times - good ) ] ) )
active = 0
for c in children:
if c.is_alive( ) and not c.is_done( ):
active += 1
print( ''.join( [ "Children (peak): ", str( len( children ) ), ( " " * 3 ), "Children (active): ", str( active ) ] ) )
if len( timetaken ) > 0:
print( ''.join( [ "Failure Rate: ", format( bad / float( good + bad ) * 100 ), "%" ] ) );
# This gives us our jobs per second
jps = good / ( time.time( ) - starttime )
print( "Average / Estimates:" )
print( ''.join( [ " Time per job: ", format( avg( timetaken ) ), "s" ] ) )
print( ''.join( [ " Jobs/s: ", format( jps ), ( " " * 3 ), "Jobs/m: ", format( jps * 60 ), ( " " * 3 ), "Jobs/hr: ",
format( jps * 60 * 60 ), ( " " * 3 ), "Jobs/day: ", format( jps * 60 * 60 * 24 ) ] ) )
else:
print "No data to extrapolate or average from"
print( ''.join( [ ( "=" * 40 ), "\n" ] ) )
[docs]def errorLevelToStr( level, parens=True ):
"""Takes a numeric error level and translates it into a string for displaying in a
log.
:param level: The level to translate into a loggable string.
:param True parens: If the level will be contained in parenthesis
:return: String of the level in parenthesis.
"""
r = ""
if level == NOTICE:
r = "NOTICE"
elif level == WARNING:
r = "WARNING"
elif level == ERR:
r = "ERROR"
elif level == CRITICAL:
r = "CRITICAL"
elif level == INFO:
r = "INFO"
elif level == NONE and parens == True:
parens = False
if parens:
return ''.join( [ "(", r, ")", (10-len(r))*" " ] )
else:
return ''.join( [ r, (10-len(r))*" " ] )
[docs]def avg( numbers ):
"""Averages a list of numbers.
:param numbers: A list of numbers.
:return: The average
"""
if len( numbers ) == 0:
return 0
else:
return( sum( numbers ) / len( numbers ) )