Rename to performance_dashboard

Fix totals to return totals for all time rather than for date range.
Added more test data
This commit is contained in:
Rebecca Law
2021-03-10 11:12:29 +00:00
parent b06850e611
commit 11d10d5293
13 changed files with 149 additions and 89 deletions

View File

@@ -150,7 +150,7 @@ def register_blueprint(application):
from app.organisation.rest import organisation_blueprint
from app.organisation.invite_rest import organisation_invite_blueprint
from app.complaint.complaint_rest import complaint_blueprint
from app.performance_platform.rest import performance_platform_blueprint
from app.performance_dashboard.rest import performance_dashboard_blueprint
from app.platform_stats.rest import platform_stats_blueprint
from app.template_folder.rest import template_folder_blueprint
from app.letter_branding.letter_branding_rest import letter_branding_blueprint
@@ -229,8 +229,8 @@ def register_blueprint(application):
complaint_blueprint.before_request(requires_admin_auth)
application.register_blueprint(complaint_blueprint)
performance_platform_blueprint.before_request(requires_admin_auth)
application.register_blueprint(performance_platform_blueprint)
performance_dashboard_blueprint.before_request(requires_admin_auth)
application.register_blueprint(performance_dashboard_blueprint)
platform_stats_blueprint.before_request(requires_admin_auth)
application.register_blueprint(platform_stats_blueprint, url_prefix='/platform-stats')

View File

