Refactor to make code more readible.

This commit is contained in:
Rebecca Law
2017-04-06 11:18:21 +01:00
parent 0506bc3b0c
commit 9e175f42e8

View File

@@ -326,47 +326,29 @@ def dao_fetch_todays_stats_for_all_services(include_from_test_key=True):
def fetch_stats_by_date_range_for_all_services(start_date, end_date, include_from_test_key=True): def fetch_stats_by_date_range_for_all_services(start_date, end_date, include_from_test_key=True):
start_date = get_london_midnight_in_utc(start_date) start_date = get_london_midnight_in_utc(start_date)
end_date = get_london_midnight_in_utc(end_date + timedelta(days=1)) end_date = get_london_midnight_in_utc(end_date + timedelta(days=1))
table = NotificationHistory
if start_date >= datetime.utcnow() - timedelta(days=7): if start_date >= datetime.utcnow() - timedelta(days=7):
# Use notifications table. table = Notification
# This should improve performance for the default query and allow us to see test messages for the last week.
query = db.session.query(
Notification.notification_type,
Notification.status,
Notification.service_id,
func.count(Notification.id).label('count')
).filter(
Notification.created_at >= start_date,
Notification.created_at < end_date
).group_by(
Notification.notification_type,
Notification.status,
Notification.service_id
).order_by(
Notification.service_id
)
if not include_from_test_key:
query = query.filter(NotificationHistory.key_type != KEY_TYPE_TEST)
else: query = db.session.query(
query = db.session.query( table.notification_type,
NotificationHistory.notification_type, table.status,
NotificationHistory.status, table.service_id,
NotificationHistory.service_id, func.count(table.id).label('count')
func.count(NotificationHistory.id).label('count') ).filter(
).filter( table.created_at >= start_date,
NotificationHistory.created_at >= start_date, table.created_at < end_date
NotificationHistory.created_at < end_date ).group_by(
).group_by( table.notification_type,
NotificationHistory.notification_type, table.status,
NotificationHistory.status, table.service_id
NotificationHistory.service_id ).order_by(
).order_by( table.service_id
NotificationHistory.service_id )
)
if not include_from_test_key: if not include_from_test_key:
query = query.filter(NotificationHistory.key_type != KEY_TYPE_TEST) query = query.filter(table.key_type != KEY_TYPE_TEST)
return query.all() return query.all()