Commit Graph

7355 Commits

Author SHA1 Message Date
Pea Tyczynska
efdaadbdf4 Do not update notification to sending if the status is already final
This prevents a race condition when we get delivery receipt before
updating notification to sending, and so the sending status would
supersede the delivered status, and the notification would time out
as temporary-failure after three days.
2020-07-03 17:19:03 +01:00
Pea Tyczynska
e02508d6f7 Turn off the sms stub and email stub in staging 2020-07-02 17:23:50 +01:00
David McDonald
7e4acda633 Merge pull request #2910 from alphagov/inbound-sms-metric
Add prometheus client metric for number of inbound text messages
2020-06-30 11:49:57 +01:00
Leo Hemsted
c04c79a0a7 Merge pull request #2907 from alphagov/broadcast-permission
broadcast service permission
2020-06-30 10:15:17 +01:00
David McDonald
1e253b2257 Add prometheus client metric for number of inbound text messages
As we gradually move from statsd to prometheus, we change the metric to
be a prometheus metric rather than statsd.

The change worth pointing out is that we have dropped the 'successful'
and 'failed' statuses from the metrics. I don't think it's useful to
have these statuses. It's very rare for an inbound message to fail when
we receive it and when it does, we raise an error and see it in our
logs. We aren't going to be looking at a graph of it as it's a rare
event, not typical behaviour that we want to monitor with a graph.
2020-06-30 10:13:55 +01:00
Chris Hill-Scott
ab3d25ea91 Merge pull request #2909 from alphagov/fix-international-one-off
Fix sending one-off international text messages
2020-06-30 09:29:13 +01:00
Chris Hill-Scott
5ef9ad2953 Fix sending one-off international text messages
This was broken because sometimes `service.permissions` is a list of
strings (for when we’re caching the service object) and sometimes it’s a
list of permission objects (when we’re dealing with ORM objects).

Because the validator code is shared, the least-messy way to fix it is
to make sure it can handle both types.

It can’t just take a list of permissions as argument, because it uses
other fields on the service.

It would be messy to rewrite the endpoint to use a serialised service
because the tests all expect to be dealing with database objects, so it
would be a faff to change what they’re mocking.
2020-06-29 14:43:33 +01:00
Chris Hill-Scott
40f09097c8 Merge pull request #2908 from alphagov/fix-template-history-created-by
Fix template history created by
2020-06-29 13:12:44 +01:00
Chris Hill-Scott
bf6e468a7c Keep excluding created_by from TemplateSchemaNoDetail 2020-06-29 12:48:24 +01:00
David McDonald
7d68280445 Merge pull request #2901 from alphagov/turn-off-sync-statsd-for-posts
Turn off statsd wrapper for synchronous statsd calls during POSTs
2020-06-29 12:47:49 +01:00
David McDonald
12f460adc5 Turn off statsd wrapper for synchronous statsd calls during POSTs
This commit turns off StatsD metrics for the following
- the `dao_create_notification` function
- the `user-agent` metric
- the response times and response codes per flask endpoint

This has been done with the purpose of not having the creation of text
messages or emails call out to StatsD during the request process. These
are the three current metrics that are currently called during the
processing of one of those requests and so have been removed from the
API.

The reason for removing the calls out to StatsD when processing a
request to send a notification is that we have seen two incidents
recently affected by DNS resolution for StatsD (either by a slow down in
resolution time or a failure to resolve). These POST requests are our
most critical code path and we don't want them to be affected by any
potential unforeseen trouble with StatsD DNS resolution.

We are not going to miss the removal of these metrics.
- the `dao_create_notification` metric is rarely/never looked at
- the `user-agent` metric is rarely/never looked at and can be got from
  our logs if we want it
- the response times and response codes per flask endpoint are already
  exposed using the gds metrics python library

I did not remove the statsd metrics from any other parts of the API
because
- As the POST notification endpoints are the main source of web traffic,
  we should have already removed most calls to StatsD which should
  greatly reduce the chance of their being any further issues with
  DNS resolution
- Some of the other metrics still provide value so no point deleting
  them if we don't need to
- The metrics on celery tasks will not affect any HTTP requests from
  users as they are async and also we do not currently have the
  infrastructure set up to replace them with a prometheus HTTP endpoint that
  can be scraped so this would require more work
2020-06-29 12:40:22 +01:00
Chris Hill-Scott
f541ad42d6 Revert "Avoid extra query when serialising Template created_by"
This reverts commit 58a9862cd1.

That commit tried to optimize the fetch template query by

However it had the side effect of making Marshmallow ignore `created_by`
when loading the JSON in the post request. So the field on the model was
never being updated, just copied from the original template.

