Commit Graph

444 Commits

Author SHA1 Message Date
Rebecca Law
b8399b8b9b Add a where clause to join to NotificationHistory, this is some extra assurance that the Notification will not be deleted unless the history exists. 2019-06-03 11:47:02 +01:00
Rebecca Law
c23ae15f32 Remove insert to NotificationHistory
Fix all test failures
2019-05-31 16:52:22 +01:00
Rebecca Law
4154251970 Addd missing reference to the update statement. 2019-05-30 10:54:47 +01:00
Rebecca Law
3374e03ce9 Prepare to stop inserting NotificationHistory at the time of inserting a notificaiton.
Need to remove foreign key to complaints.
Make sure if getting Notification.id we look to both tables.
2019-05-21 16:08:18 +01:00
Rebecca Law
198fd21f7e Update Notification history if there is a mismatch in the number of notifications to be updated and the number actually updated. 2019-05-15 15:30:15 +01:00
Rebecca Law
43334d63f3 Stop updating NotificationHistory
Doing my bit to remove imports of fixtures.
2019-05-15 10:58:39 +01:00
Rebecca Law
d5d2b3d2a6 Update insert to use select_from - this allows the insert query to run as a single bulk insert and should be more efficient. 2019-05-02 13:46:15 +01:00
Rebecca Law
c9265aab68 Don't do anything if the query doesn't yield results. 2019-05-01 15:07:59 +01:00
Rebecca Law
0def0b7fd0 We want to staop inserting and updating NotificationHistory each time we insert/update Notification.
This PR adds a function to upsert (insert or update if exists) NotificationHistory all the rows from Notification that we are about to delete in the nightly task. This will happen just before the delete function. Since it is a upsert query the function can be called more than once.
This should allow us remove all the insert/updates to NotificationHistory.

However, there is a consern that this will double the length of time the tasks take. So do we do these upserts in a separate task or in the same one?
2019-05-01 14:26:11 +01:00
Rebecca Law
a53340b4d7 Update the query that gets the number of notifications that have been sent under 10 seconds to use Notifications rather than NotificationHistory.
Also removed a test that is not useful
2019-04-10 10:06:27 +01:00
Rebecca Law
e9607f227d Remove query that's no longer needed. 2019-03-29 15:38:48 +00:00
Leo Hemsted
38f0ea6cca remove functions to not talk about 7 days
remind us that data retention is flexible
2019-02-26 17:57:35 +00:00
Leo Hemsted
f00bfdfe85 move slow sms provider threshold from 10% to 20%
provider switching is a process that can happen as often as we like
without disrupting the flow of the system - however, there are some
reasons why we might not want to switch. One problem we've seen is
when a provider is having an issue, we might switch away from them
manually only for the app to automatically switch back to them again
and again.

Long term we'd like to have a system better suited for sharing the load
equally between our two sms providers, but short term, by increasing
the threshold for switching from 10% (of messages sent are slow) to
20%, we hope to make switching happen less often.

A notification is considered slow if it was sent in the last ten
minutes, on the current provider, and is either

* still in sending or pending after 4 minutes
* in delivered, but took at least 4 minutes to send
2019-02-25 14:29:39 +00:00
Leo Hemsted
a617ccca9d allow pending notifications to influence switchover.
Currently we switch if:

* status = delivered and updated_at - sent_at > threshold
* status = sending and now - sent_at > threshold

firetext can leave notifications in the pending state, which is
equivalent to sending in terms of how we should handle it, so this
commit changes the second case to allow pending as well as sending.
2019-02-21 16:30:42 +00:00
Leo Hemsted
0065afad11 downgrade error to info for notification not found for callback
When we get a callback from SES, we identify the notification by the
SES reference that we set on the notification after sending. When we
wrote the log message, we assumed that we'd always have a notification
for every callback, so if one couldn't be found we would raise an error
log. This isn't the case for a few reasons:

* We might receive a callback before the sender worker has persisted
  the reference to the database.
* We might have deleted the notification, especially if the service has
  a short data retention period
* We sometimes receive callbacks for references that we have no record
  of whatsoever (this is quite alarming but we have no way of knowing
  why this happens)

