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,8 +299,6 @@ def status(self):
315
299
related_name = "dependent_alert_groups" ,
316
300
)
317
301
318
- cached_render_for_web = JSONField (default = dict )
319
-
320
302
last_unique_unacknowledge_process_id = models .CharField (max_length = 100 , null = True , default = None )
321
303
is_archived = models .BooleanField (default = False )
322
304
@@ -404,18 +386,6 @@ def skip_escalation_in_slack(self):
404
386
def is_alert_a_resolve_signal (self , alert ):
405
387
raise NotImplementedError
406
388
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
389
@property
420
390
def permalink (self ):
421
391
if self .slack_message is not None :
@@ -425,10 +395,6 @@ def permalink(self):
425
395
def web_link (self ):
426
396
return urljoin (self .channel .organization .web_link , f"?page=incident&id={ self .public_primary_key } " )
427
397
428
- @property
429
- def alerts_count (self ):
430
- return self .alerts .count ()
431
-
432
398
@property
433
399
def happened_while_maintenance (self ):
434
400
return self .root_alert_group is not None and self .root_alert_group .maintenance_uuid is not None
@@ -449,10 +415,6 @@ def acknowledge_by_user(self, user: User, action_source: Optional[str] = None) -
449
415
self .unresolve ()
450
416
self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_RESOLVED , author = user , reason = "Acknowledge button" )
451
417
452
- # clear resolve report cache
453
- cache_key = "render_after_resolve_report_json_{}" .format (self .pk )
454
- cache .delete (cache_key )
455
-
456
418
self .acknowledge (acknowledged_by_user = user , acknowledged_by = AlertGroup .USER )
457
419
self .stop_escalation ()
458
420
if self .is_root_alert_group :
@@ -673,9 +635,6 @@ def un_resolve_by_user(self, user: User, action_source: Optional[str] = None) ->
673
635
self .unresolve ()
674
636
log_record = self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_RESOLVED , author = user )
675
637
676
- # clear resolve report cache
677
- self .drop_cached_after_resolve_report_json ()
678
-
679
638
if self .is_root_alert_group :
680
639
self .start_escalation_if_needed ()
681
640
@@ -848,10 +807,6 @@ def silence_by_user(self, user: User, silence_delay: Optional[int], action_sourc
848
807
self .unresolve ()
849
808
self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_RESOLVED , author = user , reason = "Silence button" )
850
809
851
- # clear resolve report cache
852
- cache_key = "render_after_resolve_report_json_{}" .format (self .pk )
853
- cache .delete (cache_key )
854
-
855
810
if self .acknowledged :
856
811
self .unacknowledge ()
857
812
self .log_records .create (type = AlertGroupLogRecord .TYPE_UN_ACK , author = user , reason = "Silence button" )
@@ -1060,8 +1015,6 @@ def bulk_acknowledge(user: User, alert_groups: "QuerySet[AlertGroup]") -> None:
1060
1015
author = user ,
1061
1016
reason = "Bulk action acknowledge" ,
1062
1017
)
1063
- # clear resolve report cache
1064
- alert_group .drop_cached_after_resolve_report_json ()
1065
1018
1066
1019
for alert_group in alert_groups_to_unsilence_before_acknowledge_list :
1067
1020
alert_group .log_records .create (
@@ -1194,8 +1147,6 @@ def bulk_restart(user: User, alert_groups: "QuerySet[AlertGroup]") -> None:
1194
1147
reason = "Bulk action restart" ,
1195
1148
)
1196
1149
1197
- alert_group .drop_cached_after_resolve_report_json ()
1198
-
1199
1150
if alert_group .is_root_alert_group :
1200
1151
alert_group .start_escalation_if_needed ()
1201
1152
@@ -1293,7 +1244,6 @@ def bulk_silence(user: User, alert_groups: "QuerySet[AlertGroup]", silence_delay
1293
1244
author = user ,
1294
1245
reason = "Bulk action silence" ,
1295
1246
)
1296
- alert_group .drop_cached_after_resolve_report_json ()
1297
1247
1298
1248
for alert_group in alert_groups_to_unsilence_before_silence_list :
1299
1249
alert_group .log_records .create (
@@ -1483,7 +1433,7 @@ def get_acknowledge_text(self, mention_user=False):
1483
1433
else :
1484
1434
return "Acknowledged"
1485
1435
1486
- def non_cached_after_resolve_report_json (self ):
1436
+ def render_after_resolve_report_json (self ):
1487
1437
AlertGroupLogRecord = apps .get_model ("alerts" , "AlertGroupLogRecord" )
1488
1438
UserNotificationPolicyLogRecord = apps .get_model ("base" , "UserNotificationPolicyLogRecord" )
1489
1439
ResolutionNote = apps .get_model ("alerts" , "ResolutionNote" )
@@ -1501,21 +1451,6 @@ def non_cached_after_resolve_report_json(self):
1501
1451
result_log_report .append (log_record .render_log_line_json ())
1502
1452
return result_log_report
1503
1453
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
1454
@property
1520
1455
def has_resolution_notes (self ):
1521
1456
return self .resolution_notes .exists ()
@@ -1595,14 +1530,3 @@ def last_stop_escalation_log(self):
1595
1530
)
1596
1531
1597
1532
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