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,9 +108,11 @@ 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
108
114
new_status : IncidentStatus
115
+ subscription : QuerySubscription | None
109
116
metric_value : float | None
110
117
111
118
@classmethod
@@ -128,6 +135,10 @@ 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 | None :
140
+ return occurrence .evidence_data .get ("subscription_id" )
141
+
131
142
@classmethod
132
143
def _get_metric_value (cls , occurrence : IssueOccurrence ) -> float :
133
144
if (metric_value := occurrence .evidence_data .get ("metric_value" )) is None :
@@ -144,8 +155,11 @@ def from_group_event(cls, group_event: GroupEvent) -> MetricIssueContext:
144
155
# TODO(iamrajjoshi): Replace with something once we know how we want to build the link
145
156
# If we store open periods in the database, we can use the id from that
146
157
# Otherwise, we can use the issue id
158
+ id = group .id ,
159
+ # TODO(iamrajjoshi): This should probably be the id of the latest open period
147
160
open_period_identifier = group .id ,
148
161
snuba_query = cls ._get_snuba_query (occurrence ),
162
+ subscription = cls ._get_subscription (occurrence ),
149
163
new_status = cls ._get_new_status (group , occurrence ),
150
164
metric_value = cls ._get_metric_value (occurrence ),
151
165
)
@@ -158,8 +172,10 @@ def from_legacy_models(
158
172
metric_value : float | None = None ,
159
173
) -> MetricIssueContext :
160
174
return cls (
175
+ id = incident .id ,
161
176
open_period_identifier = incident .identifier ,
162
177
snuba_query = incident .alert_rule .snuba_query ,
178
+ subscription = incident .subscription ,
163
179
new_status = new_status ,
164
180
metric_value = metric_value ,
165
181
)
0 commit comments