Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Batch email notifications #276

Open
wants to merge 13 commits into
base: gsoc24
Choose a base branch
from
Open

[feat] Batch email notifications #276

wants to merge 13 commits into from

Conversation

Dhanus3133
Copy link
Contributor

Fixes #132

Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dhanus3133 I have left some suggestions. I hope these solve your queries.

openwisp_notifications/settings.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
@Dhanus3133 Dhanus3133 marked this pull request as ready for review June 6, 2024 14:56
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dhanus3133 please start writin a short README section that explains how this feature works and add the new setting, remember to add a screenshot (push it to the docs branch).

@pandafy you know what would be handy?

  • Allow to change the batching using an organization setting that allows user to change this per org via the admin
  • Allow to specify whether the system should start batching right away instead of waiting 1 email, this could be disabled by default, and could also be available from the organization page

openwisp_notifications/handlers.py Show resolved Hide resolved

# Check if the rest of the notifications are sent in a batch
self.assertEqual(len(mail.outbox), 2)
self.assertEqual(mail.outbox[1].subject, "Summary of Notifications")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd try to explore the possibility of using freezegun to simulate the passing of time and ensure that a new notification sent after the batch interval is not batched.

@Dhanus3133
Copy link
Contributor Author

In our weekly call, @nemesifier and @pandafy suggested some improvements.

  1. The date should be displayed below the title.
  2. Update the email title with the number of notifications and site name.
  3. Some changes have to be made regarding the email template
    1. If there is only one notification in the batch, just use a single mail template.
    2. Can follow the current template if there are 2 - 5 notifications.
    3. If there are more than 5 emails, a new template can be used where only the level, message, and date can be displayed. Along with a ‘View all new notifications’ link to the admin site.

Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good. I have done a quick code review and left some suggestion on improving the code.

I will do a functional test tomorrow and report if I find something to improve.

openwisp_notifications/settings.py Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Outdated Show resolved Hide resolved
openwisp_notifications/handlers.py Show resolved Hide resolved
@@ -37,6 +37,10 @@
'openwisp-notifications/audio/notification_bell.mp3',
)

OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL = getattr(
settings, 'EMAIL_BATCH_INTERVAL', 30 * 60 # 30 minutes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking out loud

I think 30 minutes is a lot of time. It will affect the real-timeness of the email notifications. Most of the consecutive notifications would be delayed by this time. We should also point this out in the README.

@nemesifier what do you think?

Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediate email after batch email

If a notification is created just after the batch email is processed, then the email for this notification is sent immediately after the batch email.

Is this a desired behaviour? Can you please add a test for this?

Duplicate subject in email

The send_email function adds a subject in the email which servers as a heading for all the email communication.

Screenshot from 2024-06-18 13-05-59

I think, we should keep using the subject provided by the send_email function for consistency.

openwisp_notifications/settings.py Outdated Show resolved Hide resolved
README.rst Outdated Show resolved Hide resolved
openwisp_notifications/templates/emails/batch_email.html Outdated Show resolved Hide resolved
openwisp_notifications/templates/emails/batch_email.html Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/tasks.py Outdated Show resolved Hide resolved
openwisp_notifications/utils.py Outdated Show resolved Hide resolved
openwisp_notifications/tests/test_notifications.py Outdated Show resolved Hide resolved
Copy link
Member

@pandafy pandafy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dhanus3133 the test is failing because the notification email contains direct URL to the target object while the test expects redirect url

-<div class="msg"><p>Default notification with default verb and level info by <a href="https://example.com/api/v1/notifications/notification/2632e308-0f2d-43a1-8617-7350e48b8fd3/redirect/">Tester Tester (test org)</a></p></div>
+<div class="msg"><p>Default notification with default verb and level info by <a href="https://example.com/admin/openwisp_users/organizationuser/dea5c3cc-8c2a-4021-94a7-9cc36885f9ff/change/">Tester Tester (test org)</a></p></div>

@Dhanus3133 Dhanus3133 changed the base branch from master to gsoc24 June 23, 2024 13:30
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @Dhanus3133 @pandafy, see my comments below.

@@ -1013,6 +1013,19 @@ The default configuration is as follows:
'max_allowed_backoff': 15,
}
``OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems longer than it should

# Case 2: Single email sending logic
# Update the last email sent time and cache the data
cache_data['last_email_sent_time'] = timezone.now()
cache.set(cache_key, cache_data, timeout=EMAIL_BATCH_INTERVAL)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if EMAIL_BATCH_INTERVAL is zero, wouldn't this data be cached indefinitely? If that's the case, it's a problem.



@shared_task(base=OpenwispCeleryTask)
def batch_email_notification(instance_id):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not convinced of the name of this function here.

Does this function batch emails, or does it send batched emails? From the name it seems it does the former, but from the code it seems to me it does the latter. Please clarify.

If it's the latter, we should rename it to something like send_batched_email_notifications or something similar.

@@ -37,6 +37,10 @@
'openwisp-notifications/audio/notification_bell.mp3',
)

OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL = getattr(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's shorten this, we usually do this in most modules, eg:

Suggested change
OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL = getattr(
EMAIL_BATCH_INTERVAL = getattr(

plain_text_content = render_to_string('emails/batch_email.txt', context)

extra_context = {}
if notifications_count > 5:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we showing only 5 notifications? Let's show more and make this configurable with a setting, eg: OPENWISP_NOTIFICATIONS_EMAIL_BATCH_DISPLAY_LIMIT (add to README). I think we can default this to 15.

@@ -0,0 +1,15 @@
Summary of {{ notifications_count }} Notifications from {{ site_name }}
Copy link
Member

@nemesifier nemesifier Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flag all strings as translatable using translate and blocktranslate: https://docs.djangoproject.com/en/5.0/topics/i18n/translation/#internationalization-in-template-code

Also here let's change this sentence as for the change I recommended in the subject, let's maintain consistency, eg: "{notifications_count} new notifications since <UTC_DATE_AND_TIME>".



def send_notification_email(notification):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove this blank line or does black complain about it?

notification = unsent_notifications.first()
send_notification_email(notification)
else:
# Show notification description upto 5 notifications
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Show notification description upto 5 notifications
# Show the amount of notifications according to configured display limit

send_notification_email(notification)
else:
# Show notification description upto 5 notifications
show_notification_description = notifications_count <= 5
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use setting instead of hardcoding 5

self.assertIn('Summary of 10 Notifications', mail.outbox[1].subject)
self.assertIn('View all Notifications', mail.outbox[1].body)
self.assertNotIn('Test Notification', mail.outbox[1].body)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add another test in which this feature is disabled and ensure that no batching happens in that case?
Using the override_settings from django or mocking the setting should work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] Batch email notifications to prevent email flooding
3 participants