Class: WavefrontCsvOutput::Query
- Defined in:
- lib/wavefront-cli/output/csv/query.rb
Overview
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.
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#data_map ⇒ Object
readonly
Returns the value of attribute data_map.
-
#formatopts ⇒ Object
readonly
Returns the value of attribute formatopts.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Attributes inherited from Base
Instance Method Summary collapse
- #_run ⇒ Object
-
#all_keys(data = data_map) ⇒ Array
Unique list of all keys in an array of hashes.
- #csv_body ⇒ Object
-
#csv_format(path, value, timestamp, source, tags = nil) ⇒ Object
Take the data describing a point, and turn it into a CSV row.
-
#csv_headers ⇒ Array
Single element of comma-separated CSV column headers if requested, otherwise [].
-
#csv_value(value) ⇒ Object
Do escaping and quoting.
-
#extract_formatopts ⇒ Object
Turn a string of output options into an easy-to-query array.
- #map_row_to_csv(row) ⇒ Object
- #post_initialize ⇒ Object
-
#query_output ⇒ Array[Hash]
Which goes in the @data_map.
- #quote_value(value) ⇒ Object
-
#raw_output ⇒ Array[Hash]
Which goes in the @data_map.
-
#tag_val(key, val) ⇒ Object
We may be doing key=val or just val, depending on the formatter options.
Methods inherited from Base
#check_query_response, #initialize, #run
Constructor Details
This class inherits a constructor from WavefrontCsvOutput::Base
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
17 18 19 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 17 def columns @columns end |
#data_map ⇒ Object (readonly)
Returns the value of attribute data_map.
17 18 19 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 17 def data_map @data_map end |
#formatopts ⇒ Object (readonly)
Returns the value of attribute formatopts.
17 18 19 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 17 def formatopts @formatopts end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
17 18 19 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 17 def headers @headers end |
Instance Method Details
#_run ⇒ Object
19 20 21 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 19 def _run csv_headers + csv_body end |
#all_keys(data = data_map) ⇒ Array
Returns unique list of all keys in an array of hashes.
62 63 64 |
# 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 ⇒ Object
75 76 77 |
# 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) ⇒ Object
Take the data describing a point, and turn it into a CSV row. Tags have their keys removed.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 107 def csv_format(path, value, , source, = nil) ret = { path: path, value: value, timestamp: , source: source } return ret if .nil? ret.tap { |r| .each { |k, v| r[k.to_sym] = tag_val(k, v) } } end |
#csv_headers ⇒ Array
Returns single element of comma-separated CSV column headers if requested, otherwise [].
69 70 71 72 73 |
# 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) ⇒ Object
Do escaping and quoting
85 86 87 88 89 90 91 92 |
# 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 ⇒ Object
Turn a string of output options into an easy-to-query array
100 101 102 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 100 def extract_formatopts [:formatopts].nil? ? [] : [:formatopts].split(',') end |
#map_row_to_csv(row) ⇒ Object
79 80 81 |
# 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 ⇒ Object
23 24 25 26 27 28 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 23 def post_initialize @headers = [] @formatopts = extract_formatopts @data_map = [:raw] ? raw_output : query_output @columns = all_keys.freeze end |
#query_output ⇒ Array[Hash]
Returns which goes in the @data_map.
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
94 95 96 |
# 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 ⇒ Array[Hash]
Returns which goes in the @data_map.
32 33 34 35 36 37 38 39 40 41 42 |
# 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([:'<metric>'], p[:value], p[:timestamp], [:host], point[:tags]) end end end |
#tag_val(key, val) ⇒ Object
We may be doing key=val or just val, depending on the formatter options
120 121 122 123 124 125 126 |
# 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 |