Commit Graph

8586 Commits

Author SHA1 Message Date
Pea Tyczynska
32cd7a0eb6 Merge pull request #3395 from alphagov/fix_org_usage_report
Fix calculating remaining free allowance for SMS
2021-12-21 15:02:54 +00:00
Pea Tyczynska
d334e405c5 Refactor tests for sms remainder to make them easier to read 2021-12-21 14:43:56 +00:00
Ben Thorner
e55b654a0b Merge pull request #3407 from alphagov/downgrade-inbound-log
Downgrade log about orphaned inbound SMS
2021-12-21 13:36:10 +00:00
Ben Thorner
f65fb519c7 Merge pull request #3404 from alphagov/remove-redundant-conditional-180477467
Remove redundant conditional for letter branding
2021-12-21 13:35:59 +00:00
Ben Thorner
3d30965193 Downgrade log about orphaned inbound SMS
We can't control who might be sending messages on inbound numbers
that we own i.e. this log isn't an actionable error. Looks like it
used to represent something that _was_ an error [1], but that's not
the case anymore, so it seems reasonable to downgrade it.

[1]: d99ab329eb (diff-80d123d9abb40f80a221979940657a2751cc7cb33f255aa8f352a8324023e022L125)
2021-12-21 12:49:00 +00:00
Ben Thorner
c52cb4a8a8 Merge pull request #3406 from alphagov/bump-utils-51-3-0-180693991
Bump utils to 51.3.0
2021-12-20 16:59:43 +00:00
Ben Thorner
491b7ce9ee Bump utils to 51.3.0
This brings in new logging for the NotifyCelery base class [1].

[1]: https://github.com/alphagov/notifications-utils/pull/938
2021-12-20 16:45:47 +00:00
Ben Thorner
f4d967c0f1 Merge pull request #3405 from alphagov/downgrade-delete-letter-log-180692253
Downgrade log for letter deletion exceptions
2021-12-20 13:39:24 +00:00
Ben Thorner
de9ae08ecc Downgrade log for letter deletion exceptions
If the S3 object is missing [1], then that's what we want, so we
don't need such a severe log for it, but we still want to know as
it's not expected. This is separate to more general "ClientError"
exceptions, which could mean anything.

There weren't any tests to cover missing S3 objects, so I've added
one. I don't think we need a test for ClientErrors:

- If there was no handler, the task would fail and we'd learn about
it that way.

- The scope of the calling task is now much smaller, so it matters
less than it used to [2].

[1]: 81a79e56ce/app/letters/utils.py (L52)
[2]: f965322f25
2021-12-20 12:45:48 +00:00
Ben Thorner
76da31c32a Remove redundant conditional for letter branding
This is no longer used when creating a service [1]. It was likely
added at a migration point when Admin _did_ specify branding.

[1]: 50c3c3e10c/app/main/views/add_service.py (L15-L22)
2021-12-16 17:54:33 +00:00
Pea Tyczynska
6c04deaec2 Get rid of unnecessary coalesce 2021-12-14 17:36:03 +00:00
Leo Hemsted
81a79e56ce Merge pull request #3397 from alphagov/delete-per-service
run the notification delete task per service
2021-12-14 15:37:22 +00:00
Leo Hemsted
228d72dc8f update log messages in delete task.
less prose, clearer output. (hopefully)
2021-12-14 15:24:35 +00:00
Leo Hemsted
0dc0e184b9 clean up and rewrite notification_dao_delete_notifications
a bunch of these tests are now covered in the task test, so got rid of
some. Now that the "how long ago to delete" questions is asked in the
task rather than in the dao, and only one service is looked at at a
time, we don't need to worry about data retention, etc. Hopefully made
the tests simpler - there may still be some duplicates or overlaps
between the various cases.
2021-12-14 15:24:35 +00:00
Leo Hemsted
49cc1b643f split delete task up into per service
we really don't gain anything by running each service delete in sequence
- we get the services, and then just loop through them deleting per
service. By deleting per service in separate tasks, we can take
advantage of parallelism. the only thing we lose is some log lines but I
don't think we're that interested in them.

