|
@@ -6,7 +6,6 @@ from snuba_sdk import And, Column, Condition, Entity, Function, Join, Op, Relati
|
|
|
from sentry.exceptions import (
|
|
|
IncompatibleMetricsQuery,
|
|
|
InvalidQuerySubscription,
|
|
|
- InvalidSearchQuery,
|
|
|
UnsupportedQuerySubscription,
|
|
|
)
|
|
|
from sentry.models.group import GroupStatus
|
|
@@ -21,7 +20,6 @@ from sentry.snuba.entity_subscription import (
|
|
|
MetricsSetsEntitySubscription,
|
|
|
PerformanceMetricsEntitySubscription,
|
|
|
PerformanceTransactionsEntitySubscription,
|
|
|
- SessionsEntitySubscription,
|
|
|
get_entity_key_from_snuba_query,
|
|
|
get_entity_subscription,
|
|
|
get_entity_subscription_from_snuba_query,
|
|
@@ -53,7 +51,7 @@ class EntitySubscriptionTestCase(TestCase):
|
|
|
with pytest.raises(UnsupportedQuerySubscription):
|
|
|
get_entity_subscription(
|
|
|
query_type=SnubaQuery.Type.CRASH_RATE,
|
|
|
- dataset=Dataset.Sessions,
|
|
|
+ dataset=Dataset.Metrics,
|
|
|
aggregate=aggregate,
|
|
|
time_window=3600,
|
|
|
extra_fields={"org_id": self.organization.id},
|
|
@@ -64,7 +62,7 @@ class EntitySubscriptionTestCase(TestCase):
|
|
|
with pytest.raises(InvalidQuerySubscription):
|
|
|
get_entity_subscription(
|
|
|
query_type=SnubaQuery.Type.CRASH_RATE,
|
|
|
- dataset=Dataset.Sessions,
|
|
|
+ dataset=Dataset.Metrics,
|
|
|
aggregate=aggregate,
|
|
|
time_window=3600,
|
|
|
)
|
|
@@ -73,7 +71,7 @@ class EntitySubscriptionTestCase(TestCase):
|
|
|
entities = [
|
|
|
get_entity_subscription(
|
|
|
query_type=SnubaQuery.Type.CRASH_RATE,
|
|
|
- dataset=Dataset.Sessions,
|
|
|
+ dataset=Dataset.Metrics,
|
|
|
aggregate="percentage(sessions_crashed, sessions) AS _crash_rate_alert_aggregate",
|
|
|
time_window=3600,
|
|
|
extra_fields={"org_id": self.organization.id},
|
|
@@ -86,50 +84,9 @@ class EntitySubscriptionTestCase(TestCase):
|
|
|
),
|
|
|
]
|
|
|
for entity in entities:
|
|
|
- with pytest.raises(InvalidSearchQuery, match="Invalid key for this search: timestamp"):
|
|
|
+ with pytest.raises(Exception):
|
|
|
entity.build_query_builder("timestamp:-24h", [self.project.id], None)
|
|
|
|
|
|
- def test_get_entity_subscriptions_for_sessions_dataset(self) -> None:
|
|
|
- aggregate = "percentage(sessions_crashed, sessions) AS _crash_rate_alert_aggregate"
|
|
|
- entity_subscription = get_entity_subscription(
|
|
|
- query_type=SnubaQuery.Type.CRASH_RATE,
|
|
|
- dataset=Dataset.Sessions,
|
|
|
- aggregate=aggregate,
|
|
|
- time_window=3600,
|
|
|
- extra_fields={"org_id": self.organization.id},
|
|
|
- )
|
|
|
- assert isinstance(entity_subscription, SessionsEntitySubscription)
|
|
|
- assert entity_subscription.aggregate == aggregate
|
|
|
- assert entity_subscription.get_entity_extra_params() == {
|
|
|
- "organization": self.organization.id
|
|
|
- }
|
|
|
- assert entity_subscription.dataset == Dataset.Sessions
|
|
|
- snql_query = entity_subscription.build_query_builder(
|
|
|
- "", [self.project.id], None
|
|
|
- ).get_snql_query()
|
|
|
- snql_query.query.select.sort(key=lambda q: q.function)
|
|
|
- assert snql_query.query.select == [
|
|
|
- Function(
|
|
|
- function="identity", parameters=[Column(name="sessions")], alias="_total_count"
|
|
|
- ),
|
|
|
- Function(
|
|
|
- function="if",
|
|
|
- parameters=[
|
|
|
- Function(function="greater", parameters=[Column(name="sessions"), 0]),
|
|
|
- Function(
|
|
|
- function="divide",
|
|
|
- parameters=[Column(name="sessions_crashed"), Column(name="sessions")],
|
|
|
- ),
|
|
|
- None,
|
|
|
- ],
|
|
|
- alias="_crash_rate_alert_aggregate",
|
|
|
- ),
|
|
|
- ]
|
|
|
- assert snql_query.query.where == [
|
|
|
- Condition(Column(name="project_id"), Op.IN, [self.project.id]),
|
|
|
- Condition(Column(name="org_id"), Op.EQ, self.organization.id),
|
|
|
- ]
|
|
|
-
|
|
|
def test_get_entity_subscription_for_metrics_dataset_non_supported_aggregate(self) -> None:
|
|
|
aggregate = "count(sessions)"
|
|
|
with pytest.raises(UnsupportedQuerySubscription):
|
|
@@ -151,67 +108,6 @@ class EntitySubscriptionTestCase(TestCase):
|
|
|
time_window=3600,
|
|
|
)
|
|
|
|
|
|
- # This test has been kept in order to validate whether the old queries through metrics are supported, in the future
|
|
|
- # this should be removed.
|
|
|
- def test_get_entity_subscription_for_metrics_dataset_for_users(self) -> None:
|
|
|
- org_id = self.organization.id
|
|
|
- use_case_id = UseCaseID.SESSIONS
|
|
|
-
|
|
|
- aggregate = "percentage(users_crashed, users) AS _crash_rate_alert_aggregate"
|
|
|
- entity_subscription = get_entity_subscription(
|
|
|
- query_type=SnubaQuery.Type.CRASH_RATE,
|
|
|
- dataset=Dataset.Metrics,
|
|
|
- aggregate=aggregate,
|
|
|
- time_window=3600,
|
|
|
- extra_fields={"org_id": self.organization.id},
|
|
|
- )
|
|
|
- assert isinstance(entity_subscription, MetricsSetsEntitySubscription)
|
|
|
- assert entity_subscription.aggregate == aggregate
|
|
|
- assert entity_subscription.get_entity_extra_params() == {
|
|
|
- "organization": self.organization.id,
|
|
|
- "granularity": 10,
|
|
|
- }
|
|
|
- assert entity_subscription.dataset == Dataset.Metrics
|
|
|
- session_status = resolve_tag_key(use_case_id, org_id, "session.status")
|
|
|
- session_status_crashed = resolve_tag_value(use_case_id, org_id, "crashed")
|
|
|
- snql_query = entity_subscription.build_query_builder(
|
|
|
- "", [self.project.id], None, {"organization_id": self.organization.id}
|
|
|
- ).get_snql_query()
|
|
|
- key = lambda func: func.alias
|
|
|
- assert sorted(snql_query.query.select, key=key) == sorted(
|
|
|
- [
|
|
|
- Function("uniq", parameters=[Column("value")], alias="count"),
|
|
|
- Function(
|
|
|
- "uniqIf",
|
|
|
- parameters=[
|
|
|
- Column(name="value"),
|
|
|
- Function(
|
|
|
- function="equals",
|
|
|
- parameters=[
|
|
|
- Column(session_status),
|
|
|
- session_status_crashed,
|
|
|
- ],
|
|
|
- ),
|
|
|
- ],
|
|
|
- alias="crashed",
|
|
|
- ),
|
|
|
- ],
|
|
|
- key=key,
|
|
|
- )
|
|
|
- assert snql_query.query.where == [
|
|
|
- Condition(Column("project_id"), Op.IN, [self.project.id]),
|
|
|
- Condition(Column("org_id"), Op.EQ, self.organization.id),
|
|
|
- Condition(
|
|
|
- Column("metric_id"),
|
|
|
- Op.EQ,
|
|
|
- resolve(
|
|
|
- UseCaseID.SESSIONS,
|
|
|
- self.organization.id,
|
|
|
- entity_subscription.metric_key.value,
|
|
|
- ),
|
|
|
- ),
|
|
|
- ]
|
|
|
-
|
|
|
def test_get_entity_subscription_for_metrics_dataset_for_users_with_metrics_layer(self) -> None:
|
|
|
with Feature("organizations:use-metrics-layer-in-alerts"):
|
|
|
org_id = self.organization.id
|