diff --git a/controllers/myprofile/dialoga_plugin_myprofile_controller.rb b/controllers/myprofile/dialoga_plugin_myprofile_controller.rb index 6292f2d..6768e73 100644 --- a/controllers/myprofile/dialoga_plugin_myprofile_controller.rb +++ b/controllers/myprofile/dialoga_plugin_myprofile_controller.rb @@ -1,19 +1,18 @@ +# encoding: UTF-8 class DialogaPluginMyprofileController < MyProfileController before_filter :is_admin def send_report - path = File.join(Rails.root,'plugins','dialoga','script') - scripts = ['sent_event_report', 'sent_ranking', 'sent_proposal_report'] - scripts.map do |script| - cmd = File.join(path,script) + ' ' + current_person.email.to_s - fork {IO.popen(cmd).read} - end - session[:notice] = _("The report wil be sent to email %s") % current_person.email + 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") redirect_to :back end protected + def is_admin render_access_denied unless current_person.is_admin? end diff --git a/lib/dialoga_plugin/event_job.rb b/lib/dialoga_plugin/event_job.rb new file mode 100644 index 0000000..4bb6f6b --- /dev/null +++ b/lib/dialoga_plugin/event_job.rb @@ -0,0 +1,34 @@ +# encoding: UTF-8 +class DialogaPlugin::EventJob < DialogaPlugin::ReportJob + + def perform + profile = Profile.find(profile_id) + report_folder = create_report_path(profile) + create_event_report(profile, report_folder) + end + + 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 + file = File.open(File.join(filepath), 'w+') + file.write(event.name+ "\n") + header = "'Nome';'Email'\n" + file.write(header) + count = 0 + event.person_followers.map do |person| + count += 1 + puts "%s de %s: adicionando evento: %s" % [count, event.person_followers.count, event.id ] + info = [] + info.push(person.name) + info.push(person.email) + 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 + end + end + +end diff --git a/lib/dialoga_plugin/ranking_job.rb b/lib/dialoga_plugin/ranking_job.rb new file mode 100644 index 0000000..7c5ba26 --- /dev/null +++ b/lib/dialoga_plugin/ranking_job.rb @@ -0,0 +1,30 @@ +# encoding: UTF-8 +require 'csv' +class DialogaPlugin::RankingJob < DialogaPlugin::ReportJob + + def perform + profile = Profile.find(profile_id) + report_folder = create_report_path(profile) + create_ranking_report(profile, report_folder) + end + + def create_ranking_report(profile, report_folder) + ProposalsDiscussionPlugin::Discussion.where(:profile_id => profile.id).map do |discussion| + articles = discussion.topics + articles.each do |article| + puts "#{article.slug}" + ranking = article.ranking + next if ranking.empty? + + filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + "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 + end + +end diff --git a/lib/dialoga_plugin/report_job.rb b/lib/dialoga_plugin/report_job.rb new file mode 100644 index 0000000..107259e --- /dev/null +++ b/lib/dialoga_plugin/report_job.rb @@ -0,0 +1,58 @@ +class DialogaPlugin::ReportJob < Struct.new(:profile_id) + + 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') + + report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d')) + + report_folder ||= Folder.create!(:profile => profile, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder) + end + + def perform + profile = Profile.find(profile_id) + report_folder = create_report_path(profile) + create_proposals_report(profile, report_folder) + end + + def create_proposals_report(profile, report_folder) + filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + '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" + file.write(header) + status_translation = { + 1 => 'Pendente de Moderacao', + 2 => 'Rejeitada', + 3 => 'Aprovada', + 5 => 'Pre Aprovada', + 6 => 'Pre Rejeitada', + } + tasks.map do |task| + count += 1 + puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.id ] + info = [] + info.push(task.proposal_source) + info.push(status_translation[task.status]) + info.push(task.created_at.strftime("%d/%m/%y %H:%M")) + info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.evaluated_by.name : '') + info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.created_at.strftime("%d/%m/%y %H:%M") : '') + 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 : '') + 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 + end + +end diff --git a/script/sent_event_report b/script/sent_event_report deleted file mode 100755 index b6d2124..0000000 --- a/script/sent_event_report +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env ruby -# encoding: UTF-8 -include ActionDispatch::TestProcess - -puts 'Iniciando script' - -filebasepath = '/tmp/' - -dialoga = Community['dialoga'] -root_report_folder = dialoga.folders.where(:slug => 'relatorios').first -root_report_folder ||= Folder.create!(:profile => dialoga, :name => 'Relatorios') - -report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d')) -report_folder ||= Folder.create!(:profile => dialoga, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder) - -events = Event.all -events.map do |event| - filepath = filebasepath + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + event.slug - file = File.open(File.join(filepath), 'w+') - file.write(event.name+ "\n") - header = "'Nome';'Email'\n" - file.write(header) - count = 0 - event.person_followers.map do |person| - count += 1 - puts "%s de %s: adicionando evento: %s" % [count, event.person_followers.count, event.id ] - info = [] - info.push(person.name) - info.push(person.email) - 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 => dialoga, :parent => report_folder) - uploaded_file.save -end - diff --git a/script/sent_proposal_report b/script/sent_proposal_report deleted file mode 100755 index 4ece0a2..0000000 --- a/script/sent_proposal_report +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env ruby -# encoding: UTF-8 -include ActionDispatch::TestProcess - -puts 'Iniciando script propostas' - -filebasepath = '/tmp/' - -dialoga = Community['dialoga'] -root_report_folder = dialoga.folders.where(:slug => 'relatorios').first -root_report_folder ||= Folder.create!(:profile => dialoga, :name => 'Relatorios') - -report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d')) - -report_folder ||= Folder.create!(:profile => dialoga, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder) - - - -filepath = filebasepath + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + '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" -file.write(header) -STATUS_TRANSLATION = { - 1 => 'Pendente de Moderacao', - 2 => 'Rejeitada', - 3 => 'Aprovada', - 5 => 'Pre Aprovada', - 6 => 'Pre Rejeitada', -} -tasks.map do |task| - count += 1 - puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.id ] - info = [] - info.push(task.proposal_source) - info.push(STATUS_TRANSLATION[task.status]) - info.push(task.created_at.strftime("%d/%m/%y %H:%M")) - info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.evaluated_by.name : '') - info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.created_at.strftime("%d/%m/%y %H:%M") : '') - 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 : '') - 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 => dialoga, :parent => report_folder) -uploaded_file.save diff --git a/script/sent_ranking b/script/sent_ranking deleted file mode 100755 index 31a04c5..0000000 --- a/script/sent_ranking +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env ruby -# encoding: UTF-8 -include ActionDispatch::TestProcess -require 'csv' - -puts 'Iniciando script ranking' - -filebasepath = '/tmp/' - -dialoga = Community['dialoga'] -root_report_folder = dialoga.folders.where(:slug => 'relatorios').first -root_report_folder ||= Folder.create!(:profile => dialoga, :name => 'Relatorios') - -report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d')) - -report_folder ||= Folder.create!(:profile => dialoga, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder) - -discussion = ProposalsDiscussionPlugin::Discussion.first - -articles = discussion.topics -articles.each do |article| - puts "#{article.slug}" - ranking = article.ranking - filepath = filebasepath + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + "ranking_#{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 => dialoga, :parent => report_folder) - uploaded_file.save -end -- libgit2 0.21.2