Skip to content

Commit

Permalink
[#3725] Process feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
vaszig committed Apr 10, 2024
1 parent c9d1e2a commit 0eee5b5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
17 changes: 9 additions & 8 deletions src/openforms/emails/templates/emails/admin_digest.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h5>{% trans "Emails that failed to send" %}</h5>
<ul>
{% for email in failed_emails %}
<li>
{% blocktranslate with event=email.event submission_uuid=email.submission_uuid %}
{% blocktranslate with event=email.event submission_uuid=email.submission_uuid trimmed %}
Email for the event "{{ event }}" for submission {{ submission_uuid }}.
{% endblocktranslate %}
</li>
Expand All @@ -22,15 +22,16 @@ <h5>{% trans "Registrations" %}</h5>
<ul>
{% for registration in failed_registrations %}
<li>
{% blocktranslate with form_name=registration.form_name counter=registration.failed_submissions_counter first_failure_at=registration.initial_failure_at|time:"H:i" last_failure_at=registration.last_failure_at|time:"H:i" %}
Form '{{ form_name }}' failed {{ counter }} times between {{ first_failure_at }} and {{ last_failure_at }}.
{% blocktranslate with form_name=registration.form_name counter=registration.failed_submissions_counter first_failure_at=registration.initial_failure_at|time:"H:i" last_failure_at=registration.last_failure_at|time:"H:i" trimmed %}
Form '{{ form_name }}' failed {{ counter }} times between {{ first_failure_at }} and {{ last_failure_at }}.</br>
{% endblocktranslate %}

<a href="{{ registration.admin_link }}">
{% blocktranslate with form_name=registration.form_name %}
View failed '{{ form_name }}' submissions
{% endblocktranslate %}
</a>
</li>
<a href="{{ registration.admin_link }}">
{% blocktranslate with form_name=registration.form_name %}
View failed '{{ form_name }}' submissions
{% endblocktranslate %}
</a>
{% endfor %}
</ul>
{% endif %}
4 changes: 2 additions & 2 deletions src/openforms/emails/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ def test_create_digest_email(self):
send_email_digest()

sent_email = mail.outbox[0]
submission_occurencies = sent_email.body.count(str(submission.uuid))
submission_occurences = sent_email.body.count(str(submission.uuid))

self.assertEqual(
sent_email.subject, "[Open Forms] Daily summary of detected problems"
)
self.assertEqual(
sent_email.recipients(), ["[email protected]", "[email protected]"]
)
self.assertEqual(submission_occurencies, 1)
self.assertEqual(submission_occurences, 1)

def test_that_repeated_failures_are_not_mentioned_multiple_times(self):
submission = SubmissionFactory.create()
Expand Down
6 changes: 2 additions & 4 deletions src/openforms/emails/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ def get_email_content(self):
content = strip_tags_plus(content, keep_leading_whitespace=True)

case {"email_digest": True}:
interval = timezone.now() - timedelta(days=1)
if days_before := self.request.GET.get("days_before"):
interval = timezone.now() - timedelta(days=int(days_before))

days_before = int(self.request.GET.get("days_before", 1))
interval = timezone.now() - timedelta(days=days_before)
digest = Digest(since=interval)
return digest.render()

Expand Down
31 changes: 15 additions & 16 deletions src/openforms/logging/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class FailedEmail:

@dataclass
class FailedRegistration:
form_uuid: uuid.UUID
form_name: str
failed_submissions_counter: int
initial_failure_at: datetime
Expand All @@ -37,17 +36,18 @@ def collect_failed_emails(since: datetime) -> Iterable[FailedEmail]:
return []

failed_emails = [
{
"submission_uuid": log.content_object.uuid,
"event": log.extra_data["event"],
}
FailedEmail(
submission_uuid=log.content_object.uuid, event=log.extra_data["event"]
)
for log in logs
]

return failed_emails


def collect_failed_registrations(since: datetime) -> Iterable[FailedRegistration]:
def collect_failed_registrations(
since: datetime,
) -> Iterable[dict[str, FailedRegistration]]:
logs = TimelineLogProxy.objects.filter(
timestamp__gt=since,
extra_data__log_event="registration_failure",
Expand All @@ -56,23 +56,22 @@ def collect_failed_registrations(since: datetime) -> Iterable[FailedRegistration
if not logs:
return []

form_sorted_logs = sorted(logs, key=lambda x: x.content_object.form.id)
form_sorted_logs = sorted(logs, key=lambda x: x.content_object.form.admin_name)

grouped_logs = {}
for form, logs_group in groupby(
form_sorted_logs, key=lambda log: log.content_object.form
):
grouped_logs[form] = list(logs_group)
grouped_logs = groupby(form_sorted_logs, key=lambda log: log.content_object.form)

failed_registrations = {}
for form, submission_logs in grouped_logs.items():
timestamps = [log.timestamp for log in submission_logs]
for form, submission_logs in grouped_logs:
logs = list(submission_logs)
timestamps = [log.timestamp for log in logs]
failed_registrations[str(form.uuid)] = {
"form_name": form.name,
"failed_submissions_counter": len(submission_logs),
"failed_submissions_counter": len(logs),
"initial_failure_at": min(timestamps),
"last_failure_at": max(timestamps),
"admin_link": get_filtered_submission_admin_url(form.id, 1, "24hAgo"),
"admin_link": get_filtered_submission_admin_url(
form.id, filter_retry=True, registration_time="24hAgo"
),
}

return list(failed_registrations.values())
6 changes: 3 additions & 3 deletions src/openforms/submissions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def expected_parameters(self):


class SubmissionTimeListFilter(admin.SimpleListFilter):
title = _("time")
parameter_name = "from_time"
title = _("registration time")
parameter_name = "registration_time"

def lookups(self, request, model_admin):
return [
("24hAgo", _("Registered in the past 24 hours")),
("24hAgo", _("In the past 24 hours")),
]

def queryset(self, request, queryset):
Expand Down
8 changes: 3 additions & 5 deletions src/openforms/submissions/tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,13 @@ def test_time_filtering(self):
model_admin = SubmissionAdmin(Submission, site)
filter_instance = SubmissionTimeListFilter(
request=None,
params={"from_time": "24hAgo"},
params={"registration_time": "24hAgo"},
model=Submission,
model_admin=model_admin,
)

queryset = Submission.objects.all()
filtered_queryset = filter_instance.queryset(None, queryset)

self.assertIn(submission_1, queryset)
self.assertIn(submission_2, queryset)
self.assertIn(submission_1, filtered_queryset)
self.assertNotIn(submission_2, filtered_queryset)
self.assertQuerySetEqual(queryset, [submission_1, submission_2], ordered=False)
self.assertQuerySetEqual(filtered_queryset, [submission_1], ordered=False)
8 changes: 5 additions & 3 deletions src/openforms/submissions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,13 @@ def get_report_download_url(request: Request, report: SubmissionReport) -> str:
return request.build_absolute_uri(download_url)


def get_filtered_submission_admin_url(form_id: int, state: int, from_time: str) -> str:
def get_filtered_submission_admin_url(
form_id: int, *, filter_retry: bool, registration_time: str
) -> str:
query_params = {
"form__id__exact": form_id,
"needs_on_completion_retry__exact": state,
"from_time": from_time,
"needs_on_completion_retry__exact": 1 if filter_retry else 0,
"registration_time": registration_time,
}
submissions_admin_url = furl(reverse("admin:submissions_submission_changelist"))
return submissions_admin_url.add(query_params).url

0 comments on commit 0eee5b5

Please sign in to comment.