fix platform admin stats row-order bug

now that we're reading from two tables (ft_notification_status and
notifications) for stats, we'll get a couple of rows for each
notification type. If a service doesn't have any rows in one of those
tables, the query will return a row with nulls for the notification
types and counts. Some services will have history but no stats from
today, others will have data from today but no history.

This commit acknowledges that any row might have nulls, not just the
first row.
This commit is contained in:
Leo Hemsted
2019-01-09 11:43:40 +00:00
parent 5d838415d3
commit 3e21f57481
3 changed files with 9 additions and 5 deletions

View File

@@ -478,10 +478,7 @@ def get_detailed_services(start_date, end_date, only_active=False, include_from_
results = []
for service_id, rows in itertools.groupby(stats, lambda x: x.service_id):
rows = list(rows)
if rows[0].count is None:
s = statistics.create_zeroed_stats_dicts()
else:
s = statistics.format_statistics(rows)
s = statistics.format_statistics(rows)
results.append({
'id': str(rows[0].service_id),
'name': rows[0].name,

View File

@@ -13,7 +13,10 @@ def format_statistics(statistics):
# 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)
# any row could be null, if the service either has no notifications in the notifications table,
# or no historical data in the ft_notification_status table.
if row.notification_type:
_update_statuses_from_row(counts[row.notification_type], row)
return counts

View File

@@ -45,6 +45,10 @@ NewStatsRow = collections.namedtuple('row', ('notification_type', 'status', 'key
StatsRow('sms', 'delivered', 1),
StatsRow('sms', 'sent', 1),
], [0, 0, 0], [3, 2, 0], [0, 0, 0]),
'handles_none_rows': ([
StatsRow('sms', 'sending', 1),
StatsRow(None, None, None)
], [0, 0, 0], [1, 0, 0], [0, 0, 0])
})
def test_format_statistics(stats, email_counts, sms_counts, letter_counts):