only set query limit at the move_notifications dao function - the task
doesn't really care about the technical implementation of how it deletes
the notifications
2021-12-14 15:24:34 +00:00
Leo Hemsted
bbc68293bb Merge pull request #3400 from alphagov/lxml-bump
bump lxml to fix security warning
2021-12-14 14:53:32 +00:00
Ben Thorner
7dd3e1fa87 Merge pull request #3399 from alphagov/timeout-stats-180344294
Add new metrics for slow / unknown delivery
2021-12-14 14:02:34 +00:00
Leo Hemsted
d916b07e80 remove old unused scripts
common_functions is full of AWS commands to manipulate workers running
on ec2 instances. We haven't done any of that for years since we moved
to AWS

delete_sqs_queues contains scripts to get a list of sqs queues and put
their details in a csv, or take a details csv and then delete all those
queues.

it's not clear what the use-case was for it but no-one's used it for
years and we can just use the admin console if we really need to.
2021-12-14 14:02:28 +00:00
Leo Hemsted
b7c1fcb66d bump lxml to fix security warning
two vulnerabilities in <4.6.5 (GHSL-2021-1037 and GHSL-2021-1038)
https://github.com/lxml/lxml/blob/master/CHANGES.txt

also removes docopt as we don't use it except for a dev script (which we
might not need anyway)
2021-12-14 13:47:38 +00:00
Ben Thorner
c8cf057eba Record providers we time out notifications for
This will help us monitor issues with delivery receipts and keep
track of provider performance over time.

I'm not concerned about performance here:

- The number of notifications to time out is usually small.
- This task only runs once a day.
- Calls to StatsD are quick and cheap.
2021-12-14 13:04:39 +00:00
Ben Thorner
11278c47f5 Replace log with StatsD gauge for slow delivery
A gauge is more useful as we can visualise it and combine it with
other stats - we already have other stats for the total number of
notifications sent by provider, and we can extrapolate the number
of slow notifications using this, if needed.

We also still have logs to say the task is running, as well as a
log in the calling code when we actually make a switch [1], so
we're not losing anything by removing the log here.

[1]: a9306c4557/app/celery/scheduled_tasks.py (L117)
2021-12-14 13:03:43 +00:00
Ben Thorner
a9306c4557 Merge pull request #3398 from alphagov/infinity-timeout-180344153
Scale timeout task to work on arbitrary volumes
2021-12-14 11:21:26 +00:00
Ben Thorner
c1f0c24d82 Trim down tests for DAO timeout function a bit
The first test is enough to cover that "created" and "delivered"
notifications aren't affected by this function.
2021-12-13 17:17:41 +00:00
Ben Thorner
87cd40d00a Scale timeout task to work on arbitrary volumes
Previously this was limited to 500K notifications. While we don't
expect to reach this limit, it's not impossible e.g. if we had a
repeat of the incident where one of our providers stopped sending
us status updates. Although that's not great, it's worse if our
code can't cope with the unexpectedly high volume.

This reuses the technique we have elsewhere [1] to keep processing
in batches until there's nothing left. Specifying a cutoff point
means the total amount of work to do can't keep growing.

