Commit 39fe1dc9176fa224eb8b382c18fc2aec732a6154
1 parent
bde27f29
Exists in
master
Improve report generation
Showing
4 changed files
with
25 additions
and
19 deletions
Show diff stats
controllers/myprofile/dialoga_plugin_myprofile_controller.rb
| ... | ... | @@ -4,10 +4,11 @@ class DialogaPluginMyprofileController < MyProfileController |
| 4 | 4 | before_filter :is_admin |
| 5 | 5 | |
| 6 | 6 | def send_report |
| 7 | - Delayed::Job.enqueue(DialogaPlugin::ReportJob.new(profile.id)) | |
| 8 | - Delayed::Job.enqueue(DialogaPlugin::RankingJob.new(profile.id)) | |
| 9 | - Delayed::Job.enqueue(DialogaPlugin::EventJob.new(profile.id)) | |
| 10 | - session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios") | |
| 7 | + report_path = Time.zone.now.strftime('%Y-%m-%d-%H-%m-%S') | |
| 8 | + Delayed::Job.enqueue(DialogaPlugin::ReportJob.new(profile.id, report_path)) | |
| 9 | + Delayed::Job.enqueue(DialogaPlugin::RankingJob.new(profile.id, report_path)) | |
| 10 | + Delayed::Job.enqueue(DialogaPlugin::EventJob.new(profile.id, report_path)) | |
| 11 | + session[:notice] = _("Favor aguardar: o relatório será criado na pasta Relatorios/#{report_path}") | |
| 11 | 12 | redirect_to :back |
| 12 | 13 | end |
| 13 | 14 | ... | ... |
lib/dialoga_plugin/event_job.rb
| ... | ... | @@ -10,7 +10,7 @@ class DialogaPlugin::EventJob < DialogaPlugin::ReportJob |
| 10 | 10 | def create_event_report(profile, report_folder) |
| 11 | 11 | events = Event.where(:profile_id => profile.id) |
| 12 | 12 | events.map do |event| |
| 13 | - filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + event.slug + '.csv' | |
| 13 | + filepath = "/tmp/#{report_path}/evento-#{event.slug}.csv" | |
| 14 | 14 | file = File.open(File.join(filepath), 'w+') |
| 15 | 15 | file.write(event.name+ "\n") |
| 16 | 16 | header = "'Nome';'Email'\n" |
| ... | ... | @@ -26,9 +26,8 @@ class DialogaPlugin::EventJob < DialogaPlugin::ReportJob |
| 26 | 26 | file.write("\n") |
| 27 | 27 | end |
| 28 | 28 | file.close |
| 29 | - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) | |
| 30 | - uploaded_file.save | |
| 31 | 29 | end |
| 30 | + upload_file(compress_files('eventos', 'evento-*'), profile, report_folder) | |
| 32 | 31 | end |
| 33 | 32 | |
| 34 | 33 | end | ... | ... |
lib/dialoga_plugin/ranking_job.rb
| ... | ... | @@ -16,15 +16,14 @@ class DialogaPlugin::RankingJob < DialogaPlugin::ReportJob |
| 16 | 16 | ranking = article.ranking |
| 17 | 17 | next if ranking.empty? |
| 18 | 18 | |
| 19 | - filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + "ranking_#{discussion.slug}_#{article.slug}.csv" | |
| 19 | + filepath = "/tmp/#{report_path}/ranking-#{discussion.slug}_#{article.slug}.csv" | |
| 20 | 20 | CSV.open(filepath, 'w' ) do |csv| |
| 21 | 21 | csv << ['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor'] |
| 22 | 22 | ranking.each_with_index {|r, i| csv << [i+1, r.values].flatten} |
| 23 | 23 | end |
| 24 | - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) | |
| 25 | - uploaded_file.save | |
| 26 | 24 | end |
| 27 | 25 | end |
| 26 | + upload_file(compress_files('rankings', 'ranking-*'), profile, report_folder) | |
| 28 | 27 | end |
| 29 | 28 | |
| 30 | 29 | end | ... | ... |
lib/dialoga_plugin/report_job.rb
| 1 | -class DialogaPlugin::ReportJob < Struct.new(:profile_id) | |
| 1 | +class DialogaPlugin::ReportJob < Struct.new(:profile_id, :report_path) | |
| 2 | 2 | |
| 3 | 3 | include ActionDispatch::TestProcess |
| 4 | 4 | |
| 5 | 5 | def create_report_path(profile) |
| 6 | 6 | root_report_folder = profile.folders.where(:slug => 'relatorios').first |
| 7 | 7 | root_report_folder ||= Folder.create!(:profile => profile, :name => 'Relatorios') |
| 8 | + FileUtils.mkdir_p "/tmp/#{report_path}" | |
| 9 | + report_folder = Folder.find_by_slug(report_path) | |
| 10 | + report_folder ||= Folder.create!(:profile => profile, :name => report_path, :parent => root_report_folder) | |
| 11 | + end | |
| 8 | 12 | |
| 9 | - report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d')) | |
| 13 | + def upload_file(filepath, profile, report_folder) | |
| 14 | + UploadedFile.create!(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) | |
| 15 | + end | |
| 10 | 16 | |
| 11 | - report_folder ||= Folder.create!(:profile => profile, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder) | |
| 17 | + def compress_files(filename, pattern) | |
| 18 | + filepath = "/tmp/#{report_path}/#{filename}.tar.gz" | |
| 19 | + system("cd /tmp/#{report_path} && tar -zcvf #{filepath} #{pattern}") | |
| 20 | + filepath | |
| 12 | 21 | end |
| 13 | 22 | |
| 14 | 23 | def perform |
| ... | ... | @@ -18,12 +27,12 @@ class DialogaPlugin::ReportJob < Struct.new(:profile_id) |
| 18 | 27 | end |
| 19 | 28 | |
| 20 | 29 | def create_proposals_report(profile, report_folder) |
| 21 | - filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + 'propostas.csv' | |
| 30 | + filepath = "/tmp/#{report_path}/propostas.csv" | |
| 22 | 31 | file = File.open(filepath, 'w+') |
| 23 | 32 | |
| 24 | 33 | tasks = ProposalsDiscussionPlugin::ProposalTask.all |
| 25 | 34 | count = 0 |
| 26 | - header = "'Origem';'Status';'Criada em';'Moderado por';'Data de Moderado';'Validado por';'Data de Validado';'Autor';'Proposta'\n" | |
| 35 | + header = "'Origem';'Status';'Criada em';'Moderado por';'Data de Moderado';'Validado por';'Data de Validado';'Autor';'Proposta';'Categorias'\n" | |
| 27 | 36 | file.write(header) |
| 28 | 37 | status_translation = { |
| 29 | 38 | 1 => 'Pendente de Moderacao', |
| ... | ... | @@ -44,15 +53,13 @@ class DialogaPlugin::ReportJob < Struct.new(:profile_id) |
| 44 | 53 | info.push(task.closed_by.present? ? task.closed_by.name : '') |
| 45 | 54 | info.push(task.closed_by.present? ? task.end_date.strftime("%d/%m/%y %H:%M") : '') |
| 46 | 55 | info.push(task.requestor.present? ? task.requestor.name : '') |
| 47 | - info.push(task.abstract.present? ? task.abstract.gsub(/\s+/, ' ').strip : '') | |
| 56 | + info.push(task.categories.map {|c| c.name}.join(', ')) | |
| 48 | 57 | file.write(info.map{|i| "'" + i.to_s + "'"}.join(";")) |
| 49 | 58 | file.write("\n") |
| 50 | 59 | end |
| 51 | 60 | |
| 52 | 61 | file.close |
| 53 | - | |
| 54 | - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => profile, :parent => report_folder) | |
| 55 | - uploaded_file.save | |
| 62 | + upload_file(filepath, profile, report_folder) | |
| 56 | 63 | end |
| 57 | 64 | |
| 58 | 65 | end | ... | ... |