The error logs were happening pretty frequently, and we don't have a
real way to solve them at the moment, so lets cut down on noise and
downgrade them to info level for now.
2019-02-05 14:44:51 +00:00
Pea Tyczynska
3ce0024eec Remove unused functions for getting template statistics 2019-01-15 12:15:20 +00:00
Alexey Bezhan
47c403f6ab Don't return pagination links for API Message log requests
Flask-SQLAlchemy paginate function issues a separate query to get
the total count of rows for a given filter. This query (with
filters used by the API integration Message log page) is slow for
services with large number of notifications.

Since Message log page doesn't actually allow users to paginate
through the response (it only shows the last 50 messages) we can
use limit instead of paginate, which requires passing in another
flag from admin to the dao method.

`count` flag has been added to `paginate` in March 2018, however
there was no release of flask-sqlalchemy since then, so we need
to pull the dev version of the package from Github.
2019-01-08 13:22:27 +00:00
Leo Hemsted
021625abb3 make sure log line works if notification still in created 2019-01-03 17:08:17 +00:00
Leo Hemsted
2355ee011f log more info when we receive multiple delivery callbacks for one notification
Previously, we logged a warning containing the notification reference
and new status. However it wasn't a great message - this new one
includes the notification id, the old status, the time difference and
more.

This separates out logs for callbacks for notifications we don't know
(error level) and duplicates (info level).
2019-01-03 17:08:16 +00:00
Rebecca Law
941e14f71a Added the limit to the query for the services with data retention.
Also did a bit of refactoring.
2018-12-27 14:00:53 +00:00
Rebecca Law
8e832a1178 Adding a log message 2018-12-21 14:09:29 +00:00
Rebecca Law
62a8076161 Commit the deletes every 10,000 rows. 2018-12-21 13:57:35 +00:00
Rebecca Law
89923eab71 Updated the query to improve the performance.
The scheduled job for deleting emails has failed silently for many nights now.
2018-12-20 16:09:38 +00:00
Katie Smith
7d026ad385 wip 2018-12-20 12:31:00 +00:00
Rebecca Law
4bb68c3e52 Merge pull request #2249 from alphagov/improve-performance-of-platform-admin-page
Improve performance of platform stats page
2018-12-12 12:20:13 +00:00
Pea (Malgorzata Tyczynska)
8b4655d8af Merge pull request #2255 from alphagov/switch_on_slow_goes_live
Switch providers on slow delivery goes live
2018-12-12 11:35:18 +00:00
Pea Tyczynska
af185adf4c Log the ratio of slow notifications 2018-12-11 15:28:38 +00:00
Rebecca Law
dfc12cc354 Remove the join to TemplateHistory.
We are adding an index to Notifications to optimize the get_notifications_for_service. We need to build the index concurrently which can not be run inside a transaction block so the index will need to be run on the db directly.

CREATE INDEX CONCURRENTLY ix_notifications_service_created_at ON notifications (service_id, created_at);
DROP INDEX CONCURRENTLY ix_notifications_service_created_at
2018-12-11 14:57:10 +00:00
Pea (Malgorzata Tyczynska)
d7fcd564e0 Merge pull request #2250 from alphagov/switch_providers_update
Update switch providers on slow delivery method and query
2018-12-11 10:27:29 +00:00
Rebecca Law
5b90fd6fb0 Removed unused method 2018-12-10 16:27:59 +00:00
Pea Tyczynska
418060fbdb Update switch provider on slow delivery task to change max once evey 10 minutes 2018-12-05 15:56:16 +00:00
Pea Tyczynska
39ca5b9525 New query for finding if provider is slow
The delivery for provider is slow if more than threshold (currently
we pass in threshold 10%) either took x (for now 4) minutes to deliver,
or are still sending after that time. We look at all notifications
for current provider which are delivered or sending, and are not under
test key, for the last 10 minutes.

We are using created_at to establish if notifications are from last
10 minutes because we have an index on it, so the query is faster.

