14
14
from sentry .incidents .models .incident import Incident , IncidentStatus
15
15
from sentry .issues .issue_occurrence import IssueOccurrence
16
16
from sentry .models .group import Group , GroupStatus
17
- from sentry .snuba .models import SnubaQuery
17
+ from sentry .snuba .models import QuerySubscription , SnubaQuery
18
18
from sentry .types .group import PriorityLevel
19
19
from sentry .workflow_engine .models import Action , Detector
20
20
@@ -60,6 +60,7 @@ class NotificationContext:
60
60
NotificationContext is a dataclass that represents the context required send a notification.
61
61
"""
62
62
63
+ id : int
63
64
integration_id : int | None = None
64
65
target_identifier : str | None = None
65
66
target_display : str | None = None
@@ -69,6 +70,7 @@ class NotificationContext:
69
70
@classmethod
70
71
def from_alert_rule_trigger_action (cls , action : AlertRuleTriggerAction ) -> NotificationContext :
71
72
return cls (
73
+ id = action .id ,
72
74
integration_id = action .integration_id ,
73
75
target_identifier = action .target_identifier ,
74
76
target_display = action .target_display ,
@@ -79,6 +81,7 @@ def from_alert_rule_trigger_action(cls, action: AlertRuleTriggerAction) -> Notif
79
81
def from_action_model (cls , action : Action ) -> NotificationContext :
80
82
if action .type == Action .Type .SENTRY_APP :
81
83
return cls (
84
+ id = action .id ,
82
85
integration_id = action .integration_id ,
83
86
target_display = action .config .get ("target_display" ),
84
87
sentry_app_config = action .data .get ("settings" ),
@@ -87,6 +90,7 @@ def from_action_model(cls, action: Action) -> NotificationContext:
87
90
)
88
91
elif action .type == Action .Type .OPSGENIE or action .type == Action .Type .PAGERDUTY :
89
92
return cls (
93
+ id = action .id ,
90
94
integration_id = action .integration_id ,
91
95
target_identifier = action .config .get ("target_identifier" ),
92
96
target_display = action .config .get ("target_display" ),
@@ -95,6 +99,7 @@ def from_action_model(cls, action: Action) -> NotificationContext:
95
99
# TODO(iamrajjoshi): Add support for email here
96
100
97
101
return cls (
102
+ id = action .id ,
98
103
integration_id = action .integration_id ,
99
104
target_identifier = action .config .get ("target_identifier" ),
100
105
target_display = action .config .get ("target_display" ),
@@ -103,8 +108,10 @@ def from_action_model(cls, action: Action) -> NotificationContext:
103
108
104
109
@dataclass
105
110
class MetricIssueContext :
106
- open_period_identifier : int
111
+ id : int
112
+ open_period_identifier : int # Used for link building
107
113
snuba_query : SnubaQuery
114
+ subscription : QuerySubscription
108
115
new_status : IncidentStatus
109
116
metric_value : float | None
110
117
@@ -128,6 +135,17 @@ def _get_snuba_query(cls, occurrence: IssueOccurrence) -> SnubaQuery:
128
135
raise ValueError ("Snuba query does not exist" ) from e
129
136
return query
130
137
138
+ @classmethod
139
+ def _get_subscription (cls , occurrence : IssueOccurrence ) -> QuerySubscription :
140
+ subscription_id = occurrence .evidence_data .get ("subscription_id" )
141
+ if not subscription_id :
142
+ raise ValueError ("Subscription ID is required for alert context" )
143
+ try :
144
+ subscription = QuerySubscription .objects .get (id = subscription_id )
145
+ except QuerySubscription .DoesNotExist as e :
146
+ raise ValueError ("Subscription does not exist" ) from e
147
+ return subscription
148
+
131
149
@classmethod
132
150
def _get_metric_value (cls , occurrence : IssueOccurrence ) -> float :
133
151
if (metric_value := occurrence .evidence_data .get ("metric_value" )) is None :
@@ -144,8 +162,11 @@ def from_group_event(cls, group_event: GroupEvent) -> MetricIssueContext:
144
162
# TODO(iamrajjoshi): Replace with something once we know how we want to build the link
145
163
# If we store open periods in the database, we can use the id from that
146
164
# Otherwise, we can use the issue id
165
+ id = group .id ,
166
+ # TODO(iamrajjoshi): This should probably be the id of the latest open period
147
167
open_period_identifier = group .id ,
148
168
snuba_query = cls ._get_snuba_query (occurrence ),
169
+ subscription = cls ._get_subscription (occurrence ),
149
170
new_status = cls ._get_new_status (group , occurrence ),
150
171
metric_value = cls ._get_metric_value (occurrence ),
151
172
)
@@ -158,8 +179,10 @@ def from_legacy_models(
158
179
metric_value : float | None = None ,
159
180
) -> MetricIssueContext :
160
181
return cls (
182
+ id = incident .id ,
161
183
open_period_identifier = incident .identifier ,
162
184
snuba_query = incident .alert_rule .snuba_query ,
185
+ subscription = incident .subscription ,
163
186
new_status = new_status ,
164
187
metric_value = metric_value ,
165
188
)
0 commit comments