Class: Wavefront::Response::Human

Inherits:
Ruby
  • Object
show all
Defined in:
lib/wavefront/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, options = {}) ⇒ Human

Returns a new instance of Human.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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

Instance Attribute Details

#humanObject (readonly)

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



119
120
121
# File 'lib/wavefront/response.rb', line 119

def human
  @human
end

#optionsObject (readonly)

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



119
120
121
# File 'lib/wavefront/response.rb', line 119

def options
  @options
end

#responseObject (readonly)

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



119
120
121
# File 'lib/wavefront/response.rb', line 119

def response
  @response
end

Instance Method Details

#format_event_duration(ts, te) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# 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) ⇒ Object



188
189
190
# File 'lib/wavefront/response.rb', line 188

def format_event_time(tms)
  Time.at(tms / 1000).strftime('%F %T')
end

#process_eventsObject



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# 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_timeseriesObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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