set 0'd dict in redis if no data exists in redis

to get the data for a day can be reasonably slow (a few hundred
milliseconds), and if someone's viewing a service with no activity we
don't want to do that query seven times every two seconds. So if there
is no data in redis, when we get the data out of the database, we
should put it in redis so we can just grab it from there next time.

This'll happen in two cases:
* redis data is deleted
* the service sent no messages that day

additionally, make sure that we convert nicely from redis' return
values (ascii strings) to unicode keys and integer counts.
This commit is contained in:
Leo Hemsted
2018-04-13 14:21:10 +01:00
parent 85fd7c3869
commit 7dc34fc3a4
4 changed files with 81 additions and 33 deletions

View File

@@ -114,23 +114,28 @@ def test_template_usage_should_filter_by_service(notify_db_session):
template_1 = create_template(service_1)
template_2 = create_template(service_2) # noqa
template_3 = create_template(service_3)
template_3a = create_template(service_3)
template_3b = create_template(service_3) # noqa
# two for service_1, one for service_3
create_notification(template_1)
create_notification(template_1)
create_notification(template_3)
create_notification(template_3a)
res1 = dao_get_template_usage(service_1.id, limit_days=1)
res2 = dao_get_template_usage(service_2.id, limit_days=1)
res3 = dao_get_template_usage(service_3.id, limit_days=1)
assert len(res1) == 1
assert len(res2) == 0
assert len(res3) == 1
assert res1[0].count == 2
assert len(res2) == 1
assert res2[0].count == 0
assert len(res3) == 2
assert res3[0].count == 1
assert res3[1].count == 0
def test_template_usage_should_by_able_to_get_zero_count_from_notifications_history_if_no_rows(sample_service):