Merge pull request #1070 from alphagov/return-streamed-csv

Fix timeout on download of large CSV reports
This commit is contained in:
imdadahad
2017-01-16 10:34:25 +00:00
committed by GitHub
7 changed files with 256 additions and 144 deletions

View File

@@ -12,7 +12,8 @@ from flask import (
request,
url_for,
current_app,
redirect
redirect,
Response
)
from flask_login import login_required
from werkzeug.datastructures import MultiDict
@@ -103,7 +104,6 @@ def view_job(service_id, job_id):
total_notifications = job.get('notification_count', 0)
processed_notifications = job.get('notifications_delivered', 0) + job.get('notifications_failed', 0)
return render_template(
'views/jobs/job.html',
finished=(total_notifications == processed_notifications),
@@ -142,23 +142,20 @@ def view_job_csv(service_id, job_id):
filter_args = _parse_filter_args(request.args)
filter_args['status'] = _set_status_filters(filter_args)
return (
return Response(
generate_notifications_csv(
notification_api_client.get_notifications_for_service(
service_id,
job_id,
status=filter_args.get('status'),
page_size=job['notification_count']
)['notifications']
service_id=service_id,
job_id=job_id,
status=filter_args.get('status'),
page=request.args.get('page', 1),
page_size=5000
),
200,
{
'Content-Type': 'text/csv; charset=utf-8',
mimetype='text/csv',
headers={
'Content-Disposition': 'inline; filename="{} - {}.csv"'.format(
template['name'],
format_datetime_short(job['created_at'])
)
}
)}
)
@@ -208,9 +205,22 @@ def get_notifications(service_id, message_type, status_override=None):
abort(404, "Invalid page argument ({}) reverting to page 1.".format(request.args['page'], None))
if message_type not in ['email', 'sms']:
abort(404)
filter_args = _parse_filter_args(request.args)
filter_args['status'] = _set_status_filters(filter_args)
if request.path.endswith('csv'):
return Response(
generate_notifications_csv(
service_id=service_id,
page=page,
page_size=5000,
template_type=[message_type],
status=filter_args.get('status'),
limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS']
),
mimetype='text/csv',
headers={
'Content-Disposition': 'inline; filename="notifications.csv"'}
)
notifications = notification_api_client.get_notifications_for_service(
service_id=service_id,
@@ -224,25 +234,14 @@ def get_notifications(service_id, message_type, status_override=None):
'status': request.args.get('status')
}
prev_page = None
if notifications['links'].get('prev', None):
prev_page = generate_previous_dict('main.view_notifications', service_id, page, url_args=url_args)
next_page = None
if notifications['links'].get('next', None):
next_page = generate_next_dict('main.view_notifications', service_id, page, url_args)
if request.path.endswith('csv'):
csv_content = generate_notifications_csv(
notification_api_client.get_notifications_for_service(
service_id=service_id,
page=page,
page_size=notifications['total'],
template_type=[message_type],
status=filter_args.get('status'),
limit_days=current_app.config['ACTIVITY_STATS_LIMIT_DAYS'])['notifications'])
return csv_content, 200, {
'Content-Type': 'text/csv; charset=utf-8',
'Content-Disposition': 'inline; filename="notifications.csv"'
}
return {
'counts': render_template(
'views/activity/counts.html',
@@ -296,7 +295,7 @@ def get_status_filters(service, message_type, statistics):
stats[key]
)
for key, label, option in filters
]
]
def _get_job_counts(job, help_argument):