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 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
Exemplo n.º 3
0
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)]
Exemplo n.º 4
0
 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)
Exemplo n.º 5
0
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)