Use table class directly instead of "table" var

In response to [^1].

[^1]: https://github.com/alphagov/notifications-api/pull/3546#discussion_r879541366
This commit is contained in:
Ben Thorner
2022-05-24 10:16:28 +01:00
parent 33645c7747
commit 8e837cf681
2 changed files with 48 additions and 53 deletions

View File

@@ -467,11 +467,9 @@ def fetch_billing_data_for_day(process_day, service_id=None, check_permissions=F
def _query_for_billing_data(notification_type, start_date, end_date, service):
table = NotificationAllTimeView
def _email_query():
return db.session.query(
table.template_id,
NotificationAllTimeView.template_id,
literal(service.crown).label('crown'),
literal(service.id).label('service_id'),
literal(notification_type).label('notification_type'),
@@ -483,22 +481,22 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
literal(0).label('billable_units'),
func.count().label('notifications_sent'),
).filter(
table.status.in_(NOTIFICATION_STATUS_TYPES_SENT_EMAILS),
table.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
table.created_at >= start_date,
table.created_at < end_date,
table.notification_type == notification_type,
table.service_id == service.id
NotificationAllTimeView.status.in_(NOTIFICATION_STATUS_TYPES_SENT_EMAILS),
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type,
NotificationAllTimeView.service_id == service.id
).group_by(
table.template_id,
NotificationAllTimeView.template_id,
)
def _sms_query():
sent_by = func.coalesce(table.sent_by, 'unknown')
rate_multiplier = func.coalesce(table.rate_multiplier, 1).cast(Integer)
international = func.coalesce(table.international, False)
sent_by = func.coalesce(NotificationAllTimeView.sent_by, 'unknown')
rate_multiplier = func.coalesce(NotificationAllTimeView.rate_multiplier, 1).cast(Integer)
international = func.coalesce(NotificationAllTimeView.international, False)
return db.session.query(
table.template_id,
NotificationAllTimeView.template_id,
literal(service.crown).label('crown'),
literal(service.id).label('service_id'),
literal(notification_type).label('notification_type'),
@@ -507,50 +505,50 @@ def _query_for_billing_data(notification_type, start_date, end_date, service):
international.label('international'),
literal(None).label('letter_page_count'),
literal('none').label('postage'),
func.sum(table.billable_units).label('billable_units'),
func.sum(NotificationAllTimeView.billable_units).label('billable_units'),
func.count().label('notifications_sent'),
).filter(
table.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE_SMS),
table.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
table.created_at >= start_date,
table.created_at < end_date,
table.notification_type == notification_type,
table.service_id == service.id
NotificationAllTimeView.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE_SMS),
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type,
NotificationAllTimeView.service_id == service.id
).group_by(
table.template_id,
NotificationAllTimeView.template_id,
sent_by,
rate_multiplier,
international,
)
def _letter_query():
rate_multiplier = func.coalesce(table.rate_multiplier, 1).cast(Integer)
postage = func.coalesce(table.postage, 'none')
rate_multiplier = func.coalesce(NotificationAllTimeView.rate_multiplier, 1).cast(Integer)
postage = func.coalesce(NotificationAllTimeView.postage, 'none')
return db.session.query(
table.template_id,
NotificationAllTimeView.template_id,
literal(service.crown).label('crown'),
literal(service.id).label('service_id'),
literal(notification_type).label('notification_type'),
literal('dvla').label('sent_by'),
rate_multiplier.label('rate_multiplier'),
table.international,
table.billable_units.label('letter_page_count'),
NotificationAllTimeView.international,
NotificationAllTimeView.billable_units.label('letter_page_count'),
postage.label('postage'),
func.sum(table.billable_units).label('billable_units'),
func.sum(NotificationAllTimeView.billable_units).label('billable_units'),
func.count().label('notifications_sent'),
).filter(
table.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS),
table.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
table.created_at >= start_date,
table.created_at < end_date,
table.notification_type == notification_type,
table.service_id == service.id
NotificationAllTimeView.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS),
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type,
NotificationAllTimeView.service_id == service.id
).group_by(
table.template_id,
NotificationAllTimeView.template_id,
rate_multiplier,
table.billable_units,
NotificationAllTimeView.billable_units,
postage,
table.international
NotificationAllTimeView.international
)
query_funcs = {

View File

@@ -46,30 +46,27 @@ def update_fact_notification_status(process_day, notification_type, service_id):
FactNotificationStatus.service_id == service_id,
).delete()
# query notifications or notification_history for the day, depending on their data retention
source_table = NotificationAllTimeView
query = db.session.query(
literal(process_day).label("process_day"),
source_table.template_id,
NotificationAllTimeView.template_id,
literal(service_id).label("service_id"),
func.coalesce(source_table.job_id, '00000000-0000-0000-0000-000000000000').label('job_id'),
func.coalesce(NotificationAllTimeView.job_id, '00000000-0000-0000-0000-000000000000').label('job_id'),
literal(notification_type).label("notification_type"),
source_table.key_type,
source_table.status,
NotificationAllTimeView.key_type,
NotificationAllTimeView.status,
func.count().label('notification_count')
).filter(
source_table.created_at >= start_date,
source_table.created_at < end_date,
source_table.notification_type == notification_type,
source_table.service_id == service_id,
source_table.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
NotificationAllTimeView.created_at >= start_date,
NotificationAllTimeView.created_at < end_date,
NotificationAllTimeView.notification_type == notification_type,
NotificationAllTimeView.service_id == service_id,
NotificationAllTimeView.key_type.in_((KEY_TYPE_NORMAL, KEY_TYPE_TEAM)),
).group_by(
source_table.template_id,
source_table.template_id,
NotificationAllTimeView.template_id,
NotificationAllTimeView.template_id,
'job_id',
source_table.key_type,
source_table.status
NotificationAllTimeView.key_type,
NotificationAllTimeView.status
)
db.session.connection().execute(