[1]: 2fb432adaf/app/dao/notifications_dao.py (L441)
2021-12-13 17:14:28 +00:00
Ben Thorner
2adaaac3ae Remove redundant conditions for update query
Filtering by ID is enough, noting the other conditions were the
same between both queries.
2021-12-13 17:03:07 +00:00
Ben Thorner
c8ebb365d4 Make limit of DAO timeout function more obvious
We're going to iterate how we use the function with a limit, so we
shouldn't say it's "temporary" anymore. We don't need to change the
default, but having it in the function parameters makes it easier to
see the funtion doesn't time out all notifications, just some.
2021-12-13 17:01:41 +00:00
Ben Thorner
76aeab24ce Rewrite DAO timeout method to take cutoff_time
Previously we specified the period and calculated the cutoff time
in the function. Passing it in means we can run the method multiple
times and avoid getting "new" notifications to time out in the time
it takes to process each batch.
2021-12-13 16:56:21 +00:00
Ben Thorner
b81a66da50 Fix assertions in tests for timeout DAO function
Previously most of the assertions were being run *before* we had
actually called the function. There was also a redundant block of
assertions that just asserted the initial state of the test data.
2021-12-13 16:48:30 +00:00
Ben Thorner
3bcaf8330e Simplify comment for DAO timeout function 2021-12-13 16:39:55 +00:00
Ben Thorner
2fb432adaf Merge pull request #3383 from alphagov/email-sms-created-alert-180344153
Add new log / alert for 'created' email / SMS
2021-12-13 12:56:05 +00:00
David McDonald
c25585fd60 Merge pull request #3394 from alphagov/dont-count-pages
Improve response times for figuring out pagination links by doing it ourselves rather than use Flask-Sqlalchemy
2021-12-13 11:43:02 +00:00
David McDonald
eba625a9f5 Merge pull request #3389 from alphagov/improve-pagination-queries
Set `count_pages` as False to stop running of redundant query
2021-12-13 11:41:33 +00:00
Ben Thorner
6fe4cd932a Merge pull request #3396 from alphagov/bump-utils-177535141
Bump utils to 51.2.1
2021-12-13 09:58:29 +00:00
David McDonald
7d8eed8228 Optimise queries run for creating pagination links
We have been running in to the problem in
pallets/flask-sqlalchemy#518 where
our page loads very slow when viewing a single page of notifications
for a service in the admin app. Tracing this back and using SQL
explain analyze I can see that getting the notifications takes about
a second but the second query to count how many notifications there
are (to work out if there is a next page of pagination) can take up
to 100 seconds.

As suggested in that issue, we do the pagination ourselves.
Our pagination doesn't need us to know exactly how many notifications
there are, just whether there are any on the next page and that can
be done without running the slow query to count how many
notifications in total by using `count_pages=False`.

This commit is analagous to
c68d1a2f23

The only difference is that in that case, the pagination links are
used to show prev and/or next links in the admin app. In this case,
the pagination links are only used to see if there is a page 2, and
if there is, say that we are only showing the first 50 results.
2021-12-10 17:47:27 +00:00
Ben Thorner
a7560af9c4 Bump utils to 51.2.1
This includes performance improvements for RecipientCSV, which may
reduce the processing time in some edge cases - this depends on if
the Admin app rejects CSVs with these edge cases.
2021-12-10 16:38:28 +00:00
David McDonald
edadeb9131 Use get_prev_next_pagination_links when searching by to field
The only change in behaviour is that we are no longer including a
`last` pagination link.

This is OK because the frontend doesnt use it, just the prev and
next links as per
https://github.com/alphagov/notifications-admin/blob/master/app/main/views/jobs.py#L248
2021-12-10 12:29:55 +00:00
David McDonald
6ac4e67f78 Add test for pagination behaviour
We already have a test case for over 50 results, but this adds
one for 50 (ie a single page of results or less)
2021-12-10 12:29:12 +00:00
David McDonald
ec6ed3958c Move get_prev_next_pagination_links to utils
This will mean it can later be reused whereever we want
2021-12-10 12:26:57 +00:00
Pea Tyczynska
a74d1b026f Fix calculating remaining free allowance for SMS
The way it was done before, the remainder was incorrect in the
billing report and in the org usage query - it was the sms remainder
left at the start of the report period, not at the end of that period.

