def test_redis(self): redis = RedisController() user_profile = dict(test='test') assert redis.save(USER_ID, ACTION, user_profile) assert user_profile == redis.load(USER_ID, ACTION) # Delete assert redis.clean(USER_ID, ACTION)
def __init__(self, feature: str, redis_controller: RedisController = None): self._feature = feature if redis_controller: self.redis = redis_controller else: self.redis = RedisController()
class QueryHelper: def __init__(self, table: TicketsModel): self.tickets_table = table self.redis = RedisController(db_name='quick_search') # Quick Search # FIXME: Implement on Redis def save_quick_search(self, query: Query) -> bool: logger.debug(query.to_dict()) return self.redis.save(query.user_id, 'quick_search', query.to_dict(), expiration=2592000) def load_quick_search(self, user_id: int) -> Query: result = self.redis.load(user_id, 'quick_search') logger.debug(result) if result: return Query(category_id=result['category']).to_obj(result) return Query(category_id=-1).to_obj(result) # Search Ticket def search_by_section(self, section: str) -> list: return self.tickets_table.get_ticket_by_section(section) def search_by_user_id(self, user_id: int) -> list: return self.tickets_table.get_tickets_by_user_id(user_id) def search_by_ticket_id(self, ticket_id: str) -> Ticket: return self.tickets_table.get_ticket_by_ticket_id(ticket_id) def search_by_query(self, query: Query) -> list: return self.tickets_table.get_tickets_by_conditions(query.to_dict()) def search_by_date(self, date: str) -> list: return self.tickets_table.get_tickets_by_date(date) # Util @staticmethod def split_tickets_traits(tickets: list, size: int = 5) -> list: trait = [ticket.to_human_readable() for ticket in tickets] return [trait[i: i+size] for i in range(0, len(trait), size)]
def test_generate_query(self): redis = RedisController(db_name='search') helper = FeatureHelper(feature='search', redis_controller=redis) query = Query(category_id=1, user_id=USER_ID, username=USERNAME) assert helper.save_drafting_query(USER_ID, query) cached_query = helper.load_drafting_query(USER_ID) assert cached_query.category == 1 assert cached_query.user_id == USER_ID assert cached_query.username == USERNAME # Update Field query = cached_query query.update_field('dates', 504) helper.save_drafting_query(USER_ID, query) cached_query = helper.load_drafting_query(USER_ID) assert cached_query.dates == query.dates # Update Field query = cached_query query.update_field('dates', 505) helper.save_drafting_query(USER_ID, query) cached_query = helper.load_drafting_query(USER_ID) assert cached_query.dates == query.dates assert cached_query.to_human_readable() == dict( category='原價轉讓', dates='5.4(Sat), 5.5(Sun)', prices='', quantities='', status='待交易', user_id=8081, username='******') # Save to Quick Search query_helper = QueryHelper( TicketsModel(mayday.engine, mayday.metadata, role='writer')) quick_search = helper.load_drafting_query(USER_ID) assert query_helper.save_quick_search(quick_search) quick_search_query = query_helper.load_quick_search(user_id=USER_ID) assert query.category == quick_search_query.category assert query.dates == quick_search_query.dates assert query.to_dict() == quick_search_query.to_dict()
def test_redis_direct_read(self): redis = RedisController(redis_client=FakeStrictRedis()) user_profile = dict(test='test') assert redis.save(USER_ID, ACTION, user_profile) assert redis.direct_read(USER_ID, ACTION) == json.dumps(user_profile)
def test_redis_key(self): redis = RedisController(redis_client=FakeStrictRedis()) assert redis.get_key(USER_ID, ACTION) == '123456789_test'
def __init__(self, table: TicketsModel): self.tickets_table = table self.redis = RedisController(db_name='quick_search')
from mayday.helpers.auth_helper import AuthHelper from mayday.helpers.feature_helpers.search_helper import SearchHelper from mayday.helpers.feature_helpers.update_helper import UpdateHelper from mayday.helpers.query_helper import QueryHelper from mayday.helpers.ticket_helper import TicketHelper from mayday.objects.user import User auth_helper = AuthHelper( UsersModel(mayday.engine, mayday.metadata, role='writer')) query_helper = QueryHelper( TicketsModel(mayday.engine, mayday.metadata, role='writer')) ticket_helper = TicketHelper( TicketsModel(mayday.engine, mayday.metadata, role='writer')) search_helper = SearchHelper('search') update_helper = UpdateHelper(feature='update') redis = RedisController( redis_conection_pool=mayday.FEATURE_REDIS_CONNECTION_POOL) event_logger: logging.Logger = logging.getLogger('event') logger: logging.Logger = logging.getLogger('') @run_async def start(bot, update, *args, **kwargs): user = User(telegram_user=update.effective_user) redis.clean_all(user.user_id, 'start') if user.is_username_blank(): update.message.reply_text(conversations.MAIN_PANEL_USERNAME_MISSING) return stages.END access_pass = auth_helper.auth(user) if access_pass['is_admin']: pass
def before_all(self): redis = RedisController(redis_client=FakeStrictRedis()) self.helper = FeatureHelper(feature='test', redis_controller=redis)
def test_clean_all_cache(self): redis = RedisController() redis.save(USER_ID, 'search', dict(text='test')) redis.save(USER_ID, 'post', dict(text='test')) redis.save(USER_ID, 'quick_search', dict(text='test')) assert redis.count(USER_ID) == 4 redis.clean_all(USER_ID, 'start') assert redis.count(USER_ID) == 0
def test_redis_key(self): redis = RedisController() assert redis.get_key(USER_ID, ACTION) == '123456789_test'
class FeatureHelper: def __init__(self, feature: str, redis_controller: RedisController = None): self._feature = feature if redis_controller: self.redis = redis_controller else: self.redis = RedisController() # Feature Cache def load_cache(self, user_id: int): result = self.redis.load(user_id=user_id, action='{}_cache'.format(self._feature)) logger.info(dict(user_id=user_id, result=result)) event_logger.info(dict(user_id=user_id, result=result)) return result['field'] def save_cache(self, user_id: int, field=None) -> bool: logger.info( dict(user_id=user_id, action='{}_cache'.format(self._feature), content=dict(field=field))) event_logger.info( dict(user_id=user_id, action='{}_cache'.format(self._feature), content=dict(field=field))) return self.redis.save(user_id=user_id, action='{}_cache'.format(self._feature), content=dict(field=field)) def reset_cache(self, user_id: int, username: str): raise NotImplementedError def update_cache(self, user_id: int, value: (str, int)): raise NotImplementedError # Last Choice def load_last_choice(self, user_id: int) -> str: result = self.redis.load(user_id=user_id, action='{}_last_choice'.format(self._feature)) event_logger.info(dict(user_id=user_id, result=result)) return result['field'] def save_last_choice(self, user_id: int, field=None) -> bool: if field in {'row', 'remarks', 'reset', 'check'} | set( KEYBOARDS.conditions_keyboard_mapping.keys()): logger.info( dict(user_id=user_id, action='{}_last_choice'.format(self._feature), content=dict(field=field))) event_logger.info( dict(user_id=user_id, action='{}_last_choice'.format(self._feature), content=dict(field=field))) return self.redis.save(user_id=user_id, action='{}_last_choice'.format( self._feature), content=dict(field=field)) return False # Ticket def load_posting_ticket_category(self, user_id: int) -> int: return self.redis.load(user_id=user_id, action='ticket_category')['category'] def save_posting_ticket_cateogry(self, user_id: int, category: int) -> bool: return self.redis.save(user_id=user_id, action='ticket_category', content=dict(category=category)) def load_drafting_ticket(self, user_id: int) -> Ticket: result = self.redis.load(user_id=user_id, action='ticket') logger.info(result) event_logger.info(result) return Ticket(user_id=user_id).to_obj(result) def save_drafting_ticket(self, user_id: int, ticket: Ticket) -> bool: logger.info(ticket.to_str()) event_logger.info(ticket.to_str()) return self.redis.save(user_id=user_id, action='ticket', content=ticket.to_dict()) def reset_drafting_ticket(self, user_id: int, username: str) -> Ticket: self.redis.clean(user_id=user_id, action='ticket') self.redis.save(user_id=user_id, action='ticket', content=Ticket(user_id=user_id, username=username).to_dict()) ticket = self.redis.load(user_id=user_id, action='ticket') event_logger.info(ticket) return Ticket(user_id=user_id, username=username).to_obj(ticket) # Query def load_drafting_query(self, user_id: int) -> Query: result = self.redis.load(user_id=user_id, action='query') logger.info(result) event_logger.info(result) return Query(category_id=-1, user_id=user_id).to_obj(result) def save_drafting_query(self, user_id: int, query: Query) -> bool: logger.info(query.to_dict()) event_logger.info(query.to_dict()) return self.redis.save(user_id=user_id, action='query', content=query.to_dict()) def reset_drafting_query(self, user_id: int, username: str, category: int) -> Ticket: self.redis.clean(user_id=user_id, action='query') self.redis.save(user_id=user_id, action='query', content=Query(category_id=category, user_id=user_id, username=username).to_dict()) query = self.redis.load(user_id=user_id, action='query') event_logger.info(query) return Query(category_id=-1, user_id=user_id, username=username).to_obj(query) # Util def tickets_tostr(self, tickets: list, string_template: str) -> str: results = [] for ticket in tickets: logger.info(type(ticket)) logger.info(ticket) logger.info(type(string_template)) tmplate = string_template.format_map(ticket) logger.info(tmplate) results.append(tmplate) logger.info(results) return '\n'.join(results)