11
11
12
12
from apps .alerts import tasks
13
13
from apps .alerts .constants import ActionSource
14
- from apps .alerts .incident_appearance .renderers .constants import DEFAULT_BACKUP_TITLE
15
14
from apps .alerts .utils import render_relative_timeline
16
15
from apps .slack .slack_formatter import SlackFormatter
17
16
from common .utils import clean_markup
18
17
19
18
if typing .TYPE_CHECKING :
20
19
from apps .alerts .models import AlertGroup , CustomButton , EscalationPolicy , Invitation
21
- from apps .user_management .models import Organization , User
20
+ from apps .user_management .models import User
22
21
23
22
logger = logging .getLogger (__name__ )
24
23
logger .setLevel (logging .DEBUG )
25
24
26
25
27
- class RelatedIncidentData (typing .TypedDict ):
28
- incident_link : typing .Optional [str ]
29
- incident_title : str
30
-
31
-
32
26
class AlertGroupLogRecord (models .Model ):
33
27
alert_group : "AlertGroup"
34
28
author : typing .Optional ["User" ]
@@ -167,9 +161,7 @@ class AlertGroupLogRecord(models.Model):
167
161
ERROR_ESCALATION_TRIGGER_CUSTOM_WEBHOOK_ERROR ,
168
162
ERROR_ESCALATION_NOTIFY_TEAM_MEMBERS_STEP_IS_NOT_CONFIGURED ,
169
163
ERROR_ESCALATION_TRIGGER_WEBHOOK_IS_DISABLED ,
170
- ERROR_ESCALATION_DECLARE_INCIDENT_STEP_IS_NOT_ENABLED ,
171
- ERROR_ESCALATION_INCIDENT_COULD_NOT_BE_DECLARED ,
172
- ) = range (22 )
164
+ ) = range (20 )
173
165
174
166
type = models .IntegerField (choices = TYPE_CHOICES )
175
167
@@ -233,60 +225,16 @@ class AlertGroupLogRecord(models.Model):
233
225
escalation_policy_step = models .IntegerField (null = True , default = None )
234
226
step_specific_info = JSONField (null = True , default = None )
235
227
236
- STEP_SPECIFIC_INFO_KEYS = [
237
- "schedule_name" ,
238
- "custom_button_name" ,
239
- "usergroup_handle" ,
240
- "source_integration_name" ,
241
- "incident_link" ,
242
- "incident_title" ,
243
- ]
244
-
245
- def _make_log_line_link (self , url , title , html = False , for_slack = False , substitute_with_tag = False ):
246
- if html and url :
247
- return f"<a href='{ url } '>{ title } </a>"
248
- elif for_slack and url :
249
- return f"<{ url } |{ title } >"
250
- elif substitute_with_tag :
251
- return f"{{{{{ substitute_with_tag } }}}}"
252
- else :
253
- return title
228
+ STEP_SPECIFIC_INFO_KEYS = ["schedule_name" , "custom_button_name" , "usergroup_handle" , "source_integration_name" ]
254
229
255
230
def render_log_line_json (self ):
256
231
time = humanize .naturaldelta (self .alert_group .started_at - self .created_at )
257
232
created_at = DateTimeField ().to_representation (self .created_at )
258
233
organization = self .alert_group .channel .organization
259
234
author = self .author .short (organization ) if self .author is not None else None
260
- escalation_chain = self .alert_group .channel_filter .escalation_chain if self .alert_group .channel_filter else None
261
- step_info = self .get_step_specific_info ()
262
- related_incident = self .render_incident_data_from_step_info (organization , step_info )
263
- escalation_chain_data = (
264
- {
265
- "pk" : escalation_chain .public_primary_key ,
266
- "title" : escalation_chain .name ,
267
- }
268
- if escalation_chain
269
- else None
270
- )
271
- schedule = (
272
- {
273
- "pk" : self .escalation_policy .notify_schedule .public_primary_key ,
274
- "title" : self .escalation_policy .notify_schedule .name ,
275
- }
276
- if self .escalation_policy and self .escalation_policy .notify_schedule
277
- else None
278
- )
279
- webhook = (
280
- {
281
- "pk" : step_info ["webhook_id" ],
282
- "title" : step_info .get ("webhook_name" , "webhook" ),
283
- }
284
- if step_info and "webhook_id" in step_info
285
- else None
286
- )
287
235
288
236
sf = SlackFormatter (organization )
289
- action = sf .format (self .rendered_log_line_action (substitute_with_tag = True ))
237
+ action = sf .format (self .rendered_log_line_action (substitute_author_with_tag = True ))
290
238
action = clean_markup (action )
291
239
292
240
result = {
@@ -296,10 +244,6 @@ def render_log_line_json(self):
296
244
"type" : self .type ,
297
245
"created_at" : created_at ,
298
246
"author" : author ,
299
- "incident" : related_incident ,
300
- "escalation_chain" : escalation_chain_data ,
301
- "schedule" : schedule ,
302
- "webhook" : webhook ,
303
247
}
304
248
return result
305
249
@@ -314,7 +258,7 @@ def rendered_incident_log_line(self, for_slack=False, html=False):
314
258
result += self .rendered_log_line_action (for_slack = for_slack , html = html )
315
259
return result
316
260
317
- def rendered_log_line_action (self , for_slack = False , html = False , substitute_with_tag = False ):
261
+ def rendered_log_line_action (self , for_slack = False , html = False , substitute_author_with_tag = False ):
318
262
from apps .alerts .models import EscalationPolicy
319
263
320
264
result = ""
@@ -332,7 +276,7 @@ def rendered_log_line_action(self, for_slack=False, html=False, substitute_with_
332
276
elif self .action_source == ActionSource .BACKSYNC :
333
277
author_name = "source integration " + step_specific_info .get ("source_integration_name" , "" )
334
278
elif self .author :
335
- if substitute_with_tag :
279
+ if substitute_author_with_tag :
336
280
author_name = "{{author}}"
337
281
elif for_slack :
338
282
author_name = self .author .get_username_with_slack_verbal ()
@@ -359,9 +303,7 @@ def rendered_log_line_action(self, for_slack=False, html=False, substitute_with_
359
303
result += f'alert group assigned to route "{ channel_filter .str_for_clients } "'
360
304
361
305
if escalation_chain is not None :
362
- tag = "escalation_chain" if substitute_with_tag else False
363
- escalation_chain_text = self ._make_log_line_link (None , escalation_chain .name , html , for_slack , tag )
364
- result += f' with escalation chain "{ escalation_chain_text } "'
306
+ result += f' with escalation chain "{ escalation_chain .name } "'
365
307
else :
366
308
result += " with no escalation chain, skipping escalation"
367
309
else :
@@ -437,19 +379,9 @@ def rendered_log_line_action(self, for_slack=False, html=False, substitute_with_
437
379
important_text = ""
438
380
if escalation_policy_step == EscalationPolicy .STEP_NOTIFY_SCHEDULE_IMPORTANT :
439
381
important_text = " (Important)"
440
- tag = "schedule" if substitute_with_tag else False
441
- schedule_text = self ._make_log_line_link (None , schedule_name , html , for_slack , tag )
442
- result += f'triggered step "Notify on-call from Schedule { schedule_text } { important_text } "'
382
+ result += f'triggered step "Notify on-call from Schedule { schedule_name } { important_text } "'
443
383
elif escalation_policy_step == EscalationPolicy .STEP_REPEAT_ESCALATION_N_TIMES :
444
384
result += "escalation started from the beginning"
445
- elif escalation_policy_step == EscalationPolicy .STEP_DECLARE_INCIDENT :
446
- organization = self .alert_group .channel .organization
447
- incident_data = self .render_incident_data_from_step_info (organization , step_specific_info )
448
- incident_link = incident_data ["incident_link" ]
449
- incident_title = incident_data ["incident_title" ]
450
- tag = "related_incident" if substitute_with_tag else False
451
- incident_text = self ._make_log_line_link (incident_link , incident_title , html , for_slack , tag )
452
- result += self .reason + f": { incident_text } "
453
385
else :
454
386
result += f'triggered step "{ EscalationPolicy .get_step_display_name (escalation_policy_step )} "'
455
387
elif self .type == AlertGroupLogRecord .TYPE_SILENCE :
@@ -553,10 +485,7 @@ def rendered_log_line_action(self, for_slack=False, html=False, substitute_with_
553
485
trigger = f"{ author_name } "
554
486
else :
555
487
trigger = trigger or "escalation chain"
556
- tag = "webhook" if substitute_with_tag else False
557
- webhook_text = self ._make_log_line_link (None , webhook_name , html , for_slack , tag )
558
- result += f"outgoing webhook `{ webhook_text } ` triggered by { trigger } "
559
-
488
+ result += f"outgoing webhook `{ webhook_name } ` triggered by { trigger } "
560
489
elif self .type == AlertGroupLogRecord .TYPE_FAILED_ATTACHMENT :
561
490
if self .alert_group .slack_message is not None :
562
491
result += (
@@ -665,32 +594,8 @@ def rendered_log_line_action(self, for_slack=False, html=False, substitute_with_
665
594
result += f"failed to notify User Group{ usergroup_handle_text } in Slack"
666
595
elif self .escalation_error_code == AlertGroupLogRecord .ERROR_ESCALATION_TRIGGER_WEBHOOK_IS_DISABLED :
667
596
result += 'skipped escalation step "Trigger Outgoing Webhook" because it is disabled'
668
- elif (
669
- self .escalation_error_code == AlertGroupLogRecord .ERROR_ESCALATION_DECLARE_INCIDENT_STEP_IS_NOT_ENABLED
670
- ):
671
- result += 'skipped escalation step "Declare Incident": step is not enabled'
672
- elif self .escalation_error_code == AlertGroupLogRecord .ERROR_ESCALATION_INCIDENT_COULD_NOT_BE_DECLARED :
673
- result += "failed to declare an Incident"
674
- if self .reason :
675
- result += f": { self .reason } "
676
597
return result
677
598
678
- def render_incident_data_from_step_info (
679
- self , organization : "Organization" , step_specific_info : dict
680
- ) -> RelatedIncidentData | None :
681
- from apps .alerts .models .declared_incident import get_incident_url
682
-
683
- if not step_specific_info or not all (key in step_specific_info for key in ["incident_title" , "incident_id" ]):
684
- return None
685
-
686
- incident_link = (
687
- get_incident_url (organization , step_specific_info ["incident_id" ])
688
- if step_specific_info ["incident_id" ]
689
- else None
690
- )
691
- incident_title = step_specific_info ["incident_title" ] or DEFAULT_BACKUP_TITLE
692
- return {"incident_link" : incident_link , "incident_title" : incident_title }
693
-
694
599
def get_step_specific_info (self ):
695
600
step_specific_info = None
696
601
# in some cases step_specific_info was saved with using json.dumps
0 commit comments