Added new endpoints to return the yearly-usage and monthly-usage for a given financial year and service id.

Since the response has changed I have created new endpoints so that the deployments for Admin are more managable.

Removed print statements from some tests.
This commit is contained in:
Rebecca Law
2017-04-27 10:00:09 +01:00
committed by Ken Tsang
parent e1e55edd9c
commit a186fc95be
7 changed files with 144 additions and 51 deletions

View File

@@ -1,6 +1,4 @@
from datetime import datetime
from decimal import Decimal
from sqlalchemy import func
from app import db
@@ -23,9 +21,9 @@ def get_yearly_billing_data(service_id, year):
result = []
for r, n in zip(rates, rates[1:]):
result.append(
sms_billing_data_query(str(r.rate), service_id, r.valid_from, n.valid_from))
sms_billing_data_query(r.rate, service_id, r.valid_from, n.valid_from))
result.append(sms_billing_data_query(str(rates[-1].rate), service_id, rates[-1].valid_from, end_date))
result.append(sms_billing_data_query(rates[-1].rate, service_id, rates[-1].valid_from, end_date))
result.append(email_billing_data_query(service_id, start_date, end_date))
@@ -47,7 +45,6 @@ def email_billing_data_query(service_id, start_date, end_date):
result = db.session.query(
func.count(NotificationHistory.id),
NotificationHistory.notification_type,
"0",
NotificationHistory.rate_multiplier,
NotificationHistory.international,
NotificationHistory.phone_prefix
@@ -59,17 +56,14 @@ def email_billing_data_query(service_id, start_date, end_date):
NotificationHistory.international,
NotificationHistory.phone_prefix
).first()
if not result:
return 0, EMAIL_TYPE, Decimal("0"), None, False, None
else:
return result
return tuple(result) + (0,)
def sms_billing_data_query(rate, service_id, start_date, end_date):
result = db.session.query(
func.sum(NotificationHistory.billable_units),
NotificationHistory.notification_type,
rate,
NotificationHistory.rate_multiplier,
NotificationHistory.international,
NotificationHistory.phone_prefix
@@ -82,9 +76,9 @@ def sms_billing_data_query(rate, service_id, start_date, end_date):
NotificationHistory.phone_prefix
).first()
if not result:
return 0, SMS_TYPE, Decimal("0"), None, False, None
return 0, SMS_TYPE, None, False, None, 0
else:
return result
return tuple(result) + (rate,)
def get_rates_for_year(start_date, end_date, notification_type):
@@ -94,11 +88,10 @@ def get_rates_for_year(start_date, end_date, notification_type):
def sms_billing_data_per_month_query(rate, service_id, start_date, end_date):
month = get_london_month_from_utc_column(NotificationHistory.created_at)
return db.session.query(
return [result + (rate,) for result in db.session.query(
month,
func.sum(NotificationHistory.billable_units),
NotificationHistory.notification_type,
rate,
NotificationHistory.rate_multiplier,
NotificationHistory.international,
NotificationHistory.phone_prefix
@@ -112,15 +105,15 @@ def sms_billing_data_per_month_query(rate, service_id, start_date, end_date):
).order_by(
month
).all()
]
def email_billing_data_per_month_query(rate, service_id, start_date, end_date):
month = get_london_month_from_utc_column(NotificationHistory.created_at)
return db.session.query(
return [result + (rate,) for result in db.session.query(
month,
func.count(NotificationHistory.id),
NotificationHistory.notification_type,
rate,
NotificationHistory.rate_multiplier,
NotificationHistory.international,
NotificationHistory.phone_prefix
@@ -134,18 +127,19 @@ def email_billing_data_per_month_query(rate, service_id, start_date, end_date):
).order_by(
month
).all()
]
@statsd(namespace="dao")
def get_notification_billing_data_per_month(service_id, year):
def get_monthly_billing_data(service_id, year):
start_date, end_date = get_financial_year(year)
rates = get_rates_for_year(start_date, end_date, SMS_TYPE)
result = []
for r, n in zip(rates, rates[1:]):
result.extend(sms_billing_data_per_month_query(str(r.rate), service_id, r.valid_from, n.valid_from))
result.extend(sms_billing_data_per_month_query(str(rates[-1].rate), service_id, rates[-1].valid_from, end_date))
result.extend(sms_billing_data_per_month_query(r.rate, service_id, r.valid_from, n.valid_from))
result.extend(sms_billing_data_per_month_query(rates[-1].rate, service_id, rates[-1].valid_from, end_date))
result.extend(email_billing_data_per_month_query("0", service_id, start_date, end_date))
result.extend(email_billing_data_per_month_query(0, service_id, start_date, end_date))
return [(datetime.strftime(x[0], "%B"), x[1], x[2], x[3], x[4], x[5], x[6]) for x in result]

View File

@@ -968,5 +968,5 @@ class Rate(db.Model):
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
valid_from = db.Column(db.DateTime, nullable=False)
rate = db.Column(db.Numeric(), nullable=False)
rate = db.Column(db.Numeric(asdecimal=False), nullable=False)
notification_type = db.Column(notification_types, index=True, nullable=False)

View File

@@ -1,4 +1,5 @@
import itertools
import json
from datetime import datetime
from flask import (
@@ -8,6 +9,7 @@ from flask import (
)
from sqlalchemy.orm.exc import NoResultFound
from app.dao import notification_usage_dao
from app.dao.dao_utils import dao_rollback
from app.dao.api_key_dao import (
save_model_api_key,
@@ -411,3 +413,39 @@ def get_monthly_template_stats(service_id):
))
except ValueError:
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 = [{"billing_units": x[0],
"notification_type": x[1],
"rate_multiplier": x[2],
"international": x[3],
"phone_prefix": 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],
"notification_type": x[2],
"rate_multiplier": x[3],
"international": x[4],
"phone_prefix": x[5],
"rate": x[6]
} for x in results]
return json.dumps(json_results)
except TypeError:
return jsonify(result='error', message='No valid year provided'), 400