Skip to content

Commit

Permalink
[#3725] Started implementing the registration failures
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Apr 2, 2024
1 parent bdf53af commit 04e78fd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
61 changes: 58 additions & 3 deletions src/openforms/emails/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import timedelta

from django.conf import settings
from django.db import models
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
Expand All @@ -13,17 +14,17 @@

from .utils import send_mail_html

PERIOD_START_DAY = timezone.now() - timedelta(days=1)


@app.task
def send_email_digest() -> None:
config = GlobalConfiguration.get_solo()
if not (recipients := config.recipients_email_digest):
return

period_start = timezone.now() - timedelta(days=1)

logs = TimelineLogProxy.objects.filter(
timestamp__gt=period_start,
timestamp__gt=PERIOD_START_DAY,
extra_data__status=Message.STATUS_FAILED,
extra_data__include_in_daily_digest=True,
).distinct("content_type", "extra_data__status", "extra_data__event")
Expand All @@ -50,3 +51,57 @@ def send_email_digest() -> None:
settings.DEFAULT_FROM_EMAIL,
recipients,
)


def send_registrations_failures() -> models.QuerySet[TimelineLogProxy]:
config = GlobalConfiguration.get_solo()
if not (recipients := config.recipients_email_digest):
return

logs = TimelineLogProxy.objects.filter(
timestamp__gt=PERIOD_START_DAY,
extra_data__log_event="registration_failure",
extra_data__include_in_daily_digest=True,
)

if not logs:
return

connected_forms = {}
for log in logs:
submission = log.content_object
form = log.content_object.form
if form.uuid in connected_forms:
connected_forms[form.uuid]["form_counter"] += 1
connected_forms[form.uuid]["last_failure_at"] = log.timestamp
else:
connected_forms[form.uuid] = {
"submission": str(submission),
"form": form.title,
"failed_submissions_counter": 1,
"initial_failure_at": log.timestamp,
}

content = render_to_string(
"emails/admin_failed_submissions_digest.html",
{
"forms": [
{
"form_title": form["form_title"],
"failed_submissions_counter": form["failed_submissions_counter"],
"initial_failure_timestamp": form["initial_failure_at"],
"last_failure_timestamp": form["last_failure_at"],
}
for form in connected_forms
],
},
)

send_mail_html(
_("[Open Forms] Daily summary of failed registrations"),
content,
settings.DEFAULT_FROM_EMAIL,
recipients,
)

return logs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% load static i18n %}

<p>
{% blocktranslate %}Here is a summary of the failed registrations in the past 24 hours:{% endblocktranslate %}
</p>
<ul>
{% for form in forms %}
<li>
- {{ form.form_title }} failed {{ form.failed_submissions_counter }} times between {{ form.initial_failure_timestamp }} and {{ initial_failure_timestamp }}.
</li>
{% endfor %}
</ul>

10 changes: 9 additions & 1 deletion src/openforms/logging/logevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,20 @@ def registration_success(submission: Submission, plugin):
)


def registration_failure(submission: Submission, error: Exception, plugin=None):
def registration_failure(
submission: Submission,
error: Exception,
plugin=None,
include_in_daily_digest: bool = False,
):
_create_log(
submission,
"registration_failure",
plugin=plugin,
error=error,
extra_data={
"include_in_daily_digest": include_in_daily_digest,
},
)


Expand Down
15 changes: 11 additions & 4 deletions src/openforms/registrations/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def register_submission(submission_id: int, event: PostSubmissionEvents | str) -
RegistrationStatuses.failed,
{"traceback": "".join(traceback.format_exception(exc))},
)
logevent.registration_failure(submission, exc)
logevent.registration_failure(submission, exc, include_in_daily_digest=True)
if event == PostSubmissionEvents.on_retry:
raise exc
return
Expand All @@ -238,7 +238,9 @@ def register_submission(submission_id: int, event: PostSubmissionEvents | str) -
try:
options_serializer.is_valid(raise_exception=True)
except ValidationError as exc:
logevent.registration_failure(submission, exc, plugin)
logevent.registration_failure(
submission, exc, plugin, include_in_daily_digest=True
)
logger.warning(
"Registration using plugin '%r' for submission '%s' failed",
plugin,
Expand Down Expand Up @@ -268,7 +270,10 @@ def register_submission(submission_id: int, event: PostSubmissionEvents | str) -
submission.save_registration_status(
RegistrationStatuses.failed, {"traceback": traceback.format_exc()}
)
logevent.registration_failure(submission, exc, plugin)
logevent.registration_failure(
submission, exc, plugin, include_in_daily_digest=True
)

submission.save_registration_status(
RegistrationStatuses.failed, {"traceback": traceback.format_exc()}
)
Expand All @@ -285,7 +290,9 @@ def register_submission(submission_id: int, event: PostSubmissionEvents | str) -
submission.save_registration_status(
RegistrationStatuses.failed, {"traceback": traceback.format_exc()}
)
logevent.registration_failure(submission, exc, plugin)
logevent.registration_failure(
submission, exc, plugin, include_in_daily_digest=True
)
if event == PostSubmissionEvents.on_retry:
raise exc
return
Expand Down

0 comments on commit 04e78fd

Please sign in to comment.