mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-18 21:48:49 -04:00
Merge pull request #983 from alphagov/remove-nasty-query-from-dashboard
Remove nasty query from dashboard
This commit is contained in:
@@ -153,3 +153,70 @@ def rate_multiplier():
|
||||
(NotificationHistory.rate_multiplier == None, literal_column("'1'")), # noqa
|
||||
(NotificationHistory.rate_multiplier != None, NotificationHistory.rate_multiplier), # noqa
|
||||
]), Integer())
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def get_total_billable_units_for_sent_sms_notifications_in_date_range(start_date, end_date, service_id):
|
||||
|
||||
billable_units = 0
|
||||
total_cost = 0.0
|
||||
|
||||
rate_boundaries = discover_rate_bounds_for_billing_query(start_date, end_date)
|
||||
for rate_boundary in rate_boundaries:
|
||||
result = db.session.query(
|
||||
func.sum(
|
||||
NotificationHistory.billable_units * func.coalesce(NotificationHistory.rate_multiplier, 1)
|
||||
).label('billable_units')
|
||||
).filter(
|
||||
NotificationHistory.service_id == service_id,
|
||||
NotificationHistory.notification_type == 'sms',
|
||||
NotificationHistory.created_at >= rate_boundary['start_date'],
|
||||
NotificationHistory.created_at < rate_boundary['end_date'],
|
||||
NotificationHistory.status.in_(NOTIFICATION_STATUS_TYPES_BILLABLE)
|
||||
)
|
||||
billable_units_by_rate_boundry = result.scalar()
|
||||
if billable_units_by_rate_boundry:
|
||||
billable_units += int(billable_units_by_rate_boundry)
|
||||
total_cost += int(billable_units_by_rate_boundry) * rate_boundary['rate']
|
||||
|
||||
return billable_units, total_cost
|
||||
|
||||
|
||||
def discover_rate_bounds_for_billing_query(start_date, end_date):
|
||||
bounds = []
|
||||
rates = get_rates_for_year(start_date, end_date, SMS_TYPE)
|
||||
|
||||
def current_valid_from(index):
|
||||
return rates[index].valid_from
|
||||
|
||||
def next_valid_from(index):
|
||||
return rates[index + 1].valid_from
|
||||
|
||||
def current_rate(index):
|
||||
return rates[index].rate
|
||||
|
||||
def append_rate(rate_start_date, rate_end_date, rate):
|
||||
bounds.append({
|
||||
'start_date': rate_start_date,
|
||||
'end_date': rate_end_date,
|
||||
'rate': rate
|
||||
})
|
||||
|
||||
if len(rates) == 1:
|
||||
append_rate(start_date, end_date, current_rate(0))
|
||||
return bounds
|
||||
|
||||
for i in range(len(rates)):
|
||||
# first boundary
|
||||
if i == 0:
|
||||
append_rate(start_date, next_valid_from(i), current_rate(i))
|
||||
|
||||
# last boundary
|
||||
elif i == (len(rates) - 1):
|
||||
append_rate(current_valid_from(i), end_date, current_rate(i))
|
||||
|
||||
# other boundaries
|
||||
else:
|
||||
append_rate(current_valid_from(i), next_valid_from(i), current_rate(i))
|
||||
|
||||
return bounds
|
||||
|
||||
@@ -671,6 +671,7 @@ NOTIFICATION_STATUS_TYPES_BILLABLE = [
|
||||
NOTIFICATION_PERMANENT_FAILURE,
|
||||
]
|
||||
|
||||
|
||||
NOTIFICATION_STATUS_TYPES = [
|
||||
NOTIFICATION_CREATED,
|
||||
NOTIFICATION_SENDING,
|
||||
@@ -683,6 +684,8 @@ NOTIFICATION_STATUS_TYPES = [
|
||||
NOTIFICATION_PERMANENT_FAILURE,
|
||||
]
|
||||
|
||||
NOTIFICATION_STATUS_TYPES_NON_BILLABLE = list(set(NOTIFICATION_STATUS_TYPES) - set(NOTIFICATION_STATUS_TYPES_BILLABLE))
|
||||
|
||||
NOTIFICATION_STATUS_TYPES_ENUM = db.Enum(*NOTIFICATION_STATUS_TYPES, name='notify_status_type')
|
||||
|
||||
|
||||
@@ -1099,6 +1102,12 @@ class Rate(db.Model):
|
||||
rate = db.Column(db.Float(asdecimal=False), nullable=False)
|
||||
notification_type = db.Column(notification_types, index=True, nullable=False)
|
||||
|
||||
def __str__(self):
|
||||
the_string = "{}".format(self.rate)
|
||||
the_string += " {}".format(self.notification_type)
|
||||
the_string += " {}".format(self.valid_from)
|
||||
return the_string
|
||||
|
||||
|
||||
class JobStatistics(db.Model):
|
||||
__tablename__ = 'job_statistics'
|
||||
|
||||
@@ -10,6 +10,7 @@ from flask import (
|
||||
)
|
||||
from sqlalchemy.orm.exc import NoResultFound
|
||||
|
||||
from app import redis_store
|
||||
from app.dao import notification_usage_dao, notifications_dao
|
||||
from app.dao.dao_utils import dao_rollback
|
||||
from app.dao.api_key_dao import (
|
||||
@@ -17,6 +18,8 @@ from app.dao.api_key_dao import (
|
||||
get_model_api_keys,
|
||||
get_unsigned_secret,
|
||||
expire_api_key)
|
||||
from app.dao.date_util import get_financial_year
|
||||
from app.dao.notification_usage_dao import get_total_billable_units_for_sent_sms_notifications_in_date_range
|
||||
from app.dao.services_dao import (
|
||||
dao_fetch_service_by_id,
|
||||
dao_fetch_all_services,
|
||||
@@ -60,6 +63,7 @@ from app.schemas import (
|
||||
detailed_service_schema
|
||||
)
|
||||
from app.utils import pagination_links
|
||||
from notifications_utils.clients.redis import sms_billable_units_cache_key
|
||||
|
||||
service_blueprint = Blueprint('service', __name__)
|
||||
|
||||
@@ -447,6 +451,39 @@ def get_monthly_template_stats(service_id):
|
||||
raise InvalidRequest('Year must be a number', status_code=400)
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/yearly-sms-billable-units')
|
||||
def get_yearly_sms_billable_units(service_id):
|
||||
cache_key = sms_billable_units_cache_key(service_id)
|
||||
cached_billable_sms_units = redis_store.get_all_from_hash(cache_key)
|
||||
if cached_billable_sms_units:
|
||||
return jsonify({
|
||||
'billable_sms_units': int(cached_billable_sms_units[b'billable_units']),
|
||||
'total_cost': float(cached_billable_sms_units[b'total_cost'])
|
||||
})
|
||||
else:
|
||||
try:
|
||||
start_date, end_date = get_financial_year(int(request.args.get('year')))
|
||||
except (ValueError, TypeError) as e:
|
||||
current_app.logger.exception(e)
|
||||
return jsonify(result='error', message='No valid year provided'), 400
|
||||
|
||||
billable_units, total_cost = get_total_billable_units_for_sent_sms_notifications_in_date_range(
|
||||
start_date,
|
||||
end_date,
|
||||
service_id)
|
||||
|
||||
cached_values = {
|
||||
'billable_units': billable_units,
|
||||
'total_cost': total_cost
|
||||
}
|
||||
|
||||
redis_store.set_hash_and_expire(cache_key, cached_values, expire_in_seconds=60)
|
||||
return jsonify({
|
||||
'billable_sms_units': billable_units,
|
||||
'total_cost': total_cost
|
||||
})
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/yearly-usage')
|
||||
def get_yearly_billing_usage(service_id):
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user