download manager done

This commit is contained in:
IT 2017-04-11 18:43:22 +08:00
parent ff75d0bef8
commit b6987db8aa
5 changed files with 59 additions and 31 deletions

View File

@ -146,18 +146,21 @@ class Admin::SurveysController < OrbitAdminController
end
def checkforthread
render :json => {"status" => @download_thread.alive?}.to_json
running = !File.exists?("public/uploads/survey_export/#{params[:survey_id]}/#{params[:survey_title]}.xlsx")
render :json => {"status" => running}.to_json
end
def export
@download_thread = Thread.new do
# %x[rake survey_tasks:prepare_download[#{@survey.id}]]
(1..10).each do |i|
sleep(1)
I18n.locale = :zh_tw
title = @survey.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')
f = "public/uploads/survey_export/#{@survey.id}/#{title}.xlsx"
File.delete(f) if File.exists?(f)
Thread.new do
system "rake survey_tasks:prepare_download[#{@survey.id}] >> #{Rails.root}/log/rake.log &"
end
end
@download_thread.join
render :json => {"success" => true}.to_json
render :json => {"success" => true, "title" => title}.to_json
# @chart_data, @survey_questions, @survey_answers = @survey.generate_chart_data
# respond_to do |format|
# format.xlsx {

View File

@ -30,7 +30,7 @@
<li><a href="/admin/surveys/<%=survey.id.to_s%>/duplicate_it"><%= t('survey.duplicate_it') %></a></li>
<li><a href="/admin/surveys/<%=survey.id.to_s%>/jump"><%= t('survey.jump') %></a></li>
<li><a href="/admin/surveys/<%=survey.id.to_s%>/set_answers"><%= t('survey.set_answers') %></a></li>
<li><a href="/admin/surveys/<%=survey.id.to_s%>/export?format=xlsx" target="_blank" class="export-xls"><%= t('survey.export_csv') %></a></li>
<li><a href="/admin/surveys/<%=survey.id.to_s%>/export?format=xlsx" data-survey-id="<%= survey.id.to_s %>" class="export-xls"><%= t('survey.export_csv') %></a></li>
<li><a href="<%=page_for_survey(survey)%>?method=result&force_chart=true" target="_blank"><%= t('survey.chart') %></a></li>
<li><a href="#" class="delete text-error" rel="/admin/surveys/<%=survey.id.to_s%>"><%= t(:delete_) %></a></li>
<% end %>

View File

@ -5,7 +5,7 @@
<%= render 'layouts/delete_modal', delete_options: @delete_options %>
<div id="downloadModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="downloadModalLabel" aria-hidden="true">
<div id="downloadModal" data-backdrop="static" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="downloadModalLabel" aria-hidden="true">
<div class="modal-header">
<h3 id="downloadModalLabel">Download</h3>
</div>
@ -15,38 +15,63 @@
<br />
<img src="/assets/spin.gif" />
</p>
<p id="link-zone" style="display: none; text-align: center;">
Please click the link below to download.
<br />
<a href="" id="download-link" target="_blank">Download</a>
</p>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button class="btn" id="modal-close-btn" style="display:none;" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>
<script type="text/javascript" src="/assets/lib/process.manager.js"></script>
<script type="text/javascript">
var downloadModal = $("#downloadModal");
var downloadModal = $("#downloadModal"),
checkForThread = null,
waitZone = $("#wait-zone"),
linkZone = $("#link-zone"),
downloadLink = $("a#download-link"),
modalBtn = $("#modal-close-btn"),
processManager = new ProcessManager();
$(document).on("click", ".export-xls", function(){
var link = $(this).attr("href");
var link = $(this).attr("href"),
title = null,
id = $(this).data("survey-id");
linkZone.hide();
waitZone.show();
modalBtn.hide();
$.ajax({
url : link,
type : "get",
dataType : "json"
}).done(function(data){
title = data.title;
checkForThread = new Process(function(){
$.ajax({
url : "/admin/surveys/checkforthread",
type : "get",
data : {"survey_id" : id, "survey_title" : title},
dataType : "json"
}).done(function(data){
if(!data.status){
downloadLink.attr("href", "/uploads/survey_export/" + id + "/" + title + ".xlsx");
waitZone.hide();
linkZone.show();
modalBtn.show();
checkForThread.kill();
}
})
})
checkForThread.setTimeInterval(1000);
checkForThread.setRepeat(Process.CONSTANTS.REPEAT_INFINITE);
processManager.queue(checkForThread);
})
checkForThread();
downloadModal.modal("show");
return false;
})
var checkForThread = function(){
$.ajax({
url : "/admin/surveys/checkforthread",
type : "get",
dataType : "json"
}).done(function(data){
console.log("Status : " + data.status);
if(!data.status){
checkForThread();
}
})
}
</script>

View File

@ -1,7 +1,7 @@
# encoding: utf-8
wb = xlsx_package.workbook
wb.add_worksheet(name: remove_illegal_utf8(survey.title[0..15])) do |sheet|
wb.add_worksheet(name: "WorkSheet1") do |sheet|
row = []
survey_questions.each_with_index do |question, i|

View File

@ -11,9 +11,9 @@ namespace :survey_tasks do
chart_data, survey_questions, survey_answers = survey.generate_chart_data
ac = ActionController::Base.new()
xlsx = ac.render_to_string handlers: [:axlsx], formats: [:xlsx], template: "survey_export/export", locals: {survey: survey, survey_questions: survey_questions, survey_answers: survey_answers}
dirname = "public/survey_export/#{id}"
dirname = "public/uploads/survey_export/#{id}"
FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
f = "#{dirname}/#{survey.title.gusb(' ', '_')}.xlsx"
f = "#{dirname}/#{survey.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\\/<~_!:,、。!?;「」〈〉【】/]/,'')}.xlsx"
if File.exists?(f)
File.delete(f)
end