Exemplo n.º 1
0
    async def get_messages(
            self,
            session_id,
            start=float('-inf'),
            end=float('inf'),
            limit=None,
    ):
        if limit is None:
            limit = -1
        if limit > 0:
            limit = limit + 1
        message_ids = await self.redis.zrevrangebyscore(self.make_redis_key(
            session_id, 'messages_by_time'),
                                                        min=start,
                                                        max=end,
                                                        offset=0,
                                                        count=limit)
        if len(message_ids) == 0:
            return []
        message_ids = list(reversed(message_ids))

        key = self.make_redis_key(session_id, 'messages')
        msgs = await self.redis.hmget(key, *message_ids)
        msgs = [FixMessage.from_raw(m) for m in msgs]
        return msgs
Exemplo n.º 2
0
    async def get_msgs(
        self,
        min: float = float("-inf"),
        max: float = float("inf"),
        limit: float = float("inf"),
        index: "str" = "by_time",
        sort: "str" = "ascending",
    ) -> t.AsyncIterator[FixMessage]:

        limit = -1 if limit == float("inf") else limit

        if sort == "descending":
            min, max = max, min

        id_key = self._make_key(f"messages_{index}")
        msg_key = self._make_key("messages")

        raw_msgs = await self.redis.eval(
            _get_msgs,
            keys=[id_key, msg_key],
            args=[sort, _str_bound(min),
                  _str_bound(max),
                  str(limit)],
        )

        for raw_msg in raw_msgs:
            msg = FixMessage.from_raw(raw_msg)
            if msg is None:
                raise TypeError(
                    f"Unable to parse fix message in store. "
                    f"The message may be corrupted. Raw msg: {raw_msg}")
            yield msg
Exemplo n.º 3
0
def parse_msg(raw_msg: bytes) -> FixMessage:
    msg = FixMessage.from_raw(raw_msg)
    if msg is None:
        raise RuntimeError(
            "Not able to parse fix msg from string")
    if msg.seq_num is None:
        raise RuntimeError(
            "Msg is missing seq num. Msg may be corrupted")
    return msg
Exemplo n.º 4
0
 async def get_received(self,
                        session_id,
                        min=float('-inf'),
                        max=float('inf'),
                        limit=None):
     msgs = self._get_messages(session_id)
     rv = []
     for time, msg in msgs.values():
         msg = FixMessage.from_raw(msg)
         if msg.get(49) != session_id.target:
             continue
         if not min <= msg.seq_num <= max:
             continue
         rv.append(msg)
     if limit is not None:
         rv = rv[limit * -1:]
     return rv
Exemplo n.º 5
0
 async def get_received(self,
                        session_id,
                        min=float('-inf'),
                        max=float('inf'),
                        limit=None):
     if limit is None:
         limit = -1
     if limit > 0:
         limit = limit + 1
     message_ids = await self.redis.zrevrangebyscore(self.make_redis_key(
         session_id, 'messages_received'),
                                                     min=min,
                                                     max=max,
                                                     offset=0,
                                                     count=limit)
     if len(message_ids) == 0:
         return []
     message_ids = list(reversed(message_ids))
     key = self.make_redis_key(session_id, 'messages')
     msgs = await self.redis.hmget(key, *message_ids)
     return [FixMessage.from_raw(m) for m in msgs]