Merge pull request #1390 from alphagov/rc-monthly-template-usage-endpoint

Added new endpoint to get the new template stats
This commit is contained in:
Richard Chapman
2017-11-15 16:20:04 +00:00
committed by GitHub
9 changed files with 602 additions and 39 deletions

View File

@@ -31,23 +31,25 @@ from app.dao.service_sms_sender_dao import (
update_existing_sms_sender_with_inbound_number
)
from app.dao.services_dao import (
dao_fetch_service_by_id,
dao_fetch_all_services,
dao_create_service,
dao_update_service,
dao_fetch_all_services_by_user,
dao_add_user_to_service,
dao_remove_user_from_service,
dao_archive_service,
dao_create_service,
dao_fetch_all_services,
dao_fetch_all_services_by_user,
dao_fetch_monthly_historical_stats_for_service,
dao_fetch_monthly_historical_stats_by_template_for_service,
dao_fetch_monthly_historical_usage_by_template_for_service,
dao_fetch_service_by_id,
dao_fetch_stats_for_service,
dao_fetch_todays_stats_for_service,
dao_fetch_todays_stats_for_all_services,
dao_archive_service,
fetch_stats_by_date_range_for_all_services,
dao_suspend_service,
dao_resume_service,
dao_fetch_monthly_historical_stats_for_service,
dao_fetch_monthly_historical_stats_by_template_for_service,
fetch_aggregate_stats_by_date_range_for_all_services)
dao_remove_user_from_service,
dao_suspend_service,
dao_update_service,
fetch_aggregate_stats_by_date_range_for_all_services,
fetch_stats_by_date_range_for_all_services
)
from app.dao.service_whitelist_dao import (
dao_fetch_service_whitelist,
dao_add_and_commit_whitelisted_contacts,
@@ -523,6 +525,31 @@ def resume_service(service_id):
return '', 204
@service_blueprint.route('/<uuid:service_id>/notifications/templates_usage/monthly', methods=['GET'])
def get_monthly_template_usage(service_id):
try:
data = dao_fetch_monthly_historical_usage_by_template_for_service(
service_id,
int(request.args.get('year', 'NaN'))
)
stats = list()
for i in data:
stats.append(
{
'template_id': str(i.template_id),
'name': i.name,
'month': i.month,
'year': i.year,
'count': i.count
}
)
return jsonify(stats=stats), 200
except ValueError:
raise InvalidRequest('Year must be a number', status_code=400)
@service_blueprint.route('/<uuid:service_id>/notifications/templates/monthly', methods=['GET'])
def get_monthly_template_stats(service_id):
service = dao_fetch_service_by_id(service_id)