Commit 83e49757139c22fb9db799149ef25e422808914a

Authored by Victor Costa
1 parent a10d907d
Exists in master

Add jobs for report creation

controllers/myprofile/dialoga_plugin_myprofile_controller.rb
  1 +# encoding: UTF-8
1 2 class DialogaPluginMyprofileController < MyProfileController
2 3  
3 4 before_filter :is_admin
4 5  
5 6 def send_report
6   - path = File.join(Rails.root,'plugins','dialoga','script')
7   - scripts = ['sent_event_report', 'sent_ranking', 'sent_proposal_report']
8   - scripts.map do |script|
9   - cmd = File.join(path,script) + ' ' + current_person.email.to_s
10   - fork {IO.popen(cmd).read}
11   - end
12   - session[:notice] = _("The report wil be sent to email %s") % current_person.email
  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")
13 11 redirect_to :back
14 12 end
15 13  
16 14 protected
  15 +
17 16 def is_admin
18 17 render_access_denied unless current_person.is_admin?
19 18 end
... ...
lib/dialoga_plugin/event_job.rb 0 → 100644
... ... @@ -0,0 +1,34 @@
  1 +# encoding: UTF-8
  2 +class DialogaPlugin::EventJob < DialogaPlugin::ReportJob
  3 +
  4 + def perform
  5 + profile = Profile.find(profile_id)
  6 + report_folder = create_report_path(profile)
  7 + create_event_report(profile, report_folder)
  8 + end
  9 +
  10 + def create_event_report(profile, report_folder)
  11 + events = Event.where(:profile_id => profile.id)
  12 + events.map do |event|
  13 + filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + event.slug
  14 + file = File.open(File.join(filepath), 'w+')
  15 + file.write(event.name+ "\n")
  16 + header = "'Nome';'Email'\n"
  17 + file.write(header)
  18 + count = 0
  19 + event.person_followers.map do |person|
  20 + count += 1
  21 + puts "%s de %s: adicionando evento: %s" % [count, event.person_followers.count, event.id ]
  22 + info = []
  23 + info.push(person.name)
  24 + info.push(person.email)
  25 + file.write(info.map{|i| "'" + i.to_s + "'"}.join(";"))
  26 + file.write("\n")
  27 + end
  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 + end
  32 + end
  33 +
  34 +end
... ...
lib/dialoga_plugin/ranking_job.rb 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +# encoding: UTF-8
  2 +require 'csv'
  3 +class DialogaPlugin::RankingJob < DialogaPlugin::ReportJob
  4 +
  5 + def perform
  6 + profile = Profile.find(profile_id)
  7 + report_folder = create_report_path(profile)
  8 + create_ranking_report(profile, report_folder)
  9 + end
  10 +
  11 + def create_ranking_report(profile, report_folder)
  12 + ProposalsDiscussionPlugin::Discussion.where(:profile_id => profile.id).map do |discussion|
  13 + articles = discussion.topics
  14 + articles.each do |article|
  15 + puts "#{article.slug}"
  16 + ranking = article.ranking
  17 + next if ranking.empty?
  18 +
  19 + filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + "ranking_#{discussion.slug}_#{article.slug}.csv"
  20 + CSV.open(filepath, 'w' ) do |csv|
  21 + csv << ['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor']
  22 + ranking.each_with_index {|r, i| csv << [i+1, r.values].flatten}
  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 + end
  27 + end
  28 + end
  29 +
  30 +end
