Class: WavefrontCli::Event

Inherits:
Base
  • Object
show all
Includes:
Wavefront::Mixins, Mixin::Tag
Defined in:
lib/wavefront-cli/event.rb

Overview

CLI coverage for the v2 ‘event’ API.

Constant Summary

Constants included from Constants

Constants::ALL_PAGE_SIZE, Constants::DEFAULT_CONFIG, Constants::DEFAULT_OPTS, Constants::EVENT_STATE_DIR, Constants::HUMAN_TIME_FORMAT, Constants::HUMAN_TIME_FORMAT_MS, Constants::SEARCH_SPLIT

Instance Attribute Summary collapse

Attributes inherited from Base

#klass, #klass_word, #options, #wf

Instance Method Summary collapse

Methods included from Mixin::Tag

#do_tag_add, #do_tag_clear, #do_tag_delete, #do_tag_pathsearch, #do_tag_set, #do_tags

Methods inherited from Base

#_sdk_class, #cannot_noop!, #check_response_blocks, #check_status, #cli_output_class, #conds_to_query, #descriptive_name, #dispatch, #display, #display_api_error, #display_class, #display_no_api_response, #do_delete, #do_describe, #do_dump, #do_import, #do_search, #do_set, #do_undelete, #dump_json, #dump_yaml, #extract_values, #failed_validation_message, #format_var, #handle_error, #handle_response, #hcl_fields, #import_to_create, #initialize, #item_dump_call, #load_display_class, #matching_method, #method_word_list, #mk_creds, #mk_opts, #name_of_do_method, #no_api_response, #ok_exit, #one_or_all, #options_and_exit, #parseable_output, #range_hash, #require_sdk_class, #run, #search_key, #smart_delete, #smart_delete_message, #status_error_handler, #unsupported_format_message, #validate_id, #validate_opts, #validate_tags, #validator_exception, #validator_method, #warning_message

Constructor Details

This class inherits a constructor from WavefrontCli::Base

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



13
14
15
# File 'lib/wavefront-cli/event.rb', line 13

def state
  @state
end

Instance Method Details

#annotations(opts) ⇒ Object

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



125
126
127
128
129
130
131
# 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) ⇒ Object

return [Hash] body for #create() method

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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) ⇒ Object

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.



66
67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# 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_listObject



22
23
24
# File 'lib/wavefront-cli/event.rb', line 22

def do_list
  wf.list(*list_args)
end

#do_showObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wavefront-cli/event.rb', line 38

def do_show
  events = state.list

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

  exit
end

#do_wrapObject



50
51
52
53
54
55
56
57
58
# 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_argsObject



89
90
91
92
93
94
# 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) ⇒ Object



18
19
20
# File 'lib/wavefront-cli/event.rb', line 18

def post_initialize(options)
  @state = WavefrontCli::EventStore.new(options)
end

#validate_inputObject

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.



82
83
84
85
86
87
# 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_endObject



100
101
102
# File 'lib/wavefront-cli/event.rb', line 100

def window_end
  parse_time((options[:end] || Time.now), true)
end

#window_startObject



96
97
98
# File 'lib/wavefront-cli/event.rb', line 96

def window_start
  parse_time((options[:start] || (Time.now - 600)), true)
end