Query to get letter and sheet volumes

So we can send daily email with these volumes to DVLA.
This commit is contained in:
Pea Tyczynska
2021-02-22 17:23:49 +00:00
parent 88233543c3
commit c8ffebcce8
2 changed files with 50 additions and 0 deletions

View File

@@ -772,6 +772,22 @@ def dao_get_letters_to_be_printed(print_run_deadline, postage, query_limit=10000
return notifications
def dao_get_letters_and_sheets_volume_by_postage(print_run_deadline):
notifications = db.session.query(
func.count(Notification.id).label('letters_count'),
func.sum(Notification.billable_units).label('sheets_count'),
Notification.postage
).filter(
Notification.created_at < convert_bst_to_utc(print_run_deadline),
Notification.notification_type == LETTER_TYPE,
Notification.status == NOTIFICATION_CREATED,
Notification.key_type == KEY_TYPE_NORMAL,
).group_by(
Notification.postage
).all()
return notifications
def dao_old_letters_with_created_status():
yesterday_bst = convert_utc_to_bst(datetime.utcnow()) - timedelta(days=1)
last_processing_deadline = yesterday_bst.replace(hour=17, minute=30, second=0, microsecond=0)

View File

@@ -12,6 +12,7 @@ from app.dao.notifications_dao import (
dao_create_notification,
dao_delete_notifications_by_id,
dao_get_last_notification_added_for_job_id,
dao_get_letters_and_sheets_volume_by_postage,
dao_get_notifications_by_recipient_or_reference,
dao_get_notification_count_for_job_id,
dao_timeout_notifications,
@@ -1705,3 +1706,36 @@ def test_letters_to_be_printed_sort_by_service(notify_db_session):
dao_get_letters_to_be_printed(print_run_deadline=datetime(2020, 12, 1, 17, 30), postage='second', query_limit=4)
)
assert [x.id for x in results] == [x.id for x in letters_ordered_by_service_then_time]
def test_dao_get_letters_and_sheets_volume_by_postage(notify_db_session):
first_service = create_service(service_name='first service', service_id='3a5cea08-29fd-4bb9-b582-8dedd928b149')
second_service = create_service(service_name='second service', service_id='642bf33b-54b5-45f2-8c13-942a46616704')
first_template = create_template(service=first_service, template_type='letter', postage='second')
second_template = create_template(service=second_service, template_type='letter', postage='second')
letters = [
create_notification(template=first_template, created_at=datetime(2020, 12, 1, 9, 30), postage='first'),
create_notification(template=first_template, created_at=datetime(2020, 12, 1, 12, 30), postage='europe'),
create_notification(template=first_template, created_at=datetime(2020, 12, 1, 13, 30), postage='rest-of-world'),
create_notification(template=first_template, created_at=datetime(2020, 12, 1, 14, 30), billable_units=3),
create_notification(template=first_template, created_at=datetime(2020, 12, 1, 15, 30)),
create_notification(template=second_template, created_at=datetime(2020, 12, 1, 8, 30), postage='first'),
create_notification(template=second_template, created_at=datetime(2020, 12, 1, 8, 31), postage='first'),
create_notification(template=second_template, created_at=datetime(2020, 12, 1, 8, 32)),
create_notification(template=second_template, created_at=datetime(2020, 12, 1, 8, 33)),
create_notification(template=second_template, created_at=datetime(2020, 12, 1, 8, 34))
]
results = dao_get_letters_and_sheets_volume_by_postage(print_run_deadline=datetime(2020, 12, 1, 17, 30))
assert len(results) == 4
expected_results = [
{'letters_count': 1, 'sheets_count': 1, 'postage': 'europe'},
{'letters_count': 3, 'sheets_count': 3, 'postage': 'first'},
{'letters_count': 1, 'sheets_count': 1, 'postage': 'rest-of-world'},
{'letters_count': 5, 'sheets_count': 7, 'postage': 'second'}
]
for result in results:
assert result._asdict() in expected_results