The quick way of getting things to work again is to revert this
optimisation.

There’s probably some Marshmallow magic we could use to avoid the extra
query and still have created_by not be ignored.

It does mean we’re introducing an extra query, but I’m not too fussed
about that since the caching seems to be working well.
2020-06-29 12:30:08 +01:00
Chris Hill-Scott
02e5ff61a4 Test that template history updates created by 2020-06-29 12:30:08 +01:00
Leo Hemsted
63f6d8df6b broadcast service permission
it'll eventually be a template type too, so add the _TYPE suffix to the var name to make refactoring easier
2020-06-29 11:53:57 +01:00
Chris Hill-Scott
a0a5cde15e Merge pull request #2906 from alphagov/fix-callback-serialisation
Fix serialisation of callbacks
2020-06-26 16:52:26 +01:00
Chris Hill-Scott
8def7d0d3b Fix serialisation of callbacks
Because the IDs of our callback and inbound SMS APIs were stored in
lists instead of directly on the serialised model they weren’t getting
cast to a string before trying to JSONify them. And JSON doesn’t know
what to do with a UUID object.

For some reason this was only affecting the endpoint for fetching
inbound SMS.
2020-06-26 16:31:49 +01:00
Chris Hill-Scott
8cef34d770 Merge pull request #2904 from alphagov/validate-from-serialised-model
Validate from serialised model
2020-06-26 15:35:18 +01:00
Chris Hill-Scott
aa0c019fea Merge pull request #2896 from alphagov/get-letter-data-from-filenames
Get letter data for provided filenames
2020-06-26 14:51:19 +01:00
Chris Hill-Scott
9f41e77bf7 Add rate_limit and message_limit to SerialisedService
The API needs these to check whether a service can send a notification.
This commit also updates all the tests in `test_validators.py` to take
a serialised service, not a database object.
2020-06-26 14:10:25 +01:00
Chris Hill-Scott
1f315b06e2 Revert "Revert "Merge pull request #2902 from alphagov/fix-imports""
This reverts commit b8fe7b8e61.
2020-06-26 14:10:21 +01:00
Chris Hill-Scott
3ffdb3093b Revert "Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things""
This reverts commit 7e85e37e1d.
2020-06-26 14:10:12 +01:00
Chris Hill-Scott
6d52d733a4 Merge pull request #2903 from alphagov/revert-serialisation-changes
Revert serialisation changes
2020-06-26 14:07:58 +01:00
Chris Hill-Scott
59aba018bd Ensure rate limit is in serialised service
Once we start using the serialised service to power the `POST`
notifications endpoint it needs to include rate limit to do the rate
limit checks.
2020-06-26 13:46:32 +01:00
Chris Hill-Scott
7e85e37e1d Revert "Merge pull request #2887 from alphagov/cache-the-serialised-things"
This reverts commit b8c2c6b291, reversing
changes made to 351aca2c5a.
2020-06-26 13:42:44 +01:00
Chris Hill-Scott
b8fe7b8e61 Revert "Merge pull request #2902 from alphagov/fix-imports"
This reverts commit e00c0355b6, reversing
changes made to 832b589980.
2020-06-26 13:42:22 +01:00
Chris Hill-Scott
e00c0355b6 Merge pull request #2902 from alphagov/fix-imports
Fix circular import
2020-06-26 12:25:42 +01:00
Chris Hill-Scott
616523bf74 Fix circular import
We changed auth.py to import from app.serialised_models here:
https://github.com/alphagov/notifications-api/pull/2887/files#diff-77cbb1e03185c7319f0311371c438b0cR11

`serialised_models.py` imports from `templates_dao.py`

`templates_dao.py` imports from `users_dao.py`

`users_dao.py` imports from `errors.py`

`errors.py` imports from `auth.py` … and the circle is complete 💥

For some reason this caused the Celery workers to crash on startup, but
not the app. Which I guess is why the integration tests didn’t catch
this?
2020-06-26 10:20:32 +01:00
Chris Hill-Scott
832b589980 Merge pull request #2898 from alphagov/fix-log-line
remove whitespace from log line
2020-06-26 09:18:54 +01:00
Chris Hill-Scott
b8c2c6b291 Merge pull request #2887 from alphagov/cache-the-serialised-things
Serialise and cache services and API keys
2020-06-26 09:18:45 +01:00
Rebecca Law
351aca2c5a Merge pull request #2897 from alphagov/get-rid-of-scheduled_for
Remove the use of schedule_for in post_notifications
2020-06-26 07:51:17 +01:00
Leo Hemsted
6318cd2a84 remove whitespace from log line
multi line strings don't handle indentation
2020-06-25 14:25:04 +01:00
Leo Hemsted
3af4974757 Merge pull request #2900 from alphagov/disable-statsd
disable statsd
2020-06-24 16:44:17 +01:00
Leo Hemsted
bb05dcf221 disable statsd 2020-06-24 16:35:44 +01:00
Rebecca Law
ce32e577b7 Remove the use of schedule_for in post_notifications.
Years ago we started to implement a way to schedule a notification. We hit a problem but we never came up with a good solution and the feature never made it back to the top of the priority list.

