<s> import datetime <EOL> from django . core . cache import cache <EOL> from utils . sql import query_to_dicts <EOL> from utils . enum import EnumMetaclass <EOL> def topreq_data_hits_available ( days ) : <EOL> """<STR_LIT>""" <EOL> start_time = datetime . date . today ( ) - datetime . timedelta ( int ( days ) ) <EOL> return list ( query_to_dicts ( """<STR_LIT>""" . format ( start_time . isoformat ( ) ) ) ) <EOL> def topreq_data_hits_posted ( days ) : <EOL> """<STR_LIT>""" <EOL> start_time = datetime . date . today ( ) - datetime . timedelta ( int ( days ) ) <EOL> return list ( query_to_dicts ( """<STR_LIT>""" . format ( start_time . isoformat ( ) ) ) ) <EOL> class ToprequestersReport : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = EnumMetaclass <EOL> cache_key_base = "<STR_LIT>" <EOL> AVAILABLE = <NUM_LIT:0> <EOL> POSTED = <NUM_LIT:1> <EOL> display_names = { <EOL> AVAILABLE : '<STR_LIT>' , <EOL> POSTED : '<STR_LIT>' , <EOL> } <EOL> REPORT_FUNCTION = { <EOL> AVAILABLE : topreq_data_hits_available , <EOL> POSTED : topreq_data_hits_posted , <EOL> } <EOL> @ classmethod <EOL> def get_cache_key ( cls , report_type ) : <EOL> """<STR_LIT>""" <EOL> return cls . cache_key_base + str ( report_type ) <EOL> @ classmethod <EOL> def __get_cache_key_for_meta ( cls , report_type ) : <EOL> """<STR_LIT>""" <EOL> return cls . cache_key_base + str ( report_type ) + '<STR_LIT>' <EOL> @ classmethod <EOL> def is_cached ( cls , report_type ) : <EOL> """<STR_LIT>""" <EOL> return cls . get_report_data ( report_type ) is not None <EOL> @ classmethod <EOL> def get_report_data ( cls , report_type ) : <EOL> """<STR_LIT>""" <EOL> return cache . get ( cls . get_cache_key ( report_type ) ) <EOL> @ classmethod <EOL> def get_report_meta ( cls , report_type ) : <EOL> """<STR_LIT>""" <EOL> return cache . get ( cls . __get_cache_key_for_meta ( report_type ) ) <EOL> @ classmethod <EOL> def get_available_as_str ( cls ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> for rid , name in cls . display_names . iteritems ( ) : <EOL> in_cache = cls . is_cached ( rid ) <EOL> in_cache = '<STR_LIT>' if in_cache else '<STR_LIT>' <EOL> meta = cls . get_report_meta ( rid ) <EOL> if meta : <EOL> meta = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( meta . get ( '<STR_LIT>' ) , meta . get ( '<STR_LIT>' ) , <EOL> int ( meta . get ( '<STR_LIT>' ) ) ) <EOL> else : <EOL> meta = '<STR_LIT>' <EOL> lines . append ( "<STR_LIT>" . format ( in_cache , rid , name , meta ) ) <EOL> lines . append ( '<STR_LIT:\n>' ) <EOL> return '<STR_LIT>' . join ( lines ) <EOL> @ classmethod <EOL> def store ( cls , report_type , data , meta = None , cache_expiry = <NUM_LIT> * <NUM_LIT> * <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> cls . __store_meta ( report_type , meta , cache_expiry ) <EOL> return cache . set ( cls . get_cache_key ( report_type ) , data , cache_expiry ) <EOL> @ classmethod <EOL> def __store_meta ( cls , report_type , meta , cache_expiry ) : <EOL> """<STR_LIT>""" <EOL> return cache . set ( <EOL> cls . __get_cache_key_for_meta ( report_type ) , meta , cache_expiry ) <EOL> @ classmethod <EOL> def purge ( cls , report_type ) : <EOL> """<STR_LIT>""" <EOL> return cache . delete ( cls . get_cache_key ( report_type ) ) </s>
