2017-11-28 10:35:16 +00:00
|
|
|
from datetime import datetime
|
2016-07-28 13:34:24 +01:00
|
|
|
|
2017-02-14 17:59:18 +00:00
|
|
|
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
|
2016-07-28 13:34:24 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_statistics(statistics):
|
|
|
|
|
# statistics come in a named tuple with uniqueness from 'notification_type', 'status' - however missing
|
|
|
|
|
# statuses/notification types won't be represented and the status types need to be simplified/summed up
|
|
|
|
|
# so we can return emails/sms * created, sent, and failed
|
2016-08-23 17:08:53 +01:00
|
|
|
counts = create_zeroed_stats_dicts()
|
2016-07-28 13:34:24 +01:00
|
|
|
for row in statistics:
|
|
|
|
|
_update_statuses_from_row(counts[row.notification_type], row)
|
|
|
|
|
|
|
|
|
|
return counts
|
|
|
|
|
|
|
|
|
|
|
2018-06-20 14:04:38 +01:00
|
|
|
def format_admin_stats(statistics):
|
|
|
|
|
counts = create_stats_dict()
|
|
|
|
|
|
|
|
|
|
for row in statistics:
|
|
|
|
|
if row.key_type == 'test':
|
|
|
|
|
counts[row.notification_type]['test-key'] += row.count
|
|
|
|
|
else:
|
|
|
|
|
counts[row.notification_type]['total'] += row.count
|
|
|
|
|
if row.status in ('technical-failure', 'permanent-failure', 'temporary-failure', 'virus-scan-failed'):
|
|
|
|
|
counts[row.notification_type]['failures'][row.status] += row.count
|
|
|
|
|
|
|
|
|
|
return counts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def create_stats_dict():
|
|
|
|
|
stats_dict = {}
|
|
|
|
|
for template in TEMPLATE_TYPES:
|
|
|
|
|
stats_dict[template] = {}
|
|
|
|
|
|
|
|
|
|
for status in ('total', 'test-key'):
|
|
|
|
|
stats_dict[template][status] = 0
|
|
|
|
|
|
|
|
|
|
stats_dict[template]['failures'] = {
|
|
|
|
|
'technical-failure': 0,
|
|
|
|
|
'permanent-failure': 0,
|
|
|
|
|
'temporary-failure': 0,
|
|
|
|
|
'virus-scan-failed': 0,
|
|
|
|
|
}
|
|
|
|
|
return stats_dict
|
|
|
|
|
|
|
|
|
|
|
2017-02-14 17:59:18 +00:00
|
|
|
def format_monthly_template_notification_stats(year, rows):
|
2017-02-20 11:29:15 +00:00
|
|
|
stats = {
|
2017-02-14 17:59:18 +00:00
|
|
|
datetime.strftime(date, '%Y-%m'): {}
|
|
|
|
|
for date in [
|
|
|
|
|
datetime(year, month, 1) for month in range(4, 13)
|
|
|
|
|
] + [
|
|
|
|
|
datetime(year + 1, month, 1) for month in range(1, 4)
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
|
formatted_month = row.month.strftime('%Y-%m')
|
2017-02-20 11:29:15 +00:00
|
|
|
if str(row.template_id) not in stats[formatted_month]:
|
|
|
|
|
stats[formatted_month][str(row.template_id)] = {
|
2017-02-14 17:59:18 +00:00
|
|
|
"name": row.name,
|
2017-02-20 11:29:15 +00:00
|
|
|
"type": row.template_type,
|
2017-02-14 17:59:18 +00:00
|
|
|
"counts": dict.fromkeys(NOTIFICATION_STATUS_TYPES, 0)
|
|
|
|
|
}
|
2017-02-20 11:29:15 +00:00
|
|
|
stats[formatted_month][str(row.template_id)]["counts"][row.status] += row.count
|
2017-02-14 17:59:18 +00:00
|
|
|
|
2017-02-20 11:29:15 +00:00
|
|
|
return stats
|
2017-02-14 17:59:18 +00:00
|
|
|
|
|
|
|
|
|
2016-08-23 17:08:53 +01:00
|
|
|
def create_zeroed_stats_dicts():
|
2016-07-28 13:34:24 +01:00
|
|
|
return {
|
|
|
|
|
template_type: {
|
|
|
|
|
status: 0 for status in ('requested', 'delivered', 'failed')
|
2017-01-31 11:32:53 +00:00
|
|
|
} for template_type in TEMPLATE_TYPES
|
2016-07-28 13:34:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _update_statuses_from_row(update_dict, row):
|
|
|
|
|
update_dict['requested'] += row.count
|
2017-04-28 11:56:12 +01:00
|
|
|
if row.status in ('delivered', 'sent'):
|
2016-07-28 13:34:24 +01:00
|
|
|
update_dict['delivered'] += row.count
|
|
|
|
|
elif row.status in ('failed', 'technical-failure', 'temporary-failure', 'permanent-failure'):
|
|
|
|
|
update_dict['failed'] += row.count
|