mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-16 20:49:00 -04:00
Update page to handle international stats
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -82,22 +82,12 @@
|
||||
smallest=True
|
||||
) }}
|
||||
<ul>
|
||||
{% if month.rate_groups %}
|
||||
rate_group {{ month.rate_groups|length }}
|
||||
{% for rate in month.rate_groups %}
|
||||
{% if rate.free_units %}
|
||||
<li class="tabular-numbers">{{ "{:,}".format(rate.free_units) }} {% if rate.international %}international{% else %}UK{% endif %} free text messages</li>
|
||||
{% endif %}
|
||||
{% if rate.units %}
|
||||
<li class="tabular-numbers">{{ "{:,}".format(rate.units) }} {% if rate.international %}international{% else %}UK{% endif %} text messages at
|
||||
{{- ' {:.2f}p'.format(rate.multiplier * sms_rate * 100) }}</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
{% if month.paid %}
|
||||
<li class="tabular-numbers">{{ "{:,}".format(month.paid) }} text messages at
|
||||
{{- ' {:.2f}p'.format(sms_rate * 100) }}</li>
|
||||
{% endif %}
|
||||
{% if month.free %}
|
||||
<li class="tabular-numbers">{{ "{:,}".format(month.free) }} free text messages</li>
|
||||
{% endif %}
|
||||
{% if month.paid %}
|
||||
<li class="tabular-numbers">{{ "{:,}".format(month.paid) }} text messages at
|
||||
{{- ' {:.2f}p'.format(sms_rate * 100) }}</li>
|
||||
{% endif %}
|
||||
{% if not (month.free or month.paid) %}
|
||||
<li aria-hidden="true">–</li>
|
||||
|
||||
Reference in New Issue
Block a user