Remove validate_and_format for email and phone numbers, using normalised_to instead because that function has already happened when persisting the notificaiton.

Remove 2 extra select queries after the update and commit. Once a transaction is committed SQLAlchemy will query for the db model if referenced after a commit.
This commit is contained in:
Rebecca Law
2021-02-11 13:51:15 +00:00
parent 8f99da525d
commit 2270832873
2 changed files with 21 additions and 67 deletions

View File

@@ -3,10 +3,7 @@ from urllib import parse
from datetime import datetime, timedelta
from cachetools import TTLCache, cached
from flask import current_app
from notifications_utils.recipients import (
validate_and_format_phone_number,
validate_and_format_email_address
)
from notifications_utils.template import HTMLEmailTemplate, PlainTextEmailTemplate, SMSMessageTemplate
from app import notification_provider_clients, statsd_client, create_uuid
@@ -35,7 +32,7 @@ from app.models import (
def send_sms_to_provider(notification):
service = notification.service
service_id = service.id
if not service.active:
technical_failure(notification=notification)
return
@@ -52,14 +49,14 @@ def send_sms_to_provider(notification):
show_prefix=service.prefix_sms,
)
created_at = notification.created_at
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
update_notification_to_sending(notification, provider)
send_sms_response(provider.get_name(), str(notification.id), notification.to)
else:
try:
provider.send_sms(
to=validate_and_format_phone_number(notification.to, international=notification.international),
to=notification.normalised_to,
content=str(template),
reference=str(notification.id),
sender=notification.reply_to_text
@@ -73,14 +70,14 @@ def send_sms_to_provider(notification):
notification.billable_units = template.fragment_count
update_notification_to_sending(notification, provider)
delta_seconds = (datetime.utcnow() - notification.created_at).total_seconds()
delta_seconds = (datetime.utcnow() - created_at).total_seconds()
statsd_client.timing("sms.total-time", delta_seconds)
if notification.key_type == KEY_TYPE_TEST:
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 str(service_id) in current_app.config.get('HIGH_VOLUME_SERVICE'):
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)
@@ -88,6 +85,7 @@ def send_sms_to_provider(notification):
def send_email_to_provider(notification):
service = notification.service
service_id = service.id
if not service.active:
technical_failure(notification=notification)
return
@@ -107,6 +105,7 @@ def send_email_to_provider(notification):
values=notification.personalisation
)
created_at = notification.created_at
if service.research_mode or notification.key_type == KEY_TYPE_TEST:
notification.reference = str(create_uuid())
update_notification_to_sending(notification, provider)
@@ -114,27 +113,24 @@ def send_email_to_provider(notification):
else:
from_address = '"{}" <{}@{}>'.format(service.name, service.email_from,
current_app.config['NOTIFY_EMAIL_DOMAIN'])
email_reply_to = notification.reply_to_text
reference = provider.send_email(
from_address,
validate_and_format_email_address(notification.to),
notification.normalised_to,
plain_text_email.subject,
body=str(plain_text_email),
html_body=str(html_email),
reply_to_address=validate_and_format_email_address(email_reply_to) if email_reply_to else None,
reply_to_address=notification.reply_to_text,
)
notification.reference = reference
update_notification_to_sending(notification, provider)
delta_seconds = (datetime.utcnow() - notification.created_at).total_seconds()
delta_seconds = (datetime.utcnow() - created_at).total_seconds()
if notification.key_type == KEY_TYPE_TEST:
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 str(service_id) in current_app.config.get('HIGH_VOLUME_SERVICE'):
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)