Commit 190120b4af270bd5d767faf5253c488e216f11b8

Authored by Carlos Purificação
1 parent c11ebee2
Exists in master

Added zip functionality to reports

controllers/myprofile/dialoga_plugin_myprofile_controller.rb
... ... @@ -4,7 +4,7 @@ class DialogaPluginMyprofileController < MyProfileController
4 4  
5 5 def send_report
6 6 path = File.join(Rails.root,'plugins','dialoga','script')
7   - scripts = ['sent_event_report', 'sent_proposal_report']
  7 + scripts = ['exec_scripts']
8 8 scripts.map do |script|
9 9 cmd = File.join(path,script) + ' ' + current_person.email.to_s
10 10 fork {IO.popen(cmd).read}
... ...
script/exec_scripts 0 → 100755
... ... @@ -0,0 +1,40 @@
  1 +#!/usr/bin/env ruby
  2 +# encoding: UTF-8
  3 +require_relative '../../../config/environment'
  4 +#require 'zip'
  5 +require_relative 'zipfilegenerator'
  6 +
  7 +# The emails to send the report.
  8 +emails = ['leandronunes@gmail.com', 'leandro.santos@serpro.gov.br'] + ARGV
  9 +emails.uniq!
  10 +
  11 +# All reports will go to this path
  12 +reports_dir_path = File.join(Rails.root, "reports_dir")
  13 +if (Dir.exist?(reports_dir_path))
  14 + FileUtils.rm_rf(reports_dir_path)
  15 +end
  16 +Dir.mkdir(reports_dir_path, 0700) #=> 0
  17 +
  18 +path = File.join(Rails.root,'plugins','dialoga','script')
  19 +scripts = ['export_ranking_report', 'sent_event_report', 'sent_proposal_report']
  20 +scripts.map do |script|
  21 + cmd = File.join(path,script) + ' ' + reports_dir_path
  22 + IO.popen(cmd).read
  23 +end
  24 +
  25 +# Compress all files in reports_dir_path to a file named reports_dir.zip
  26 +# in this same directory
  27 +outputFile = File.join(reports_dir_path, "reports_dir.zip")
  28 +zf = ZipFileGenerator.new(reports_dir_path, outputFile)
  29 +zf.write()
  30 +
  31 +ActionMailer::Base.logger = Logger.new(STDOUT)
  32 +class Sender < ActionMailer::Base
  33 + def send_report(to, from, path)
  34 + attachments["reports_dir.zip"] = File.read(path)
  35 + mail to: to, from: from,
  36 + subject: "Relatorio do Dialoga", body: "Segue em anexo os relatorios do Dialoga"
  37 + end
  38 +end
  39 +
  40 +Sender.send_report(emails, 'dialoga@dialoga.gov.br', outputFile).deliver
... ...
script/export_ranking_report 0 → 100755
... ... @@ -0,0 +1,29 @@
  1 +#!/usr/bin/env ruby
  2 +# encoding: UTF-8
  3 +require_relative '../../../config/environment'
  4 +
  5 +#discussion = Article.find(402)
  6 +discussion = ProposalsDiscussionPlugin::Discussion.first
  7 +#discussion = Article.find(103358)
  8 +
  9 +articles = discussion.topics
  10 +articles.each do |article|
  11 + ranking = article.ranking
  12 + #file = File.open(File.join(Rails.root, "reports_dir", "ranking_#{article.slug}.csv"), 'w+')
  13 + file = File.open(File.join(ARGV, "ranking_#{article.slug}.csv"), 'w+')
  14 + #CSV.open( "ranking_#{article.slug}.csv", 'w' ) do |csv|
  15 + #print "\nCSV is: #{csv.class}"
  16 + #csv << ['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor']
  17 + file.write(['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor'].join(', '))
  18 + file.write("\n")
  19 + #file.write(info.map{|i| "'" + i.to_s + "'"}.join(";"))
  20 + #ranking.each_with_index {|r, i| csv << [i+1, r.values].flatten}
  21 + ranking.each_with_index { |r, i|
  22 + file.write([i+1, r.values].flatten.join(', '))
  23 + file.write("\n")
  24 + }
  25 + file.write("\n")
  26 + file.close
  27 + #end
  28 +end
  29 +
