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.
15 16 17 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 15 def columns @columns end |
#data_map ⇒ Object (readonly)
Returns the value of attribute data_map.
15 16 17 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 15 def data_map @data_map end |
#formatopts ⇒ Object (readonly)
Returns the value of attribute formatopts.
15 16 17 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 15 def formatopts @formatopts end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
15 16 17 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 15 def headers @headers end |
Instance Method Details
#_run ⇒ Object
17 18 19 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 17 def _run csv_headers + csv_body end |
#all_keys(data = data_map) ⇒ Array
60 61 62 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 60 def all_keys(data = data_map) data.each_with_object(Set.new) { |row, a| a.merge(row.keys) }.to_a end |
#csv_body ⇒ Object
72 73 74 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 72 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.
104 105 106 107 108 109 110 111 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 104 def csv_format(path, value, , source, = nil) ret = { path: path, value: value, timestamp: , source: source } ret.tap { |r| .each { |k, v| r[k.to_sym] = tag_val(k, v) } } end |
#csv_headers ⇒ Array
67 68 69 70 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 67 def csv_headers return [] unless formatopts.include?('headers') [columns.map { |c| csv_value(c) }.join(',')] end |
#csv_value(value) ⇒ Object
Do escaping and quoting
82 83 84 85 86 87 88 89 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 82 def csv_value(value) if (formatopts.include?('quote') || value =~ /[,\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
97 98 99 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 97 def extract_formatopts [:formatopts].nil? ? [] : [:formatopts].split(',') end |
#map_row_to_csv(row) ⇒ Object
76 77 78 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 76 def map_row_to_csv(row) columns.map { |col| csv_value(row[col]) }.join(',') end |
#post_initialize ⇒ Object
21 22 23 24 25 26 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 21 def post_initialize @headers = [] @formatopts = extract_formatopts @data_map = [:raw] ? raw_output : query_output @columns = all_keys.freeze end |
#query_output ⇒ Array[Hash]
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 44 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
91 92 93 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 91 def quote_value(value) format('"%s"', value.to_s.gsub(/"/, '\"')) end |
#raw_output ⇒ Array[Hash]
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 30 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
115 116 117 |
# File 'lib/wavefront-cli/output/csv/query.rb', line 115 def tag_val(key, val) formatopts.include?('tagkeys') ? format('%s=%s', key, val) : val end |