From 1de022f0054cee8476134dc73be91451ffed96a8 Mon Sep 17 00:00:00 2001 From: Rebecca Law Date: Thu, 29 Dec 2016 13:28:55 +0000 Subject: [PATCH] Update the query to execute immediately. Fix indent. Left a comment as to why start and end date are not set. --- app/dao/services_dao.py | 4 ++-- app/service/rest.py | 3 ++- tests/app/dao/test_services_dao.py | 12 ++++++------ 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 8e982c9da..aaefec145 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -262,7 +262,7 @@ def dao_fetch_todays_stats_for_all_services(include_from_test_key=True): if not include_from_test_key: query = query.filter(Notification.key_type != KEY_TYPE_TEST) - return query + return query.all() @statsd(namespace='dao') @@ -290,4 +290,4 @@ def fetch_stats_by_date_range_for_all_services(start_date, end_date, include_fro if not include_from_test_key: query = query.filter(NotificationHistory.key_type != KEY_TYPE_TEST) - return query + return query.all() diff --git a/app/service/rest.py b/app/service/rest.py index 29738141f..02eb65830 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -64,6 +64,7 @@ def get_services(): detailed = request.args.get('detailed') == 'True' user_id = request.args.get('user_id', None) include_from_test_key = request.args.get('include_from_test_key', 'True') != 'False' + # If start and end date are not set in the request.args, we are expecting today's stats. start_date = request.args.get('start_date', None) end_date = request.args.get('end_date', None) @@ -283,7 +284,7 @@ def get_detailed_services(only_active=False, include_from_test_key=True, start_d stats = dao_fetch_todays_stats_for_all_services(include_from_test_key=include_from_test_key) for service_id, rows in itertools.groupby(stats, lambda x: x.service_id): - services[service_id].statistics = statistics.format_statistics(rows) + services[service_id].statistics = statistics.format_statistics(rows) # if service has not sent anything, query will not have set statistics correctly for service in services.values(): diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index 896089a24..a3f5d1de7 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -576,7 +576,7 @@ def test_dao_fetch_todays_stats_for_all_services_includes_all_services(notify_db notify_db, notify_db_session, service=service2, template=create_email_template(notify_db, notify_db_session, service=service2)) - stats = dao_fetch_todays_stats_for_all_services().all() + stats = dao_fetch_todays_stats_for_all_services() assert len(stats) == 4 # services are ordered by service id; not explicit on email/sms or status @@ -591,7 +591,7 @@ def test_dao_fetch_todays_stats_for_all_services_only_includes_today(notify_db): just_after_midnight_today = create_notification(notify_db, None, to_field='2', status='failed') with freeze_time('2001-01-02T12:00:00'): - stats = dao_fetch_todays_stats_for_all_services().all() + stats = dao_fetch_todays_stats_for_all_services() stats = {row.status: row.count for row in stats} assert 'delivered' not in stats @@ -611,7 +611,7 @@ def test_dao_fetch_todays_stats_for_all_services_groups_correctly(notify_db, not # service2: 1 sms "created" create_notification(notify_db, notify_db_session, service=service2) - stats = dao_fetch_todays_stats_for_all_services().all() + stats = dao_fetch_todays_stats_for_all_services() assert len(stats) == 4 assert ('sms', 'created', service1.id, 2) in stats @@ -625,7 +625,7 @@ def test_dao_fetch_todays_stats_for_all_services_includes_all_keys_by_default(no create_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEAM) create_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEST) - stats = dao_fetch_todays_stats_for_all_services().all() + stats = dao_fetch_todays_stats_for_all_services() assert len(stats) == 1 assert stats[0].count == 3 @@ -636,7 +636,7 @@ def test_dao_fetch_todays_stats_for_all_services_can_exclude_from_test_key(notif create_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEAM) create_notification(notify_db, notify_db_session, key_type=KEY_TYPE_TEST) - stats = dao_fetch_todays_stats_for_all_services(include_from_test_key=False).all() + stats = dao_fetch_todays_stats_for_all_services(include_from_test_key=False) assert len(stats) == 1 assert stats[0].count == 2 @@ -652,7 +652,7 @@ def test_fetch_stats_by_date_range_for_all_services(notify_db, notify_db_session start_date = (datetime.utcnow() - timedelta(days=2)).date() end_date = (datetime.utcnow() - timedelta(days=1)).date() - results = fetch_stats_by_date_range_for_all_services(start_date, end_date).all() + results = fetch_stats_by_date_range_for_all_services(start_date, end_date) assert len(results) == 1 assert results[0] == ('sms', 'created', result_one.service_id, 2)