class WavefrontCli::Event

CLI coverage for the v2 'event' API.

Attributes

state[R]

Public Instance Methods

annotations(opts) click to toggle source

rubocop:enable Metrics/AbcSize rubocop:enable Metrics/MethodLength

# File lib/wavefront-cli/event.rb, line 125
def annotations(opts)
  {}.tap do |r|
    r[:details] = opts[:desc] if opts[:desc]
    r[:severity] = opts[:severity] if opts[:severity]
    r[:type] = opts[:type] if opts[:type]
  end
end
create_body(opts, t_start) click to toggle source

return [Hash] body for create() method

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize

# File lib/wavefront-cli/event.rb, line 108
def create_body(opts, t_start)
  { name: opts[:'<event>'],
    startTime: t_start,
    annotations: annotations(opts) }.tap do |r|
      r[:hosts] = opts[:host] if opts[:host]
      r[:tags] = opts[:evtag] if opts[:evtag]

      if opts[:instant]
        r[:endTime] = t_start + 1
      elsif opts[:end]
        r[:endTime] = parse_time(opts[:end], true)
      end
    end
end
do_close(id = nil) click to toggle source

The user doesn't have to give us an event ID. If no event name is given, we'll pop the last event off the stack. If an event name is given and it doesn't look like a full WF event name, we'll look for something on the stack. If it does look like a real event, we'll make an API call straight away.

# File lib/wavefront-cli/event.rb, line 66
def do_close(id = nil)
  id ||= options[:'<id>']
  ev = state.event(id)
  ev_file = state.event_file(id)

  abort "No locally stored event matches '#{id}'." unless ev

  res = wf.close(ev)
  ev_file.unlink if ev_file&.exist? && res.status.code == 200
  res
end
do_create(opts = nil) click to toggle source
# File lib/wavefront-cli/event.rb, line 26
def do_create(opts = nil)
  opts ||= options
  opts[:start] = Time.now unless opts[:start]
  t_start = parse_time(opts[:start], true)
  body = create_body(opts, t_start)
  resp = wf.create(body)
  return if opts[:noop]

  state.create!(resp.response[:id])
  resp
end
do_list() click to toggle source
# File lib/wavefront-cli/event.rb, line 22
def do_list
  wf.list(*list_args)
end
do_show() click to toggle source
# File lib/wavefront-cli/event.rb, line 38
def do_show
  events = state.list

  if events.size.zero?
    puts 'No open events.'
  else
    events.sort.reverse_each { |e| puts e.basename }
  end

  exit
end
do_wrap() click to toggle source
# File lib/wavefront-cli/event.rb, line 50
def do_wrap
  create_opts = options
  create_opts[:desc] ||= create_opts[:command]
  event_id = do_create(create_opts).response.id
  exit_code = state.run_wrapped_cmd(options[:command])
  do_close(event_id)
  puts "Command exited #{exit_code}."
  exit exit_code
end
list_args() click to toggle source
# File lib/wavefront-cli/event.rb, line 89
def list_args
  [window_start,
   window_end,
   options[:limit] || 100,
   options[:cursor] || nil]
end
post_initialize(options) click to toggle source
# File lib/wavefront-cli/event.rb, line 18
def post_initialize(options)
  @state = WavefrontCli::EventStore.new(options)
end
validate_input() click to toggle source

We have to override the normal validation methods because an event can be referred to by only a part of its name. This happens when the user refers to events on the local stack.

# File lib/wavefront-cli/event.rb, line 82
def validate_input
  validate_id if options[:'<id>'] && !options[:close]
  validate_tags if options[:'<tag>']
  validate_tags(:evtag) if options[:evtag]
  send(:extra_validation) if respond_to?(:extra_validation)
end
window_end() click to toggle source
# File lib/wavefront-cli/event.rb, line 100
def window_end
  parse_time((options[:end] || Time.now), true)
end
window_start() click to toggle source
# File lib/wavefront-cli/event.rb, line 96
def window_start
  parse_time((options[:start] || Time.now - 600), true)
end