Correct the daily limits cache.

Last year we had an issue with the daily limit cache and the query that was populating it. As a result we have not been checking the daily limit properly. This PR should correct all that.

The daily limit cache is not being incremented in app.notifications.process_notifications.persist_notification, this method is and should always be the only method used to create a notification.
We increment the daily limit cache is redis is enabled (and it is always enabled for production) and the key type for the notification is team or normal.

We check if the daily limit is exceed in many places:
 - app.celery.tasks.process_job
 -  app.v2.notifications.post_notifications.post_notification
 - app.v2.notifications.post_notifications.post_precompiled_letter_notification
 - app.service.send_notification.send_one_off_notification
 - app.service.send_notification.send_pdf_letter_notification

If the daily limits cache is not found, set the cache to 0 with an expiry of 24 hours. The daily limit cache key is service_id-yyy-mm-dd-count, so each day a new cache is created.

The best thing about this PR is that the app.service_dao.fetch_todays_total_message_count query has been removed. This query was not performant and had been wrong for ages.
This commit is contained in:
Rebecca Law
2021-06-21 16:03:24 +01:00
parent c44ec57c17
commit 35b20ba363
8 changed files with 196 additions and 230 deletions

View File

@@ -21,6 +21,7 @@ from app.notifications.process_notifications import (
from app.serialised_models import SerialisedTemplate
from app.v2.errors import BadRequestError
from tests.app.db import create_api_key, create_service, create_template
from tests.conftest import set_config
def test_create_content_for_notification_passes(sample_email_template):
@@ -112,50 +113,47 @@ def test_persist_notification_throws_exception_when_missing_template(sample_api_
assert NotificationHistory.query.count() == 0
def test_cache_is_not_incremented_on_failure_to_persist_notification(sample_api_key, mocker):
mocked_redis = mocker.patch('app.redis_store.get')
mock_service_template_cache = mocker.patch('app.redis_store.get_all_from_hash')
def test_cache_is_not_incremented_on_failure_to_persist_notification(notify_api, sample_api_key, mocker):
mocked_redis = mocker.patch('app.redis_store.incr')
with pytest.raises(SQLAlchemyError):
persist_notification(template_id=None,
template_version=None,
recipient='+447111111111',
service=sample_api_key.service,
personalisation=None,
notification_type='sms',
api_key_id=sample_api_key.id,
key_type=sample_api_key.key_type)
with set_config(notify_api, 'REDIS_ENABLED', True):
persist_notification(template_id=None,
template_version=None,
recipient='+447111111111',
service=sample_api_key.service,
personalisation=None,
notification_type='sms',
api_key_id=sample_api_key.id,
key_type=sample_api_key.key_type)
mocked_redis.assert_not_called()
mock_service_template_cache.assert_not_called()
def test_persist_notification_does_not_increment_cache_if_test_key(
sample_template, sample_job, mocker, sample_test_api_key
notify_api, sample_template, sample_job, mocker, sample_test_api_key
):
mocker.patch('app.notifications.process_notifications.redis_store.get', return_value="cache")
mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash', return_value="cache")
daily_limit_cache = mocker.patch('app.notifications.process_notifications.redis_store.incr')
template_usage_cache = mocker.patch('app.notifications.process_notifications.redis_store.increment_hash_value')
assert Notification.query.count() == 0
assert NotificationHistory.query.count() == 0
persist_notification(
template_id=sample_template.id,
template_version=sample_template.version,
recipient='+447111111111',
service=sample_template.service,
personalisation={},
notification_type='sms',
api_key_id=sample_test_api_key.id,
key_type=sample_test_api_key.key_type,
job_id=sample_job.id,
job_row_number=100,
reference="ref",
)
assert Notification.query.count() == 1
with set_config(notify_api, 'REDIS_ENABLED', True):
persist_notification(
template_id=sample_template.id,
template_version=sample_template.version,
recipient='+447111111111',
service=sample_template.service,
personalisation={},
notification_type='sms',
api_key_id=sample_test_api_key.id,
key_type=sample_test_api_key.key_type,
job_id=sample_job.id,
job_row_number=100,
reference="ref",
)
assert not daily_limit_cache.called
assert not template_usage_cache.called
assert Notification.query.count() == 1
assert not daily_limit_cache.called
@freeze_time("2016-01-01 11:09:00.061258")
@@ -198,77 +196,48 @@ def test_persist_notification_with_optionals(sample_job, sample_api_key):
@freeze_time("2016-01-01 11:09:00.061258")
def test_persist_notification_doesnt_touch_cache_for_old_keys_that_dont_exist(notify_db_session, mocker):
service = create_service(restricted=True)
template = create_template(service=service)
api_key = create_api_key(service=service)
mock_incr = mocker.patch('app.notifications.process_notifications.redis_store.incr')
mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=None)
mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash', return_value=None)
persist_notification(
template_id=template.id,
template_version=template.version,
recipient='+447111111111',
service=template.service,
personalisation={},
notification_type='sms',
api_key_id=api_key.id,
key_type=api_key.key_type,
reference="ref"
)
mock_incr.assert_not_called()
@freeze_time("2016-01-01 11:09:00.061258")
def test_persist_notification_increments_cache_if_key_exists_and_for_trial_service(
notify_db_session, mocker
def test_persist_notification_increments_cache_for_trial_service(
notify_api, notify_db_session, mocker
):
service = create_service(restricted=True)
template = create_template(service=service)
api_key = create_api_key(service=service)
mock_incr = mocker.patch('app.notifications.process_notifications.redis_store.incr')
mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=1)
mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash',
return_value={template.id, 1})
with set_config(notify_api, 'REDIS_ENABLED', True):
persist_notification(
template_id=template.id,
template_version=template.version,
recipient='+447111111122',
service=template.service,
personalisation={},
notification_type='sms',
api_key_id=api_key.id,
key_type=api_key.key_type,
reference="ref2")
persist_notification(
template_id=template.id,
template_version=template.version,
recipient='+447111111122',
service=template.service,
personalisation={},
notification_type='sms',
api_key_id=api_key.id,
key_type=api_key.key_type,
reference="ref2")
mock_incr.assert_called_once_with(str(service.id) + "-2016-01-01-count", )
mock_incr.assert_called_once_with(str(service.id) + "-2016-01-01-count", )
def test_persist_notification_does_not_increments_cache_live_service(
notify_db_session, mocker
def test_persist_notification_increments_cache_live_service(
notify_api, notify_db_session, mocker
):
service = create_service(restricted=False)
template = create_template(service=service)
api_key = create_api_key(service=service)
mock_incr = mocker.patch('app.notifications.process_notifications.redis_store.incr')
mocker.patch('app.notifications.process_notifications.redis_store.get', return_value=1)
mocker.patch('app.notifications.process_notifications.redis_store.get_all_from_hash',
return_value={template.id, 1})
with set_config(notify_api, 'REDIS_ENABLED', True):
persist_notification(
template_id=template.id,
template_version=template.version,
recipient='+447111111122',
service=template.service,
personalisation={},
notification_type='sms',
api_key_id=api_key.id,
key_type=api_key.key_type,
reference="ref2")
persist_notification(
template_id=template.id,
template_version=template.version,
recipient='+447111111122',
service=template.service,
personalisation={},
notification_type='sms',
api_key_id=api_key.id,
key_type=api_key.key_type,
reference="ref2")
assert not mock_incr.called
assert mock_incr.called
@pytest.mark.parametrize((