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(