Beispiel #1
0
def test_basic(
    builder: Callable[[EntityKey, Optional[int]], SubscriptionData],
    organization: Optional[int],
    entity_key: EntityKey,
) -> None:
    codec = SubscriptionDataCodec(entity_key)
    data = builder(entity_key, organization)
    assert codec.decode(codec.encode(data)) == data
Beispiel #2
0
def test_encode_snql() -> None:
    codec = SubscriptionDataCodec()
    subscription = build_snql_subscription_data()

    payload = codec.encode(subscription)
    data = json.loads(payload.decode("utf-8"))
    assert data["project_id"] == subscription.project_id
    assert data["time_window"] == int(subscription.time_window.total_seconds())
    assert data["resolution"] == int(subscription.resolution.total_seconds())
    assert data["query"] == subscription.query
Beispiel #3
0
def test_encode() -> None:
    codec = SubscriptionDataCodec()
    subscription = build_legacy_subscription_data()

    payload = codec.encode(subscription)
    data = json.loads(payload.decode("utf-8"))
    assert data["project_id"] == subscription.project_id
    assert data["conditions"] == subscription.conditions
    assert data["aggregations"] == subscription.aggregations
    assert data["time_window"] == int(subscription.time_window.total_seconds())
    assert data["resolution"] == int(subscription.resolution.total_seconds())
Beispiel #4
0
def test_encode_snql(
    builder: Callable[[EntityKey, Optional[int]], SubscriptionData],
    organization: Optional[int],
    entity_key: EntityKey,
) -> None:
    codec = SubscriptionDataCodec(entity_key)
    subscription = builder(entity_key, organization)

    payload = codec.encode(subscription)
    data = json.loads(payload.decode("utf-8"))
    assert data["project_id"] == subscription.project_id
    assert data["time_window"] == subscription.time_window_sec
    assert data["resolution"] == subscription.resolution_sec
    assert data["query"] == subscription.query
    assert_entity_subscription_on_subscription_class(organization,
                                                     subscription, entity_key)
Beispiel #5
0
class RedisSubscriptionDataStore(SubscriptionDataStore):
    """
    A Redis backed store for subscription data. Stores subscriptions using
    `SubscriptionDataCodec`. Each instance of the store operates on a
    partition of data, defined by the `key` constructor param.
    """

    KEY_TEMPLATE = "subscriptions:{}:{}"

    def __init__(
        self, client: RedisClientType, entity: EntityKey, partition_id: PartitionId
    ):
        self.client = client
        self.codec = SubscriptionDataCodec(entity)
        self.__key = f"subscriptions:{entity.value}:{partition_id}"

    def create(self, key: UUID, data: SubscriptionData) -> None:
        """
        Stores subscription data in Redis. Will overwrite any existing
        subscriptions with the same id.
        """
        self.client.hset(self.__key, key.hex.encode("utf-8"), self.codec.encode(data))

    def delete(self, key: UUID) -> None:
        """
        Removes a subscription from the Redis store.
        """
        self.client.hdel(self.__key, key.hex.encode("utf-8"))

    def all(self) -> Iterable[Tuple[UUID, SubscriptionData]]:
        """
        Fetches all subscriptions from the store.
        :return: An iterable of `Subscriptions`.
        """
        return [
            (UUID(key.decode("utf-8")), self.codec.decode(val))
            for key, val in self.client.hgetall(self.__key).items()
        ]
Beispiel #6
0
def test_basic(builder: Callable[[], SubscriptionData]) -> None:
    codec = SubscriptionDataCodec()
    data = builder()
    assert codec.decode(codec.encode(data)) == data
Beispiel #7
0
 def test_basic(self):
     data = self.build_subscription_data()
     codec = SubscriptionDataCodec()
     assert codec.decode(codec.encode(data)) == data