class WavefrontDisplay::Query

Format human-readable output for queries.

Public Instance Methods

default_data_object() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/wavefront-cli/display/query.rb, line 20
def default_data_object
  { name: data.name,
    query: data.query,
    timeseries: mk_timeseries(data),
    traces: mk_traces(data),
    spans: mk_spans(data),
    events: mk_events(data) }.tap do |d|
      d[:warnings] = data[:warnings] if show_warnings?
    end
end
do_aliases() click to toggle source
# File lib/wavefront-cli/display/query.rb, line 85
def do_aliases
  if data.empty?
    puts 'No aliases defined.'
  else
    data.each_key { |k| puts k.to_s[2..-1] }
  end
end
do_default() click to toggle source
# File lib/wavefront-cli/display/query.rb, line 11
def do_default
  @data = default_data_object
  long_output
rescue StandardError
  raise(WavefrontCli::Exception::InvalidQuery,
        data[:errorMessage].split("\n").first)
end
do_raw() click to toggle source
# File lib/wavefront-cli/display/query.rb, line 77
def do_raw
  data.each { |ts| puts humanize_series(ts[:points]).join("\n") }
end
do_raw_404() click to toggle source
# File lib/wavefront-cli/display/query.rb, line 81
def do_raw_404
  puts 'API 404: metric does not exist.'
end
do_run() click to toggle source
# File lib/wavefront-cli/display/query.rb, line 73
def do_run
  do_default
end
mk_events(data) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 55
def mk_events(data)
  return [] unless data.key?(:events)

  data[:events].map { |s| humanize_event(s) }
end
mk_spans(data) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 67
def mk_spans(data)
  return [] unless data.key?(:spans)

  data[:spans].map { |t| humanize_span(t) }
end
mk_timeseries(data) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 42
def mk_timeseries(data)
  return [] unless data.key?(:timeseries)

  data[:timeseries].each do |s|
    unless options[:nospark]
      s[:sparkline] = WavefrontSparkline.new(s[:data]).sparkline
      s.reorder!(label: nil, sparkline: nil)
    end

    s[:data] = humanize_series(s[:data])
  end
end
mk_traces(data) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 61
def mk_traces(data)
  return [] unless data.key?(:traces)

  data[:traces].map { |t| humanize_trace(t) }
end
prioritize_keys(data, _keys) click to toggle source

Prioritizing keys does not make sense in this context

# File lib/wavefront-cli/display/query.rb, line 38
def prioritize_keys(data, _keys)
  data
end
show_warnings?() click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/wavefront-cli/display/query.rb, line 32
def show_warnings?
  data.key?(:warnings) && !options[:nowarn]
end

Private Instance Methods

humanize_event(data) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 95
def humanize_event(data)
  data[:start] = human_time(data[:start])
  data[:end] = human_time(data[:end]) if data[:end]
  data.delete(:isEphemeral)
  data
end
humanize_series(data) click to toggle source

Prepare a padded line with the timestamp and value. If it's the @return [String]

# File lib/wavefront-cli/display/query.rb, line 105
def humanize_series(data)
  last_date = nil

  data.map do |row|
    ht, val = row_time_and_val(row)
    date, time = ht.split
    date_string = date == last_date ? '' : date
    last_date = date
    format('%-12<series>s %<time>s    %<value>s',
           series: date_string, time: time, value: val)
  end
end
humanize_span(data) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 131
def humanize_span(data)
  @printer_opts[:sep_depth] = 2
  data
end
humanize_trace(data) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 118
def humanize_trace(data)
  @printer_opts[:sep_depth] = 3

  data.tap do |t|
    t[:start] = human_time(t[:start_ms])
    t[:end] = human_time(t[:end_ms])
    t.delete(:start_ms)
    t.delete(:end_ms)
    t.delete(:startMs)
    t.spans = t.spans.map { |s| humanize_trace_span(s) }
  end
end
humanize_trace_span(span) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 136
def humanize_trace_span(span)
  span.tap do |s|
    s[:startMs] = human_time(s[:startMs])
  end
end
row_time_and_val(row) click to toggle source
# File lib/wavefront-cli/display/query.rb, line 142
def row_time_and_val(row)
  if row.is_a?(Hash)
    [human_time(row[:timestamp]), row[:value]]
  else
    [human_time(row[0]), row[1]]
  end
end