Update the organsition usage endpoint to use the new query.

This endpoint may need to change, but we'd like to see how this performs, so we'll test this with a real data set. Then come back to make sure the format is correct and check for missing tests for the endpoint,
This commit is contained in:
Rebecca Law
2020-02-24 17:02:25 +00:00
parent b1b457eea0
commit a2d18f8598
4 changed files with 34 additions and 14 deletions

View File

@@ -594,7 +594,6 @@ def fetch_email_usage_for_organisation(organisation_id, start_date, end_date):
@statsd(namespace="dao")
def fetch_sms_billing_for_organisation(organisation_id, start_date, end_date):
# ASSUMPTION: AnnualBilling has been populated for year.
free_allowance_remainder = fetch_sms_free_allowance_remainder(start_date).subquery()
@@ -675,15 +674,15 @@ def fetch_usage_year_for_organisation(organisation_id, year):
for usage in sms_usages:
service_with_usage[str(usage.service_id)] = {
'service_id': usage.service_id,
'service_name': usage.service_name,
'free_sms_limit': usage.free_sms_fragment_limit,
'sms_remainder': usage.sms_remainder,
'sms_billable_units': usage.sms_billable_units,
'chargeable_billable_sms': usage.chargeable_billable_sms,
'sms_cost': usage.sms_cost,
'letter_cost': 0,
'emails_sent': 0
'service_id': usage.service_id,
'service_name': usage.service_name,
'free_sms_limit': usage.free_sms_fragment_limit,
'sms_remainder': usage.sms_remainder,
'sms_billable_units': usage.sms_billable_units,
'chargeable_billable_sms': usage.chargeable_billable_sms,
'sms_cost': usage.sms_cost,
'letter_cost': 0,
'emails_sent': 0
}
for letter_usage in letter_usages:
service_with_usage[str(letter_usage.service_id)]['letter_cost'] = letter_usage.letter_cost

View File

@@ -1,7 +1,10 @@
from datetime import datetime
from flask import abort, Blueprint, jsonify, request, current_app
from sqlalchemy.exc import IntegrityError
from app.config import QueueNames
from app.dao.fact_billing_dao import fetch_usage_year_for_organisation
from app.dao.organisation_dao import (
dao_create_organisation,
dao_get_organisations,
@@ -127,9 +130,9 @@ def get_organisation_services(organisation_id):
@organisation_blueprint.route('/<uuid:organisation_id>/services-with-usage', methods=['GET'])
def get_organisation_services_usage(organisation_id):
services = dao_get_organisation_services(organisation_id)
sorted_services = sorted(services, key=lambda s: (-s.active, s.name))
return jsonify([s.serialize_for_org_dashboard() for s in sorted_services])
services = fetch_usage_year_for_organisation(organisation_id, datetime.utcnow().year)
return jsonify(services=services)
@organisation_blueprint.route('/<uuid:organisation_id>/users/<uuid:user_id>', methods=['POST'])

View File

@@ -1,3 +1,5 @@
from datetime import datetime
import uuid
import pytest
@@ -11,6 +13,8 @@ from tests.app.db import (
create_organisation,
create_service,
create_user,
create_template,
create_ft_billing
)
@@ -737,3 +741,16 @@ def test_is_organisation_name_unique_returns_400_when_name_does_not_exist(admin_
assert response["message"][0]["org_id"] == ["Can't be empty"]
assert response["message"][1]["name"] == ["Can't be empty"]
def test_get_organisation_services_usage(admin_request, notify_db_session):
org = create_organisation(name='Organisation without live services')
service = create_service()
template = create_template(service=service)
dao_add_service_to_organisation(service=service, organisation_id=org.id)
create_ft_billing(bst_date=datetime.utcnow().date(), template=template, billable_unit=19, notifications_sent=19)
response = admin_request.get(
'organisation.get_organisation_services_usage',
organisation_id=org.id
)
assert len(response) == 1

View File

@@ -122,7 +122,8 @@ def test_validate_date_is_within_a_financial_year_when_input_is_not_a_date(start
def test_get_usage_for_all_services(notify_db_session, admin_request):
org, org_2, service, service_2, service_3, service_sms_only = set_up_usage_data(datetime(2019, 5, 1))
org, org_2, service, service_2, service_3, service_sms_only, \
org_with_emails, service_with_emails = set_up_usage_data(datetime(2019, 5, 1))
response = admin_request.get("platform_stats.get_usage_for_all_services",
start_date='2019-05-01',
end_date='2019-06-30')