@@ -443,8 +443,7 @@ def get_total_sent_notifications_for_day_and_type(day, notification_type):
def get_total_notifications_for_date_range(start_date, end_date):
result = db.session.query(
query = db.session.query(
FactNotificationStatus.bst_date.cast(db.Text).label("bst_date"),
func.sum(case(
[
@@ -463,14 +462,17 @@ def get_total_notifications_for_date_range(start_date, end_date):
else_=0)).label('letters'),
).filter(
FactNotificationStatus.key_type != KEY_TYPE_TEST,
FactNotificationStatus.bst_date >= start_date,
FactNotificationStatus.bst_date <= end_date
).group_by(
FactNotificationStatus.bst_date
).order_by(
FactNotificationStatus.bst_date
)
return result.all()
if start_date and end_date:
query = query.filter(
FactNotificationStatus.bst_date >= start_date,
FactNotificationStatus.bst_date <= end_date
)
return query.all()
def fetch_monthly_notification_statuses_per_service(start_date, end_date):

View File

@@ -633,4 +633,4 @@ def get_live_services_with_organisation():
Service.name
)
return query.all()
return query.all()

View File

View File

@@ -1,8 +1,8 @@
performance_platform_request = {
performance_dashboard_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "Performance platform request schema",
"description": "Performance dashboard request schema",
"type": "object",
"title": "Performance platform request",
"title": "Performance dashboard request",
"properties": {
"start_date": {"type": ["string", "null"], "format": "date"},
"end_date": {"type": ["string", "null"], "format": "date"},

View File

@@ -6,29 +6,30 @@ from app.dao.fact_notification_status_dao import get_total_notifications_for_dat
from app.dao.fact_processing_time_dao import get_processing_time_percentage_for_date_range
from app.dao.services_dao import get_live_services_with_organisation
from app.errors import register_errors
from app.performance_platform.performance_platform_schema import performance_platform_request
from app.performance_dashboard.performance_dashboard_schema import performance_dashboard_request
from app.schema_validation import validate
performance_platform_blueprint = Blueprint('performance_platform', __name__, url_prefix='/performance-platform')
performance_dashboard_blueprint = Blueprint('performance_dashboard', __name__, url_prefix='/performance-dashboard')
register_errors(performance_platform_blueprint)
register_errors(performance_dashboard_blueprint)
@performance_platform_blueprint.route('')
def get_performance_platform():
@performance_dashboard_blueprint.route('')
def get_performance_dashboard():
# All statistics are as of last night this matches the existing performance platform
# and avoids the need to query notifications.
if request.args:
# Is it ok to reuse this? - should probably create a new one
validate(request.args, performance_platform_request)
validate(request.args, performance_dashboard_request)
# If start and end date are not set, we are expecting today's stats.
today = str(datetime.utcnow().date())
start_date = datetime.strptime(request.args.get('start_date', today), '%Y-%m-%d').date()
end_date = datetime.strptime(request.args.get('end_date', today), '%Y-%m-%d').date()
results = get_total_notifications_for_date_range(start_date=start_date, end_date=end_date)
total_notifications, emails, sms, letters = transform_results_into_totals(results)
total_for_all_time = get_total_notifications_for_date_range(start_date=None, end_date=None)
total_notifications, emails, sms, letters = transform_results_into_totals(total_for_all_time)
totals_for_date_range = get_total_notifications_for_date_range(start_date=start_date, end_date=end_date)
processing_time_results = get_processing_time_percentage_for_date_range(start_date=start_date, end_date=end_date)
services = get_live_services_with_organisation()
stats = {
@@ -36,7 +37,7 @@ def get_performance_platform():
"email_notifications": emails,
"sms_notifications": sms,
"letter_notifications": letters,
"notifications_by_type": transform_results_to_json(results),
"notifications_by_type": transform_into_notification_by_type_json(totals_for_date_range),
"processing_time": transform_processing_time_results_to_json(processing_time_results),
"live_service_count": len(services),
"services_using_notify": transform_services_to_json(services)
@@ -46,12 +47,12 @@ def get_performance_platform():
return jsonify(stats)
def transform_results_into_totals(results):
def transform_results_into_totals(total_notifications_results):
total_notifications = 0
emails = 0
sms = 0
letters = 0
for x in results:
for x in total_notifications_results:
total_notifications += x.emails
total_notifications += x.sms
total_notifications += x.letters
@@ -61,25 +62,25 @@ def transform_results_into_totals(results):
return total_notifications, emails, sms, letters
def transform_results_to_json(results):
def transform_into_notification_by_type_json(total_notifications):
j = []
for x in results:
for x in total_notifications:
j.append({"date": x.bst_date, "emails": x.emails, "sms": x.sms, "letters": x.letters})
return j
def transform_processing_time_results_to_json(results):
def transform_processing_time_results_to_json(processing_time_results):
j = []
for x in results:
for x in processing_time_results:
j.append({"date": x.date, "percentage_under_10_seconds": round(x.percentage, 1)})
return j
def transform_services_to_json(results):
j=[]
for x in results:
def transform_services_to_json(services_results):
j = []
for x in services_results:
j.append({"service_id": x.service_id, "service_name": x.service_name,
"organisation_id": x.organisation_id, "organisation_name": x.organisation_name}
)
return j
return j

View File

@@ -709,6 +709,19 @@ def test_get_total_notifications_for_date_range(sample_service):
template_sms = create_template(service=sample_service, template_type='sms', template_name='a')
template_email = create_template(service=sample_service, template_type='email', template_name='b')
template_letter = create_template(service=sample_service, template_type='letter', template_name='c')
create_ft_notification_status(bst_date=date(2021, 2, 28),
service=template_email.service,
template=template_email,
count=15)
create_ft_notification_status(bst_date=date(2021, 2, 28),
service=template_sms.service,
template=template_sms,
count=20)
create_ft_notification_status(bst_date=date(2021, 2, 28),
service=template_letter.service,
template=template_letter,
count=3)
create_ft_notification_status(bst_date=date(2021, 3, 1),
service=template_email.service,
template=template_email,
@@ -725,5 +738,4 @@ def test_get_total_notifications_for_date_range(sample_service):
results = get_total_notifications_for_date_range(start_date=datetime(2021, 3, 1), end_date=datetime(2021, 3, 1))
assert len(results) == 1
print(type(results[0].emails))
assert results[0] == ("2021-03-01", 15, 20, 3)
assert results[0] == ("2021-03-01", 15, 20, 3)

View File

@@ -1,11 +1,11 @@
from datetime import datetime
from decimal import Decimal
from freezegun import freeze_time
from app.dao import fact_processing_time_dao
from app.dao.fact_processing_time_dao import get_processing_time_percentage_for_date_range
from app.models import FactProcessingTime
from tests.app.db import create_process_time
def test_insert_update_processing_time(notify_db_session):
@@ -45,13 +45,21 @@ def test_insert_update_processing_time(notify_db_session):
def test_get_processing_time_percentage_for_date_range(notify_db_session):
data = FactProcessingTime(
bst_date=datetime(2021, 2, 22).date(),
create_process_time(
bst_date='2021-02-21',
messages_total=5,
messages_within_10_secs=4
)
create_process_time(
bst_date='2021-02-22',
messages_total=3,
messages_within_10_secs=2
)
fact_processing_time_dao.insert_update_processing_time(data)
create_process_time(
bst_date='2021-02-23',
messages_total=4,
messages_within_10_secs=3
)
results = get_processing_time_percentage_for_date_range('2021-02-22', '2021-02-22')

View File

@@ -1178,5 +1178,3 @@ def test_get_live_services_with_organisation(sample_organisation):
(live_service_diff_org.name, another_org.name),
(live_service.name, sample_organisation.name),
(service_without_org.name, None)]

View File

@@ -5,6 +5,7 @@ from datetime import datetime, date, timedelta
import pytest
from app import db
from app.dao import fact_processing_time_dao
from app.dao.email_branding_dao import dao_create_email_branding
from app.dao.inbound_sms_dao import dao_create_inbound_sms
from app.dao.invited_org_user_dao import save_invited_org_user
@@ -67,7 +68,7 @@ from app.models import (
BroadcastEvent,
BroadcastProvider,
BroadcastProviderMessage,
BroadcastProviderMessageNumber
BroadcastProviderMessageNumber, FactProcessingTime
)
@@ -752,6 +753,15 @@ def create_ft_notification_status(
return data
def create_process_time(bst_date='2021-03-01', messages_total=35, messages_within_10_secs=34):
data = FactProcessingTime(
bst_date=bst_date,
messages_total=messages_total,
messages_within_10_secs=messages_within_10_secs
)
fact_processing_time_dao.insert_update_processing_time(data)
def create_service_guest_list(service, email_address=None, mobile_number=None):
if email_address:
guest_list_user = ServiceGuestList.from_string(service.id, EMAIL_TYPE, email_address)

View File

@@ -0,0 +1,77 @@
from datetime import date
from tests.app.db import create_template, create_ft_notification_status, create_process_time
def test_performance_dashboard(sample_service, admin_request):
template_sms = create_template(service=sample_service, template_type='sms', template_name='a')
template_email = create_template(service=sample_service, template_type='email', template_name='b')
template_letter = create_template(service=sample_service, template_type='letter', template_name='c')
create_ft_notification_status(bst_date=date(2021, 2, 28),
service=template_email.service,
template=template_email,
count=10)
create_ft_notification_status(bst_date=date(2021, 2, 28),
service=template_sms.service,
template=template_sms,
count=5)
create_ft_notification_status(bst_date=date(2021, 2, 28),
service=template_letter.service,
template=template_letter,
count=2)
create_ft_notification_status(bst_date=date(2021, 3, 1),
service=template_email.service,
template=template_email,
count=15)
create_ft_notification_status(bst_date=date(2021, 3, 1),
service=template_sms.service,
template=template_sms,
count=20)
create_ft_notification_status(bst_date=date(2021, 3, 1),
service=template_letter.service,
template=template_letter,
count=3)
create_ft_notification_status(bst_date=date(2021, 3, 2),
service=template_email.service,
template=template_email,
count=25)
create_ft_notification_status(bst_date=date(2021, 3, 2),
service=template_sms.service,
template=template_sms,
count=30)
create_ft_notification_status(bst_date=date(2021, 3, 2),
service=template_letter.service,
template=template_letter,
count=10)
create_ft_notification_status(bst_date=date(2021, 3, 3),
service=template_email.service,
template=template_email,
count=45)
create_ft_notification_status(bst_date=date(2021, 3, 3),
service=template_sms.service,
template=template_sms,
count=35)
create_ft_notification_status(bst_date=date(2021, 3, 3),
service=template_letter.service,
template=template_letter,
count=15)
create_process_time(bst_date='2021-02-28', messages_total=15, messages_within_10_secs=14)
create_process_time(bst_date='2021-03-01', messages_total=35, messages_within_10_secs=34)
create_process_time(bst_date='2021-03-02', messages_total=15, messages_within_10_secs=14)
create_process_time(bst_date='2021-03-03', messages_total=15, messages_within_10_secs=14)
results = admin_request.get(endpoint="performance_dashboard.get_performance_dashboard",
start_date='2021-03-01',
end_date='2021-03-02')
assert results['total_notifications'] == 10+5+2+15+20+3+25+30+10+45+35+15
assert results['email_notifications'] == 10+15+25+45
assert results['sms_notifications'] == 5+20+30+35
assert results['letter_notifications'] == 2+3+10+15
assert results['notifications_by_type'] == [{"date": '2021-03-01', "emails": 15, "sms": 20, "letters": 3},
{"date": '2021-03-02', "emails": 25, "sms": 30, "letters": 10}]
assert results['processing_time'] == [{"date": "2021-03-01", "percentage_under_10_seconds": 97.1},
{"date": "2021-03-02", "percentage_under_10_seconds": 93.3}]
assert results["live_service_count"] == 1
assert results["services_using_notify"][0]["service_name"] == sample_service.name
assert not results["services_using_notify"][0]["organisation_name"]

View File

@@ -1,48 +0,0 @@
from datetime import date, datetime
from app.dao import fact_processing_time_dao
from app.models import FactProcessingTime
from tests.app.db import create_template, create_ft_notification_status
def test_performance_platform(sample_service, admin_request):
template_sms = create_template(service=sample_service, template_type='sms', template_name='a')
template_email = create_template(service=sample_service, template_type='email', template_name='b')
template_letter = create_template(service=sample_service, template_type='letter', template_name='c')
create_ft_notification_status(bst_date=date(2021, 3, 1),
service=template_email.service,
template=template_email,
count=15)
create_ft_notification_status(bst_date=date(2021, 3, 1),
service=template_sms.service,
template=template_sms,
count=20)
create_ft_notification_status(bst_date=date(2021, 3, 1),
service=template_letter.service,
template=template_letter,
count=3)
create_process_time()
results = admin_request.get(endpoint="performance_platform.get_performance_platform",
start_date='2021-03-01',
end_date='2021-03-01')
assert results['total_notifications'] == 15+20+3
assert results['email_notifications'] == 15
assert results['sms_notifications'] == 20
assert results['letter_notifications'] == 3
assert results['notifications_by_type'] == [{"date": '2021-03-01', "emails": 15, "sms": 20, "letters": 3}]
assert results['processing_time'] == [({"date": "2021-03-01", "percentage_under_10_seconds": 97.1})]
assert results["live_service_count"] == 1
assert results["services_using_notify"][0]["service_name"] == sample_service.name
assert not results["services_using_notify"][0]["organisation_name"]
def create_process_time():
data = FactProcessingTime(
bst_date=datetime(2021, 3, 1).date(),
messages_total=35,
messages_within_10_secs=34
)
fact_processing_time_dao.insert_update_processing_time(data)