def test(self) -> None: creator = SubscriptionCreator(self.dataset, EntityKey.EVENTS) subscription = SubscriptionData( project_id=1, query="MATCH (events) SELECT count() AS count", time_window_sec=10 * 60, resolution_sec=60, entity_subscription=create_entity_subscription(), ) identifier = creator.create(subscription, Timer("test")) assert (cast( List[Tuple[UUID, SubscriptionData]], RedisSubscriptionDataStore( redis_client, self.entity_key, identifier.partition, ).all(), )[0][1] == subscription) SubscriptionDeleter(self.entity_key, identifier.partition).delete(identifier.uuid) assert (RedisSubscriptionDataStore( redis_client, self.entity_key, identifier.partition, ).all() == [])
def test(self) -> None: creator = SubscriptionCreator(self.dataset) subscription = LegacySubscriptionData( project_id=1, conditions=[], aggregations=[["count()", "", "count"]], time_window=timedelta(minutes=10), resolution=timedelta(minutes=1), ) identifier = creator.create(subscription, Timer("test")) assert ( cast( List[Tuple[UUID, SubscriptionData]], RedisSubscriptionDataStore( redis_client, self.dataset, identifier.partition, ).all(), )[0][1] == subscription ) SubscriptionDeleter(self.dataset, identifier.partition).delete(identifier.uuid) assert ( RedisSubscriptionDataStore( redis_client, self.dataset, identifier.partition, ).all() == [] )
def test_invalid_time_window(self): creator = SubscriptionCreator(self.dataset) with raises(InvalidSubscriptionError): creator.create( SubscriptionData( 123, [["platfo", "IN", ["a"]]], [["count()", "", "count"]], timedelta(), timedelta(minutes=1), ), self.timer, ) with raises(InvalidSubscriptionError): creator.create( SubscriptionData( 123, [["platfo", "IN", ["a"]]], [["count()", "", "count"]], timedelta(hours=48), timedelta(minutes=1), ), self.timer, )
def create_subscription(*, dataset: Dataset, timer: Timer) -> RespTuple: subscription = SubscriptionDataCodec().decode(http_request.data) identifier = SubscriptionCreator(dataset).create(subscription, timer) return ( json.dumps({"subscription_id": str(identifier)}), 202, {"Content-Type": "application/json"}, )
def test_invalid_condition_column(self, subscription: SubscriptionData) -> None: creator = SubscriptionCreator(self.dataset, EntityKey.EVENTS) with raises(QueryException): creator.create( subscription, self.timer, )
def test_missing_conditions_for_groupby_clause( self, subscription: SubscriptionData) -> None: creator = SubscriptionCreator(self.dataset, EntityKey.METRICS_COUNTERS) with raises(InvalidQueryException): creator.create( subscription, self.timer, )
def create_subscription(*, dataset: Dataset, timer: Timer): subscription = SubscriptionDataCodec().decode(http_request.data) # TODO: Check for valid queries with fields that are invalid for subscriptions. For # example date fields and aggregates. identifier = SubscriptionCreator(dataset).create(subscription, timer) return ( json.dumps({"subscription_id": str(identifier)}), 202, {"Content-Type": "application/json"}, )
def test(self, subscription: SubscriptionData) -> None: creator = SubscriptionCreator(self.dataset, EntityKey.EVENTS) identifier = creator.create(subscription, self.timer) assert (cast( List[Tuple[UUID, SubscriptionData]], RedisSubscriptionDataStore( redis_client, self.entity_key, identifier.partition, ).all(), )[0][1] == subscription)
def test_invalid_resolution(self) -> None: creator = SubscriptionCreator(self.dataset) with raises(InvalidSubscriptionError): creator.create( LegacySubscriptionData( 123, timedelta(), timedelta(minutes=1), [["platfo", "IN", ["a"]]], [["count()", "", "count"]], ), self.timer, )
def test_invalid_aggregation(self): creator = SubscriptionCreator(self.dataset) with raises(QueryException): creator.create( SubscriptionData( 123, [["platform", "IN", ["a"]]], [["cout()", "", "count"]], timedelta(minutes=10), timedelta(minutes=1), ), self.timer, )
def test_invalid_condition_column(self, subscription: SubscriptionData) -> None: creator = SubscriptionCreator(self.dataset) with raises(QueryException): creator.create( LegacySubscriptionData( 123, timedelta(minutes=1), timedelta(minutes=10), [["platfo", "IN", ["a"]]], [["count()", "", "count"]], ), self.timer, )
def test(self): creator = SubscriptionCreator(self.dataset) subscription = SubscriptionData( project_id=123, conditions=[["platform", "IN", ["a"]]], aggregations=[["count()", "", "count"]], time_window=timedelta(minutes=10), resolution=timedelta(minutes=1), ) identifier = creator.create(subscription, self.timer) RedisSubscriptionDataStore( redis_client, self.dataset, identifier.partition, ).all()[0][1] == subscription
def test_invalid_condition_column(self): creator = SubscriptionCreator(self.dataset) with raises(RawQueryException): creator.create( SubscriptionData( 123, [["platfo", "IN", ["a"]]], [["count()", "", "count"]], timedelta(minutes=10), timedelta(minutes=1), ), Mock(), )
def test_invalid_aggregation(self) -> None: creator = SubscriptionCreator(self.dataset, EntityKey.EVENTS) with raises(QueryException): creator.create( SubscriptionData( project_id=123, time_window_sec=10 * 60, resolution_sec=60, query= "MATCH (events) SELECT cout() AS count WHERE platform IN tuple('a')", entity_subscription=create_entity_subscription(), ), self.timer, )
def create_subscription(*, dataset: Dataset, timer: Timer, entity: Entity) -> RespTuple: if entity not in dataset.get_all_entities(): raise InvalidSubscriptionError( "Invalid subscription dataset and entity combination" ) entity_key = ENTITY_NAME_LOOKUP[entity] subscription = SubscriptionDataCodec(entity_key).decode(http_request.data) identifier = SubscriptionCreator(dataset, entity_key).create(subscription, timer) metrics.increment("subscription_created", tags={"entity": entity_key.value}) return ( json.dumps({"subscription_id": str(identifier)}), 202, {"Content-Type": "application/json"}, )
def test(self): creator = SubscriptionCreator(self.dataset) subscription = SubscriptionData( project_id=1, conditions=[], aggregations=[["count()", "", "count"]], time_window=timedelta(minutes=10), resolution=timedelta(minutes=1), ) identifier = creator.create(subscription, Timer("test")) RedisSubscriptionDataStore( redis_client, self.dataset, identifier.partition, ).all()[0][1] == subscription SubscriptionDeleter(self.dataset, identifier.partition).delete(identifier.uuid) RedisSubscriptionDataStore( redis_client, self.dataset, identifier.partition, ).all() == []
def test_invalid_time_window(self) -> None: creator = SubscriptionCreator(self.dataset, EntityKey.EVENTS) with raises(InvalidSubscriptionError): creator.create( SubscriptionData( project_id=123, time_window_sec=0, resolution_sec=60, query= "MATCH (events) SELECT count() AS count WHERE platfo IN tuple('a')", entity_subscription=create_entity_subscription(), ), self.timer, ) with raises(InvalidSubscriptionError): creator.create( SubscriptionData( project_id=123, query=("MATCH (events) " "SELECT count() AS count BY time " "WHERE " "platform IN tuple('a') "), time_window_sec=0, resolution_sec=60, entity_subscription=create_entity_subscription(), ), self.timer, ) with raises(InvalidSubscriptionError): creator.create( SubscriptionData( project_id=123, time_window_sec=48 * 60 * 60, resolution_sec=60, query= "MATCH (events) SELECT count() AS count WHERE platfo IN tuple('a')", entity_subscription=create_entity_subscription(), ), self.timer, )
def test_invalid_time_window(self) -> None: creator = SubscriptionCreator(self.dataset) with raises(InvalidSubscriptionError): creator.create( LegacySubscriptionData( 123, timedelta(minutes=1), timedelta(), [["platfo", "IN", ["a"]]], [["count()", "", "count"]], ), self.timer, ) with raises(InvalidSubscriptionError): creator.create( SnQLSubscriptionData( project_id=123, query=( "MATCH (events) " "SELECT count() AS count BY time " "WHERE " "platform IN tuple('a') " ), time_window=timedelta(minutes=1), resolution=timedelta(), ), self.timer, ) with raises(InvalidSubscriptionError): creator.create( LegacySubscriptionData( 123, timedelta(minutes=1), timedelta(hours=48), [["platfo", "IN", ["a"]]], [["count()", "", "count"]], ), self.timer, )