Class: WavefrontCsvOutput::Query

Inherits:
Base
  • Object
show all
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

Attributes inherited from Base

#options, #resp

Instance Method Summary collapse

Methods inherited from Base

#check_query_response, #initialize, #run

Constructor Details

This class inherits a constructor from WavefrontCsvOutput::Base

Instance Attribute Details

#columnsObject (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_mapObject (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

#formatoptsObject (readonly)

Returns the value of attribute formatopts.



15
16
17
# File 'lib/wavefront-cli/output/csv/query.rb', line 15

def formatopts
  @formatopts
end

#headersObject (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

#_runObject



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_bodyObject



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, timestamp, source, tags = nil)
  ret = { path:      path,
          value:     value,
          timestamp: timestamp,
          source:    source }

  ret.tap { |r| tags.each { |k, v| r[k.to_sym] = tag_val(k, v) } }
end

#csv_headersArray



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_formatoptsObject

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
  options[:formatopts].nil? ? [] : options[: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_initializeObject



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    = options[:raw] ? raw_output : query_output
  @columns     = all_keys.freeze
end

#query_outputArray[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_outputArray[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(options[:'<metric>'],
                      p[:value],
                      p[:timestamp],
                      options[: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