mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-05 02:41:14 -05:00
Merge pull request #3417 from alphagov/optimise-status-query-180693991
Optimise query to populate notification statuses
This commit is contained in:
@@ -10,6 +10,8 @@ from sqlalchemy.types import DateTime, Integer
|
|||||||
from app import db
|
from app import db
|
||||||
from app.dao.dao_utils import autocommit
|
from app.dao.dao_utils import autocommit
|
||||||
from app.models import (
|
from app.models import (
|
||||||
|
KEY_TYPE_NORMAL,
|
||||||
|
KEY_TYPE_TEAM,
|
||||||
KEY_TYPE_TEST,
|
KEY_TYPE_TEST,
|
||||||
NOTIFICATION_CANCELLED,
|
NOTIFICATION_CANCELLED,
|
||||||
NOTIFICATION_CREATED,
|
NOTIFICATION_CREATED,
|
||||||
@@ -72,7 +74,7 @@ def query_for_fact_status_data(table, start_date, end_date, notification_type, s
|
|||||||
table.created_at < end_date,
|
table.created_at < end_date,
|
||||||
table.notification_type == notification_type,
|
table.notification_type == notification_type,
|
||||||
table.service_id == service_id,
|
table.service_id == service_id,
|
||||||
table.key_type != KEY_TYPE_TEST
|
table.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
|
||||||
).group_by(
|
).group_by(
|
||||||
table.template_id,
|
table.template_id,
|
||||||
table.service_id,
|
table.service_id,
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ from app.dao.fact_notification_status_dao import (
|
|||||||
)
|
)
|
||||||
from app.models import (
|
from app.models import (
|
||||||
EMAIL_TYPE,
|
EMAIL_TYPE,
|
||||||
|
KEY_TYPE_NORMAL,
|
||||||
KEY_TYPE_TEAM,
|
KEY_TYPE_TEAM,
|
||||||
KEY_TYPE_TEST,
|
KEY_TYPE_TEST,
|
||||||
LETTER_TYPE,
|
LETTER_TYPE,
|
||||||
@@ -63,7 +64,11 @@ def test_update_fact_notification_status(notify_db_session):
|
|||||||
# 2nd service email has 3 day data retention - data has been moved to history and doesn't exist in notifications
|
# 2nd service email has 3 day data retention - data has been moved to history and doesn't exist in notifications
|
||||||
create_notification_history(template=second_template, status='temporary-failure')
|
create_notification_history(template=second_template, status='temporary-failure')
|
||||||
|
|
||||||
create_notification(template=third_template, status='sending')
|
# team API key notifications are included
|
||||||
|
create_notification(template=third_template, status='sending', key_type=KEY_TYPE_TEAM)
|
||||||
|
|
||||||
|
# test notifications are ignored
|
||||||
|
create_notification(template=third_template, status='sending', key_type=KEY_TYPE_TEST)
|
||||||
|
|
||||||
# these created notifications from a different day get ignored
|
# these created notifications from a different day get ignored
|
||||||
with freeze_time(datetime.combine(date.today() - timedelta(days=4), time.min)):
|
with freeze_time(datetime.combine(date.today() - timedelta(days=4), time.min)):
|
||||||
@@ -87,6 +92,7 @@ def test_update_fact_notification_status(notify_db_session):
|
|||||||
assert new_fact_data[0].notification_type == 'email'
|
assert new_fact_data[0].notification_type == 'email'
|
||||||
assert new_fact_data[0].notification_status == 'temporary-failure'
|
assert new_fact_data[0].notification_status == 'temporary-failure'
|
||||||
assert new_fact_data[0].notification_count == 1
|
assert new_fact_data[0].notification_count == 1
|
||||||
|
assert new_fact_data[0].key_type == KEY_TYPE_NORMAL
|
||||||
|
|
||||||
assert new_fact_data[1].bst_date == process_day
|
assert new_fact_data[1].bst_date == process_day
|
||||||
assert new_fact_data[1].template_id == third_template.id
|
assert new_fact_data[1].template_id == third_template.id
|
||||||
@@ -95,6 +101,7 @@ def test_update_fact_notification_status(notify_db_session):
|
|||||||
assert new_fact_data[1].notification_type == 'letter'
|
assert new_fact_data[1].notification_type == 'letter'
|
||||||
assert new_fact_data[1].notification_status == 'sending'
|
assert new_fact_data[1].notification_status == 'sending'
|
||||||
assert new_fact_data[1].notification_count == 1
|
assert new_fact_data[1].notification_count == 1
|
||||||
|
assert new_fact_data[1].key_type == KEY_TYPE_TEAM
|
||||||
|
|
||||||
assert new_fact_data[2].bst_date == process_day
|
assert new_fact_data[2].bst_date == process_day
|
||||||
assert new_fact_data[2].template_id == first_template.id
|
assert new_fact_data[2].template_id == first_template.id
|
||||||
@@ -103,6 +110,7 @@ def test_update_fact_notification_status(notify_db_session):
|
|||||||
assert new_fact_data[2].notification_type == 'sms'
|
assert new_fact_data[2].notification_type == 'sms'
|
||||||
assert new_fact_data[2].notification_status == 'delivered'
|
assert new_fact_data[2].notification_status == 'delivered'
|
||||||
assert new_fact_data[2].notification_count == 1
|
assert new_fact_data[2].notification_count == 1
|
||||||
|
assert new_fact_data[2].key_type == KEY_TYPE_NORMAL
|
||||||
|
|
||||||
|
|
||||||
def test__update_fact_notification_status_updates_row(notify_db_session):
|
def test__update_fact_notification_status_updates_row(notify_db_session):
|
||||||
|
|||||||
Reference in New Issue
Block a user