From e49687be51dc7c934e2a1795050c0b976e739019 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 18 Feb 2014 17:50:08 +0100 Subject: [PATCH 1/2] feature #1484: add csv output to table formater --- src/cli/cli_helper.rb | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/cli/cli_helper.rb b/src/cli/cli_helper.rb index 30e7116320..5cdb3f5bbf 100644 --- a/src/cli/cli_helper.rb +++ b/src/cli/cli_helper.rb @@ -14,6 +14,8 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'csv' + module CLIHelper LIST = { :name => "list", @@ -23,6 +25,12 @@ module CLIHelper :description => "Selects columns to display with list command" } + CSV = { + :name => "csv", + :large => "--csv", + :description => "Write table in csv format" + } + #ORDER = { # :name => "order", # :short => "-o x,y,z", @@ -56,7 +64,7 @@ module CLIHelper } #OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY] - OPTIONS = [LIST, DELAY, FILTER] + OPTIONS = [LIST, DELAY, FILTER, CSV] # Sets bold font def CLIHelper.scr_bold @@ -154,6 +162,8 @@ module CLIHelper class ShowTable require 'yaml' + attr_reader :default_columns + def initialize(conf=nil, ext=nil, &block) @columns = Hash.new @default_columns = Array.new @@ -241,7 +251,7 @@ module CLIHelper private def print_table(data, options) - CLIHelper.print_header(header_str) + CLIHelper.print_header(header_str) if !options[:csv] data ? print_data(data, options) : puts end @@ -257,17 +267,24 @@ module CLIHelper end begin - res_data.each{|l| - puts (0..ncolumns-1).collect{ |i| - dat=l[i] - col=@default_columns[i] + if options[:csv] + CSV($stdout, :write_headers => true, + :headers => @default_columns) do |csv| + res_data.each {|l| csv << l } + end + else + res_data.each{|l| + puts (0..ncolumns-1).collect{ |i| + dat=l[i] + col=@default_columns[i] - str=format_str(col, dat) - str=CLIHelper.color_state(str) if i==stat_column + str=format_str(col, dat) + str=CLIHelper.color_state(str) if i==stat_column - str - }.join(' ').rstrip - } + str + }.join(' ').rstrip + } + end rescue Errno::EPIPE end end From 308484fa54f519a49573329b10f8a8eede693f83 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Tue, 18 Feb 2014 17:52:54 +0100 Subject: [PATCH 2/2] feature #1484: merge all user data in oneacct csv --- src/cli/one_helper/oneacct_helper.rb | 4 ++++ src/cli/oneacct | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/cli/one_helper/oneacct_helper.rb b/src/cli/one_helper/oneacct_helper.rb index f3048e3aa5..572777bf70 100644 --- a/src/cli/one_helper/oneacct_helper.rb +++ b/src/cli/one_helper/oneacct_helper.rb @@ -100,6 +100,10 @@ class AcctHelper < OpenNebulaHelper::OneHelper ACCT_TABLE = CLIHelper::ShowTable.new("oneacct.yaml", nil) do + column :UID, "User ID", :size=>4 do |d| + d["UID"] + end + column :VID, "Virtual Machine ID", :size=>4 do |d| d["OID"] end diff --git a/src/cli/oneacct b/src/cli/oneacct index 6573ec879f..9f27f732d8 100755 --- a/src/cli/oneacct +++ b/src/cli/oneacct @@ -44,7 +44,7 @@ cmd = CommandParser::CmdParser.new(ARGV) do end option AcctHelper::ACCT_OPTIONS + CommandParser::OPTIONS + - [OpenNebulaHelper::DESCRIBE, CLIHelper::LIST] + + [OpenNebulaHelper::DESCRIBE, CLIHelper::LIST, CLIHelper::CSV] + OpenNebulaHelper::CLIENT_OPTIONS main do @@ -87,7 +87,10 @@ cmd = CommandParser::CmdParser.new(ARGV) do else order_by = Hash.new order_by[:order_by_1] = 'VM/UID' - order_by[:order_by_2] = 'VM/ID' if options[:split] + + if options[:split] && !options[:csv] + order_by[:order_by_2] = 'VM/ID' + end acct_hash = pool.accounting(filter_flag, common_opts.merge(order_by)) @@ -96,6 +99,21 @@ cmd = CommandParser::CmdParser.new(ARGV) do exit -1 end + if options[:csv] + a=Array.new + acct_hash.each do |user_id, value| + value['HISTORY_RECORDS']['HISTORY'].each do |l| + l['UID']=user_id + a << l + end + end + + cols=AcctHelper::ACCT_TABLE.default_columns + AcctHelper::ACCT_TABLE.default(:UID, *cols) + + AcctHelper::ACCT_TABLE.show(a, options) + exit(0) + end if ( start_time != -1 or end_time != -1 ) AcctHelper.print_start_end_time_header(start_time, end_time)