8
8
from celery import uuid as celery_uuid
9
9
from django .apps import apps
10
10
from django .conf import settings
11
- from django .core .cache import cache
12
11
from django .core .validators import MinLengthValidator
13
- from django .db import IntegrityError , models , transaction
12
+ from django .db import IntegrityError , models
14
13
from django .db .models import JSONField , Q , QuerySet
15
- from django .db .models .signals import post_save
16
- from django .dispatch import receiver
17
14
from django .utils import timezone
18
15
from django .utils .functional import cached_property
19
16
22
19
from apps .alerts .incident_appearance .renderers .slack_renderer import AlertGroupSlackRenderer
23
20
from apps .alerts .incident_log_builder import IncidentLogBuilder
24
21
from apps .alerts .signals import alert_group_action_triggered_signal
25
- from apps .alerts .tasks import (
26
- acknowledge_reminder_task ,
27
- call_ack_url ,
28
- schedule_cache_for_alert_group ,
29
- send_alert_group_signal ,
30
- unsilence_task ,
31
- )
22
+ from apps .alerts .tasks import acknowledge_reminder_task , call_ack_url , send_alert_group_signal , unsilence_task
32
23
from apps .slack .slack_formatter import SlackFormatter
33
24
from apps .user_management .models import User
34
- from common .mixins .use_random_readonly_db_manager_mixin import UseRandomReadonlyDbManagerMixin
35
25
from common .public_primary_keys import generate_public_primary_key , increase_public_primary_key_length
36
26
from common .utils import clean_markup , str_or_backup
37
27
@@ -108,10 +98,6 @@ def filter(self, *args, **kwargs):
108
98
return super ().filter (* args , ** kwargs , is_archived = False )
109
99
110
100
111
- class AlertGroupManager (UseRandomReadonlyDbManagerMixin , models .Manager ):
112
- pass
113
-
114
-
115
101
class AlertGroupSlackRenderingMixin :
116
102
"""
117
103
Ideally this mixin should not exist. Instead of this instance of AlertGroupSlackRenderer should be created and used
@@ -134,8 +120,8 @@ def slack_templated_first_alert(self):
134
120
135
121
136
122
class AlertGroup (AlertGroupSlackRenderingMixin , EscalationSnapshotMixin , models .Model ):
137
- all_objects = AlertGroupManager . from_queryset ( AlertGroupQuerySet ) ()
138
- unarchived_objects = AlertGroupManager . from_queryset ( UnarchivedAlertGroupQuerySet ) ()
123
+ all_objects = AlertGroupQuerySet . as_manager ()
124
+ unarchived_objects = UnarchivedAlertGroupQuerySet . as_manager ()
139
125
140
126
(
141
127
NEW ,
@@ -242,8 +228,6 @@ class AlertGroup(AlertGroupSlackRenderingMixin, EscalationSnapshotMixin, models.
242
228
243
229
active_escalation_id = models .CharField (max_length = 100 , null = True , default = None ) # ID generated by celery
244
230
active_resolve_calculation_id = models .CharField (max_length = 100 , null = True , default = None ) # ID generated by celery
245
- # ID generated by celery
246
- active_cache_for_web_calculation_id = models .CharField (max_length = 100 , null = True , default = None )
247
231
248
232
SILENCE_DELAY_OPTIONS = (
249
233
(1800 , "30 minutes" ),
@@ -315,7 +299,9 @@ def status(self):
315
299
related_name = "dependent_alert_groups" ,
316
300
)
317
301
318
- cached_render_for_web = JSONField (default = dict )
302
+ # cached_render_for_web and active_cache_for_web_calculation_id are deprecated
303
+ cached_render_for_web = models .JSONField (default = dict )
304
+ active_cache_for_web_calculation_id = models .CharField (max_length = 100 , null = True , default = None )
319
305
320
306
last_unique_unacknowledge_process_id = models .CharField (max_length = 100 , null = True , default = None )
321
307
is_archived = models .BooleanField (default = False )
@@ -364,6 +350,11 @@ class Meta:
364
350
"distinction" ,
365
351
"is_open_for_grouping" ,
366
352
]
353
+ indexes = [
354
+ models .Index (
355
+ fields = ["channel_id" , "resolved" , "acknowledged" , "silenced" , "root_alert_group_id" , "is_archived" ]
356
+ ),
357
+ ]
367
358
368
359
def __str__ (self ):
369
360
return f"{ self .pk } : { self .verbose_name } "
@@ -404,18 +395,6 @@ def skip_escalation_in_slack(self):
404
395
def is_alert_a_resolve_signal (self , alert ):
405
396
raise NotImplementedError
406
397
407
- def cache_for_web (self , organization ):
408
- from apps .api .serializers .alert_group import AlertGroupSerializer
409
-
410
- # Re-take object to switch connection from readonly db to master.
411
- _self = AlertGroup .all_objects .get (pk = self .pk )
412
- _self .cached_render_for_web = AlertGroupSerializer (self , context = {"organization" : organization }).data
413
- self .cached_render_for_web = _self .cached_render_for_web
414
- _self .save (update_fields = ["cached_render_for_web" ])
415
-
416
- def schedule_cache_for_web (self ):
417
- schedule_cache_for_alert_group .apply_async ((self .pk ,))
418
-
419
398
@property
420
399
def permalink (self ):
421
400
if self .slack_message is not None :
@@ -425,10 +404,6 @@ def permalink(self):
425
404
def web_link (self ):
426
405
return urljoin (self .channel .organization .web_link , f"?page=incident&id={ self .public_primary_key } " )
427
406
428
- @property
429
- def alerts_count (self ):
430
- return self .alerts .count ()
431
-
432
407
@property
433
408
def happened_while_maintenance (self ):
434
409
return self .root_alert_group is not None and self .root_alert_group .maintenance_uuid is not None
@@ -449,10 +424,6 @@ def acknowledge_by_user(self, user: User, action_source: Optional[str] = None) -
449
424
self .unresolve ()
450
425
self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_RESOLVED , author = user , reason = "Acknowledge button" )
451
426
452
- # clear resolve report cache
453
- cache_key = "render_after_resolve_report_json_{}" .format (self .pk )
454
- cache .delete (cache_key )
455
-
456
427
self .acknowledge (acknowledged_by_user = user , acknowledged_by = AlertGroup .USER )
457
428
self .stop_escalation ()
458
429
if self .is_root_alert_group :
@@ -673,9 +644,6 @@ def un_resolve_by_user(self, user: User, action_source: Optional[str] = None) ->
673
644
self .unresolve ()
674
645
log_record = self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_RESOLVED , author = user )
675
646
676
- # clear resolve report cache
677
- self .drop_cached_after_resolve_report_json ()
678
-
679
647
if self .is_root_alert_group :
680
648
self .start_escalation_if_needed ()
681
649
@@ -848,10 +816,6 @@ def silence_by_user(self, user: User, silence_delay: Optional[int], action_sourc
848
816
self .unresolve ()
849
817
self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_RESOLVED , author = user , reason = "Silence button" )
850
818
851
- # clear resolve report cache
852
- cache_key = "render_after_resolve_report_json_{}" .format (self .pk )
853
- cache .delete (cache_key )
854
-
855
819
if self .acknowledged :
856
820
self .unacknowledge ()
857
821
self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_ACK , author = user , reason = "Silence button" )
@@ -1060,8 +1024,6 @@ def bulk_acknowledge(user: User, alert_groups: "QuerySet[AlertGroup]") -> None:
1060
1024
author = user ,
1061
1025
reason = "Bulk action acknowledge" ,
1062
1026
)
1063
- # clear resolve report cache
1064
- alert_group .drop_cached_after_resolve_report_json ()
1065
1027
1066
1028
for alert_group in alert_groups_to_unsilence_before_acknowledge_list :
1067
1029
alert_group .log_records .create (
@@ -1194,8 +1156,6 @@ def bulk_restart(user: User, alert_groups: "QuerySet[AlertGroup]") -> None:
1194
1156
reason = "Bulk action restart" ,
1195
1157
)
1196
1158
1197
- alert_group .drop_cached_after_resolve_report_json ()
1198
-
1199
1159
if alert_group .is_root_alert_group :
1200
1160
alert_group .start_escalation_if_needed ()
1201
1161
@@ -1293,7 +1253,6 @@ def bulk_silence(user: User, alert_groups: "QuerySet[AlertGroup]", silence_delay
1293
1253
author = user ,
1294
1254
reason = "Bulk action silence" ,
1295
1255
)
1296
- alert_group .drop_cached_after_resolve_report_json ()
1297
1256
1298
1257
for alert_group in alert_groups_to_unsilence_before_silence_list :
1299
1258
alert_group .log_records .create (
@@ -1483,7 +1442,7 @@ def get_acknowledge_text(self, mention_user=False):
1483
1442
else :
1484
1443
return "Acknowledged"
1485
1444
1486
- def non_cached_after_resolve_report_json (self ):
1445
+ def render_after_resolve_report_json (self ):
1487
1446
AlertGroupLogRecord = apps .get_model ("alerts" , "AlertGroupLogRecord" )
1488
1447
UserNotificationPolicyLogRecord = apps .get_model ("base" , "UserNotificationPolicyLogRecord" )
1489
1448
ResolutionNote = apps .get_model ("alerts" , "ResolutionNote" )
@@ -1501,21 +1460,6 @@ def non_cached_after_resolve_report_json(self):
1501
1460
result_log_report .append (log_record .render_log_line_json ())
1502
1461
return result_log_report
1503
1462
1504
- def render_after_resolve_report_json (self ):
1505
- cache_key = "render_after_resolve_report_json_{}" .format (self .pk )
1506
-
1507
- # cache.get_or_set in some cases returns None, so use get and set cache methods separately
1508
- log_report = cache .get (cache_key )
1509
- if log_report is None :
1510
- log_report = self .non_cached_after_resolve_report_json ()
1511
- cache .set (cache_key , log_report )
1512
- return log_report
1513
-
1514
- def drop_cached_after_resolve_report_json (self ):
1515
- cache_key = "render_after_resolve_report_json_{}" .format (self .pk )
1516
- if cache_key in cache :
1517
- cache .delete (cache_key )
1518
-
1519
1463
@property
1520
1464
def has_resolution_notes (self ):
1521
1465
return self .resolution_notes .exists ()
@@ -1595,14 +1539,3 @@ def last_stop_escalation_log(self):
1595
1539
)
1596
1540
1597
1541
return stop_escalation_log
1598
-
1599
-
1600
- @receiver (post_save , sender = AlertGroup )
1601
- def listen_for_alert_group_model_save (sender , instance , created , * args , ** kwargs ):
1602
- if (
1603
- kwargs is not None
1604
- and "update_fields" in kwargs
1605
- and kwargs ["update_fields" ] is dict
1606
- and "cached_render_for_web" not in kwargs ["update_fields" ]
1607
- ):
1608
- transaction .on_commit (instance .schedule_cache_for_alert_group )
0 commit comments