From 8ad0236f28db336405779e3331d9cbfe78af385c Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Thu, 26 Jan 2017 16:40:52 +0000 Subject: [PATCH] Allow filtering of billing info by financial year We already filter the usage-by-month query by financial year. When we show the total usage for a service, we should be able to filter this by financial year. Then, when the two lots of data are put side by side, it all adds up. --- app/dao/provider_statistics_dao.py | 8 +++++++- app/service/rest.py | 8 +++++++- tests/app/dao/test_provider_statistics_dao.py | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/dao/provider_statistics_dao.py b/app/dao/provider_statistics_dao.py index 588e9a7dc..761e79de6 100644 --- a/app/dao/provider_statistics_dao.py +++ b/app/dao/provider_statistics_dao.py @@ -8,15 +8,21 @@ from app.models import ( NOTIFICATION_STATUS_TYPES_BILLABLE, KEY_TYPE_TEST ) +from app.dao.notifications_dao import get_financial_year -def get_fragment_count(service_id): +def get_fragment_count(service_id, year=None): shared_filters = [ NotificationHistory.service_id == service_id, NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE), NotificationHistory.key_type != KEY_TYPE_TEST ] + if year: + shared_filters.append(NotificationHistory.created_at.between( + *get_financial_year(year) + )) + sms_count = db.session.query( func.sum(NotificationHistory.billable_units) ).filter( diff --git a/app/service/rest.py b/app/service/rest.py index cdca5045b..3b9ace8b2 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -190,7 +190,13 @@ def remove_user_from_service(service_id, user_id): @service_blueprint.route('//fragment/aggregate_statistics') def get_service_provider_aggregate_statistics(service_id): - return jsonify(data=get_fragment_count(service_id)) + year = request.args.get('year') + if year is not None: + try: + year = int(year) + except ValueError: + raise InvalidRequest('Year must be a number', status_code=400) + return jsonify(data=get_fragment_count(service_id, year=year)) # This is placeholder get method until more thought diff --git a/tests/app/dao/test_provider_statistics_dao.py b/tests/app/dao/test_provider_statistics_dao.py index 2a1faad4e..e3249ec64 100644 --- a/tests/app/dao/test_provider_statistics_dao.py +++ b/tests/app/dao/test_provider_statistics_dao.py @@ -2,6 +2,7 @@ from datetime import datetime import uuid import pytest +from freezegun import freeze_time from app.models import NotificationHistory, KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST, NOTIFICATION_STATUS_TYPES from app.dao.provider_statistics_dao import get_fragment_count @@ -35,6 +36,20 @@ def test_get_fragment_count_filters_on_service_id(notify_db, sample_template, se assert get_fragment_count(service_2.id)['sms_count'] == 0 +@pytest.mark.parametrize('creation_time, expected_count', [ + ('2000-03-31 22:59:59', 0), # before the start of the year + ('2000-04-01 00:00:00', 1), # after the start of the year + ('2001-03-31 22:59:59', 1), # before the end of the year + ('2001-04-01 00:00:00', 0), # after the end of the year +]) +def test_get_fragment_count_filters_on_year( + notify_db, sample_template, creation_time, expected_count +): + with freeze_time(creation_time): + noti_hist(notify_db, sample_template) + assert get_fragment_count(sample_template.service_id, year=2000)['sms_count'] == expected_count + + def test_get_fragment_count_sums_billable_units_for_sms(notify_db, sample_template): noti_hist(notify_db, sample_template, billable_units=1) noti_hist(notify_db, sample_template, billable_units=2)