class WavefrontCsvOutput::Query
Display query results in CSV format.
The following options are supported:
quote -- puts all values in soft quotes headers -- print CSV column headers tagkeys -- normally point tag keys go in the header and values in the CSV data. This option puts key=value in the CSV.
Attributes
columns[R]
data_map[R]
formatopts[R]
headers[R]
Public Instance Methods
_run()
click to toggle source
# File lib/wavefront-cli/output/csv/query.rb, line 19 def _run csv_headers + csv_body end
all_keys(data = data_map)
click to toggle source
@return [Array] unique list of all keys in an array of hashes
# File lib/wavefront-cli/output/csv/query.rb, line 62 def all_keys(data = data_map) data.each_with_object(Set.new) { |row, a| a.merge(row.keys) }.to_a end
csv_body()
click to toggle source
# File lib/wavefront-cli/output/csv/query.rb, line 75 def csv_body data_map.map { |r| map_row_to_csv(r) } end
csv_format(path, value, timestamp, source, tags = nil)
click to toggle source
Take the data describing a point, and turn it into a CSV row. Tags have their keys removed.
# File lib/wavefront-cli/output/csv/query.rb, line 107 def csv_format(path, value, timestamp, source, tags = nil) ret = { path: path, value: value, timestamp: timestamp, source: source } return ret if tags.nil? ret.tap { |r| tags.each { |k, v| r[k.to_sym] = tag_val(k, v) } } end
csv_headers()
click to toggle source
@return [Array] single element of comma-separated CSV column
headers if requested, otherwise []
# File lib/wavefront-cli/output/csv/query.rb, line 69 def csv_headers return [] unless formatopts.include?('headers') [columns.map { |c| csv_value(c) }.join(',')] end
csv_value(value)
click to toggle source
Do escaping and quoting
# File lib/wavefront-cli/output/csv/query.rb, line 85 def csv_value(value) if (formatopts.include?('quote') || value.to_s =~ /[,\s"]/) && !value.to_s.empty? quote_value(value) else value end end
extract_formatopts()
click to toggle source
Turn a string of output options into an easy-to-query array
# File lib/wavefront-cli/output/csv/query.rb, line 100 def extract_formatopts options[:formatopts].nil? ? [] : options[:formatopts].split(',') end
map_row_to_csv(row)
click to toggle source
# File lib/wavefront-cli/output/csv/query.rb, line 79 def map_row_to_csv(row) columns.map { |col| csv_value(row[col]) }.join(',') end
post_initialize()
click to toggle source
# File lib/wavefront-cli/output/csv/query.rb, line 23 def post_initialize @headers = [] @formatopts = extract_formatopts @data_map = options[:raw] ? raw_output : query_output @columns = all_keys.freeze end
query_output()
click to toggle source
@return [Array] which goes in the @data_map
# File lib/wavefront-cli/output/csv/query.rb, line 46 def query_output check_query_response resp[:timeseries].each_with_object([]) do |ts, a| ts[:data].each do |point| a.<< csv_format(ts[:label], point[1], point[0], ts[:host], ts[:tags]) end end end
quote_value(value)
click to toggle source
# File lib/wavefront-cli/output/csv/query.rb, line 94 def quote_value(value) format('"%<value>s"', value: value.to_s.gsub(/"/, '\"')) end
raw_output()
click to toggle source
@return [Array] which goes in the @data_map
# File lib/wavefront-cli/output/csv/query.rb, line 32 def raw_output resp.each_with_object([]) do |point, a| point[:points].each do |p| a.<< csv_format(options[:'<metric>'], p[:value], p[:timestamp], options[:host], point[:tags]) end end end
tag_val(key, val)
click to toggle source
We may be doing key=val or just val, depending on the formatter options
# File lib/wavefront-cli/output/csv/query.rb, line 120 def tag_val(key, val) if formatopts.include?('tagkeys') format('%<key>s=%<value>s', key: key, value: val) else val end end