2021-03-04 16:10:53 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from flask import Blueprint, jsonify, request
|
|
|
|
|
|
|
|
|
|
from app.dao.fact_notification_status_dao import (
|
|
|
|
|
get_total_notifications_for_date_range,
|
|
|
|
|
)
|
|
|
|
|
from app.dao.fact_processing_time_dao import (
|
|
|
|
|
get_processing_time_percentage_for_date_range,
|
|
|
|
|
)
|
2023-07-10 11:06:29 -07:00
|
|
|
from app.dao.services_dao import get_live_services_with_organization
|
2021-03-04 16:10:53 +00:00
|
|
|
from app.errors import register_errors
|
2021-03-10 13:55:06 +00:00
|
|
|
from app.performance_dashboard.performance_dashboard_schema import (
|
|
|
|
|
performance_dashboard_request,
|
|
|
|
|
)
|
2021-03-04 16:10:53 +00:00
|
|
|
from app.schema_validation import validate
|
|
|
|
|
|
2021-03-10 11:12:29 +00:00
|
|
|
performance_dashboard_blueprint = Blueprint('performance_dashboard', __name__, url_prefix='/performance-dashboard')
|
2021-03-04 16:10:53 +00:00
|
|
|
|
2021-03-10 11:12:29 +00:00
|
|
|
register_errors(performance_dashboard_blueprint)
|
2021-03-04 16:10:53 +00:00
|
|
|
|
|
|
|
|
|
2021-03-10 11:12:29 +00:00
|
|
|
@performance_dashboard_blueprint.route('')
|
|
|
|
|
def get_performance_dashboard():
|
2021-03-04 16:10:53 +00:00
|
|
|
# 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
|
2021-03-10 11:12:29 +00:00
|
|
|
validate(request.args, performance_dashboard_request)
|
2021-03-04 16:10:53 +00:00
|
|
|
|
|
|
|
|
# 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()
|
2021-03-10 11:12:29 +00:00
|
|
|
total_for_all_time = get_total_notifications_for_date_range(start_date=None, end_date=None)
|
2023-03-02 20:20:31 -05:00
|
|
|
total_notifications, emails, sms = transform_results_into_totals(total_for_all_time)
|
2021-03-10 11:12:29 +00:00
|
|
|
totals_for_date_range = get_total_notifications_for_date_range(start_date=start_date, end_date=end_date)
|
2021-03-04 16:10:53 +00:00
|
|
|
processing_time_results = get_processing_time_percentage_for_date_range(start_date=start_date, end_date=end_date)
|
2023-07-10 11:06:29 -07:00
|
|
|
services = get_live_services_with_organization()
|
2021-03-04 16:10:53 +00:00
|
|
|
stats = {
|
|
|
|
|
"total_notifications": total_notifications,
|
|
|
|
|
"email_notifications": emails,
|
|
|
|
|
"sms_notifications": sms,
|
2021-03-10 11:12:29 +00:00
|
|
|
"notifications_by_type": transform_into_notification_by_type_json(totals_for_date_range),
|
2021-03-04 16:10:53 +00:00
|
|
|
"processing_time": transform_processing_time_results_to_json(processing_time_results),
|
|
|
|
|
"live_service_count": len(services),
|
|
|
|
|
"services_using_notify": transform_services_to_json(services)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return jsonify(stats)
|
|
|
|
|
|
|
|
|
|
|
2021-03-10 11:12:29 +00:00
|
|
|
def transform_results_into_totals(total_notifications_results):
|
2021-03-04 16:10:53 +00:00
|
|
|
total_notifications = 0
|
|
|
|
|
emails = 0
|
|
|
|
|
sms = 0
|
2021-03-10 11:12:29 +00:00
|
|
|
for x in total_notifications_results:
|
2021-03-04 16:10:53 +00:00
|
|
|
total_notifications += x.emails
|
|
|
|
|
total_notifications += x.sms
|
|
|
|
|
emails += x.emails
|
|
|
|
|
sms += x.sms
|
2023-03-02 20:20:31 -05:00
|
|
|
return total_notifications, emails, sms
|
2021-03-04 16:10:53 +00:00
|
|
|
|
|
|
|
|
|
2021-03-10 11:12:29 +00:00
|
|
|
def transform_into_notification_by_type_json(total_notifications):
|
2021-03-04 16:10:53 +00:00
|
|
|
j = []
|
2021-03-10 11:12:29 +00:00
|
|
|
for x in total_notifications:
|
2023-03-02 20:20:31 -05:00
|
|
|
j.append({"date": x.local_date, "emails": x.emails, "sms": x.sms})
|
2021-03-04 16:10:53 +00:00
|
|
|
return j
|
|
|
|
|
|
|
|
|
|
|
2021-03-10 11:12:29 +00:00
|
|
|
def transform_processing_time_results_to_json(processing_time_results):
|
2021-03-04 16:10:53 +00:00
|
|
|
j = []
|
2021-03-10 11:12:29 +00:00
|
|
|
for x in processing_time_results:
|
2021-11-25 14:57:31 +00:00
|
|
|
j.append({"date": x.date, "percentage_under_10_seconds": x.percentage})
|
2021-03-04 16:10:53 +00:00
|
|
|
|
|
|
|
|
return j
|
|
|
|
|
|
|
|
|
|
|
2021-03-10 11:12:29 +00:00
|
|
|
def transform_services_to_json(services_results):
|
|
|
|
|
j = []
|
|
|
|
|
for x in services_results:
|
2021-03-04 16:10:53 +00:00
|
|
|
j.append({"service_id": x.service_id, "service_name": x.service_name,
|
2023-07-10 11:06:29 -07:00
|
|
|
"organization_id": x.organization_id, "organization_name": x.organization_name}
|
2021-03-04 16:10:53 +00:00
|
|
|
)
|
2021-03-10 11:12:29 +00:00
|
|
|
return j
|