2017-11-17 14:35:25 +00:00
|
|
|
import calendar
|
2024-02-09 17:15:44 -08:00
|
|
|
from collections import defaultdict
|
2017-02-15 14:56:22 +00:00
|
|
|
from datetime import datetime
|
2017-02-16 15:08:16 +00:00
|
|
|
from functools import partial
|
2019-01-15 15:55:04 +00:00
|
|
|
from itertools import groupby
|
2018-02-20 11:22:17 +00:00
|
|
|
|
2023-08-25 08:57:24 -07:00
|
|
|
from flask import Response, abort, jsonify, render_template, request, session, url_for
|
2019-07-01 15:22:08 +01:00
|
|
|
from flask_login import current_user
|
2024-06-03 13:45:36 -07:00
|
|
|
from flask_socketio import emit
|
2017-11-24 16:34:45 +00:00
|
|
|
from werkzeug.utils import redirect
|
2017-06-06 12:57:55 +01:00
|
|
|
|
2016-04-05 11:40:13 +01:00
|
|
|
from app import (
|
2017-08-16 16:31:47 +01:00
|
|
|
billing_api_client,
|
2018-02-20 11:22:17 +00:00
|
|
|
current_service,
|
2024-01-05 12:45:21 -08:00
|
|
|
job_api_client,
|
|
|
|
|
notification_api_client,
|
2016-04-05 11:40:13 +01:00
|
|
|
service_api_client,
|
2024-06-03 13:51:59 -07:00
|
|
|
socketio,
|
2017-08-24 13:57:12 +01:00
|
|
|
template_statistics_client,
|
2016-04-05 11:40:13 +01:00
|
|
|
)
|
2024-01-04 13:40:36 -08:00
|
|
|
from app.formatters import format_date_numeric, format_datetime_numeric, get_time_left
|
2018-02-20 11:22:17 +00:00
|
|
|
from app.main import main
|
2020-01-08 14:29:56 +00:00
|
|
|
from app.statistics_utils import get_formatted_percentage
|
2017-01-30 17:27:09 +00:00
|
|
|
from app.utils import (
|
2019-01-15 17:16:57 +00:00
|
|
|
DELIVERED_STATUSES,
|
2017-01-30 17:27:09 +00:00
|
|
|
FAILURE_STATUSES,
|
|
|
|
|
REQUESTED_STATUSES,
|
2020-07-03 10:00:55 +01:00
|
|
|
service_has_permission,
|
2017-01-30 17:27:09 +00:00
|
|
|
)
|
2021-06-09 15:15:35 +01:00
|
|
|
from app.utils.csv import Spreadsheet
|
2021-06-14 11:00:05 +01:00
|
|
|
from app.utils.pagination import generate_next_dict, generate_previous_dict
|
2021-06-14 12:40:12 +01:00
|
|
|
from app.utils.time import get_current_financial_year
|
2021-06-09 13:19:05 +01:00
|
|
|
from app.utils.user import user_has_permissions
|
2024-05-16 10:37:37 -04:00
|
|
|
from notifications_utils.recipients import format_phone_number_human_readable
|
2024-01-25 15:32:44 -08:00
|
|
|
|
2024-01-08 15:33:24 -08:00
|
|
|
|
2024-06-03 15:34:43 -07:00
|
|
|
@socketio.on("fetch_daily_stats")
|
2024-05-30 21:17:10 -07:00
|
|
|
def handle_fetch_daily_stats(service_id):
|
|
|
|
|
if service_id:
|
|
|
|
|
date_range = get_stats_date_range()
|
2024-06-03 13:35:30 -07:00
|
|
|
daily_stats = service_api_client.get_service_notification_statistics_by_day(
|
2024-06-03 15:34:43 -07:00
|
|
|
service_id, start_date=date_range["start_date"], days=date_range["days"]
|
2024-06-03 13:35:30 -07:00
|
|
|
)
|
2024-06-03 15:34:43 -07:00
|
|
|
emit("daily_stats_update", daily_stats)
|
2024-05-30 21:17:10 -07:00
|
|
|
else:
|
2024-06-03 15:34:43 -07:00
|
|
|
emit("error", {"error": "No service_id provided"})
|
2024-05-20 16:21:38 -07:00
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/dashboard")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity", "send_messages")
|
2018-02-14 10:38:00 +00:00
|
|
|
def old_service_dashboard(service_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for(".service_dashboard", service_id=service_id))
|
2018-02-14 10:38:00 +00:00
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>")
|
2018-08-06 11:09:57 +01:00
|
|
|
@user_has_permissions()
|
2016-01-15 17:46:09 +00:00
|
|
|
def service_dashboard(service_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
if session.get("invited_user_id"):
|
|
|
|
|
session.pop("invited_user_id", None)
|
|
|
|
|
session["service_id"] = service_id
|
2016-03-10 11:53:29 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
if not current_user.has_permissions("view_activity"):
|
|
|
|
|
return redirect(url_for("main.choose_template", service_id=service_id))
|
2018-07-05 11:45:09 +01:00
|
|
|
|
2024-02-12 15:29:26 -08:00
|
|
|
job_response = job_api_client.get_jobs(service_id)["data"]
|
2024-02-21 07:45:21 -08:00
|
|
|
notifications_response = notification_api_client.get_notifications_for_service(
|
|
|
|
|
service_id
|
2024-01-08 14:53:34 -08:00
|
|
|
)["notifications"]
|
2024-01-12 13:32:10 -08:00
|
|
|
service_data_retention_days = 7
|
2024-01-05 12:45:21 -08:00
|
|
|
|
2024-02-09 17:09:48 -08:00
|
|
|
aggregate_notifications_by_job = defaultdict(list)
|
|
|
|
|
for notification in notifications_response:
|
2024-02-15 10:40:15 -08:00
|
|
|
job_id = notification.get("job", {}).get("id", None)
|
2024-02-09 17:09:48 -08:00
|
|
|
if job_id:
|
|
|
|
|
aggregate_notifications_by_job[job_id].append(notification)
|
2024-06-03 13:17:31 -07:00
|
|
|
|
2024-02-09 17:09:48 -08:00
|
|
|
job_and_notifications = [
|
2024-01-23 16:18:43 -08:00
|
|
|
{
|
|
|
|
|
"job_id": job["id"],
|
|
|
|
|
"time_left": get_time_left(job["created_at"]),
|
2024-01-26 09:43:41 -08:00
|
|
|
"download_link": url_for(
|
|
|
|
|
".view_job_csv", service_id=current_service.id, job_id=job["id"]
|
|
|
|
|
),
|
2024-02-09 17:09:48 -08:00
|
|
|
"view_job_link": url_for(
|
|
|
|
|
".view_job", service_id=current_service.id, job_id=job["id"]
|
|
|
|
|
),
|
2024-02-12 15:29:26 -08:00
|
|
|
"created_at": job["created_at"],
|
2024-01-23 16:18:43 -08:00
|
|
|
"notification_count": job["notification_count"],
|
2024-01-25 15:32:44 -08:00
|
|
|
"created_by": job["created_by"],
|
2024-02-09 17:09:48 -08:00
|
|
|
"notifications": aggregate_notifications_by_job.get(job["id"], []),
|
2024-01-23 15:24:57 -08:00
|
|
|
}
|
2024-02-12 15:29:26 -08:00
|
|
|
for job in job_response
|
2024-02-23 17:50:25 -08:00
|
|
|
if aggregate_notifications_by_job.get(job["id"], [])
|
2024-01-23 16:18:43 -08:00
|
|
|
]
|
2015-12-18 10:26:56 +00:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/dashboard/dashboard.html",
|
2016-06-28 09:21:46 +01:00
|
|
|
updates_url=url_for(".service_dashboard_updates", service_id=service_id),
|
2023-08-25 09:12:23 -07:00
|
|
|
partials=get_dashboard_partials(service_id),
|
2024-02-09 17:09:48 -08:00
|
|
|
job_and_notifications=job_and_notifications,
|
2024-01-08 14:53:34 -08:00
|
|
|
service_data_retention_days=service_data_retention_days,
|
2024-06-03 15:34:43 -07:00
|
|
|
service_id=service_id,
|
2024-01-08 14:53:34 -08:00
|
|
|
)
|
2016-03-17 11:45:48 +00:00
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/dashboard.json")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity")
|
2016-03-23 08:43:45 +00:00
|
|
|
def service_dashboard_updates(service_id):
|
2016-06-28 11:23:43 +01:00
|
|
|
return jsonify(**get_dashboard_partials(service_id))
|
2016-03-23 08:43:45 +00:00
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/template-activity")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity")
|
2016-04-20 14:09:38 +01:00
|
|
|
def template_history(service_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
return redirect(url_for("main.template_usage", service_id=service_id), code=301)
|
2016-04-20 14:09:38 +01:00
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/template-usage")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity")
|
2017-11-16 16:01:03 +00:00
|
|
|
def template_usage(service_id):
|
|
|
|
|
year, current_financial_year = requested_and_current_financial_year(request)
|
2023-08-25 09:12:23 -07:00
|
|
|
stats = template_statistics_client.get_monthly_template_usage_for_service(
|
|
|
|
|
service_id, year
|
|
|
|
|
)
|
2017-11-16 16:01:03 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
stats = sorted(stats, key=lambda x: (x["count"]), reverse=True)
|
2017-11-16 16:01:03 +00:00
|
|
|
|
2017-11-22 15:50:31 +00:00
|
|
|
def get_monthly_template_stats(month_name, stats):
|
|
|
|
|
return {
|
2023-08-25 09:12:23 -07:00
|
|
|
"name": month_name,
|
|
|
|
|
"templates_used": [
|
2017-11-22 15:50:31 +00:00
|
|
|
{
|
2023-08-25 09:12:23 -07:00
|
|
|
"id": stat["template_id"],
|
|
|
|
|
"name": stat["name"],
|
|
|
|
|
"type": stat["type"],
|
|
|
|
|
"requested_count": stat["count"],
|
2017-11-22 15:50:31 +00:00
|
|
|
}
|
|
|
|
|
for stat in stats
|
2023-08-25 09:12:23 -07:00
|
|
|
if calendar.month_name[int(stat["month"])] == month_name
|
2017-11-22 15:50:31 +00:00
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
months = [
|
|
|
|
|
get_monthly_template_stats(month, stats)
|
2023-08-25 09:12:23 -07:00
|
|
|
for month in get_months_for_financial_year(year, time_format="%B")
|
2017-11-22 15:50:31 +00:00
|
|
|
]
|
2017-11-16 16:01:03 +00:00
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/dashboard/all-template-statistics.html",
|
2017-11-16 16:01:03 +00:00
|
|
|
months=months,
|
|
|
|
|
stats=stats,
|
|
|
|
|
most_used_template_count=max(
|
2024-02-07 09:38:18 -07:00
|
|
|
(
|
|
|
|
|
max(
|
|
|
|
|
(
|
|
|
|
|
template["requested_count"]
|
|
|
|
|
for template in month["templates_used"]
|
|
|
|
|
),
|
|
|
|
|
default=0,
|
|
|
|
|
)
|
|
|
|
|
for month in months
|
|
|
|
|
),
|
|
|
|
|
default=0,
|
2017-11-16 16:01:03 +00:00
|
|
|
),
|
|
|
|
|
years=get_tuples_of_financial_years(
|
2023-08-25 09:12:23 -07:00
|
|
|
partial(url_for, ".template_usage", service_id=service_id),
|
2018-04-03 18:13:38 +01:00
|
|
|
start=current_financial_year - 2,
|
2017-11-16 16:01:03 +00:00
|
|
|
end=current_financial_year,
|
|
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
selected_year=year,
|
2017-11-16 16:01:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/usage")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("manage_service", allow_org_user=True)
|
2018-05-29 15:31:40 +01:00
|
|
|
def usage(service_id):
|
2018-05-16 12:26:11 +01:00
|
|
|
year, current_financial_year = requested_and_current_financial_year(request)
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
free_sms_allowance = billing_api_client.get_free_sms_fragment_limit_for_year(
|
2024-02-07 09:38:18 -07:00
|
|
|
service_id
|
2023-08-25 09:12:23 -07:00
|
|
|
)
|
2024-02-07 09:38:18 -07:00
|
|
|
|
2022-04-29 16:25:58 +01:00
|
|
|
units = billing_api_client.get_monthly_usage_for_service(service_id, year)
|
2024-02-07 09:38:18 -07:00
|
|
|
|
2022-04-29 16:25:58 +01:00
|
|
|
yearly_usage = billing_api_client.get_annual_usage_for_service(service_id, year)
|
2022-12-02 11:27:41 -05:00
|
|
|
|
|
|
|
|
more_stats = format_monthly_stats_to_list(
|
2023-08-25 09:12:23 -07:00
|
|
|
service_api_client.get_monthly_notification_stats(service_id, year)["data"]
|
2022-12-02 11:27:41 -05:00
|
|
|
)
|
2018-05-16 12:26:11 +01:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/usage.html",
|
2022-12-01 21:49:04 -05:00
|
|
|
months=list(get_monthly_usage_breakdown(year, units, more_stats)),
|
2018-05-16 12:26:11 +01:00
|
|
|
selected_year=year,
|
|
|
|
|
years=get_tuples_of_financial_years(
|
2023-08-25 09:12:23 -07:00
|
|
|
partial(url_for, ".usage", service_id=service_id),
|
2020-05-07 16:36:50 +01:00
|
|
|
start=current_financial_year - 2,
|
|
|
|
|
end=current_financial_year,
|
2018-05-16 12:26:11 +01:00
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
**get_annual_usage_breakdown(yearly_usage, free_sms_allowance),
|
2018-05-16 12:26:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/monthly")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity")
|
2017-01-30 17:27:09 +00:00
|
|
|
def monthly(service_id):
|
|
|
|
|
year, current_financial_year = requested_and_current_financial_year(request)
|
2016-05-03 13:25:22 +01:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/dashboard/monthly.html",
|
2017-01-30 17:27:09 +00:00
|
|
|
months=format_monthly_stats_to_list(
|
2023-08-25 09:12:23 -07:00
|
|
|
service_api_client.get_monthly_notification_stats(service_id, year)["data"]
|
2017-01-30 17:27:09 +00:00
|
|
|
),
|
2017-02-16 15:08:16 +00:00
|
|
|
years=get_tuples_of_financial_years(
|
2023-08-25 09:12:23 -07:00
|
|
|
partial_url=partial(url_for, ".monthly", service_id=service_id),
|
2018-04-03 18:13:38 +01:00
|
|
|
start=current_financial_year - 2,
|
2017-02-16 15:08:16 +00:00
|
|
|
end=current_financial_year,
|
|
|
|
|
),
|
2017-01-30 17:27:09 +00:00
|
|
|
selected_year=year,
|
2016-05-03 13:25:22 +01:00
|
|
|
)
|
|
|
|
|
|
2016-03-17 11:45:48 +00:00
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/inbox")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity")
|
|
|
|
|
@service_has_permission("inbound_sms")
|
2017-05-22 17:02:03 +01:00
|
|
|
def inbox(service_id):
|
2017-07-07 15:51:59 +01:00
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
"views/dashboard/inbox.html",
|
2017-07-07 15:51:59 +01:00
|
|
|
partials=get_inbox_partials(service_id),
|
2023-08-25 09:12:23 -07:00
|
|
|
updates_url=url_for(
|
|
|
|
|
".inbox_updates", service_id=service_id, page=request.args.get("page")
|
|
|
|
|
),
|
2017-07-07 15:51:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/inbox.json")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity")
|
|
|
|
|
@service_has_permission("inbound_sms")
|
2017-07-07 15:51:59 +01:00
|
|
|
def inbox_updates(service_id):
|
|
|
|
|
return jsonify(get_inbox_partials(service_id))
|
|
|
|
|
|
|
|
|
|
|
2019-11-04 11:07:55 +00:00
|
|
|
@main.route("/services/<uuid:service_id>/inbox.csv")
|
2023-08-25 09:12:23 -07:00
|
|
|
@user_has_permissions("view_activity")
|
2017-10-17 11:41:12 +01:00
|
|
|
def inbox_download(service_id):
|
|
|
|
|
return Response(
|
|
|
|
|
Spreadsheet.from_rows(
|
2023-08-25 09:12:23 -07:00
|
|
|
[
|
|
|
|
|
[
|
|
|
|
|
"Phone number",
|
|
|
|
|
"Message",
|
|
|
|
|
"Received",
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
+ [
|
|
|
|
|
[
|
|
|
|
|
format_phone_number_human_readable(message["user_number"]),
|
|
|
|
|
message["content"].lstrip(("=+-@")),
|
|
|
|
|
format_datetime_numeric(message["created_at"]),
|
|
|
|
|
]
|
|
|
|
|
for message in service_api_client.get_inbound_sms(service_id)["data"]
|
|
|
|
|
]
|
2017-10-17 11:41:12 +01:00
|
|
|
).as_csv_data,
|
2023-08-25 09:12:23 -07:00
|
|
|
mimetype="text/csv",
|
2017-10-17 11:41:12 +01:00
|
|
|
headers={
|
2023-08-25 09:12:23 -07:00
|
|
|
"Content-Disposition": 'inline; filename="Received text messages {}.csv"'.format(
|
2017-10-17 11:41:12 +01:00
|
|
|
format_date_numeric(datetime.utcnow().isoformat())
|
|
|
|
|
)
|
2023-08-25 09:12:23 -07:00
|
|
|
},
|
2017-10-17 11:41:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2017-07-07 15:51:59 +01:00
|
|
|
def get_inbox_partials(service_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
page = int(request.args.get("page", 1))
|
|
|
|
|
inbound_messages_data = service_api_client.get_most_recent_inbound_sms(
|
|
|
|
|
service_id, page=page
|
|
|
|
|
)
|
|
|
|
|
inbound_messages = inbound_messages_data["data"]
|
2017-08-24 13:57:12 +01:00
|
|
|
if not inbound_messages:
|
2019-03-29 15:16:29 +00:00
|
|
|
inbound_number = current_service.inbound_number
|
2017-08-24 13:57:12 +01:00
|
|
|
else:
|
|
|
|
|
inbound_number = None
|
|
|
|
|
|
2018-03-21 15:08:03 +00:00
|
|
|
prev_page = None
|
|
|
|
|
if page > 1:
|
2023-08-25 09:12:23 -07:00
|
|
|
prev_page = generate_previous_dict("main.inbox", service_id, page)
|
2018-03-21 15:08:03 +00:00
|
|
|
next_page = None
|
2023-08-25 09:12:23 -07:00
|
|
|
if inbound_messages_data["has_next"]:
|
|
|
|
|
next_page = generate_next_dict("main.inbox", service_id, page)
|
2018-03-21 15:08:03 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
return {
|
|
|
|
|
"messages": render_template(
|
|
|
|
|
"views/dashboard/_inbox_messages.html",
|
|
|
|
|
messages=inbound_messages,
|
|
|
|
|
inbound_number=inbound_number,
|
|
|
|
|
prev_page=prev_page,
|
|
|
|
|
next_page=next_page,
|
|
|
|
|
)
|
|
|
|
|
}
|
2017-05-22 17:02:03 +01:00
|
|
|
|
|
|
|
|
|
2019-01-15 17:16:57 +00:00
|
|
|
def filter_out_cancelled_stats(template_statistics):
|
|
|
|
|
return [s for s in template_statistics if s["status"] != "cancelled"]
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
def aggregate_template_usage(template_statistics, sort_key="count"):
|
2019-01-15 17:16:57 +00:00
|
|
|
template_statistics = filter_out_cancelled_stats(template_statistics)
|
2019-01-15 15:55:04 +00:00
|
|
|
templates = []
|
2023-08-25 09:12:23 -07:00
|
|
|
for k, v in groupby(
|
|
|
|
|
sorted(template_statistics, key=lambda x: x["template_id"]),
|
|
|
|
|
key=lambda x: x["template_id"],
|
|
|
|
|
):
|
2019-01-15 16:19:23 +00:00
|
|
|
template_stats = list(v)
|
2019-01-15 15:55:04 +00:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
templates.append(
|
|
|
|
|
{
|
|
|
|
|
"template_id": k,
|
|
|
|
|
"template_name": template_stats[0]["template_name"],
|
|
|
|
|
"template_type": template_stats[0]["template_type"],
|
|
|
|
|
"count": sum(s["count"] for s in template_stats),
|
|
|
|
|
}
|
|
|
|
|
)
|
2019-01-15 15:55:04 +00:00
|
|
|
|
|
|
|
|
return sorted(templates, key=lambda x: x[sort_key], reverse=True)
|
2016-04-12 11:42:45 +01:00
|
|
|
|
2016-04-20 13:59:24 +01:00
|
|
|
|
2019-01-15 17:16:57 +00:00
|
|
|
def aggregate_notifications_stats(template_statistics):
|
|
|
|
|
template_statistics = filter_out_cancelled_stats(template_statistics)
|
|
|
|
|
notifications = {
|
2023-08-25 09:12:23 -07:00
|
|
|
template_type: {status: 0 for status in ("requested", "delivered", "failed")}
|
|
|
|
|
for template_type in ["sms", "email"]
|
2019-01-15 17:16:57 +00:00
|
|
|
}
|
|
|
|
|
for stat in template_statistics:
|
|
|
|
|
notifications[stat["template_type"]]["requested"] += stat["count"]
|
|
|
|
|
if stat["status"] in DELIVERED_STATUSES:
|
|
|
|
|
notifications[stat["template_type"]]["delivered"] += stat["count"]
|
|
|
|
|
elif stat["status"] in FAILURE_STATUSES:
|
|
|
|
|
notifications[stat["template_type"]]["failed"] += stat["count"]
|
|
|
|
|
|
|
|
|
|
return notifications
|
|
|
|
|
|
|
|
|
|
|
2016-06-28 11:23:43 +01:00
|
|
|
def get_dashboard_partials(service_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
all_statistics = template_statistics_client.get_template_statistics_for_service(
|
|
|
|
|
service_id, limit_days=7
|
|
|
|
|
)
|
2019-01-15 17:16:57 +00:00
|
|
|
template_statistics = aggregate_template_usage(all_statistics)
|
|
|
|
|
stats = aggregate_notifications_stats(all_statistics)
|
2019-10-25 15:34:01 +01:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
dashboard_totals = (get_dashboard_totals(stats),)
|
2019-10-25 13:27:46 +01:00
|
|
|
free_sms_allowance = billing_api_client.get_free_sms_fragment_limit_for_year(
|
|
|
|
|
current_service.id,
|
|
|
|
|
)
|
2024-02-07 14:43:39 -07:00
|
|
|
# These 2 calls will update the dashboard sms allowance count while in trial mode.
|
|
|
|
|
billing_api_client.get_monthly_usage_for_service(
|
2024-06-03 15:34:43 -07:00
|
|
|
service_id, get_current_financial_year()
|
2024-02-07 14:43:39 -07:00
|
|
|
)
|
|
|
|
|
billing_api_client.create_or_update_free_sms_fragment_limit(
|
|
|
|
|
service_id, free_sms_fragment_limit=free_sms_allowance
|
|
|
|
|
)
|
2024-06-03 15:34:43 -07:00
|
|
|
|
2022-04-29 16:25:58 +01:00
|
|
|
yearly_usage = billing_api_client.get_annual_usage_for_service(
|
2019-10-25 13:27:46 +01:00
|
|
|
service_id,
|
2024-06-03 15:34:43 -07:00
|
|
|
get_current_financial_year(),
|
2019-10-25 13:27:46 +01:00
|
|
|
)
|
2016-04-20 13:59:24 +01:00
|
|
|
return {
|
2023-08-25 09:12:23 -07:00
|
|
|
"upcoming": render_template(
|
|
|
|
|
"views/dashboard/_upcoming.html",
|
2016-08-09 10:39:57 +01:00
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
"inbox": render_template(
|
|
|
|
|
"views/dashboard/_inbox.html",
|
2017-05-22 17:02:03 +01:00
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
"totals": render_template(
|
|
|
|
|
"views/dashboard/_totals.html",
|
2016-07-19 17:10:48 +01:00
|
|
|
service_id=service_id,
|
2017-11-09 17:17:50 +00:00
|
|
|
statistics=dashboard_totals[0],
|
2016-06-28 11:23:43 +01:00
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
"template-statistics": render_template(
|
|
|
|
|
"views/dashboard/template-statistics.html",
|
2016-06-28 11:23:43 +01:00
|
|
|
template_statistics=template_statistics,
|
|
|
|
|
most_used_template_count=max(
|
2023-08-25 09:12:23 -07:00
|
|
|
[row["count"] for row in template_statistics] or [0]
|
2016-06-28 11:23:43 +01:00
|
|
|
),
|
|
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
"usage": render_template(
|
|
|
|
|
"views/dashboard/_usage.html",
|
2022-04-26 14:34:33 +01:00
|
|
|
**get_annual_usage_breakdown(yearly_usage, free_sms_allowance),
|
2019-10-25 13:27:46 +01:00
|
|
|
),
|
2016-06-28 11:23:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-07-20 14:12:03 +01:00
|
|
|
def get_dashboard_totals(statistics):
|
|
|
|
|
for msg_type in statistics.values():
|
2023-08-25 09:12:23 -07:00
|
|
|
msg_type["failed_percentage"] = get_formatted_percentage(
|
|
|
|
|
msg_type["failed"], msg_type["requested"]
|
|
|
|
|
)
|
|
|
|
|
msg_type["show_warning"] = float(msg_type["failed_percentage"]) > 3
|
2016-07-20 14:12:03 +01:00
|
|
|
return statistics
|
2016-07-19 13:53:27 +01:00
|
|
|
|
|
|
|
|
|
2022-04-26 14:34:33 +01:00
|
|
|
def get_annual_usage_breakdown(usage, free_sms_fragment_limit):
|
2023-08-25 09:12:23 -07:00
|
|
|
sms = get_usage_breakdown_by_type(usage, "sms")
|
|
|
|
|
sms_chargeable_units = sum(row["chargeable_units"] for row in sms)
|
2017-11-09 13:18:09 +00:00
|
|
|
sms_free_allowance = free_sms_fragment_limit
|
2023-08-25 09:12:23 -07:00
|
|
|
sms_cost = sum(row["cost"] for row in sms)
|
2017-05-02 14:10:56 +01:00
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
emails = get_usage_breakdown_by_type(usage, "email")
|
|
|
|
|
emails_sent = sum(row["notifications_sent"] for row in emails)
|
2016-06-28 11:23:43 +01:00
|
|
|
|
|
|
|
|
return {
|
2023-08-25 09:12:23 -07:00
|
|
|
"emails_sent": emails_sent,
|
|
|
|
|
"sms_free_allowance": sms_free_allowance,
|
|
|
|
|
"sms_sent": sms_chargeable_units,
|
|
|
|
|
"sms_allowance_remaining": max(0, (sms_free_allowance - sms_chargeable_units)),
|
|
|
|
|
"sms_cost": sms_cost,
|
|
|
|
|
"sms_breakdown": sms,
|
2016-04-20 14:09:38 +01:00
|
|
|
}
|
2016-07-28 16:23:22 +01:00
|
|
|
|
|
|
|
|
|
2017-01-30 17:27:09 +00:00
|
|
|
def format_monthly_stats_to_list(historical_stats):
|
2023-08-25 09:12:23 -07:00
|
|
|
return sorted(
|
|
|
|
|
(
|
|
|
|
|
dict(
|
|
|
|
|
date=key,
|
|
|
|
|
future=yyyy_mm_to_datetime(key) > datetime.utcnow(),
|
|
|
|
|
name=yyyy_mm_to_datetime(key).strftime("%B"),
|
|
|
|
|
**aggregate_status_types(value),
|
|
|
|
|
)
|
|
|
|
|
for key, value in historical_stats.items()
|
|
|
|
|
),
|
|
|
|
|
key=lambda x: x["date"],
|
|
|
|
|
)
|
2016-07-28 16:23:22 +01:00
|
|
|
|
|
|
|
|
|
2017-10-27 10:56:03 +01:00
|
|
|
def yyyy_mm_to_datetime(string):
|
2017-02-08 11:16:11 +00:00
|
|
|
return datetime(int(string[0:4]), int(string[5:7]), 1)
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 17:27:09 +00:00
|
|
|
def aggregate_status_types(counts_dict):
|
2023-08-25 09:12:23 -07:00
|
|
|
return get_dashboard_totals(
|
|
|
|
|
{
|
|
|
|
|
"{}_counts".format(message_type): {
|
|
|
|
|
"failed": sum(stats.get(status, 0) for status in FAILURE_STATUSES),
|
|
|
|
|
"requested": sum(stats.get(status, 0) for status in REQUESTED_STATUSES),
|
|
|
|
|
}
|
|
|
|
|
for message_type, stats in counts_dict.items()
|
|
|
|
|
}
|
|
|
|
|
)
|
2016-09-29 18:44:10 +01:00
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
def get_months_for_financial_year(year, time_format="%B"):
|
2024-02-07 09:38:18 -07:00
|
|
|
return [month.strftime(time_format) for month in (get_months_for_year(1, 13, year))]
|
2016-09-29 18:44:10 +01:00
|
|
|
|
|
|
|
|
|
2024-05-29 12:13:09 -07:00
|
|
|
def get_current_month_for_financial_year(year):
|
|
|
|
|
current_month = datetime.now().month
|
|
|
|
|
return current_month
|
|
|
|
|
|
2024-06-03 13:35:30 -07:00
|
|
|
|
2024-05-30 21:17:10 -07:00
|
|
|
def get_stats_date_range():
|
|
|
|
|
current_financial_year = get_current_financial_year()
|
|
|
|
|
current_month = get_current_month_for_financial_year(current_financial_year)
|
2024-06-03 15:34:43 -07:00
|
|
|
start_date = datetime.now().strftime("%Y-%m-%d")
|
2024-05-30 21:17:10 -07:00
|
|
|
days = 7
|
|
|
|
|
return {
|
|
|
|
|
"current_financial_year": current_financial_year,
|
|
|
|
|
"current_month": current_month,
|
|
|
|
|
"start_date": start_date,
|
|
|
|
|
"days": days,
|
|
|
|
|
}
|
2024-05-29 12:13:09 -07:00
|
|
|
|
2024-06-03 13:35:30 -07:00
|
|
|
|
2016-09-29 18:44:10 +01:00
|
|
|
def get_months_for_year(start, end, year):
|
|
|
|
|
return [datetime(year, month, 1) for month in range(start, end)]
|
|
|
|
|
|
|
|
|
|
|
2022-04-26 14:34:33 +01:00
|
|
|
def get_usage_breakdown_by_type(usage, notification_type):
|
2023-08-25 09:12:23 -07:00
|
|
|
return [row for row in usage if row["notification_type"] == notification_type]
|
2022-04-26 14:34:33 +01:00
|
|
|
|
|
|
|
|
|
2022-12-01 21:49:04 -05:00
|
|
|
def get_monthly_usage_breakdown(year, monthly_usage, more_stats):
|
2023-08-25 09:12:23 -07:00
|
|
|
sms = get_usage_breakdown_by_type(monthly_usage, "sms")
|
2022-04-29 16:30:53 +01:00
|
|
|
|
2016-09-29 18:44:10 +01:00
|
|
|
for month in get_months_for_financial_year(year):
|
2023-08-25 09:12:23 -07:00
|
|
|
monthly_sms = [row for row in sms if row["month"] == month]
|
|
|
|
|
sms_free_allowance_used = sum(row["free_allowance_used"] for row in monthly_sms)
|
|
|
|
|
sms_cost = sum(row["cost"] for row in monthly_sms)
|
|
|
|
|
sms_breakdown = [row for row in monthly_sms if row["charged_units"]]
|
|
|
|
|
sms_counts = [
|
|
|
|
|
row["sms_counts"]
|
|
|
|
|
for row in more_stats
|
|
|
|
|
if row["sms_counts"] and row["name"] == month
|
|
|
|
|
]
|
2020-07-03 17:39:45 +01:00
|
|
|
|
2016-09-29 18:44:10 +01:00
|
|
|
yield {
|
2023-08-25 09:12:23 -07:00
|
|
|
"month": month,
|
|
|
|
|
"sms_free_allowance_used": sms_free_allowance_used,
|
|
|
|
|
"sms_breakdown": sms_breakdown,
|
|
|
|
|
"sms_cost": sms_cost,
|
|
|
|
|
"sms_counts": sms_counts,
|
2017-04-25 19:03:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-30 17:27:09 +00:00
|
|
|
def requested_and_current_financial_year(request):
|
|
|
|
|
try:
|
|
|
|
|
return (
|
2023-08-25 09:12:23 -07:00
|
|
|
int(request.args.get("year", get_current_financial_year())),
|
2017-01-30 17:27:09 +00:00
|
|
|
get_current_financial_year(),
|
|
|
|
|
)
|
|
|
|
|
except ValueError:
|
|
|
|
|
abort(404)
|
2017-02-16 15:08:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_tuples_of_financial_years(
|
|
|
|
|
partial_url,
|
|
|
|
|
start=2015,
|
|
|
|
|
end=None,
|
|
|
|
|
):
|
|
|
|
|
return (
|
|
|
|
|
(
|
2023-08-25 09:12:23 -07:00
|
|
|
"fiscal year",
|
2017-02-16 15:08:16 +00:00
|
|
|
year,
|
|
|
|
|
partial_url(year=year),
|
2023-08-25 09:12:23 -07:00
|
|
|
"{} to {}".format(year, year + 1),
|
2017-02-16 15:08:16 +00:00
|
|
|
)
|
2020-05-07 16:36:50 +01:00
|
|
|
for year in reversed(range(start, end + 1))
|
2017-02-16 15:08:16 +00:00
|
|
|
)
|