From 53e78c2b609b3841d4172f96c2e8d961b0c42d2e Mon Sep 17 00:00:00 2001 From: Ken Tsang Date: Wed, 26 Apr 2017 17:09:07 +0100 Subject: [PATCH] Update page to handle international stats --- app/main/views/dashboard.py | 89 +++------- app/notify_client/service_api_client.py | 7 +- app/templates/views/usage.html | 22 +-- tests/app/main/views/test_dashboard.py | 226 +++++------------------- tests/conftest.py | 206 ++++++++++----------- 5 files changed, 168 insertions(+), 382 deletions(-) diff --git a/app/main/views/dashboard.py b/app/main/views/dashboard.py index 3f8c66cff..347f522df 100644 --- a/app/main/views/dashboard.py +++ b/app/main/views/dashboard.py @@ -102,19 +102,18 @@ def template_history(service_id): @user_has_permissions('manage_settings', admin_override=True) def usage(service_id): year, current_financial_year = requested_and_current_financial_year(request) - monthly_breakdown = get_free_paid_breakdown_for_billable_units( - year, service_api_client.get_billable_units(service_id, year)) return render_template( 'views/usage.html', - months=list(monthly_breakdown), + months=list(get_free_paid_breakdown_for_billable_units( + year, service_api_client.get_billable_units(service_id, year) + )), selected_year=year, years=get_tuples_of_financial_years( partial(url_for, '.usage', service_id=service_id), start=current_financial_year - 1, end=current_financial_year + 1, ), - # **calculate_usage(dict(monthly_breakdown)) - **calculate_usage(service_api_client.get_service_usage(service_id, year)['data'], monthly_breakdown) + **calculate_usage(service_api_client.get_service_usage(service_id, year)) ) @@ -190,7 +189,7 @@ def get_dashboard_partials(service_id): **calculate_usage(service_api_client.get_service_usage( service_id, get_current_financial_year(), - )['data']) + )) ), } @@ -202,15 +201,13 @@ def get_dashboard_totals(statistics): return statistics -def calculate_usage(usage, monthly_breakdown): +def calculate_usage(usage): # TODO: Don't hardcode these - get em from the API sms_free_allowance = 250000 - sms_rate = 0.0165 + sms_rate = usage[0].get("rate", 0) + sms_sent = get_sum_billing_units(breakdown for breakdown in usage if breakdown['notification_type'] == 'sms') + emails_sent = [breakdown["billing_units"] for breakdown in usage if breakdown['notification_type'] == 'email'][0] - sms_sent = get_sum_units(usage.get('sms_breakdown', [])) - emails_sent = usage.get('email_count', 0) - sms_breakdown = get_sms_breakdown_adjusted_free_allowance( - usage.get('sms_breakdown', []), sms_free_allowance) return { 'emails_sent': emails_sent, 'sms_free_allowance': sms_free_allowance, @@ -218,8 +215,6 @@ def calculate_usage(usage, monthly_breakdown): 'sms_allowance_remaining': max(0, (sms_free_allowance - sms_sent)), 'sms_chargeable': max(0, sms_sent - sms_free_allowance), 'sms_rate': sms_rate, - 'sms_breakdown': sms_breakdown, - 'sms_free_units_used': usage.get('free_units_used', 0) } @@ -266,67 +261,29 @@ def get_months_for_year(start, end, year): return [datetime(year, month, 1) for month in range(start, end)] -def get_sum_billable_units(usage): - return sum(rate['units'] * rate['multiplier'] for rate in usage) +def get_sum_billing_units(billing_units, month=None): + if month: + return sum(b['billing_units'] * b.get('rate_multiplier', 1) for b in billing_units if b['month'] == month) + return sum(b['billing_units'] * b.get('rate_multiplier', 1) for b in billing_units) -def get_sum_units(usage): - return sum(rate['units'] for rate in usage) - - -def get_sum_free_units(usage): - return sum(rate.get('free_units', 0) * rate['multiplier'] for rate in usage) - - -def get_free_paid_breakdown_for_billable_units(year, billable_units): +def get_free_paid_breakdown_for_billable_units(year, billing_units): cumulative = 0 for month in get_months_for_financial_year(year): previous_cumulative = cumulative - monthly_usage = get_sum_billable_units(billable_units.get(month, [])) + monthly_usage = get_sum_billing_units(billing_units, month) cumulative += monthly_usage breakdown = get_free_paid_breakdown_for_month( - cumulative, previous_cumulative, billable_units.get(month, []) + cumulative, previous_cumulative, + [billing_month for billing_month in billing_units if billing_month['month'] == month] ) yield { 'name': month, 'paid': breakdown['paid'], - 'free': breakdown['free'], - 'sms_breakdown': breakdown.get('sms_breakdown', []), - 'free_units_used': get_sum_free_units(breakdown.get('sms_breakdown', [])) + 'free': breakdown['free'] } -def get_sms_breakdown_adjusted_free_allowance(breakdown_list, free_allowance=0): - sms_breakdown = [] - - for breakdown in breakdown_list: - free_units = 0 - if free_allowance > 0: - normalised_units = breakdown["units"] * breakdown["multiplier"] - billable_units = normalised_units - free_allowance - - if billable_units <= 0: - free_units = breakdown["units"] - free_allowance = abs(billable_units) - else: - free_units = int(free_allowance / breakdown["multiplier"]) - - if free_allowance < normalised_units: - free_allowance = 0 - else: - free_allowance -= normalised_units - - adj_breakdown = { - "international": breakdown["international"], - "free_units": free_units, - "units": breakdown["units"] - free_units, - "multiplier": breakdown["multiplier"] - } - sms_breakdown.append(adj_breakdown) - - return sms_breakdown - - def get_free_paid_breakdown_for_month( cumulative, previous_cumulative, @@ -334,25 +291,23 @@ def get_free_paid_breakdown_for_month( ): allowance = 250000 - total_monthly_billable_units = get_sum_billable_units(monthly_usage) + total_monthly_billing_units = get_sum_billing_units(monthly_usage) if cumulative < allowance: return { 'paid': 0, - 'free': total_monthly_billable_units, + 'free': total_monthly_billing_units, } elif previous_cumulative < allowance: remaining_allowance = allowance - previous_cumulative return { - 'paid': total_monthly_billable_units - remaining_allowance, + 'paid': total_monthly_billing_units - remaining_allowance, 'free': remaining_allowance, - 'sms_breakdown': get_sms_breakdown_adjusted_free_allowance(monthly_usage, remaining_allowance) } else: return { - 'paid': total_monthly_billable_units, + 'paid': total_monthly_billing_units, 'free': 0, - 'sms_breakdown': get_sms_breakdown_adjusted_free_allowance(monthly_usage) } diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 5394ad1a6..dc48d0043 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -217,7 +217,7 @@ class ServiceAPIClient(NotifyAdminAPIClient): def get_service_usage(self, service_id, year=None): return self.get( - '/service/{0}/fragment/aggregate_statistics'.format(service_id), + '/service/{0}/yearly-usage'.format(service_id), params=dict(year=year) ) @@ -231,7 +231,10 @@ class ServiceAPIClient(NotifyAdminAPIClient): return self.put(url='/service/{}/whitelist'.format(service_id), data=data) def get_billable_units(self, service_id, year): - return self.get(url='/service/{}/billable-units?year={}'.format(service_id, year)) + return self.get( + '/service/{0}/monthly-usage'.format(service_id), + params=dict(year=year) + ) class ServicesBrowsableItem(BrowsableItem): diff --git a/app/templates/views/usage.html b/app/templates/views/usage.html index 6ccf58f7e..3b8fe412d 100644 --- a/app/templates/views/usage.html +++ b/app/templates/views/usage.html @@ -82,22 +82,12 @@ smallest=True ) }}