From 190120b4af270bd5d767faf5253c488e216f11b8 Mon Sep 17 00:00:00 2001 From: Carlos Purificacao Date: Thu, 13 Aug 2015 10:01:00 -0300 Subject: [PATCH] Added zip functionality to reports --- controllers/myprofile/dialoga_plugin_myprofile_controller.rb | 2 +- script/exec_scripts | 40 ++++++++++++++++++++++++++++++++++++++++ script/export_ranking_report | 29 +++++++++++++++++++++++++++++ script/sent_event_report | 7 ++----- script/sent_proposal_report | 22 ++-------------------- script/zipfilegenerator.rb | 38 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 112 insertions(+), 26 deletions(-) create mode 100755 script/exec_scripts create mode 100755 script/export_ranking_report create mode 100644 script/zipfilegenerator.rb diff --git a/controllers/myprofile/dialoga_plugin_myprofile_controller.rb b/controllers/myprofile/dialoga_plugin_myprofile_controller.rb index 43d2230..0ea8139 100644 --- a/controllers/myprofile/dialoga_plugin_myprofile_controller.rb +++ b/controllers/myprofile/dialoga_plugin_myprofile_controller.rb @@ -4,7 +4,7 @@ class DialogaPluginMyprofileController < MyProfileController def send_report path = File.join(Rails.root,'plugins','dialoga','script') - scripts = ['sent_event_report', 'sent_proposal_report'] + scripts = ['exec_scripts'] scripts.map do |script| cmd = File.join(path,script) + ' ' + current_person.email.to_s fork {IO.popen(cmd).read} diff --git a/script/exec_scripts b/script/exec_scripts new file mode 100755 index 0000000..f92dfcc --- /dev/null +++ b/script/exec_scripts @@ -0,0 +1,40 @@ +#!/usr/bin/env ruby +# encoding: UTF-8 +require_relative '../../../config/environment' +#require 'zip' +require_relative 'zipfilegenerator' + +# The emails to send the report. +emails = ['leandronunes@gmail.com', 'leandro.santos@serpro.gov.br'] + ARGV +emails.uniq! + +# All reports will go to this path +reports_dir_path = File.join(Rails.root, "reports_dir") +if (Dir.exist?(reports_dir_path)) + FileUtils.rm_rf(reports_dir_path) +end +Dir.mkdir(reports_dir_path, 0700) #=> 0 + +path = File.join(Rails.root,'plugins','dialoga','script') +scripts = ['export_ranking_report', 'sent_event_report', 'sent_proposal_report'] +scripts.map do |script| + cmd = File.join(path,script) + ' ' + reports_dir_path + IO.popen(cmd).read +end + +# Compress all files in reports_dir_path to a file named reports_dir.zip +# in this same directory +outputFile = File.join(reports_dir_path, "reports_dir.zip") +zf = ZipFileGenerator.new(reports_dir_path, outputFile) +zf.write() + +ActionMailer::Base.logger = Logger.new(STDOUT) +class Sender < ActionMailer::Base + def send_report(to, from, path) + attachments["reports_dir.zip"] = File.read(path) + mail to: to, from: from, + subject: "Relatorio do Dialoga", body: "Segue em anexo os relatorios do Dialoga" + end +end + +Sender.send_report(emails, 'dialoga@dialoga.gov.br', outputFile).deliver diff --git a/script/export_ranking_report b/script/export_ranking_report new file mode 100755 index 0000000..be94bc7 --- /dev/null +++ b/script/export_ranking_report @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# encoding: UTF-8 +require_relative '../../../config/environment' + +#discussion = Article.find(402) +discussion = ProposalsDiscussionPlugin::Discussion.first +#discussion = Article.find(103358) + +articles = discussion.topics +articles.each do |article| + ranking = article.ranking + #file = File.open(File.join(Rails.root, "reports_dir", "ranking_#{article.slug}.csv"), 'w+') + file = File.open(File.join(ARGV, "ranking_#{article.slug}.csv"), 'w+') + #CSV.open( "ranking_#{article.slug}.csv", 'w' ) do |csv| + #print "\nCSV is: #{csv.class}" + #csv << ['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor'] + file.write(['Posição', 'Id', 'Proposta', 'Positivo', 'Negativo', 'Exibições', 'Valor'].join(', ')) + file.write("\n") + #file.write(info.map{|i| "'" + i.to_s + "'"}.join(";")) + #ranking.each_with_index {|r, i| csv << [i+1, r.values].flatten} + ranking.each_with_index { |r, i| + file.write([i+1, r.values].flatten.join(', ')) + file.write("\n") + } + file.write("\n") + file.close + #end +end + diff --git a/script/sent_event_report b/script/sent_event_report index 60756b8..f7640f2 100755 --- a/script/sent_event_report +++ b/script/sent_event_report @@ -2,12 +2,9 @@ # encoding: UTF-8 require_relative '../../../config/environment' -puts 'Iniciando script eventos' +puts 'Iniciando script' -directory = File.join(Rails.root,'data') -Dir.mkdir(directory) unless File.exists?(directory) - -file = File.open(File.join(directory,'event.csv'), 'w+') +file = File.open(File.join(ARGV, 'event.csv'), 'w+') events = Event.all events.map do |event| diff --git a/script/sent_proposal_report b/script/sent_proposal_report index 63bafed..e2ec229 100755 --- a/script/sent_proposal_report +++ b/script/sent_proposal_report @@ -2,15 +2,10 @@ # encoding: UTF-8 require_relative '../../../config/environment' -directory = File.join(Rails.root,'data') -Dir.mkdir(directory) unless File.exists?(directory) - -emails = ['leandronunes@gmail.com', 'leandro.santos@serpro.gov.br'] + ARGV -emails.uniq! - puts 'Iniciando script propostas' -file = File.open(File.join(directory,'data.csv'), 'w+') +file = File.open(File.join(ARGV, 'data.csv'), 'w+') + tasks = ProposalsDiscussionPlugin::ProposalTask.all count = 0 @@ -42,16 +37,3 @@ end file.close -ActionMailer::Base.logger = Logger.new(STDOUT) -class Sender < ActionMailer::Base - def send_report(to, from) - directory = File.join(Rails.root,'data') - attachments["eventos.csv"] = File.read("#{directory}/event.csv") - attachments["propostas.csv"] = File.read("#{directory}/data.csv") - mail to: to, from: from, - subject: "Relatorio do Dialoga", body: "Segue em anexo os relatorios do Dialoga" - end - -end - -Sender.send_report(emails, 'dialoga@dialoga.gov.br').deliver diff --git a/script/zipfilegenerator.rb b/script/zipfilegenerator.rb new file mode 100644 index 0000000..8ebcbb0 --- /dev/null +++ b/script/zipfilegenerator.rb @@ -0,0 +1,38 @@ +require 'zip' + +class ZipFileGenerator + + # Initialize with the directory to zip and the location of the output archive. + def initialize(inputDir, outputFile) + @inputDir = inputDir + @outputFile = outputFile + end + + # Zip the input directory. + def write() + entries = Dir.entries(@inputDir); entries.delete("."); entries.delete("..") + io = Zip::File.open(@outputFile, Zip::File::CREATE); + + writeEntries(entries, "", io) + io.close(); + end + + # A helper method to make the recursion work. + private + def writeEntries(entries, path, io) + + entries.each { |e| + zipFilePath = path == "" ? e : File.join(path, e) + diskFilePath = File.join(@inputDir, zipFilePath) + puts "Deflating " + diskFilePath + if File.directory?(diskFilePath) + io.mkdir(zipFilePath) + subdir =Dir.entries(diskFilePath); subdir.delete("."); subdir.delete("..") + writeEntries(subdir, zipFilePath, io) + else + io.get_output_stream(zipFilePath) { |f| f.puts(File.open(diskFilePath, "rb").read())} + end + } + end + +end \ No newline at end of file -- libgit2 0.21.2