From 85bd17696f1ece5318127653f41c0c756f8571b0 Mon Sep 17 00:00:00 2001 From: Shuhei Tanuma Date: Tue, 1 Oct 2013 14:11:32 +0900 Subject: [PATCH] rough concept: detect pipe when calling job:show command This concept improve `td job:result` comand when playing query with small set of result. In most cases, Hive's `ORDER BY` takes 30 ~ 40 seconds as itself's initialization costs. so I'd like to sort the result on local machine. for examples: ```` td job:show 123456 | sort -k 1 -k 2 -n ```` Unfortunately, my ruby skill is really poor. so could you consider this concept at first? Thanks, Shuhei --- lib/td/command/job.rb | 47 ++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/lib/td/command/job.rb b/lib/td/command/job.rb index aa718784..5a955be9 100644 --- a/lib/td/command/job.rb +++ b/lib/td/command/job.rb @@ -26,6 +26,8 @@ module Command /\A[\+]?2\z/ => 2, } + is_tty = true + def job_list(op) page = 0 skip = 0 @@ -88,6 +90,7 @@ def job_show(op) format = 'tsv' render_opts = {} exclude = false + @is_tty = STDOUT.tty? op.on('-v', '--verbose', 'show logs', TrueClass) {|b| verbose = b @@ -117,40 +120,40 @@ def job_show(op) job = client.job(job_id) - puts "Organization : #{job.org_name}" - puts "JobID : #{job.job_id}" + puts_when_tty "Organization : #{job.org_name}" + puts_when_tty "JobID : #{job.job_id}" #puts "URL : #{job.url}" - puts "Status : #{job.status}" - puts "Type : #{job.type}" - puts "Priority : #{job_priority_name_of(job.priority)}" - puts "Retry limit : #{job.retry_limit}" - puts "Result : #{job.result_url}" - puts "Database : #{job.db_name}" - puts "Query : #{job.query}" + puts_when_tty "Status : #{job.status}" + puts_when_tty "Type : #{job.type}" + puts_when_tty "Priority : #{job_priority_name_of(job.priority)}" + puts_when_tty "Retry limit : #{job.retry_limit}" + puts_when_tty "Result : #{job.result_url}" + puts_when_tty "Database : #{job.db_name}" + puts_when_tty "Query : #{job.query}" if wait && !job.finished? wait_job(job) if job.success? && [:hive, :pig, :impala].include?(job.type) && !exclude - puts "Result :" + puts_when_tty "Result :" show_result(job, output, format, render_opts) end else if job.success? && [:hive, :pig, :impala].include?(job.type) && !exclude - puts "Result :" + puts_when_tty "Result :" show_result(job, output, format, render_opts) end if verbose - puts "" - puts "cmdout:" + puts_when_tty "" + puts_when_tty "cmdout:" job.debug['cmdout'].to_s.split("\n").each {|line| - puts " "+line + puts_when_tty " "+line } - puts "" - puts "stderr:" + puts_when_tty "" + puts_when_tty "stderr:" job.debug['stderr'].to_s.split("\n").each {|line| - puts " "+line + puts_when_tty " "+line } end end @@ -214,7 +217,9 @@ def wait_job(job) end def show_result(job, output, format, render_opts={}) - if output + if !@is_tty + write_result(job, "/dev/stdout", format) + elsif output write_result(job, output, format) puts "written to #{output} in #{format} format" else @@ -322,6 +327,12 @@ def job_priority_id_of(name) } return nil end + + def puts_when_tty(message) + if @is_tty + puts message + end + end end end