mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-20 22:39:43 -04:00
Merge pull request #928 from alphagov/update-billing-api-with-rate
Update billing api with rate
This commit is contained in:
18
app/dao/date_util.py
Normal file
18
app/dao/date_util.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from datetime import datetime
|
||||
|
||||
import pytz
|
||||
|
||||
|
||||
def get_financial_year(year):
|
||||
return get_april_fools(year), get_april_fools(year + 1)
|
||||
|
||||
|
||||
def get_april_fools(year):
|
||||
"""
|
||||
This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC,
|
||||
the tzinfo is lastly removed from the datetime becasue the database stores the timestamps without timezone.
|
||||
:param year: the year to calculate the April 1, 00:00 BST for
|
||||
:return: the datetime of April 1 for the given year, for example 2016 = 2016-03-31 23:00:00
|
||||
"""
|
||||
return pytz.timezone('Europe/London').localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace(
|
||||
tzinfo=None)
|
||||
138
app/dao/notification_usage_dao.py
Normal file
138
app/dao/notification_usage_dao.py
Normal file
@@ -0,0 +1,138 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Float, Integer
|
||||
from sqlalchemy import func, case, cast
|
||||
from sqlalchemy import literal_column
|
||||
|
||||
from app import db
|
||||
from app.dao.date_util import get_financial_year
|
||||
from app.models import (NotificationHistory,
|
||||
Rate,
|
||||
NOTIFICATION_STATUS_TYPES_BILLABLE,
|
||||
KEY_TYPE_TEST,
|
||||
SMS_TYPE,
|
||||
EMAIL_TYPE)
|
||||
from app.statsd_decorators import statsd
|
||||
from app.utils import get_london_month_from_utc_column
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def get_yearly_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.append(
|
||||
sms_yearly_billing_data_query(r.rate, service_id, r.valid_from, n.valid_from))
|
||||
|
||||
result.append(sms_yearly_billing_data_query(rates[-1].rate, service_id, rates[-1].valid_from, end_date))
|
||||
|
||||
result.append(email_yearly_billing_data_query(service_id, start_date, end_date))
|
||||
|
||||
return sum(result, [])
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
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(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))
|
||||
|
||||
return [(datetime.strftime(x[0], "%B"), x[1], x[2], x[3], x[4], x[5]) for x in result]
|
||||
|
||||
|
||||
def billing_data_filter(notification_type, start_date, end_date, service_id):
|
||||
return [
|
||||
NotificationHistory.notification_type == notification_type,
|
||||
NotificationHistory.created_at >= start_date,
|
||||
NotificationHistory.created_at < end_date,
|
||||
NotificationHistory.service_id == service_id,
|
||||
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE),
|
||||
NotificationHistory.key_type != KEY_TYPE_TEST
|
||||
]
|
||||
|
||||
|
||||
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_year(start_date, end_date, notification_type):
|
||||
return Rate.query.filter(Rate.valid_from >= start_date, Rate.valid_from < end_date,
|
||||
Rate.notification_type == notification_type).order_by(Rate.valid_from).all()
|
||||
|
||||
|
||||
def sms_billing_data_per_month_query(rate, service_id, start_date, end_date):
|
||||
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
||||
result = db.session.query(
|
||||
month,
|
||||
func.sum(NotificationHistory.billable_units),
|
||||
rate_multiplier(),
|
||||
NotificationHistory.international,
|
||||
NotificationHistory.notification_type,
|
||||
cast(rate, Float())
|
||||
).filter(
|
||||
*billing_data_filter(SMS_TYPE, start_date, end_date, service_id)
|
||||
).group_by(
|
||||
NotificationHistory.notification_type,
|
||||
month,
|
||||
NotificationHistory.rate_multiplier,
|
||||
NotificationHistory.international
|
||||
).order_by(
|
||||
month,
|
||||
rate_multiplier()
|
||||
).all()
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def rate_multiplier():
|
||||
return cast(case([
|
||||
(NotificationHistory.rate_multiplier == None, literal_column("'1'")), # noqa
|
||||
(NotificationHistory.rate_multiplier != None, NotificationHistory.rate_multiplier), # noqa
|
||||
]), Integer())
|
||||
@@ -1,5 +1,4 @@
|
||||
import functools
|
||||
import pytz
|
||||
from datetime import (
|
||||
datetime,
|
||||
timedelta,
|
||||
@@ -12,6 +11,7 @@ from sqlalchemy.orm import joinedload
|
||||
|
||||
from app import db, create_uuid
|
||||
from app.dao import days_ago
|
||||
from app.dao.date_util import get_financial_year
|
||||
from app.models import (
|
||||
Service,
|
||||
Notification,
|
||||
@@ -243,13 +243,15 @@ def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1, page
|
||||
def get_notification_billable_unit_count_per_month(service_id, year):
|
||||
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
||||
|
||||
start_date, end_date = get_financial_year(year)
|
||||
notifications = db.session.query(
|
||||
month,
|
||||
func.sum(NotificationHistory.billable_units)
|
||||
).filter(
|
||||
NotificationHistory.billable_units != 0,
|
||||
NotificationHistory.service_id == service_id,
|
||||
NotificationHistory.created_at.between(*get_financial_year(year)),
|
||||
NotificationHistory.created_at >= start_date,
|
||||
NotificationHistory.created_at < end_date
|
||||
).group_by(
|
||||
month
|
||||
).order_by(
|
||||
@@ -410,21 +412,6 @@ def dao_timeout_notifications(timeout_period_in_seconds):
|
||||
return updated
|
||||
|
||||
|
||||
def get_financial_year(year):
|
||||
return get_april_fools(year), get_april_fools(year + 1)
|
||||
|
||||
|
||||
def get_april_fools(year):
|
||||
"""
|
||||
This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC,
|
||||
the tzinfo is lastly removed from the datetime becasue the database stores the timestamps without timezone.
|
||||
:param year: the year to calculate the April 1, 00:00 BST for
|
||||
:return: the datetime of April 1 for the given year, for example 2016 = 2016-03-31 23:00:00
|
||||
"""
|
||||
return pytz.timezone('Europe/London').localize(datetime(year, 4, 1, 0, 0, 0)).astimezone(pytz.UTC).replace(
|
||||
tzinfo=None)
|
||||
|
||||
|
||||
def get_total_sent_notifications_in_date_range(start_date, end_date, notification_type):
|
||||
result = db.session.query(
|
||||
func.count(NotificationHistory.id).label('count')
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
from sqlalchemy import desc
|
||||
|
||||
from app import db
|
||||
from app.models import Rate
|
||||
|
||||
|
||||
def get_rate_for_type_and_date(notification_type, date_sent):
|
||||
return db.session.query(Rate).filter(Rate.notification_type == notification_type,
|
||||
Rate.valid_from <= date_sent
|
||||
).order_by(Rate.valid_from.desc()
|
||||
).limit(1).first()
|
||||
@@ -229,6 +229,7 @@ def _stats_for_service_query(service_id):
|
||||
def dao_fetch_monthly_historical_stats_by_template_for_service(service_id, year):
|
||||
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
||||
|
||||
start_date, end_date = get_financial_year(year)
|
||||
sq = db.session.query(
|
||||
NotificationHistory.template_id,
|
||||
NotificationHistory.status,
|
||||
@@ -236,7 +237,9 @@ def dao_fetch_monthly_historical_stats_by_template_for_service(service_id, year)
|
||||
func.count().label('count')
|
||||
).filter(
|
||||
NotificationHistory.service_id == service_id,
|
||||
NotificationHistory.created_at.between(*get_financial_year(year))
|
||||
NotificationHistory.created_at >= start_date,
|
||||
NotificationHistory.created_at < end_date
|
||||
|
||||
).group_by(
|
||||
month,
|
||||
NotificationHistory.template_id,
|
||||
@@ -262,6 +265,7 @@ def dao_fetch_monthly_historical_stats_by_template_for_service(service_id, year)
|
||||
def dao_fetch_monthly_historical_stats_for_service(service_id, year):
|
||||
month = get_london_month_from_utc_column(NotificationHistory.created_at)
|
||||
|
||||
start_date, end_date = get_financial_year(year)
|
||||
rows = db.session.query(
|
||||
NotificationHistory.notification_type,
|
||||
NotificationHistory.status,
|
||||
@@ -269,7 +273,8 @@ def dao_fetch_monthly_historical_stats_for_service(service_id, year):
|
||||
func.count(NotificationHistory.id).label('count')
|
||||
).filter(
|
||||
NotificationHistory.service_id == service_id,
|
||||
NotificationHistory.created_at.between(*get_financial_year(year)),
|
||||
NotificationHistory.created_at >= start_date,
|
||||
NotificationHistory.created_at < end_date
|
||||
).group_by(
|
||||
NotificationHistory.notification_type,
|
||||
NotificationHistory.status,
|
||||
|
||||
@@ -670,7 +670,7 @@ class Notification(db.Model):
|
||||
|
||||
international = db.Column(db.Boolean, nullable=False, default=False)
|
||||
phone_prefix = db.Column(db.String, nullable=True)
|
||||
rate_multiplier = db.Column(db.Float(), nullable=True)
|
||||
rate_multiplier = db.Column(db.Float(asdecimal=False), nullable=True)
|
||||
|
||||
@property
|
||||
def personalisation(self):
|
||||
@@ -850,7 +850,7 @@ class NotificationHistory(db.Model, HistoryModel):
|
||||
|
||||
international = db.Column(db.Boolean, nullable=False, default=False)
|
||||
phone_prefix = db.Column(db.String, nullable=True)
|
||||
rate_multiplier = db.Column(db.Float(), nullable=True)
|
||||
rate_multiplier = db.Column(db.Float(asdecimal=False), nullable=True)
|
||||
|
||||
@classmethod
|
||||
def from_original(cls, notification):
|
||||
@@ -971,5 +971,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.Float(asdecimal=False), nullable=False)
|
||||
notification_type = db.Column(notification_types, index=True, nullable=False)
|
||||
|
||||
@@ -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,38 @@ 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 = [{"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
|
||||
|
||||
Reference in New Issue
Block a user