Skip to content

Commit dbc34ed

Browse files
committed
switch dashboard queries to use new bulk DB session
This change routes dashboard queries to the new 'bulk' database via the . We are starting with the dashboards as the first real-world test of the replica connection. This is a low-risk change that can be easily rolled back if users see any issues.
1 parent 6a16d15 commit dbc34ed

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

app/dao/fact_notification_status_dao.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,12 @@ def fetch_notification_status_for_service_for_day(bst_day, service_id):
135135
)
136136

137137

138-
def fetch_notification_status_for_service_for_today_and_7_previous_days(service_id, by_template=False, limit_days=7):
138+
def fetch_notification_status_for_service_for_today_and_7_previous_days(
139+
service_id, by_template=False, limit_days=7, session=db.session
140+
):
139141
start_date = midnight_n_days_ago(limit_days)
140142
now = datetime.utcnow()
141-
stats_for_7_days = db.session.query(
143+
stats_for_7_days = session.query(
142144
FactNotificationStatus.notification_type.label("notification_type"),
143145
FactNotificationStatus.notification_status.label("status"),
144146
*([FactNotificationStatus.template_id.label("template_id")] if by_template else []),
@@ -150,7 +152,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(service_
150152
)
151153

152154
stats_for_today = (
153-
db.session.query(
155+
session.query(
154156
Notification.notification_type.cast(db.Text),
155157
Notification.status,
156158
*([Notification.template_id] if by_template else []),
@@ -169,7 +171,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(service_
169171
all_stats_table = stats_for_7_days.union_all(stats_for_today).subquery()
170172

171173
aggregation = (
172-
db.session.query(
174+
session.query(
173175
*([all_stats_table.c.template_id] if by_template else []),
174176
all_stats_table.c.notification_type,
175177
all_stats_table.c.status,
@@ -183,7 +185,7 @@ def fetch_notification_status_for_service_for_today_and_7_previous_days(service_
183185
.subquery()
184186
)
185187

186-
query = db.session.query(
188+
query = session.query(
187189
*(
188190
[Template.name.label("template_name"), Template.is_precompiled_letter, aggregation.c.template_id]
189191
if by_template

app/template_statistics/rest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from flask import Blueprint, jsonify, request
22

3+
from app import db
34
from app.dao.fact_notification_status_dao import (
45
fetch_notification_status_for_service_for_today_and_7_previous_days,
56
)
@@ -26,7 +27,7 @@ def get_template_statistics_for_service_by_day(service_id):
2627
if whole_days < 0 or whole_days > 7:
2728
raise InvalidRequest({"whole_days": ["whole_days must be between 0 and 7"]}, status_code=400)
2829
data = fetch_notification_status_for_service_for_today_and_7_previous_days(
29-
service_id, by_template=True, limit_days=whole_days
30+
service_id, by_template=True, limit_days=whole_days, session=db.session_bulk
3031
)
3132

3233
return jsonify(

tests/app/dao/test_fact_notification_status_dao.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import pytest
66
from freezegun import freeze_time
77

8+
from app import db
89
from app.constants import (
910
EMAIL_TYPE,
1011
KEY_TYPE_TEAM,
@@ -41,6 +42,7 @@
4142
create_service,
4243
create_template,
4344
)
45+
from tests.utils import QueryRecorder
4446

4547

4648
def test_fetch_notification_status_for_service_by_month(notify_db_session):
@@ -134,7 +136,16 @@ def test_fetch_notification_status_for_service_for_day(notify_db_session):
134136

135137

136138
@freeze_time("2018-10-31T18:00:00")
137-
def test_fetch_notification_status_for_service_for_today_and_7_previous_days(notify_db_session):
139+
@pytest.mark.parametrize(
140+
"sess,expected_bind_key",
141+
(
142+
(db.session, None),
143+
(db.session_bulk, "bulk"),
144+
),
145+
)
146+
def test_fetch_notification_status_for_service_for_today_and_7_previous_days(
147+
notify_db_session, sess, expected_bind_key
148+
):
138149
service_1 = create_service(service_name="service_1")
139150
sms_template = create_template(service=service_1, template_type=SMS_TYPE)
140151
sms_template_2 = create_template(service=service_1, template_type=SMS_TYPE)
@@ -154,10 +165,14 @@ def test_fetch_notification_status_for_service_for_today_and_7_previous_days(not
154165
# too early, shouldn't be included
155166
create_notification(service_1.templates[0], created_at=datetime(2018, 10, 30, 12, 0, 0), status="delivered")
156167

157-
results = sorted(
158-
fetch_notification_status_for_service_for_today_and_7_previous_days(service_1.id),
159-
key=lambda x: (x.notification_type, x.status),
160-
)
168+
service_id = service_1.id
169+
with QueryRecorder() as query_recorder:
170+
results = sorted(
171+
fetch_notification_status_for_service_for_today_and_7_previous_days(service_id=service_id, session=sess),
172+
key=lambda x: (x.notification_type, x.status),
173+
)
174+
175+
assert {query_info.bind_key for query_info in query_recorder.queries} == {expected_bind_key}
161176

162177
assert len(results) == 4
163178

tests/app/template_statistics/test_rest.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ def test_get_template_statistics_for_service_by_day_accepts_old_query_string(
6262

6363

6464
@freeze_time("2018-01-02 12:00:00")
65-
def test_get_template_statistics_for_service_by_day_goes_to_db(admin_request, mocker, sample_template):
65+
def test_get_template_statistics_for_service_by_day_goes_to_db(
66+
admin_request, mocker, sample_template, notify_db_session_bulk
67+
):
6668
# first time it is called redis returns data, second time returns none
6769
mock_dao = mocker.patch(
6870
"app.template_statistics.rest.fetch_notification_status_for_service_for_today_and_7_previous_days",
@@ -94,7 +96,9 @@ def test_get_template_statistics_for_service_by_day_goes_to_db(admin_request, mo
9496
}
9597
]
9698
# dao only called for 2nd, since redis returned values for first call
97-
mock_dao.assert_called_once_with(str(sample_template.service_id), limit_days=1, by_template=True)
99+
mock_dao.assert_called_once_with(
100+
str(sample_template.service_id), limit_days=1, by_template=True, session=notify_db_session_bulk
101+
)
98102

99103

100104
def test_get_template_statistics_for_service_by_day_returns_empty_list_if_no_templates(

0 commit comments

Comments
 (0)