Default aggregate statistics to 0, not None

If they API returns no statistics for a given time period we should
assume that this is equivalent to 0. This means that the template can
always rely on the dictionary having the same keys.
This commit is contained in:
Chris Hill-Scott
2016-05-03 09:18:24 +01:00
parent 744064e840
commit d1dec6d5a0

View File

@@ -82,20 +82,24 @@ def template_history(service_id):
def add_rates_to(delivery_statistics):
keys = [
'emails_delivered',
'emails_requested',
'emails_failed',
'sms_requested',
'sms_delivered',
'sms_failed'
]
if not delivery_statistics or not delivery_statistics[0]:
return {}
return {
key: 0 for key in keys
}
sum_of_statistics = reduce(
lambda x, y: {
key: x.get(key, 0) + y.get(key, 0)
for key in [
'emails_delivered',
'emails_requested',
'emails_failed',
'sms_requested',
'sms_delivered',
'sms_failed'
]
for key in keys
},
delivery_statistics
)