diff --git a/controllers/myprofile/dialoga_plugin_myprofile_controller.rb b/controllers/myprofile/dialoga_plugin_myprofile_controller.rb index 6768e73..5737321 100644 --- a/controllers/myprofile/dialoga_plugin_myprofile_controller.rb +++ b/controllers/myprofile/dialoga_plugin_myprofile_controller.rb @@ -4,10 +4,11 @@ class DialogaPluginMyprofileController < MyProfileController before_filter :is_admin def send_report - Delayed::Job.enqueue(DialogaPlugin::ReportJob.new(profile.id)) - Delayed::Job.enqueue(DialogaPlugin::RankingJob.new(profile.id)) - Delayed::Job.enqueue(DialogaPlugin::EventJob.new(profile.id)) - session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios") + report_path = Time.zone.now.strftime('%Y-%m-%d-%H-%m-%S') + Delayed::Job.enqueue(DialogaPlugin::ReportJob.new(profile.id, report_path)) + Delayed::Job.enqueue(DialogaPlugin::RankingJob.new(profile.id, report_path)) + Delayed::Job.enqueue(DialogaPlugin::EventJob.new(profile.id, report_path)) + session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios/#{report_path}") redirect_to :back end diff --git a/lib/dialoga_plugin/event_job.rb b/lib/dialoga_plugin/event_job.rb index 7537f25..408c3e1 100644 --- a/lib/dialoga_plugin/event_job.rb +++ b/lib/dialoga_plugin/event_job.rb @@ -10,7 +10,7 @@ class DialogaPlugin::EventJob < DialogaPlugin::ReportJob def create_event_report(profile, report_folder) events = Event.where(:profile_id => profile.id) events.map do |event| - filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + event.slug + '.csv' + filepath = "/tmp/#{report_path}/evento-#{event.slug}.csv" file = File.open(File.join(filepath), 'w+') file.write(event.name+ "\n") header = "'Nome';'Email'\n" @@ -26,9 +26,8 @@ class DialogaPlugin::EventJob < DialogaPlugin::ReportJob file.write("\n") end file.close - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) - uploaded_file.save end + upload_file(compress_files('eventos', 'evento-*'), profile, report_folder) end end diff --git a/lib/dialoga_plugin/ranking_job.rb b/lib/dialoga_plugin/ranking_job.rb index 7c5ba26..fed89e9 100644 --- a/lib/dialoga_plugin/ranking_job.rb +++ b/lib/dialoga_plugin/ranking_job.rb @@ -16,15 +16,14 @@ class DialogaPlugin::RankingJob < DialogaPlugin::ReportJob ranking = article.ranking next if ranking.empty? - filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + "ranking_#{discussion.slug}_#{article.slug}.csv" + filepath = "/tmp/#{report_path}/ranking-#{discussion.slug}_#{article.slug}.csv" CSV.open(filepath, 'w' ) do |csv| csv << ['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor'] ranking.each_with_index {|r, i| csv << [i+1, r.values].flatten} end - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) - uploaded_file.save end end + upload_file(compress_files('rankings', 'ranking-*'), profile, report_folder) end end diff --git a/lib/dialoga_plugin/report_job.rb b/lib/dialoga_plugin/report_job.rb index aab7220..4a0dda1 100644 --- a/lib/dialoga_plugin/report_job.rb +++ b/lib/dialoga_plugin/report_job.rb @@ -1,14 +1,23 @@ -class DialogaPlugin::ReportJob < Struct.new(:profile_id) +class DialogaPlugin::ReportJob < Struct.new(:profile_id, :report_path) include ActionDispatch::TestProcess def create_report_path(profile) root_report_folder = profile.folders.where(:slug => 'relatorios').first root_report_folder ||= Folder.create!(:profile => profile, :name => 'Relatorios') + FileUtils.mkdir_p "/tmp/#{report_path}" + report_folder = Folder.find_by_slug(report_path) + report_folder ||= Folder.create!(:profile => profile, :name => report_path, :parent => root_report_folder) + end - report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d')) + def upload_file(filepath, profile, report_folder) + UploadedFile.create!(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) + end - report_folder ||= Folder.create!(:profile => profile, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder) + def compress_files(filename, pattern) + filepath = "/tmp/#{report_path}/#{filename}.tar.gz" + system("cd /tmp/#{report_path} && tar -zcvf #{filepath} #{pattern}") + filepath end def perform @@ -18,12 +27,12 @@ class DialogaPlugin::ReportJob < Struct.new(:profile_id) end def create_proposals_report(profile, report_folder) - filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + 'propostas.csv' + filepath = "/tmp/#{report_path}/propostas.csv" file = File.open(filepath, 'w+') tasks = ProposalsDiscussionPlugin::ProposalTask.all count = 0 - header = "'Origem';'Status';'Criada em';'Moderado por';'Data de Moderado';'Validado por';'Data de Validado';'Autor';'Proposta'\n" + header = "'Origem';'Status';'Criada em';'Moderado por';'Data de Moderado';'Validado por';'Data de Validado';'Autor';'Proposta';'Categorias'\n" file.write(header) status_translation = { 1 => 'Pendente de Moderacao', @@ -44,15 +53,13 @@ class DialogaPlugin::ReportJob < Struct.new(:profile_id) info.push(task.closed_by.present? ? task.closed_by.name : '') info.push(task.closed_by.present? ? task.end_date.strftime("%d/%m/%y %H:%M") : '') info.push(task.requestor.present? ? task.requestor.name : '') - info.push(task.abstract.present? ? task.abstract.gsub(/\s+/, ' ').strip : '') + info.push(task.categories.map {|c| c.name}.join(', ')) file.write(info.map{|i| "'" + i.to_s + "'"}.join(";")) file.write("\n") end file.close - - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) - uploaded_file.save + upload_file(filepath, profile, report_folder) end end -- libgit2 0.21.2