From 57249b43c8db483885e68ed67d90a3cc083938ec Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Wed, 2 Jun 2021 10:58:57 +0100 Subject: [PATCH] Refactor high volume into serialised service model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Just looks a bit tidier and less repetitive. I’ve only done this for the serialised service because: - we’re only checking this in places where we’re already using the serialised service - if we want to check this elsewhere there’s a good chance that new code should be using the serialised service, since it’ll itself be doing some kind of performance optimisation --- app/delivery/send_to_providers.py | 4 ++-- app/serialised_models.py | 5 +++++ app/v2/notifications/post_notifications.py | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/delivery/send_to_providers.py b/app/delivery/send_to_providers.py index 0a0012c92..23d7f4f0e 100644 --- a/app/delivery/send_to_providers.py +++ b/app/delivery/send_to_providers.py @@ -87,7 +87,7 @@ def send_sms_to_provider(notification): statsd_client.timing("sms.test-key.total-time", delta_seconds) else: statsd_client.timing("sms.live-key.total-time", delta_seconds) - if str(service.id) in current_app.config.get('HIGH_VOLUME_SERVICE'): + if service.high_volume: statsd_client.timing("sms.live-key.high-volume.total-time", delta_seconds) else: statsd_client.timing("sms.live-key.not-high-volume.total-time", delta_seconds) @@ -142,7 +142,7 @@ def send_email_to_provider(notification): statsd_client.timing("email.test-key.total-time", delta_seconds) else: statsd_client.timing("email.live-key.total-time", delta_seconds) - if str(service.id) in current_app.config.get('HIGH_VOLUME_SERVICE'): + if service.high_volume: statsd_client.timing("email.live-key.high-volume.total-time", delta_seconds) else: statsd_client.timing("email.live-key.not-high-volume.total-time", delta_seconds) diff --git a/app/serialised_models.py b/app/serialised_models.py index 45437dd4c..6653960ba 100644 --- a/app/serialised_models.py +++ b/app/serialised_models.py @@ -3,6 +3,7 @@ from functools import partial from threading import RLock import cachetools +from flask import current_app from notifications_utils.clients.redis import RequestCache from notifications_utils.serialised_model import ( SerialisedModel, @@ -106,6 +107,10 @@ class SerialisedService(SerialisedModel): def api_keys(self): return SerialisedAPIKeyCollection.from_service_id(self.id) + @property + def high_volume(self): + return self.id in current_app.config['HIGH_VOLUME_SERVICE'] + class SerialisedAPIKey(SerialisedModel): ALLOWED_PROPERTIES = { diff --git a/app/v2/notifications/post_notifications.py b/app/v2/notifications/post_notifications.py index 89f6333b1..40cee1678 100644 --- a/app/v2/notifications/post_notifications.py +++ b/app/v2/notifications/post_notifications.py @@ -211,7 +211,7 @@ def process_sms_or_email_notification( template_with_content=template_with_content ) - if service.id in current_app.config.get('HIGH_VOLUME_SERVICE') \ + if service.high_volume \ and api_user.key_type == KEY_TYPE_NORMAL \ and notification_type in [EMAIL_TYPE, SMS_TYPE]: # Put service with high volumes of notifications onto a queue