This PR removes the code for scheduled_for. There will be another PR to drop the scheduled_notifications table and remove the schedule_notifications service permission

Unfortunately, I don't think we can remove the `scheduled_for` attribute from the notification.serialized method because out clients might fail if something is missing. For now I have left it in but defaulted the value to None.
2020-06-24 14:54:40 +01:00
Pea Tyczynska
d506451d4f Include template id, too 2020-06-24 12:20:40 +01:00
Chris Hill-Scott
d108c644bc Merge pull request #2890 from alphagov/exclude-service-letter-contacts
Don’t return letter contact blocks in service JSON
2020-06-24 09:41:08 +01:00
Chris Hill-Scott
d16d06fdef Cache serialised services in Redis
Same as we’re doing for templates.

This means avoiding a database call, even for services that don’t hit
our API so often.

They’ll still need to go to the database for the API keys, because we’re
not comfortable putting the API key secrets in Redis.

But once a service has got its keys from the database we commit the
transaction, so the connection can be freed up until we need it again to
insert the notification.
2020-06-24 08:52:12 +01:00
Chris Hill-Scott
6c0a4abd52 Serialise UUIDs to string
So that we can serialise them to JSON to store in Redis.
2020-06-24 08:51:54 +01:00
Chris Hill-Scott
6a9818b5fd Cache services and API keys in memory
Same as we’ve done for templates.

For high volume services this should mean avoiding calls to external
services, either the database or Redis.

TTL is set to 2 seconds, so that’s the maximum time it will take for
revoking an API key or renaming a service to propagate.

Some of the tests created services with the same service ID. This
caused intermittent failures because the cache relies on unique service
IDs (like we have in the real world) to key itself.
2020-06-24 08:46:13 +01:00
Pea Tyczynska
4b2a0037e3 Get letter data for provided filenames 2020-06-23 17:45:05 +01:00
Chris Hill-Scott
46dbcb7e36 Commit transactions as soon no longer needed
We think that holding open database transactions while we go and do
something else is causing us to have poor performance.

Because we’re not serialising everything as soon as we pull it out of
the database we can guarantee that we don’t need to go back to the
database again.

So let’s see if explicitly closing the transaction helps with
performance.
2020-06-23 16:00:41 +01:00
Chris Hill-Scott
320bca70f7 Serialise service, API keys and permissions
By serialising these straight away we can:
- not go back to the database later, potentially closing the connection
  sooner
- potentially cache the serialised data, meaning we don’t touch the
  database at all
2020-06-23 16:00:41 +01:00
Chris Hill-Scott
d7b2cc6403 Merge pull request #2895 from alphagov/template-schema-cache
Ensure templates are cached with correct schema
2020-06-23 15:23:32 +01:00
Chris Hill-Scott
5ae3b0fc64 Ensure templates are cached with correct schema
For some reason our V1 get template response wraps the whole template in
a dictionary with one key, `'data'`:
0d99033889/app/template/rest.py (L166)

That means when the admin app caches the response it also caches it in
this format.

The API needs to do the same, otherwise it will be cacheing data with a
schema that the admin app isn’t expecting, and vice-versa.
2020-06-23 15:09:38 +01:00
Chris Hill-Scott
0d99033889 Merge pull request #2891 from alphagov/cache-serialised-template
Cache serialised template in Redis and in memory
2020-06-23 14:18:01 +01:00
Chris Hill-Scott
af1b021dbe Add test for when template is found in Redis
Ensures that we’re not calling the dao method when it is
2020-06-23 14:01:20 +01:00
Chris Hill-Scott
cb4b809131 Add extra assertion
To be crystal clear 💎
2020-06-23 13:48:55 +01:00
David McDonald
a2753eafa9 Merge pull request #2894 from alphagov/turn-on-stub
Turn on email stub for load testing
2020-06-23 12:17:09 +01:00
Pea M. Tyczynska
75a7e9b7fd Merge pull request #2892 from alphagov/refactor-crown-dependency-check
Refactor crown dependency check
2020-06-23 11:19:17 +01:00
David McDonald
48abc8ffb5 Turn on email stub for load testing 2020-06-23 11:09:24 +01:00