... ...
lib/dialoga_plugin/report_job.rb 0 → 100644
... ... @@ -0,0 +1,58 @@
  1 +class DialogaPlugin::ReportJob < Struct.new(:profile_id)
  2 +
  3 + include ActionDispatch::TestProcess
  4 +
  5 + def create_report_path(profile)
  6 + root_report_folder = profile.folders.where(:slug => 'relatorios').first
  7 + root_report_folder ||= Folder.create!(:profile => profile, :name => 'Relatorios')
  8 +
  9 + report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d'))
  10 +
  11 + report_folder ||= Folder.create!(:profile => profile, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder)
  12 + end
  13 +
  14 + def perform
  15 + profile = Profile.find(profile_id)
  16 + report_folder = create_report_path(profile)
  17 + create_proposals_report(profile, report_folder)
  18 + end
  19 +
  20 + def create_proposals_report(profile, report_folder)
  21 + filepath = '/tmp/' + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + 'propostas.csv'
  22 + file = File.open(filepath, 'w+')
  23 +
  24 + tasks = ProposalsDiscussionPlugin::ProposalTask.all
  25 + count = 0
  26 + header = "'Origem';'Status';'Criada em';'Moderado por';'Data de Moderado';'Validado por';'Data de Validado';'Autor';'Proposta'\n"
  27 + file.write(header)
  28 + status_translation = {
  29 + 1 => 'Pendente de Moderacao',
  30 + 2 => 'Rejeitada',
  31 + 3 => 'Aprovada',
  32 + 5 => 'Pre Aprovada',
  33 + 6 => 'Pre Rejeitada',
  34 + }
  35 + tasks.map do |task|
  36 + count += 1
  37 + puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.id ]
  38 + info = []
  39 + info.push(task.proposal_source)
  40 + info.push(status_translation[task.status])
  41 + info.push(task.created_at.strftime("%d/%m/%y %H:%M"))
  42 + info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.evaluated_by.name : '')
  43 + info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.created_at.strftime("%d/%m/%y %H:%M") : '')
  44 + info.push(task.closed_by.present? ? task.closed_by.name : '')
  45 + info.push(task.closed_by.present? ? task.end_date.strftime("%d/%m/%y %H:%M") : '')
  46 + info.push(task.requestor.present? ? task.requestor.name : '')
  47 + info.push(task.abstract.present? ? task.abstract.gsub(/\s+/, ' ').strip : '')
  48 + file.write(info.map{|i| "'" + i.to_s + "'"}.join(";"))
  49 + file.write("\n")
  50 + end
  51 +
  52 + 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
  56 + end
  57 +
  58 +end
