class Wavefront::Response::Human

Attributes

human[R]

Print “human-readable” (but also easily machine-pareseable) values.

options[R]

Print “human-readable” (but also easily machine-pareseable) values.

response[R]

Print “human-readable” (but also easily machine-pareseable) values.

Public Class Methods

new(response, options={}) click to toggle source
Calls superclass method Wavefront::Response::Ruby::new
# File lib/wavefront/response.rb, line 121
def initialize(response, options={})
  super

  if self.response
    if self.respond_to?(:timeseries)
      out = process_timeseries
    elsif self.respond_to?(:events)
      out = process_events
    else
      out = []
    end
  else
    out = self.warnings
  end

  @human = out.join("\n")
end

Public Instance Methods

format_event_duration(ts, te) click to toggle source
# File lib/wavefront/response.rb, line 192
def format_event_duration(ts, te)
  #
  # turn an event start and end into a human-readable,
  # approximate, time.  Truncates after the first two parts in
  # the interests of space.
  #
  dur = (te - ts) / 1000

  return 'inst' if dur == 0

  {s: 60, m: 60, h: 24, d: 1000 }.map do |sfx, val|
    next unless dur > 0
    dur, n = dur.divmod(val)
    n.to_s + sfx.to_s
  end.compact.reverse[0..1].join(' ')
end
format_event_time(tms) click to toggle source
# File lib/wavefront/response.rb, line 188
def format_event_time(tms)
  Time.at(tms / 1000).strftime('%F %T')
end
process_events() click to toggle source
# File lib/wavefront/response.rb, line 159
def process_events
  sorted = self.events.sort_by { |k| k['start'] }

  sorted.each_with_object([]) do |e, out|
    hosts = e['hosts'] ? '[' + e['hosts'].join(',') + ']' : ''

    if e['tags']
      severity = e['tags']['severity']
      type = e['tags']['type']
      details = e['tags']['details']
    else
      severity = type = details = ''
    end

    t = [format_event_time(e['start']), '->',
         format_event_time(e['end']),
         '%-9s' % ('(' + format_event_duration(e['start'],
                                               e['end']) + ')'),
         '%-7s' % severity,
         '%-15s' % type,
         '%-25s' % e['name'],
         hosts,
         details,
        ].join(' ')

    out.<< t
  end
end
process_timeseries() click to toggle source
# File lib/wavefront/response.rb, line 139
def process_timeseries
  out = ['%-20s%s' % ['query', self.query]]

  self.timeseries.each_with_index do |ts, i|
    out.<< '%-20s%s' % ['timeseries', i]
    out += ts.select{|k,v| k != 'data' }.map do |k, v|
      if k == 'tags'
        v.map { |tk, tv| 'tag.%-16s%s' % [tk, tv] }
      else
        '%-20s%s' % [k, v]
      end
    end
    out += ts['data'].map do |t, v|
      [Time.at(t).strftime('%F %T'), v].join(' ')
    end
  end

  out
end