This became apparent when we tried to show sms_remainder on the org
usage report, where start date is always the start of the financial year.
We saw that sms sent by services did not reduce their free allowance
remainder according to the report. As a result of this, we had to
temporarily remove of sms_remainder column from the report, until
we fix the bug - it has been fixed now, yay!

I think the bug has snuck in partially because our fixtures for testing
this part of the code are quite complex, so it was
harder to see that numbers don't add up. I have added comments
to the tests to try and make it a bit clearer why the results are
as they are.

I also added comments to the code, and renamed some variables,
to make it easier to understand, as there are quite a few
moving parts in it - subqueries and the like.
I also renamed the fetch_sms_free_allowance_remainder method to
fetch_sms_free_allowance_remainder_until_date so it is clearer
what it does.
2021-12-09 18:58:10 +00:00
Ben Thorner
a8edfeb941 Remove command to replay callbacks
In response to [1].

I've already removed the runbook that referred to this.

[1]: https://github.com/alphagov/notifications-api/pull/3383#discussion_r765644576
2021-12-09 10:46:19 +00:00
David McDonald
1973994516 Merge pull request #3391 from alphagov/pagination-approach-change
Pagination approach change for `get_notifications_for_service`
2021-12-09 10:43:14 +00:00
Chris Hill-Scott
0481b14803 Merge pull request #3392 from alphagov/bump-util-51
Bump notifications-utils to 51.0.0
2021-12-06 16:51:38 +00:00
Ben Thorner
ab4cb029df Remove alert for email / sms in created
In response to [1].

[1]: https://github.com/alphagov/notifications-api/pull/3383#discussion_r759379988

It turns out the code that inspired this new alert - in the old
"timeout-sending-notifications" task - was actually redundant as
we already have a task to "replay" notifications still in "created",
which is much better than just alerting about them.

It's possible the replayed notifications will also fail, but in
both cases we should see some kind of error due to this, so I don't
think we're losing anything by not having an alert.
2021-12-06 14:11:42 +00:00
Ben Thorner
9bd2a9b427 Extract tests for conditionally creating callback
This will help ensure the function doesn't change arbitrarily, now
that it's used in multiple other places.
2021-12-06 14:11:41 +00:00
Ben Thorner
04da017558 DRY-up conditionally creating callback tasks
This removes 3 duplicate instances of the same code, which is still
tested implicitly via test_process_ses_receipt_tasks [1]. In the
next commit we'll make this test more explicit, to reflect that it's
now being reused elsewhere and shouldn't change arbitrarily.

We do lose the "print" statement from the command instance of the
code, but I think that's a very tolerable loss.

[1]: 16ec8ccb8a/tests/app/celery/test_process_ses_receipts_tasks.py (L94)
2021-12-06 14:11:34 +00:00
Ben Thorner
aea555fce2 Make test for timeout with callbacks consistent
This now matches the behaviour of the test above it: mocking out
the DAO function in order to focus on the specific behaviour of
the function under test.
2021-12-06 14:00:42 +00:00
Ben Thorner
5bf3fe6c0f Clarify no callbacks are sent in timeout test
This now complements the test below it, which we will refactor to
be consistent in the next commit.
2021-12-06 14:00:41 +00:00
Ben Thorner
8b7e81958d Delete duplicate 'timeout' tests for notifications
These scenarios are already covered by the DAO tests. It's enough
to just check the DAO function is called as expected.

While sometimes it can be better to have more end-to-end tests, the
convention across much of this app is to do unit tests.
2021-12-06 14:00:40 +00:00
Ben Thorner
c3e11d676f Remove unnecessary test_request_context manager
This doesn't affect how the tests run and just adds complexity.
2021-12-06 14:00:39 +00:00
Ben Thorner
05bd26d444 Fix names for a few tests in test_nightly_tasks.py
I find it really difficult to visually parse test files unless we
have a consistent convention for how we name our test functions.
In most of our tests the name of the test function starts with the
name of the function under test.
2021-12-06 14:00:38 +00:00