... ...
script/sent_event_report
... ... @@ -1,37 +0,0 @@
1   -#!/usr/bin/env ruby
2   -# encoding: UTF-8
3   -include ActionDispatch::TestProcess
4   -
5   -puts 'Iniciando script'
6   -
7   -filebasepath = '/tmp/'
8   -
9   -dialoga = Community['dialoga']
10   -root_report_folder = dialoga.folders.where(:slug => 'relatorios').first
11   -root_report_folder ||= Folder.create!(:profile => dialoga, :name => 'Relatorios')
12   -
13   -report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d'))
14   -report_folder ||= Folder.create!(:profile => dialoga, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder)
15   -
16   -events = Event.all
17   -events.map do |event|
18   - filepath = filebasepath + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + event.slug
19   - file = File.open(File.join(filepath), 'w+')
20   - file.write(event.name+ "\n")
21   - header = "'Nome';'Email'\n"
22   - file.write(header)
23   - count = 0
24   - event.person_followers.map do |person|
25   - count += 1
26   - puts "%s de %s: adicionando evento: %s" % [count, event.person_followers.count, event.id ]
27   - info = []
28   - info.push(person.name)
29   - info.push(person.email)
30   - file.write(info.map{|i| "'" + i.to_s + "'"}.join(";"))
31   - file.write("\n")
32   - end
33   - file.close
34   - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => dialoga, :parent => report_folder)
35   - uploaded_file.save
36   -end
37   -
script/sent_proposal_report
... ... @@ -1,53 +0,0 @@
1   -#!/usr/bin/env ruby
2   -# encoding: UTF-8
3   -include ActionDispatch::TestProcess
4   -
5   -puts 'Iniciando script propostas'
6   -
7   -filebasepath = '/tmp/'
8   -
9   -dialoga = Community['dialoga']
10   -root_report_folder = dialoga.folders.where(:slug => 'relatorios').first
11   -root_report_folder ||= Folder.create!(:profile => dialoga, :name => 'Relatorios')
12   -
13   -report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d'))
14   -
15   -report_folder ||= Folder.create!(:profile => dialoga, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder)
16   -
17   -
18   -
19   -filepath = filebasepath + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + 'propostas.csv'
20   -file = File.open(filepath, 'w+')
21   -
22   -tasks = ProposalsDiscussionPlugin::ProposalTask.all
23   -count = 0
24   -header = "'Origem';'Status';'Criada em';'Moderado por';'Data de Moderado';'Validado por';'Data de Validado';'Autor';'Proposta'\n"
25   -file.write(header)
26   -STATUS_TRANSLATION = {
27   - 1 => 'Pendente de Moderacao',
28   - 2 => 'Rejeitada',
29   - 3 => 'Aprovada',
30   - 5 => 'Pre Aprovada',
31   - 6 => 'Pre Rejeitada',
32   -}
33   -tasks.map do |task|
34   - count += 1
35   - puts "%s de %s: adicionando task: %s" % [count, tasks.count, task.id ]
36   - info = []
37   - info.push(task.proposal_source)
38   - info.push(STATUS_TRANSLATION[task.status])
39   - info.push(task.created_at.strftime("%d/%m/%y %H:%M"))
40   - info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.evaluated_by.name : '')
41   - info.push(task.proposal_evaluation.present? ? task.proposal_evaluation.created_at.strftime("%d/%m/%y %H:%M") : '')
42   - info.push(task.closed_by.present? ? task.closed_by.name : '')
43   - info.push(task.closed_by.present? ? task.end_date.strftime("%d/%m/%y %H:%M") : '')
44   - info.push(task.requestor.present? ? task.requestor.name : '')
45   - info.push(task.abstract.present? ? task.abstract.gsub(/\s+/, ' ').strip : '')
46   - file.write(info.map{|i| "'" + i.to_s + "'"}.join(";"))
47   - file.write("\n")
48   -end
49   -
50   -file.close
51   -
52   -uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => dialoga, :parent => report_folder)
53   -uploaded_file.save
script/sent_ranking
... ... @@ -1,31 +0,0 @@
1   -#!/usr/bin/env ruby
2   -# encoding: UTF-8
3   -include ActionDispatch::TestProcess
4   -require 'csv'
5   -
6   -puts 'Iniciando script ranking'
7   -
8   -filebasepath = '/tmp/'
9   -
10   -dialoga = Community['dialoga']
11   -root_report_folder = dialoga.folders.where(:slug => 'relatorios').first
12   -root_report_folder ||= Folder.create!(:profile => dialoga, :name => 'Relatorios')
13   -
14   -report_folder = Folder.find_by_slug(DateTime.now.strftime('%Y-%m-%d'))
15   -
16   -report_folder ||= Folder.create!(:profile => dialoga, :name => DateTime.now.strftime('%Y-%m-%d'), :parent => root_report_folder)
17   -
18   -discussion = ProposalsDiscussionPlugin::Discussion.first
19   -
20   -articles = discussion.topics
21   -articles.each do |article|
22   - puts "#{article.slug}"
23   - ranking = article.ranking
24   - filepath = filebasepath + DateTime.now.strftime('%Y-%m-%d-%H-%m-%S') + '-' + "ranking_#{article.slug}.csv"
25   - CSV.open(filepath, 'w' ) do |csv|
26   - csv << ['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor']
27   - ranking.each_with_index {|r, i| csv << [i+1, r.values].flatten}
28   - end
29   - uploaded_file = UploadedFile.new(:uploaded_data => fixture_file_upload(filepath, 'text/csv'), :profile => dialoga, :parent => report_folder)
30   - uploaded_file.save
31   -end