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.



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

def columns
  @columns
end

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

#formatoptsObject (readonly)

Returns the value of attribute formatopts.



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

def formatopts
  @formatopts
end

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

#_runObject



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.

Returns:

  • (Array)

    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_bodyObject



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, 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_headersArray

Returns single element of comma-separated CSV column headers if requested, otherwise [].

Returns:

  • (Array)

    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_formatoptsObject

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



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

#query_outputArray[Hash]

Returns which goes in the @data_map.

Returns:

  • (Array[Hash])

    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_outputArray[Hash]

Returns which goes in the @data_map.

Returns:

  • (Array[Hash])

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



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