<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> from datetime import datetime <EOL> from controllers . util import co <EOL> from controllers . util import request_counter_shard <EOL> from controllers . util import request_timestamp_shard <EOL> from google . appengine . api import memcache <EOL> def FormatTimedelta ( time_delta ) : <EOL> """<STR_LIT>""" <EOL> seconds = int ( time_delta . total_seconds ( ) ) <EOL> days , time_left = divmod ( seconds , <NUM_LIT> ) <EOL> hours , time_left = divmod ( time_left , <NUM_LIT> ) <EOL> minutes , seconds = divmod ( time_left , <NUM_LIT> ) <EOL> pretty_label = '<STR_LIT>' % seconds <EOL> if days > <NUM_LIT:0> : <EOL> pretty_label = '<STR_LIT>' % ( days , hours , minutes ) <EOL> elif hours > <NUM_LIT:0> : <EOL> pretty_label = '<STR_LIT>' % ( hours , minutes ) <EOL> elif minutes > <NUM_LIT:0> : <EOL> pretty_label = '<STR_LIT>' % ( minutes , seconds ) <EOL> return pretty_label <EOL> def GetApiQueryLastRequest ( query_id ) : <EOL> """<STR_LIT>""" <EOL> if query_id : <EOL> request_timestamp_key = co . REQUEST_TIMESTAMP_KEY_TEMPLATE . format ( query_id ) <EOL> request_timestamp = memcache . get ( request_timestamp_key ) <EOL> if not request_timestamp : <EOL> request_timestamp = request_timestamp_shard . GetTimestamp ( <EOL> request_timestamp_key ) <EOL> return request_timestamp <EOL> return None <EOL> def GetApiQueryRequestCount ( query_id ) : <EOL> """<STR_LIT>""" <EOL> request_counter_key = co . REQUEST_COUNTER_KEY_TEMPLATE . format ( query_id ) <EOL> request_count = memcache . get ( request_counter_key ) <EOL> if not request_count : <EOL> request_count = request_counter_shard . GetCount ( request_counter_key ) <EOL> return request_count <EOL> def GetLastRequestTimedelta ( api_query , from_time = None ) : <EOL> """<STR_LIT>""" <EOL> if not from_time : <EOL> from_time = datetime . utcnow ( ) <EOL> if api_query . last_request : <EOL> time_delta = from_time - api_query . last_request <EOL> return FormatTimedelta ( time_delta ) <EOL> return None <EOL> def GetModifiedTimedelta ( api_query , from_time = None ) : <EOL> """<STR_LIT>""" <EOL> if not from_time : <EOL> from_time = datetime . utcnow ( ) <EOL> api_query_response = api_query . api_query_responses . get ( ) <EOL> if api_query_response : <EOL> time_delta = from_time - api_query_response . modified <EOL> return FormatTimedelta ( time_delta ) <EOL> return None <EOL> def IsApiQueryAbandoned ( api_query ) : <EOL> """<STR_LIT>""" <EOL> if api_query . last_request : <EOL> last_request_age = int ( <EOL> ( datetime . utcnow ( ) - api_query . last_request ) . total_seconds ( ) ) <EOL> max_timedelta = co . ABANDONED_INTERVAL_MULTIPLE * api_query . refresh_interval <EOL> return last_request_age > max_timedelta <EOL> elif api_query . modified : <EOL> last_modified_age = int ( <EOL> ( datetime . utcnow ( ) - api_query . modified ) . total_seconds ( ) ) <EOL> max_timedelta = co . ABANDONED_INTERVAL_MULTIPLE * api_query . refresh_interval <EOL> return last_modified_age > max_timedelta <EOL> else : <EOL> api_query_response = api_query . api_query_responses . get ( ) <EOL> if api_query_response : <EOL> return True <EOL> return False <EOL> def IsErrorLimitReached ( api_query ) : <EOL> """<STR_LIT>""" <EOL> return ( api_query . api_query_errors . count ( <EOL> limit = co . QUERY_ERROR_LIMIT ) == co . QUERY_ERROR_LIMIT ) </s>