... ...
script/sent_event_report
... ... @@ -2,12 +2,9 @@
2 2 # encoding: UTF-8
3 3 require_relative '../../../config/environment'
4 4  
5   -puts 'Iniciando script eventos'
  5 +puts 'Iniciando script'
6 6  
7   -directory = File.join(Rails.root,'data')
8   -Dir.mkdir(directory) unless File.exists?(directory)
9   -
10   -file = File.open(File.join(directory,'event.csv'), 'w+')
  7 +file = File.open(File.join(ARGV, 'event.csv'), 'w+')
11 8  
12 9 events = Event.all
13 10 events.map do |event|
... ...
script/sent_proposal_report
... ... @@ -2,15 +2,10 @@
2 2 # encoding: UTF-8
3 3 require_relative '../../../config/environment'
4 4  
5   -directory = File.join(Rails.root,'data')
6   -Dir.mkdir(directory) unless File.exists?(directory)
7   -
8   -emails = ['leandronunes@gmail.com', 'leandro.santos@serpro.gov.br'] + ARGV
9   -emails.uniq!
10   -
11 5 puts 'Iniciando script propostas'
12 6  
13   -file = File.open(File.join(directory,'data.csv'), 'w+')
  7 +file = File.open(File.join(ARGV, 'data.csv'), 'w+')
  8 +
14 9  
15 10 tasks = ProposalsDiscussionPlugin::ProposalTask.all
16 11 count = 0
... ... @@ -42,16 +37,3 @@ end
42 37  
43 38 file.close
44 39  
45   -ActionMailer::Base.logger = Logger.new(STDOUT)
46   -class Sender < ActionMailer::Base
47   - def send_report(to, from)
48   - directory = File.join(Rails.root,'data')
49   - attachments["eventos.csv"] = File.read("#{directory}/event.csv")
50   - attachments["propostas.csv"] = File.read("#{directory}/data.csv")
51   - mail to: to, from: from,
52   - subject: "Relatorio do Dialoga", body: "Segue em anexo os relatorios do Dialoga"
53   - end
54   -
55   -end
56   -
57   -Sender.send_report(emails, 'dialoga@dialoga.gov.br').deliver
... ...
script/zipfilegenerator.rb 0 → 100644
... ... @@ -0,0 +1,38 @@
  1 +require 'zip'
  2 +
  3 +class ZipFileGenerator
  4 +
  5 + # Initialize with the directory to zip and the location of the output archive.
  6 + def initialize(inputDir, outputFile)
  7 + @inputDir = inputDir
  8 + @outputFile = outputFile
  9 + end
  10 +
  11 + # Zip the input directory.
  12 + def write()
  13 + entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..")
  14 + io = Zip::File.open(@outputFile, Zip::File::CREATE);
  15 +
  16 + writeEntries(entries, "", io)
  17 + io.close();
  18 + end
  19 +
  20 + # A helper method to make the recursion work.
  21 + private
  22 + def writeEntries(entries, path, io)
  23 +
  24 + entries.each { |e|
  25 + zipFilePath = path == "" ? e : File.join(path, e)
  26 + diskFilePath = File.join(@inputDir, zipFilePath)
  27 + puts "Deflating " + diskFilePath
  28 + if File.directory?(diskFilePath)
  29 + io.mkdir(zipFilePath)
  30 + subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..")
  31 + writeEntries(subdir, zipFilePath, io)
  32 + else
  33 + io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())}
  34 + end
  35 + }
  36 + end
  37 +
  38 +end
0 39 \ No newline at end of file
... ...