Merge pull request #1195 from alphagov/imdad-refactor-remove-unused-billing-code

Remove unused billing usage code
This commit is contained in:
Imdad Ahad
2017-08-31 10:39:12 +01:00
committed by GitHub
6 changed files with 19 additions and 356 deletions
-2
View File
@@ -1,7 +1,5 @@
from datetime import datetime
from sqlalchemy import func
from app import db
from app.dao.dao_utils import transactional
+1 -82
View File
@@ -1,4 +1,3 @@
from collections import namedtuple
from datetime import datetime, timedelta
from sqlalchemy import Float, Integer
@@ -17,41 +16,7 @@ from app.models import (
EMAIL_TYPE
)
from app.statsd_decorators import statsd
from app.utils import get_london_month_from_utc_column, convert_utc_to_bst
@statsd(namespace="dao")
def get_yearly_billing_data(service_id, year):
start_date, end_date = get_financial_year(year)
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
if not rates:
return []
def get_valid_from(valid_from):
return start_date if valid_from < start_date else valid_from
result = []
for r, n in zip(rates, rates[1:]):
result.append(
sms_yearly_billing_data_query(
r.rate,
service_id,
get_valid_from(r.valid_from),
n.valid_from
)
)
result.append(
sms_yearly_billing_data_query(
rates[-1].rate,
service_id,
get_valid_from(rates[-1].valid_from),
end_date
)
)
result.append(email_yearly_billing_data_query(service_id, start_date, end_date))
return sum(result, [])
from app.utils import get_london_month_from_utc_column
@statsd(namespace="dao")
@@ -112,52 +77,6 @@ def billing_data_filter(notification_type, start_date, end_date, service_id):
]
def email_yearly_billing_data_query(service_id, start_date, end_date, rate=0):
result = db.session.query(
func.count(NotificationHistory.id),
func.count(NotificationHistory.id),
rate_multiplier(),
NotificationHistory.notification_type,
NotificationHistory.international,
cast(rate, Integer())
).filter(
*billing_data_filter(EMAIL_TYPE, start_date, end_date, service_id)
).group_by(
NotificationHistory.notification_type,
rate_multiplier(),
NotificationHistory.international
).first()
if not result:
return [(0, 0, 1, EMAIL_TYPE, False, 0)]
else:
return [result]
def sms_yearly_billing_data_query(rate, service_id, start_date, end_date):
result = db.session.query(
cast(func.sum(NotificationHistory.billable_units * rate_multiplier()), Integer()),
func.sum(NotificationHistory.billable_units),
rate_multiplier(),
NotificationHistory.notification_type,
NotificationHistory.international,
cast(rate, Float())
).filter(
*billing_data_filter(SMS_TYPE, start_date, end_date, service_id)
).group_by(
NotificationHistory.notification_type,
NotificationHistory.international,
rate_multiplier()
).order_by(
rate_multiplier()
).all()
if not result:
return [(0, 0, 1, SMS_TYPE, False, rate)]
else:
return result
def get_rates_for_daterange(start_date, end_date, notification_type):
rates = Rate.query.filter(Rate.notification_type == notification_type).order_by(Rate.valid_from).all()
-37
View File
@@ -458,43 +458,6 @@ def get_monthly_template_stats(service_id):
raise InvalidRequest('Year must be a number', status_code=400)
@service_blueprint.route('/<uuid:service_id>/yearly-usage')
def get_yearly_billing_usage(service_id):
try:
year = int(request.args.get('year'))
results = notification_usage_dao.get_yearly_billing_data(service_id, year)
json_result = [{
"credits": x[0],
"billing_units": x[1],
"rate_multiplier": x[2],
"notification_type": x[3],
"international": x[4],
"rate": x[5]
} for x in results]
return json.dumps(json_result)
except TypeError:
return jsonify(result='error', message='No valid year provided'), 400
@service_blueprint.route('/<uuid:service_id>/monthly-usage')
def get_yearly_monthly_usage(service_id):
try:
year = int(request.args.get('year'))
results = notification_usage_dao.get_monthly_billing_data(service_id, year)
json_results = [{
"month": x[0],
"billing_units": x[1],
"rate_multiplier": x[2],
"international": x[3],
"notification_type": x[4],
"rate": x[5]
} for x in results]
return json.dumps(json_results)
except TypeError:
return jsonify(result='error', message='No valid year provided'), 400
@service_blueprint.route('/<uuid:service_id>/inbound-api', methods=['POST'])
def create_service_inbound_api(service_id):
data = request.get_json()
-1
View File
@@ -3,7 +3,6 @@ from dateutil.relativedelta import relativedelta
from freezegun import freeze_time
from functools import partial
from app import db
from app.dao.monthly_billing_dao import (
create_or_update_monthly_billing,
get_monthly_billing_entry,
+17 -119
View File
@@ -6,7 +6,6 @@ from freezegun import freeze_time
from app.dao.date_util import get_financial_year
from app.dao.notification_usage_dao import (
get_rates_for_daterange,
get_yearly_billing_data,
get_billing_data_for_month,
get_monthly_billing_data
)
@@ -21,7 +20,7 @@ def test_get_rates_for_daterange(notify_db, notify_db_session):
set_up_rate(notify_db, datetime(2016, 5, 18), 0.016)
set_up_rate(notify_db, datetime(2017, 3, 31, 23), 0.0158)
start_date, end_date = get_financial_year(2017)
rates = get_rates_for_daterange(start_date, end_date, 'sms')
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert len(rates) == 1
assert datetime.strftime(rates[0].valid_from, '%Y-%m-%d %H:%M:%S') == "2017-03-31 23:00:00"
assert rates[0].rate == 0.0158
@@ -32,7 +31,7 @@ def test_get_rates_for_daterange_multiple_result_per_year(notify_db, notify_db_s
set_up_rate(notify_db, datetime(2016, 5, 18), 0.016)
set_up_rate(notify_db, datetime(2017, 4, 1), 0.0158)
start_date, end_date = get_financial_year(2016)
rates = get_rates_for_daterange(start_date, end_date, 'sms')
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert len(rates) == 2
assert datetime.strftime(rates[0].valid_from, '%Y-%m-%d %H:%M:%S') == "2016-04-01 00:00:00"
assert rates[0].rate == 0.015
@@ -45,7 +44,7 @@ def test_get_rates_for_daterange_returns_correct_rates(notify_db, notify_db_sess
set_up_rate(notify_db, datetime(2016, 9, 1), 0.016)
set_up_rate(notify_db, datetime(2017, 6, 1), 0.0175)
start_date, end_date = get_financial_year(2017)
rates_2017 = get_rates_for_daterange(start_date, end_date, 'sms')
rates_2017 = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert len(rates_2017) == 2
assert datetime.strftime(rates_2017[0].valid_from, '%Y-%m-%d %H:%M:%S') == "2016-09-01 00:00:00"
assert rates_2017[0].rate == 0.016
@@ -57,7 +56,7 @@ def test_get_rates_for_daterange_in_the_future(notify_db, notify_db_session):
set_up_rate(notify_db, datetime(2016, 4, 1), 0.015)
set_up_rate(notify_db, datetime(2017, 6, 1), 0.0175)
start_date, end_date = get_financial_year(2018)
rates = get_rates_for_daterange(start_date, end_date, 'sms')
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert datetime.strftime(rates[0].valid_from, '%Y-%m-%d %H:%M:%S') == "2017-06-01 00:00:00"
assert rates[0].rate == 0.0175
@@ -66,7 +65,7 @@ def test_get_rates_for_daterange_returns_empty_list_if_year_is_before_earliest_r
set_up_rate(notify_db, datetime(2016, 4, 1), 0.015)
set_up_rate(notify_db, datetime(2017, 6, 1), 0.0175)
start_date, end_date = get_financial_year(2015)
rates = get_rates_for_daterange(start_date, end_date, 'sms')
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert rates == []
@@ -76,7 +75,7 @@ def test_get_rates_for_daterange_early_rate(notify_db, notify_db_session):
set_up_rate(notify_db, datetime(2016, 9, 1), 0.016)
set_up_rate(notify_db, datetime(2017, 6, 1), 0.0175)
start_date, end_date = get_financial_year(2016)
rates = get_rates_for_daterange(start_date, end_date, 'sms')
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert len(rates) == 3
@@ -84,7 +83,7 @@ def test_get_rates_for_daterange_edge_case(notify_db, notify_db_session):
set_up_rate(notify_db, datetime(2016, 3, 31, 23, 00), 0.015)
set_up_rate(notify_db, datetime(2017, 3, 31, 23, 00), 0.0175)
start_date, end_date = get_financial_year(2016)
rates = get_rates_for_daterange(start_date, end_date, 'sms')
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert len(rates) == 1
assert datetime.strftime(rates[0].valid_from, '%Y-%m-%d %H:%M:%S') == "2016-03-31 23:00:00"
assert rates[0].rate == 0.015
@@ -97,100 +96,12 @@ def test_get_rates_for_daterange_where_daterange_is_one_month_that_falls_between
set_up_rate(notify_db, datetime(2017, 3, 31), 0.123)
start_date = datetime(2017, 2, 1, 00, 00, 00)
end_date = datetime(2017, 2, 28, 23, 59, 59, 99999)
rates = get_rates_for_daterange(start_date, end_date, 'sms')
rates = get_rates_for_daterange(start_date, end_date, SMS_TYPE)
assert len(rates) == 1
assert datetime.strftime(rates[0].valid_from, '%Y-%m-%d %H:%M:%S') == "2017-01-01 00:00:00"
assert rates[0].rate == 0.175
def test_get_yearly_billing_data(notify_db, notify_db_session, sample_template, sample_email_template):
set_up_rate(notify_db, datetime(2016, 4, 1), 0.014)
set_up_rate(notify_db, datetime(2016, 6, 1), 0.0158)
set_up_rate(notify_db, datetime(2017, 6, 1), 0.0165)
# previous year
create_notification(template=sample_template, created_at=datetime(2016, 3, 31), sent_at=datetime(2016, 3, 31),
status='sending', billable_units=1)
# current year
create_notification(template=sample_template, created_at=datetime(2016, 4, 2), sent_at=datetime(2016, 4, 2),
status='sending', billable_units=1)
create_notification(template=sample_template, created_at=datetime(2016, 5, 18), sent_at=datetime(2016, 5, 18),
status='sending', billable_units=2)
create_notification(template=sample_template, created_at=datetime(2016, 7, 22), sent_at=datetime(2016, 7, 22),
status='sending', billable_units=3, rate_multiplier=2, international=True, phone_prefix="1")
create_notification(template=sample_template, created_at=datetime(2016, 9, 15), sent_at=datetime(2016, 9, 15),
status='sending', billable_units=4)
create_notification(template=sample_template, created_at=datetime(2017, 3, 31), sent_at=datetime(2017, 3, 31),
status='sending', billable_units=5)
create_notification(template=sample_email_template, created_at=datetime(2016, 9, 15), sent_at=datetime(2016, 9, 15),
status='sending', billable_units=0)
create_notification(template=sample_email_template, created_at=datetime(2017, 3, 31), sent_at=datetime(2017, 3, 31),
status='sending', billable_units=0)
# next year
create_notification(template=sample_template, created_at=datetime(2017, 4, 1), sent_at=datetime(2017, 4, 1),
status='sending', billable_units=6)
results = get_yearly_billing_data(sample_template.service_id, 2016)
assert len(results) == 4
assert results[0] == (3, 3, 1, 'sms', False, 0.014)
assert results[1] == (9, 9, 1, 'sms', False, 0.0158)
assert results[2] == (6, 3, 2, 'sms', True, 0.0158)
assert results[3] == (2, 2, 1, 'email', False, 0)
def test_get_future_yearly_billing_data(notify_db, notify_db_session, sample_template, sample_email_template):
set_up_rate(notify_db, datetime(2017, 4, 1), 0.0158)
create_notification(template=sample_template, created_at=datetime(2017, 3, 30), sent_at=datetime(2017, 3, 30),
status='sending', billable_units=1)
create_notification(template=sample_template, created_at=datetime(2017, 4, 6), sent_at=datetime(2017, 4, 6),
status='sending', billable_units=1)
create_notification(template=sample_template, created_at=datetime(2017, 4, 6), sent_at=datetime(2017, 4, 6),
status='sending', billable_units=1)
results = get_yearly_billing_data(sample_template.service_id, 2018)
assert len(results) == 2
assert results[0] == (0, 0, 1, 'sms', False, 0.0158)
def test_get_yearly_billing_data_with_one_rate(notify_db, notify_db_session, sample_template):
set_up_rate(notify_db, datetime(2016, 4, 1), 0.014)
# previous year
create_notification(template=sample_template, created_at=datetime(2016, 3, 31), sent_at=datetime(2016, 3, 31),
status='sending', billable_units=1)
# current year
create_notification(template=sample_template, created_at=datetime(2016, 4, 2), sent_at=datetime(2016, 4, 2),
status='sending', billable_units=1)
create_notification(template=sample_template, created_at=datetime(2016, 5, 18), sent_at=datetime(2016, 5, 18),
status='sending', billable_units=2)
create_notification(template=sample_template, created_at=datetime(2016, 7, 22), sent_at=datetime(2016, 7, 22),
status='sending', billable_units=3)
create_notification(template=sample_template, created_at=datetime(2016, 9, 15), sent_at=datetime(2016, 9, 15),
status='sending', billable_units=4)
create_notification(template=sample_template, created_at=datetime(2017, 3, 31, 22, 59, 59),
sent_at=datetime(2017, 3, 31), status='sending', billable_units=5)
# next year
create_notification(template=sample_template, created_at=datetime(2017, 3, 31, 23, 00, 00),
sent_at=datetime(2017, 3, 31), status='sending', billable_units=6)
create_notification(template=sample_template, created_at=datetime(2017, 4, 1), sent_at=datetime(2017, 4, 1),
status='sending', billable_units=7)
results = get_yearly_billing_data(sample_template.service_id, 2016)
assert len(results) == 2
assert results[0] == (15, 15, 1, 'sms', False, 0.014)
assert results[1] == (0, 0, 1, 'email', False, 0)
def test_get_yearly_billing_data_with_no_sms_notifications(notify_db, notify_db_session, sample_email_template):
set_up_rate(notify_db, datetime(2016, 4, 1), 0.014)
create_notification(template=sample_email_template, created_at=datetime(2016, 7, 31), sent_at=datetime(2016, 3, 31),
status='sending', billable_units=0)
create_notification(template=sample_email_template, created_at=datetime(2016, 10, 2), sent_at=datetime(2016, 4, 2),
status='sending', billable_units=0)
results = get_yearly_billing_data(sample_email_template.service_id, 2016)
assert len(results) == 2
assert results[0] == (0, 0, 1, 'sms', False, 0.014)
assert results[1] == (2, 2, 1, 'email', False, 0)
def test_get_monthly_billing_data(notify_db, notify_db_session, sample_template, sample_email_template):
set_up_rate(notify_db, datetime(2016, 4, 1), 0.014)
# previous year
@@ -220,10 +131,10 @@ def test_get_monthly_billing_data(notify_db, notify_db_session, sample_template,
results = get_monthly_billing_data(sample_template.service_id, 2016)
assert len(results) == 4
# (billable_units, rate_multiplier, international, type, rate)
assert results[0] == ('April', 1, 1, False, 'sms', 0.014)
assert results[1] == ('May', 2, 1, False, 'sms', 0.014)
assert results[2] == ('July', 7, 1, False, 'sms', 0.014)
assert results[3] == ('July', 6, 2, False, 'sms', 0.014)
assert results[0] == ('April', 1, 1, False, SMS_TYPE, 0.014)
assert results[1] == ('May', 2, 1, False, SMS_TYPE, 0.014)
assert results[2] == ('July', 7, 1, False, SMS_TYPE, 0.014)
assert results[3] == ('July', 6, 2, False, SMS_TYPE, 0.014)
def test_get_monthly_billing_data_with_multiple_rates(notify_db, notify_db_session, sample_template,
@@ -254,10 +165,10 @@ def test_get_monthly_billing_data_with_multiple_rates(notify_db, notify_db_sessi
sent_at=datetime(2017, 3, 31), status='sending', billable_units=6)
results = get_monthly_billing_data(sample_template.service_id, 2016)
assert len(results) == 4
assert results[0] == ('April', 1, 1, False, 'sms', 0.014)
assert results[1] == ('May', 2, 1, False, 'sms', 0.014)
assert results[2] == ('June', 3, 1, False, 'sms', 0.014)
assert results[3] == ('June', 4, 1, False, 'sms', 0.0175)
assert results[0] == ('April', 1, 1, False, SMS_TYPE, 0.014)
assert results[1] == ('May', 2, 1, False, SMS_TYPE, 0.014)
assert results[2] == ('June', 3, 1, False, SMS_TYPE, 0.014)
assert results[3] == ('June', 4, 1, False, SMS_TYPE, 0.0175)
def test_get_monthly_billing_data_with_no_notifications_for_daterange(notify_db, notify_db_session, sample_template):
@@ -267,23 +178,10 @@ def test_get_monthly_billing_data_with_no_notifications_for_daterange(notify_db,
def set_up_rate(notify_db, start_date, value):
rate = Rate(id=uuid.uuid4(), valid_from=start_date, rate=value, notification_type='sms')
rate = Rate(id=uuid.uuid4(), valid_from=start_date, rate=value, notification_type=SMS_TYPE)
notify_db.session.add(rate)
def test_get_yearly_billing_data_for_start_date_before_rate_returns_empty(
sample_template
):
create_rate(datetime(2016, 4, 1), 0.014, SMS_TYPE)
results = get_yearly_billing_data(
service_id=sample_template.service_id,
year=2015
)
assert not results
@freeze_time("2016-05-01")
def test_get_billing_data_for_month_where_start_date_before_rate_returns_empty(
sample_template
+1 -115
View File
@@ -8,12 +8,11 @@ import pytest
from flask import url_for, current_app
from freezegun import freeze_time
from app.dao.monthly_billing_dao import create_or_update_monthly_billing
from app.dao.services_dao import dao_remove_user_from_service
from app.dao.templates_dao import dao_redact_template
from app.dao.users_dao import save_model_user
from app.models import (
User, Organisation, Rate, Service, ServicePermission, Notification,
User, Organisation, Service, ServicePermission, Notification,
DVLA_ORG_LAND_REGISTRY,
KEY_TYPE_NORMAL, KEY_TYPE_TEAM, KEY_TYPE_TEST,
EMAIL_TYPE, SMS_TYPE, LETTER_TYPE, INTERNATIONAL_SMS_TYPE, INBOUND_SMS_TYPE,
@@ -1730,119 +1729,6 @@ def test_get_template_stats_by_month_returns_error_for_incorrect_year(
assert json.loads(response.get_data(as_text=True)) == expected_json
def test_get_yearly_billing_usage(client, notify_db, notify_db_session, sample_service):
rate = Rate(id=uuid.uuid4(), valid_from=datetime(2016, 3, 31, 23, 00), rate=0.0158, notification_type=SMS_TYPE)
notify_db.session.add(rate)
after_rate_created = datetime(2016, 6, 5)
notification = create_sample_notification(
notify_db,
notify_db_session,
created_at=after_rate_created,
sent_at=after_rate_created,
status='sending',
service=sample_service
)
create_or_update_monthly_billing(sample_service.id, after_rate_created)
response = client.get(
'/service/{}/yearly-usage?year=2016'.format(notification.service_id),
headers=[create_authorization_header()]
)
assert response.status_code == 200
assert json.loads(response.get_data(as_text=True)) == [{'credits': 1,
'billing_units': 1,
'rate_multiplier': 1,
'notification_type': SMS_TYPE,
'international': False,
'rate': 0.0158},
{'credits': 0,
'billing_units': 0,
'rate_multiplier': 1,
'notification_type': EMAIL_TYPE,
'international': False,
'rate': 0}]
def test_get_yearly_billing_usage_returns_400_if_missing_year(client, sample_service):
response = client.get(
'/service/{}/yearly-usage'.format(sample_service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 400
assert json.loads(response.get_data(as_text=True)) == {
'message': 'No valid year provided', 'result': 'error'
}
def test_get_monthly_billing_usage(client, notify_db, notify_db_session, sample_service):
rate = Rate(id=uuid.uuid4(), valid_from=datetime(2016, 3, 31, 23, 00), rate=0.0158, notification_type=SMS_TYPE)
notify_db.session.add(rate)
notification = create_sample_notification(notify_db, notify_db_session, created_at=datetime(2016, 6, 5),
sent_at=datetime(2016, 6, 5),
status='sending')
create_sample_notification(notify_db, notify_db_session, created_at=datetime(2016, 6, 5),
sent_at=datetime(2016, 6, 5),
status='sending', rate_multiplier=2)
create_sample_notification(notify_db, notify_db_session, created_at=datetime(2016, 7, 5),
sent_at=datetime(2016, 7, 5),
status='sending')
template = create_template(sample_service, template_type=EMAIL_TYPE)
create_sample_notification(notify_db, notify_db_session, created_at=datetime(2016, 6, 5),
sent_at=datetime(2016, 6, 5),
status='sending',
template=template)
response = client.get(
'/service/{}/monthly-usage?year=2016'.format(notification.service_id),
headers=[create_authorization_header()]
)
assert response.status_code == 200
actual = json.loads(response.get_data(as_text=True))
assert len(actual) == 3
assert actual == [{'month': 'June',
'international': False,
'rate_multiplier': 1,
'notification_type': SMS_TYPE,
'rate': 0.0158,
'billing_units': 1},
{'month': 'June',
'international': False,
'rate_multiplier': 2,
'notification_type': SMS_TYPE,
'rate': 0.0158,
'billing_units': 1},
{'month': 'July',
'international': False,
'rate_multiplier': 1,
'notification_type': SMS_TYPE,
'rate': 0.0158,
'billing_units': 1}]
def test_get_monthly_billing_usage_returns_400_if_missing_year(client, sample_service):
response = client.get(
'/service/{}/monthly-usage'.format(sample_service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 400
assert json.loads(response.get_data(as_text=True)) == {
'message': 'No valid year provided', 'result': 'error'
}
def test_get_monthly_billing_usage_returns_empty_list_if_no_notifications(client, notify_db, sample_service):
rate = Rate(id=uuid.uuid4(), valid_from=datetime(2016, 3, 31, 23, 00), rate=0.0158, notification_type=SMS_TYPE)
notify_db.session.add(rate)
response = client.get(
'/service/{}/monthly-usage?year=2016'.format(sample_service.id),
headers=[create_authorization_header()]
)
assert response.status_code == 200
results = json.loads(response.get_data(as_text=True))
assert results == []
def test_search_for_notification_by_to_field(client, notify_db, notify_db_session):
create_notification = partial(create_sample_notification, notify_db, notify_db_session)
notification1 = create_notification(to_field='+447700900855', normalised_to='447700900855')