diff --git a/app/dao/services_dao.py b/app/dao/services_dao.py index 58772b0b4..416ed87a8 100644 --- a/app/dao/services_dao.py +++ b/app/dao/services_dao.py @@ -562,47 +562,50 @@ def dao_fetch_monthly_historical_usage_by_template_for_service(service_id, year) stats.append(stat) month = get_london_month_from_utc_column(Notification.created_at) - year = func.date_trunc("year", Notification.created_at) + year_func = func.date_trunc("year", Notification.created_at) start_date = datetime.combine(date.today(), time.min) - today_results = db.session.query( - Notification.template_id, - Template.name, - Template.template_type, - extract('month', month).label('month'), - extract('year', year).label('year'), - func.count().label('count') - ).join( - Template, Notification.template_id == Template.id, - ).filter( - Notification.created_at >= start_date, - Notification.service_id == service_id - ).group_by( - Notification.template_id, - Template.name, - Template.template_type, - month, - year - ).order_by( - Notification.template_id - ).all() + fy_start, fy_end = get_financial_year(year) - for today_result in today_results: - add_to_stats = True - for stat in stats: - if today_result.template_id == stat.template_id and today_result.month == stat.month \ - and today_result.year == stat.year: - stat.count = stat.count + today_result.count - add_to_stats = False + if fy_start < datetime.now() < fy_end: + today_results = db.session.query( + Notification.template_id, + Template.name, + Template.template_type, + extract('month', month).label('month'), + extract('year', year_func).label('year'), + func.count().label('count') + ).join( + Template, Notification.template_id == Template.id, + ).filter( + Notification.created_at >= start_date, + Notification.service_id == service_id + ).group_by( + Notification.template_id, + Template.name, + Template.template_type, + month, + year_func + ).order_by( + Notification.template_id + ).all() - if add_to_stats: - new_stat = type("", (), {})() - new_stat.template_id = today_result.template_id - new_stat.template_type = today_result.template_type - new_stat.name = today_result.name - new_stat.month = int(today_result.month) - new_stat.year = int(today_result.year) - new_stat.count = today_result.count - stats.append(new_stat) + for today_result in today_results: + add_to_stats = True + for stat in stats: + if today_result.template_id == stat.template_id and today_result.month == stat.month \ + and today_result.year == stat.year: + stat.count = stat.count + today_result.count + add_to_stats = False + + if add_to_stats: + new_stat = type("", (), {})() + new_stat.template_id = today_result.template_id + new_stat.template_type = today_result.template_type + new_stat.name = today_result.name + new_stat.month = int(today_result.month) + new_stat.year = int(today_result.year) + new_stat.count = today_result.count + stats.append(new_stat) return stats diff --git a/tests/app/dao/test_services_dao.py b/tests/app/dao/test_services_dao.py index f172769e7..beefc6b86 100644 --- a/tests/app/dao/test_services_dao.py +++ b/tests/app/dao/test_services_dao.py @@ -1471,6 +1471,13 @@ def test_dao_fetch_monthly_historical_usage_by_template_for_service_returns_fina assert result[4].month == 3 assert result[4].year == 2018 + result = sorted( + dao_fetch_monthly_historical_usage_by_template_for_service(n.service_id, 2014), + key=lambda x: (x.year, x.month) + ) + + assert len(result) == 0 + @freeze_time("2018-03-10 11:09:00.000000") def test_dao_fetch_monthly_historical_usage_by_template_for_service_only_returns_for_service( diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 3d0d621da..05a1d8734 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -1857,7 +1857,7 @@ def test_get_template_usage_by_month_returns_correct_data( notify_db, notify_db_session, sample_template, - created_at=datetime(2016, 4, 1), + created_at=datetime(2017, 4, 1), status='sending' ) @@ -1865,7 +1865,7 @@ def test_get_template_usage_by_month_returns_correct_data( notify_db, notify_db_session, sample_template, - created_at=datetime(2016, 4, 1), + created_at=datetime(2017, 4, 1), status='permanent-failure' ) @@ -1873,7 +1873,7 @@ def test_get_template_usage_by_month_returns_correct_data( notify_db, notify_db_session, sample_template, - created_at=datetime(2016, 4, 1), + created_at=datetime(2017, 4, 1), status='temporary-failure' ) @@ -1885,7 +1885,7 @@ def test_get_template_usage_by_month_returns_correct_data( ) resp = client.get( - '/service/{}/notifications/templates_usage/monthly?year=2016'.format(not1.service_id), + '/service/{}/notifications/templates_usage/monthly?year=2017'.format(not1.service_id), headers=[create_authorization_header()] ) resp_json = json.loads(resp.get_data(as_text=True)).get('stats') @@ -1897,8 +1897,8 @@ def test_get_template_usage_by_month_returns_correct_data( assert resp_json[0]["name"] == sample_template.name assert resp_json[0]["type"] == sample_template.template_type assert resp_json[0]["month"] == 4 - assert resp_json[0]["year"] == 2016 - assert resp_json[0]["count"] == 4 + assert resp_json[0]["year"] == 2017 + assert resp_json[0]["count"] == 3 assert resp_json[1]["template_id"] == str(sample_template.id) assert resp_json[1]["name"] == sample_template.name @@ -1908,6 +1908,63 @@ def test_get_template_usage_by_month_returns_correct_data( assert resp_json[1]["count"] == 1 +@freeze_time('2017-11-11 02:00') +def test_get_template_usage_by_month_returns_no_data( + notify_db, + notify_db_session, + client, + sample_template +): + + # add a historical notification for template + not1 = create_notification_history( + notify_db, + notify_db_session, + sample_template, + created_at=datetime(2016, 4, 1), + ) + + create_notification_history( + notify_db, + notify_db_session, + sample_template, + created_at=datetime(2017, 4, 1), + status='sending' + ) + + create_notification_history( + notify_db, + notify_db_session, + sample_template, + created_at=datetime(2017, 4, 1), + status='permanent-failure' + ) + + create_notification_history( + notify_db, + notify_db_session, + sample_template, + created_at=datetime(2017, 4, 1), + status='temporary-failure' + ) + + daily_stats_template_usage_by_month() + + create_notification( + sample_template, + created_at=datetime.utcnow() + ) + + resp = client.get( + '/service/{}/notifications/templates_usage/monthly?year=2015'.format(not1.service_id), + headers=[create_authorization_header()] + ) + resp_json = json.loads(resp.get_data(as_text=True)).get('stats') + + assert resp.status_code == 200 + assert len(resp_json) == 0 + + @freeze_time('2017-11-11 02:00') def test_get_template_usage_by_month_returns_two_templates( notify_db, @@ -1924,14 +1981,14 @@ def test_get_template_usage_by_month_returns_two_templates( notify_db, notify_db_session, template_one, - created_at=datetime(2016, 4, 1), + created_at=datetime(2017, 4, 1), ) create_notification_history( notify_db, notify_db_session, sample_template, - created_at=datetime(2016, 4, 1), + created_at=datetime(2017, 4, 1), status='sending' ) @@ -1939,7 +1996,7 @@ def test_get_template_usage_by_month_returns_two_templates( notify_db, notify_db_session, sample_template, - created_at=datetime(2016, 4, 1), + created_at=datetime(2017, 4, 1), status='permanent-failure' ) @@ -1947,7 +2004,7 @@ def test_get_template_usage_by_month_returns_two_templates( notify_db, notify_db_session, sample_template, - created_at=datetime(2016, 4, 1), + created_at=datetime(2017, 4, 1), status='temporary-failure' ) @@ -1959,7 +2016,7 @@ def test_get_template_usage_by_month_returns_two_templates( ) resp = client.get( - '/service/{}/notifications/templates_usage/monthly?year=2016'.format(not1.service_id), + '/service/{}/notifications/templates_usage/monthly?year=2017'.format(not1.service_id), headers=[create_authorization_header()] ) resp_json = json.loads(resp.get_data(as_text=True)).get('stats') @@ -1973,14 +2030,14 @@ def test_get_template_usage_by_month_returns_two_templates( assert resp_json[0]["name"] == template_one.name assert resp_json[0]["type"] == template_one.template_type assert resp_json[0]["month"] == 4 - assert resp_json[0]["year"] == 2016 + assert resp_json[0]["year"] == 2017 assert resp_json[0]["count"] == 1 assert resp_json[1]["template_id"] == str(sample_template.id) assert resp_json[1]["name"] == sample_template.name assert resp_json[1]["type"] == sample_template.template_type assert resp_json[1]["month"] == 4 - assert resp_json[1]["year"] == 2016 + assert resp_json[1]["year"] == 2017 assert resp_json[1]["count"] == 3 assert resp_json[2]["template_id"] == str(sample_template.id)