mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-16 18:22:17 -05:00
The weekly stats endpoint wont be used once https://github.com/alphagov/notifications-admin/pull/1109 has been merged. It has been replaced with a new monthly endpoint in https://github.com/alphagov/notifications-api/pull/807
32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
import itertools
|
|
from datetime import datetime, timedelta
|
|
|
|
from app.models import TEMPLATE_TYPES
|
|
|
|
|
|
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
|
|
counts = create_zeroed_stats_dicts()
|
|
for row in statistics:
|
|
_update_statuses_from_row(counts[row.notification_type], row)
|
|
|
|
return counts
|
|
|
|
|
|
def create_zeroed_stats_dicts():
|
|
return {
|
|
template_type: {
|
|
status: 0 for status in ('requested', 'delivered', 'failed')
|
|
} for template_type in TEMPLATE_TYPES
|
|
}
|
|
|
|
|
|
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
|