Class: WavefrontDisplay::Query

Inherits:
Base
  • Object
show all
Defined in:
lib/wavefront-cli/display/query.rb

Overview

Format human-readable output for queries.

Constant Summary

Constants included from WavefrontCli::Constants

WavefrontCli::Constants::DEFAULT_OPTS, WavefrontCli::Constants::HUMAN_TIME_FORMAT, WavefrontCli::Constants::HUMAN_TIME_FORMAT_MS

Instance Attribute Summary

Attributes inherited from Base

#data, #hide_blank, #indent, #indent_step, #indent_str, #kw, #options

Instance Method Summary collapse

Methods inherited from Base

#_two_columns, #do_delete, #do_import, #do_list, #do_list_brief, #do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_set, #do_tags, #do_undelete, #drop_fields, #friendly_name, #human_time, #indent_wrap, #initialize, #key_width, #long_output, #multicolumn, #print_array, #print_line, #readable_time, #run, #run_error, #set_indent, #terse_output

Constructor Details

This class inherits a constructor from WavefrontDisplay::Base

Instance Method Details

#do_defaultObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wavefront-cli/display/query.rb', line 8

def do_default

  ts = if data.key?(:timeseries)
         data[:timeseries].each do |s|
           s[:data] = humanize_series(s[:data])
         end
       else
         []
       end

  events = if data.key?(:events)
             data[:events].map { |s| humanize_event(s) }
           else
             []
           end

  new = {
    name: data.name,
    query: data.query,
    timeseries: ts,
    events: events
  }

  @data = new
  long_output
end

#do_rawObject



35
36
37
# File 'lib/wavefront-cli/display/query.rb', line 35

def do_raw
  data.each { |ts| puts humanize_series(ts[:points]).join("\n") }
end

#do_raw_404Object



39
40
41
# File 'lib/wavefront-cli/display/query.rb', line 39

def do_raw_404
  puts 'API 404: metric does not exist.'
end

#humanize_event(data) ⇒ Object



43
44
45
46
47
48
# File 'lib/wavefront-cli/display/query.rb', line 43

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) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wavefront-cli/display/query.rb', line 50

def humanize_series(data)
  last_date = nil

  data.map! do |row|
    if row.is_a?(Hash)
      ht = human_time(row[:timestamp])
      val = row[:value]
    else
      ht = human_time(row[0])
      val = row[1]
    end

    date, time = ht.split
    ds = date == last_date ? '' : date
    last_date = date
    format('%12s %s    %s', ds, time, val)
  end
end