New endpoint for template stats to use new query. Breaking change to the returned JSON, so adding on a different url.

This commit is contained in:
Martyn Inglis
2016-08-18 14:01:31 +01:00
parent f74000f548
commit 7a5acea71b
2 changed files with 158 additions and 2 deletions

View File

@@ -5,6 +5,7 @@ from flask import (
)
from app.dao.notifications_dao import (
dao_get_template_usage,
dao_get_template_statistics_for_service,
dao_get_template_statistics_for_template
)
@@ -36,6 +37,31 @@ def get_template_statistics_for_service(service_id):
return jsonify(data=data)
@template_statistics.route('/replacement')
def get_template_statistics_for_service_by_day(service_id):
if request.args.get('limit_days'):
try:
limit_days = int(request.args['limit_days'])
except ValueError as e:
error = '{} is not an integer'.format(request.args['limit_days'])
message = {'limit_days': [error]}
raise InvalidRequest(message, status_code=400)
else:
limit_days = None
stats = dao_get_template_usage(service_id, limit_days=limit_days)
def serialize(row):
return {
'count': row.count,
'day': str(row.day),
'template_id': str(row.template_id),
'template_name': row.name,
'template_type': row.template_type
}
return jsonify(data=[serialize(row) for row in stats])
@template_statistics.route('/<template_id>')
def get_template_statistics_for_template_id(service_id, template_id):
stats = dao_get_template_statistics_for_template(template_id)