2016-05-25 16:51:09 +01:00
|
|
|
from datetime import datetime
|
|
|
|
|
from functools import reduce
|
|
|
|
|
|
2018-02-20 11:22:17 +00:00
|
|
|
from dateutil import parser
|
|
|
|
|
|
2025-08-15 15:02:54 -04:00
|
|
|
from app.utils.csv import get_user_preferred_timezone_obj
|
2023-11-16 12:24:27 -08:00
|
|
|
|
2016-05-25 16:51:09 +01:00
|
|
|
|
|
|
|
|
def sum_of_statistics(delivery_statistics):
|
|
|
|
|
statistics_keys = (
|
2023-08-25 09:12:23 -07:00
|
|
|
"emails_delivered",
|
|
|
|
|
"emails_requested",
|
|
|
|
|
"emails_failed",
|
|
|
|
|
"sms_requested",
|
|
|
|
|
"sms_delivered",
|
|
|
|
|
"sms_failed",
|
2016-05-25 16:51:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if not delivery_statistics or not delivery_statistics[0]:
|
2023-08-25 09:12:23 -07:00
|
|
|
return {key: 0 for key in statistics_keys}
|
2016-05-25 16:51:09 +01:00
|
|
|
|
|
|
|
|
return reduce(
|
2023-08-25 09:12:23 -07:00
|
|
|
lambda x, y: {key: x.get(key, 0) + y.get(key, 0) for key in statistics_keys},
|
|
|
|
|
delivery_statistics,
|
2016-05-25 16:51:09 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def add_rates_to(delivery_statistics):
|
2025-08-15 15:02:54 -04:00
|
|
|
preferred_tz = get_user_preferred_timezone_obj()
|
2016-05-25 16:51:09 +01:00
|
|
|
return dict(
|
2016-07-20 11:46:29 +01:00
|
|
|
emails_failure_rate=get_formatted_percentage(
|
2023-08-25 09:12:23 -07:00
|
|
|
delivery_statistics["emails_failed"],
|
|
|
|
|
delivery_statistics["emails_requested"],
|
|
|
|
|
),
|
2016-07-19 17:10:48 +01:00
|
|
|
sms_failure_rate=get_formatted_percentage(
|
2023-08-25 09:12:23 -07:00
|
|
|
delivery_statistics["sms_failed"], delivery_statistics["sms_requested"]
|
|
|
|
|
),
|
2016-05-25 16:51:09 +01:00
|
|
|
week_end_datetime=parser.parse(
|
2023-11-22 13:06:04 -05:00
|
|
|
delivery_statistics.get("week_end", str(datetime.now(preferred_tz)))
|
2016-05-25 16:51:09 +01:00
|
|
|
),
|
|
|
|
|
**delivery_statistics
|
|
|
|
|
)
|
2016-06-08 15:15:59 +01:00
|
|
|
|
|
|
|
|
|
2016-07-19 17:10:48 +01:00
|
|
|
def get_formatted_percentage(x, tot):
|
|
|
|
|
"""
|
|
|
|
|
Return a percentage to one decimal place (respecting )
|
|
|
|
|
"""
|
2023-08-25 09:12:23 -07:00
|
|
|
return "{0:.1f}".format((float(x) / tot * 100)) if tot else "0"
|
2016-07-19 17:10:48 +01:00
|
|
|
|
|
|
|
|
|
2018-08-14 13:34:53 +01:00
|
|
|
def get_formatted_percentage_two_dp(x, tot):
|
|
|
|
|
"""
|
|
|
|
|
Return a percentage to two decimal places
|
|
|
|
|
"""
|
2023-08-25 09:12:23 -07:00
|
|
|
return "{0:.2f}".format((float(x) / tot * 100)) if tot else "0"
|
2018-08-14 13:34:53 +01:00
|
|
|
|
|
|
|
|
|
2016-06-08 15:15:59 +01:00
|
|
|
def statistics_by_state(statistics):
|
|
|
|
|
return {
|
2023-08-25 09:12:23 -07:00
|
|
|
"sms": {
|
|
|
|
|
"processed": statistics["sms_requested"],
|
|
|
|
|
"sending": (
|
|
|
|
|
statistics["sms_requested"]
|
|
|
|
|
- statistics["sms_failed"]
|
|
|
|
|
- statistics["sms_delivered"]
|
2016-06-08 15:15:59 +01:00
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
"delivered": statistics["sms_delivered"],
|
|
|
|
|
"failed": statistics["sms_failed"],
|
2016-06-08 15:15:59 +01:00
|
|
|
},
|
2023-08-25 09:12:23 -07:00
|
|
|
"email": {
|
|
|
|
|
"processed": statistics["emails_requested"],
|
|
|
|
|
"sending": (
|
|
|
|
|
statistics["emails_requested"]
|
|
|
|
|
- statistics["emails_failed"]
|
|
|
|
|
- statistics["emails_delivered"]
|
2016-06-08 15:15:59 +01:00
|
|
|
),
|
2023-08-25 09:12:23 -07:00
|
|
|
"delivered": statistics["emails_delivered"],
|
|
|
|
|
"failed": statistics["emails_failed"],
|
|
|
|
|
},
|
2016-06-08 15:15:59 +01:00
|
|
|
}
|