From 006e778da56b666be45ca73fc440cd716531ddbf Mon Sep 17 00:00:00 2001 From: ronualdo maciel Date: Thu, 23 May 2024 20:24:09 -0300 Subject: [PATCH] Changes csv download mechanism Changes action to avoid writing the whole file to the disk before sending the response and potentially triggering response timeouts. --- app/controllers/blazer/queries_controller.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/app/controllers/blazer/queries_controller.rb b/app/controllers/blazer/queries_controller.rb index ce0130d79..8361a7e8e 100644 --- a/app/controllers/blazer/queries_controller.rb +++ b/app/controllers/blazer/queries_controller.rb @@ -271,9 +271,15 @@ def render_run # not ideal, but useful for testing raise Error, @error if @error && Rails.env.test? - data = csv_data(@columns, @rows, @data_source) filename = "#{@query.try(:name).try(:parameterize).presence || 'query'}.csv" - send_data data, type: "text/csv; charset=utf-8", disposition: "attachment", filename: filename + response.headers['Content-Disposition'] = "attachment; filename=\"#{filename}\"" + response.headers['Content-Type'] = 'text/event-stream' + + response.stream.write(CSV.generate_line(@columns)) + @rows.each do |row| + response.stream.write(CSV.generate_line(row)) + end + response.stream.close end end end @@ -369,15 +375,6 @@ def blazer_params params[:blazer] || {} end - def csv_data(columns, rows, data_source) - CSV.generate do |csv| - csv << columns - rows.each do |row| - csv << row.each_with_index.map { |v, i| v.is_a?(Time) ? blazer_time_value(data_source, columns[i], v) : v } - end - end - end - def blazer_time_value(data_source, k, v) data_source.local_time_suffix.any? { |s| k.ends_with?(s) } ? v.to_s.sub(" UTC", "") : v.in_time_zone(Blazer.time_zone) end