#NEWLINE# def _GenerateTrendingInstallsCache( since_hours = None ) : #NEWLINE# #INDENT# 'Generates trending install and failure data.' #NEWLINE# trending = { #NEWLINE# #INDENT# 'success' : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , #NEWLINE# 'failure' : { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# total_success = 0 #NEWLINE# total_failure = 0 #NEWLINE# if ( not since_hours ) : #NEWLINE# #INDENT# since_hours = 1 #NEWLINE# #UNINDENT# dt = ( datetime . datetime . utcnow ( ) - datetime . timedelta ( minutes = ( since_hours * 60 ) ) ) #NEWLINE# query = models . InstallLog . all ( ) . filter ( 'mtime >' , dt ) #NEWLINE# for install in gae_util . QueryIterator ( query ) : #NEWLINE# #INDENT# pkg = install . package . encode ( 'utf-8' ) #NEWLINE# if install . IsSuccess ( ) : #NEWLINE# #INDENT# trending [ 'success' ] [ pkg ] = ( trending [ 'success' ] . setdefault ( pkg , 0 ) + 1 ) #NEWLINE# total_success += 1 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# trending [ 'failure' ] [ pkg ] = ( trending [ 'failure' ] . setdefault ( pkg , 0 ) + 1 ) #NEWLINE# total_failure += 1 #NEWLINE# #UNINDENT# #UNINDENT# success = sorted ( trending [ 'success' ] . items ( ) , key = ( lambda i : i [ 1 ] ) , reverse = True ) #NEWLINE# success = success [ : TRENDING_INSTALLS_LIMIT ] #NEWLINE# success = [ ( pkg , count , ( ( float ( count ) / total_success ) * 100 ) ) for ( pkg , count ) in success ] #NEWLINE# failure = sorted ( trending [ 'failure' ] . items ( ) , key = ( lambda i : i [ 1 ] ) , reverse = True ) #NEWLINE# failure = failure [ : TRENDING_INSTALLS_LIMIT ] #NEWLINE# failure = [ ( pkg , count , ( ( float ( count ) / total_failure ) * 100 ) ) for ( pkg , count ) in failure ] #NEWLINE# trending = { #NEWLINE# #INDENT# 'success' : { #NEWLINE# #INDENT# 'packages' : success , #NEWLINE# 'total' : total_success , #NEWLINE# #UNINDENT# } , #NEWLINE# 'failure' : { #NEWLINE# #INDENT# 'packages' : success , #NEWLINE# 'total' : total_failure , #NEWLINE# #UNINDENT# } , #NEWLINE# #UNINDENT# } #NEWLINE# models . ReportsCache . SetTrendingInstalls ( since_hours , trending )