Also write tests for new is_delivery_slow_for_provider query
2018-12-05 15:51:40 +00:00
Katie Smith
365c462e93 Update get_notification_by_id to take an optional service_id
It can be useful to get a notification by id while checking that the
notification belongs to a given service. This changes the
get_notification_by_id DAO function to optionally also filter by
service_id so that we can check this.
2018-12-03 12:05:23 +00:00
Katie Smith
902e1b403a Update update_notification_status_by_id DAO function
Replaced `.with_for_lockmode()`, which is now deprecated, with
`.with_for_update() - https://docs.sqlalchemy.org/en/latest/orm/query.html#sqlalchemy.orm.query.Query.with_lockmode

The function should update any statuses that are not 'final', so added
`pending-virus-check` to the list of statuses that the function can
update.
2018-12-03 12:05:23 +00:00
Katie Smith
ff06d120e8 Bump notifications-utils to 3.7.0
Bumped notifications-utils to 3.7.0. Version 3.7.0 includes the
`convert_utc_to_bst` and `convert_bst_to_utc` functions and the
`LETTER_PROCESSING_DEADLINE` constant, so these have been removed from
this repo and anywhere using these has now been updated to get these
from `notifications-utils`.

Also bumped pytest by a patch version to bring in a bug fix.
2018-11-26 12:53:39 +00:00
Rebecca Law
00f04c33c8 Some minor refactoring.
- Updated notifications_dao.update_notification_status_by_id with an optional parameter to set the sent_by, this will eliminate a separate update to notifcaitons.
- Added the callback url to the log message, that way we can see if it's the same url failing.
- Stop sending the status callbacks for PENDING status.
2018-10-24 11:24:53 +01:00
Leo Hemsted
2ed50e760f Revert "Celery 4" 2018-10-09 13:27:49 +01:00
Leo Hemsted
6ca2b8277c import exception from botocore
boto (2) is no longer a dependency
2018-10-03 14:11:30 +01:00
Rebecca Law
f1b04193ca In this PR we remove trigger-letter-pdfs-for-day scheduled task and just call collate_letter_pdfs_for_day instead.
There was a datetime bug in the query which resulted in files not being sent to the postal provider.
The trigger-letter-pdfs-for-day task is no longer needed, so rather than fix the query just call collate_letter_pdfs_for_day directly.
Less code is always better.

Deployment considerations: I realized this is strictly not backwards compatible if the scheduled job is in progress and a task is on the queue that no longer exists. This is ok since we will deploy this well before 17:50.
2018-09-12 17:16:34 +01:00
Alexey Bezhan
18ab7f3337 Add updated history count to dao_update_notifications_by_reference
For returned letter updates most notifications won't exist in the
notifications table, so in order to find out whether the reference
matches any known letters we need to check the count of updated
history records.
2018-09-04 16:55:22 +01:00
Rebecca Law
f965322f25 Fixes to the delete letter notifications.
If there are no files to delete we won't get an excpetion.
Wrap the delete file in a try/except to avoid stopping the entire task.
Fix the missing slash for the file name.
2018-08-13 14:09:51 +01:00
Rebecca Law
2a7f3faec9 Merge branch 'master' into fix-delete-s3 2018-08-13 11:44:44 +01:00
Rebecca Law
eb2c878edd Fix bug with deleting the S3 file.
Removed the duplicate method.
2018-08-13 11:33:19 +01:00
Rebecca Law
ebb43082d5 Deal with letters that have not been sent, this can be test letters or letters in tech-failure. 2018-08-10 16:22:25 +01:00
Rebecca Law
f844a39ea6 Move the condition outside the method, remove the notification_type variable. 2018-08-10 13:11:23 +01:00
Rebecca Law
d0e9ab4972 If the notifications that are being deleted are letters then we need to delete the letter from s3 as well. 2018-08-08 16:20:25 +01:00
Rebecca Law
517608dff5 Update method to delete notifications that are a week old, to look at the days of retention set for the service.
If the service does not have the days of retention set, then use 7 days.
Added a method to get days of retention for a service and notificaiton type
2018-08-06 13:51:54 +01:00
Rebecca Law
019c6a4e3a Revert "Purge notifications for configured days of retention" 2018-08-03 14:35:36 +01:00
Rebecca Law
9674005c71 Updated the function to delete notifications over a week old to look at ServiceDataRetention to only delete notifications specified number of days for the notification type. 2018-07-23 09:56:03 +01:00
Katie Smith
b1cfa8942a Add one_off filter when getting all notifications for a service
Added the option to filter by one_off messages to the DAO function
`get_notifications_for_service`. Previously, one-off notifications
were not returned - this has changed so that the default is for
one-off notifications to be returned. Also simplified the `include_jobs`
filter for this function.

The DAO function gets used in 3 places - for the V1 and V2 API endpoints,
which will now start to return one-off messages. It also gets used by
the admin app which needs to pass in `include_one_off=False` to the
`get_all_notifications_for_service` where we don't want one-off
notifications to show, such as the API message log page.
2018-07-18 15:08:06 +01:00