|
1 | 1 | from asgiref.sync import async_to_sync |
2 | 2 | from channels import layers |
3 | 3 | from django.core.cache import cache |
| 4 | +from django.db.models import Count, Q |
4 | 5 | from django.utils.timezone import now, timedelta |
5 | 6 |
|
6 | 7 | from openwisp_notifications.api.serializers import NotFound, NotificationListSerializer |
|
12 | 13 | Notification = load_model("Notification") |
13 | 14 |
|
14 | 15 |
|
| 16 | +def bulk_check_notification_storm_and_unread_count(recipients): |
| 17 | + if not recipients: |
| 18 | + return {} |
| 19 | + |
| 20 | + recipient_ids = [str(recipient.pk) for recipient in recipients] |
| 21 | + cached_storm_data = cache.get_many([f"ow-noti-storm-{pk}" for pk in recipient_ids]) |
| 22 | + |
| 23 | + results = {} |
| 24 | + uncached_recipients = [] |
| 25 | + |
| 26 | + for recipient in recipients: |
| 27 | + cache_key = f"ow-noti-storm-{recipient.pk}" |
| 28 | + if cache_key in cached_storm_data: |
| 29 | + results[recipient.pk] = [cached_storm_data[cache_key], None] |
| 30 | + else: |
| 31 | + uncached_recipients.append(recipient) |
| 32 | + results[recipient.pk] = [False, None] |
| 33 | + |
| 34 | + if uncached_recipients: |
| 35 | + short_term_threshold = now() - timedelta( |
| 36 | + seconds=app_settings.NOTIFICATION_STORM_PREVENTION["short_term_time_period"] |
| 37 | + ) |
| 38 | + long_term_threshold = now() - timedelta( |
| 39 | + seconds=app_settings.NOTIFICATION_STORM_PREVENTION["long_term_time_period"] |
| 40 | + ) |
| 41 | + |
| 42 | + storm_and_unread_data = ( |
| 43 | + Notification.objects.filter(recipient_id__in=recipient_ids) |
| 44 | + .values("recipient_id") |
| 45 | + .annotate( |
| 46 | + short_term_count=Count( |
| 47 | + "id", filter=Q(timestamp__gte=short_term_threshold) |
| 48 | + ), |
| 49 | + long_term_count=Count( |
| 50 | + "id", filter=Q(timestamp__gte=long_term_threshold) |
| 51 | + ), |
| 52 | + unread_count=Count("id", filter=Q(unread=True)), |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + cache_updates = {} |
| 57 | + for data in storm_and_unread_data: |
| 58 | + recipient_id = data["recipient_id"] |
| 59 | + |
| 60 | + in_storm = ( |
| 61 | + data["short_term_count"] |
| 62 | + > app_settings.NOTIFICATION_STORM_PREVENTION[ |
| 63 | + "short_term_notification_count" |
| 64 | + ] |
| 65 | + or data["long_term_count"] |
| 66 | + > app_settings.NOTIFICATION_STORM_PREVENTION[ |
| 67 | + "long_term_notification_count" |
| 68 | + ] |
| 69 | + ) |
| 70 | + |
| 71 | + results[recipient_id] = [in_storm, data["unread_count"]] |
| 72 | + |
| 73 | + if in_storm: |
| 74 | + cache_updates[f"ow-noti-storm-{recipient_id}"] = True |
| 75 | + |
| 76 | + if cache_updates: |
| 77 | + cache.set_many(cache_updates, timeout=60) |
| 78 | + |
| 79 | + for recipient in uncached_recipients: |
| 80 | + if recipient.pk not in [ |
| 81 | + data["recipient_id"] for data in storm_and_unread_data |
| 82 | + ]: |
| 83 | + results[recipient.pk] = [False, 0] |
| 84 | + |
| 85 | + if any(results[pk][1] is None for pk in results): |
| 86 | + recipients_needing_unread = [pk for pk in results if results[pk][1] is None] |
| 87 | + unread_data = ( |
| 88 | + Notification.objects.filter( |
| 89 | + recipient_id__in=recipients_needing_unread, unread=True |
| 90 | + ) |
| 91 | + .values("recipient_id") |
| 92 | + .annotate(unread_count=Count("id")) |
| 93 | + ) |
| 94 | + |
| 95 | + for data in unread_data: |
| 96 | + results[data["recipient_id"]][1] = data["unread_count"] |
| 97 | + |
| 98 | + for pk in recipients_needing_unread: |
| 99 | + if results[pk][1] is None: |
| 100 | + results[pk][1] = 0 |
| 101 | + |
| 102 | + return {pk: (storm, unread) for pk, (storm, unread) in results.items()} |
| 103 | + |
| 104 | + |
15 | 105 | def user_in_notification_storm(user): |
16 | 106 | """ |
17 | 107 | A user is affected by notifications storm if any of short term |
@@ -52,23 +142,41 @@ def user_in_notification_storm(user): |
52 | 142 | return in_notification_storm |
53 | 143 |
|
54 | 144 |
|
55 | | -def notification_update_handler(reload_widget=False, notification=None, recipient=None): |
| 145 | +def bulk_notification_update_handler( |
| 146 | + recipients, reload_widget=False, notification=None |
| 147 | +): |
| 148 | + if not recipients: |
| 149 | + return |
| 150 | + |
56 | 151 | channel_layer = layers.get_channel_layer() |
| 152 | + |
| 153 | + serialized_notification = None |
57 | 154 | try: |
58 | | - assert notification is not None |
59 | | - notification = NotificationListSerializer(notification).data |
| 155 | + if notification is not None: |
| 156 | + serialized_notification = NotificationListSerializer(notification).data |
60 | 157 | except (NotFound, AssertionError): |
61 | 158 | pass |
62 | | - async_to_sync(channel_layer.group_send)( |
63 | | - f"ow-notification-{recipient.pk}", |
64 | | - { |
65 | | - "type": "send.updates", |
66 | | - "reload_widget": reload_widget, |
67 | | - "notification": notification, |
68 | | - "recipient": str(recipient.pk), |
69 | | - "in_notification_storm": user_in_notification_storm(recipient), |
70 | | - "notification_count": normalize_unread_count( |
71 | | - recipient.notifications.unread().count() |
72 | | - ), |
73 | | - }, |
74 | | - ) |
| 159 | + |
| 160 | + bulk_data = bulk_check_notification_storm_and_unread_count(recipients) |
| 161 | + |
| 162 | + for recipient in recipients: |
| 163 | + in_storm, unread_count = bulk_data.get(recipient.pk, (False, 0)) |
| 164 | + |
| 165 | + async_to_sync(channel_layer.group_send)( |
| 166 | + f"ow-notification-{recipient.pk}", |
| 167 | + { |
| 168 | + "type": "send.updates", |
| 169 | + "reload_widget": reload_widget, |
| 170 | + "notification": serialized_notification, |
| 171 | + "recipient": str(recipient.pk), |
| 172 | + "in_notification_storm": in_storm, |
| 173 | + "notification_count": normalize_unread_count(unread_count), |
| 174 | + }, |
| 175 | + ) |
| 176 | + |
| 177 | + |
| 178 | +def notification_update_handler(reload_widget=False, notification=None, recipient=None): |
| 179 | + if recipient is None: |
| 180 | + return |
| 181 | + |
| 182 | + bulk_notification_update_handler([recipient], reload_widget, notification) |
0 commit comments