split up _query_for_billing_data into three separate queries

the queries all return lots of columns, but each query has columns it
doesn't care about. eg emails don't have billable units or international
flag, letters don't have international flag, sms don't have a page count
etc. additionally, the query was grouping on things that never change,
like service id and notification type.

by making all of these literals (as in `select 1 as foo`) we see times
that are over 50% quicker for gov.uk email service.

Note: One of the tests changed because previously it involved emails and
sms with statuses that they could never be (eg returned-letter)
This commit is contained in:
Leo Hemsted
2020-02-19 10:58:06 +00:00
committed by Rebecca Law
parent f7f6be56c7
commit 0f6f2f1b91
4 changed files with 126 additions and 59 deletions

View File

@@ -166,6 +166,7 @@ def test_create_nightly_billing_for_day_different_templates(
multiplier = [0, 1]
billable_units = [0, 1]
rate = [0, Decimal(1.33)]
for i, record in enumerate(records):
assert record.bst_date == datetime.date(yesterday)
assert record.rate == rate[i]

View File

@@ -170,7 +170,7 @@ def test_fetch_billing_data_for_day_is_grouped_by_notification_type(notify_db_se
today = convert_utc_to_bst(datetime.utcnow())
results = fetch_billing_data_for_day(today.date())
assert len(results) == 3
notification_types = [x[2] for x in results if x[2] in ['email', 'sms', 'letter']]
notification_types = [x.notification_type for x in results]
assert len(notification_types) == 3
@@ -280,13 +280,13 @@ def test_fetch_billing_data_for_day_bills_correctly_for_status(notify_db_session
today = convert_utc_to_bst(datetime.utcnow())
results = fetch_billing_data_for_day(process_day=today.date(), service_id=service.id)
sms_results = [x for x in results if x[2] == 'sms']
email_results = [x for x in results if x[2] == 'email']
letter_results = [x for x in results if x[2] == 'letter']
assert 8 == sms_results[0][7]
assert 8 == email_results[0][7]
assert 3 == letter_results[0][7]
sms_results = [x for x in results if x.notification_type == 'sms']
email_results = [x for x in results if x.notification_type == 'email']
letter_results = [x for x in results if x.notification_type == 'letter']
# we expect as many rows as we check for notification types
assert 6 == sms_results[0].notifications_sent
assert 4 == email_results[0].notifications_sent
assert 3 == letter_results[0].notifications_sent
def test_get_rates_for_billing(notify_db_session):
create_rate(start_date=datetime.utcnow(), value=12, notification_type='email')