Added a redis cache for the template usage stats.

Cache expires every 10 minutes, but will help with the every 2 second query, especially when a job is running.
There is some clean up and qa to do for this yet
This commit is contained in:
Rebecca Law
2017-02-13 18:47:29 +00:00
parent b2267ae5fc
commit 458adefcb8
9 changed files with 351 additions and 218 deletions

View File

@@ -1,14 +1,16 @@
from flask import (
Blueprint,
jsonify,
request
)
request,
current_app)
from app import redis_store
from app.dao.notifications_dao import (
dao_get_template_usage,
dao_get_last_template_usage)
from app.dao.templates_dao import dao_get_templates_by_for_cache
from app.schemas import notifications_filter_schema, NotificationWithTemplateSchema, notification_with_template_schema
from app.schemas import notification_with_template_schema
template_statistics = Blueprint('template-statistics',
__name__,
@@ -30,7 +32,15 @@ def get_template_statistics_for_service_by_day(service_id):
raise InvalidRequest(message, status_code=400)
else:
limit_days = None
stats = dao_get_template_usage(service_id, limit_days=limit_days)
if limit_days == 7:
stats = get_template_statistics_for_7_days(limit_days, service_id)
print(stats)
# [(UUID('c2a331f8-e0b9-43de-9dd2-88300511a1d7'), 'Create with priority', 'sms', 1)]
else:
stats = dao_get_template_usage(service_id, limit_days=limit_days)
print(stats)
def serialize(data):
return {
@@ -52,3 +62,18 @@ def get_template_statistics_for_template_id(service_id, template_id):
raise InvalidRequest(errors, status_code=404)
data = notification_with_template_schema.dump(notification).data
return jsonify(data=data)
def get_template_statistics_for_7_days(limit_days, service_id):
cache_key = "{}-template-counter-limit-7-days".format(service_id)
template_stats_by_id = redis_store.get_all_from_hash(cache_key)
if not template_stats_by_id:
print("populate cache")
stats = dao_get_template_usage(service_id, limit_days=limit_days)
cache_values = dict([(x.template_id, x.count) for x in stats])
redis_store.set_hash_and_expire(cache_key, cache_values, current_app.config.get('EXPIRE_CACHE_IN_SECONDS', 600))
current_app.logger.info('use redis-client: {}'.format(cache_key))
else:
print("template_stats_by_id: {}".format(template_stats_by_id))
stats = dao_get_templates_by_for_cache(template_stats_by_id.items())
return stats