2016-07-28 13:34:24 +01:00
|
|
|
import itertools
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
2017-01-31 11:32:53 +00:00
|
|
|
from app.models import 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
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
if row.status == 'delivered':
|
|
|
|
|
update_dict['delivered'] += row.count
|
|
|
|
|
elif row.status in ('failed', 'technical-failure', 'temporary-failure', 'permanent-failure'):
|
|
|
|
|
update_dict['failed'] += row.count
|