<s> from __future__ import unicode_literals <EOL> import random <EOL> from datetime import datetime , timedelta <EOL> from . models import R <EOL> _redis_model = None <EOL> def get_r ( ) : <EOL> global _redis_model <EOL> if not _redis_model : <EOL> _redis_model = R ( ) <EOL> return _redis_model <EOL> def set_metric ( slug , value , category = None , expire = None , date = None ) : <EOL> """<STR_LIT>""" <EOL> get_r ( ) . set_metric ( slug , value , category = category , expire = expire , date = date ) <EOL> def metric ( slug , num = <NUM_LIT:1> , category = None , expire = None , date = None ) : <EOL> """<STR_LIT>""" <EOL> get_r ( ) . metric ( slug , num = num , category = category , expire = expire , date = date ) <EOL> def gauge ( slug , current_value ) : <EOL> """<STR_LIT>""" <EOL> get_r ( ) . gauge ( slug , current_value ) <EOL> def generate_test_metrics ( slug = '<STR_LIT>' , num = <NUM_LIT:100> , randomize = False , <EOL> cap = None , increment_value = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> r = get_r ( ) <EOL> i = <NUM_LIT:0> <EOL> if randomize : <EOL> random . seed ( ) <EOL> r . r . sadd ( r . _metric_slugs_key , slug ) <EOL> for date in r . _date_range ( '<STR_LIT>' , datetime . utcnow ( ) - timedelta ( days = num ) ) : <EOL> keys = r . _build_keys ( slug , date = date ) <EOL> keys = [ k for k in keys if k . split ( "<STR_LIT::>" ) [ <NUM_LIT:2> ] not in [ '<STR_LIT:i>' , '<STR_LIT:s>' , '<STR_LIT:h>' ] ] <EOL> for key in keys : <EOL> value = i <EOL> if randomize : <EOL> value = random . randint ( <NUM_LIT:0> , i + increment_value ) <EOL> if cap and r . r . get ( key ) >= cap : <EOL> value = <NUM_LIT:0> <EOL> r . r . incr ( key , value ) <EOL> i += increment_value <EOL> def delete_test_metrics ( slug = '<STR_LIT>' , num = <NUM_LIT:100> ) : <EOL> """<STR_LIT>""" <EOL> r = get_r ( ) <EOL> for date in r . _date_range ( '<STR_LIT>' , datetime . utcnow ( ) - timedelta ( days = num ) ) : <EOL> keys = r . _build_keys ( slug , date = date ) <EOL> r . r . srem ( r . _metric_slugs_key , slug ) <EOL> r . r . delete ( * keys ) </s>
