|
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 | | -from openwisp_notifications.api.serializers import NotFound, NotificationListSerializer |
7 | 7 | from openwisp_notifications.utils import normalize_unread_count |
8 | 8 |
|
9 | 9 | from .. import settings as app_settings |
|
12 | 12 | Notification = load_model("Notification") |
13 | 13 |
|
14 | 14 |
|
| 15 | +def bulk_check_notification_storm_and_unread_count(recipients): |
| 16 | + if not recipients: |
| 17 | + return {} |
| 18 | + recipient_ids = [recipient.pk for recipient in recipients] |
| 19 | + cached_storm_data = cache.get_many([f"ow-noti-storm-{pk}" for pk in recipient_ids]) |
| 20 | + results = {} |
| 21 | + uncached_recipients = [] |
| 22 | + for recipient in recipients: |
| 23 | + cache_key = f"ow-noti-storm-{recipient.pk}" |
| 24 | + if cache_key in cached_storm_data: |
| 25 | + results[recipient.pk] = [cached_storm_data[cache_key], None] |
| 26 | + else: |
| 27 | + uncached_recipients.append(recipient) |
| 28 | + results[recipient.pk] = [False, None] |
| 29 | + if uncached_recipients: |
| 30 | + short_term_threshold = now() - timedelta( |
| 31 | + seconds=app_settings.NOTIFICATION_STORM_PREVENTION["short_term_time_period"] |
| 32 | + ) |
| 33 | + long_term_threshold = now() - timedelta( |
| 34 | + seconds=app_settings.NOTIFICATION_STORM_PREVENTION["long_term_time_period"] |
| 35 | + ) |
| 36 | + uncached_recipient_ids = [recipient.pk for recipient in uncached_recipients] |
| 37 | + storm_and_unread_data = list( |
| 38 | + Notification.objects.filter(recipient_id__in=uncached_recipient_ids) |
| 39 | + .values("recipient_id") |
| 40 | + .annotate( |
| 41 | + short_term_count=Count( |
| 42 | + "id", filter=Q(timestamp__gte=short_term_threshold) |
| 43 | + ), |
| 44 | + long_term_count=Count( |
| 45 | + "id", filter=Q(timestamp__gte=long_term_threshold) |
| 46 | + ), |
| 47 | + unread_count=Count("id", filter=Q(unread=True)), |
| 48 | + ) |
| 49 | + ) |
| 50 | + cache_updates = {} |
| 51 | + for data in storm_and_unread_data: |
| 52 | + recipient_id = data["recipient_id"] |
| 53 | + in_storm = ( |
| 54 | + data["short_term_count"] |
| 55 | + > app_settings.NOTIFICATION_STORM_PREVENTION[ |
| 56 | + "short_term_notification_count" |
| 57 | + ] |
| 58 | + or data["long_term_count"] |
| 59 | + > app_settings.NOTIFICATION_STORM_PREVENTION[ |
| 60 | + "long_term_notification_count" |
| 61 | + ] |
| 62 | + ) |
| 63 | + results[recipient_id] = [in_storm, data["unread_count"]] |
| 64 | + if in_storm: |
| 65 | + cache_updates[f"ow-noti-storm-{recipient_id}"] = True |
| 66 | + if cache_updates: |
| 67 | + cache.set_many(cache_updates, timeout=60) |
| 68 | + fetched_recipient_ids = {data["recipient_id"] for data in storm_and_unread_data} |
| 69 | + for recipient in uncached_recipients: |
| 70 | + if recipient.pk not in fetched_recipient_ids: |
| 71 | + results[recipient.pk] = [False, 0] |
| 72 | + if any(results[pk][1] is None for pk in results): |
| 73 | + recipients_needing_unread = [pk for pk in results if results[pk][1] is None] |
| 74 | + unread_data = ( |
| 75 | + Notification.objects.filter( |
| 76 | + recipient_id__in=recipients_needing_unread, unread=True |
| 77 | + ) |
| 78 | + .values("recipient_id") |
| 79 | + .annotate(unread_count=Count("id")) |
| 80 | + ) |
| 81 | + for data in unread_data: |
| 82 | + results[data["recipient_id"]][1] = data["unread_count"] |
| 83 | + for pk in recipients_needing_unread: |
| 84 | + if results[pk][1] is None: |
| 85 | + results[pk][1] = 0 |
| 86 | + return {pk: (storm, unread) for pk, (storm, unread) in results.items()} |
| 87 | + |
| 88 | + |
15 | 89 | def user_in_notification_storm(user): |
16 | 90 | """ |
17 | 91 | A user is affected by notifications storm if any of short term |
@@ -52,23 +126,27 @@ def user_in_notification_storm(user): |
52 | 126 | return in_notification_storm |
53 | 127 |
|
54 | 128 |
|
55 | | -def notification_update_handler(reload_widget=False, notification=None, recipient=None): |
| 129 | +def bulk_notification_update_handler(recipients, reload_widget=False): |
| 130 | + if not recipients: |
| 131 | + return |
56 | 132 | channel_layer = layers.get_channel_layer() |
57 | | - try: |
58 | | - assert notification is not None |
59 | | - notification = NotificationListSerializer(notification).data |
60 | | - except (NotFound, AssertionError): |
61 | | - 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 | | - ) |
| 133 | + bulk_data = bulk_check_notification_storm_and_unread_count(recipients) |
| 134 | + for recipient in recipients: |
| 135 | + in_storm, unread_count = bulk_data.get(recipient.pk, (False, 0)) |
| 136 | + async_to_sync(channel_layer.group_send)( |
| 137 | + f"ow-notification-{recipient.pk}", |
| 138 | + { |
| 139 | + "type": "send.updates", |
| 140 | + "reload_widget": reload_widget, |
| 141 | + "notification": None, |
| 142 | + "recipient": str(recipient.pk), |
| 143 | + "in_notification_storm": in_storm, |
| 144 | + "notification_count": normalize_unread_count(unread_count), |
| 145 | + }, |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +def notification_update_handler(reload_widget=False, notification=None, recipient=None): |
| 150 | + if recipient is None: |
| 151 | + return |
| 152 | + bulk_notification_update_handler([recipient], reload_widget) |
0 commit comments