Class: WavefrontCli::Query

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

Overview

CLI coverage for the v2 ‘query’ 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 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_list, #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, #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_input, #validate_opts, #validate_tags, #validator_exception, #validator_method, #warning_message

Constructor Details

This class inherits a constructor from WavefrontCli::Base

Instance Attribute Details

#query_stringObject (readonly)

Returns the value of attribute query_string.



11
12
13
# File 'lib/wavefront-cli/query.rb', line 11

def query_string
  @query_string
end

Instance Method Details

#all_aliasesHash

Returns all query aliases for the active profile.

Returns:

  • (Hash)

    all query aliases for the active profile



123
124
125
126
127
# File 'lib/wavefront-cli/query.rb', line 123

def all_aliases
  WavefrontCli::OptHandler.new(options).opts.select do |line|
    line.to_s.start_with?('q_')
  end
end

#basic_q_optsObject

Every query gets these options. They’re modified by q_opts



65
66
67
68
69
70
71
72
73
# File 'lib/wavefront-cli/query.rb', line 65

def basic_q_opts
  { autoEvents: options[:events],
    i: options[:inclusive],
    summarization: options[:summarize] || 'mean',
    listMode: true,
    strict: !options[:nostrict],
    includeObsoleteMetrics: options[:obsolete],
    sorted: true }
end

#default_granularity(window) ⇒ Object

Work out a sensible granularity based on the time window



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/wavefront-cli/query.rb', line 97

def default_granularity(window)
  window = window.abs / 1000

  if window < 300
    :s
  elsif window < 10_800
    :m
  elsif window < 259_200
    :h
  else
    :d
  end
end

#do_aliasesObject



48
49
50
# File 'lib/wavefront-cli/query.rb', line 48

def do_aliases
  all_aliases
end

#do_defaultObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/wavefront-cli/query.rb', line 19

def do_default
  qs = query_string || options[:'<query>']

  t_start     = window_start
  t_end       = window_end
  granularity = granularity(t_start, t_end)
  t_end       = nil unless options[:end]

  wf.query(qs, granularity, t_start, t_end, q_opts)
end

#do_rawObject



30
31
32
33
# File 'lib/wavefront-cli/query.rb', line 30

def do_raw
  wf.raw(options[:'<metric>'], options[:host], options[:start],
         options[:end])
end

#do_runObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/wavefront-cli/query.rb', line 35

def do_run
  alias_key = format('q_%<alias>s', alias: options[:'<alias>']).to_sym
  query = all_aliases.fetch(alias_key, nil)

  unless query
    abort "Query not found. 'wf query aliases' will show all " \
          'aliased queries.'
  end

  @query_string = query
  do_default
end

#extra_validationObject



111
112
113
114
115
116
117
118
119
# File 'lib/wavefront-cli/query.rb', line 111

def extra_validation
  return unless options[:granularity]

  begin
    wf_granularity?(options[:granularity])
  rescue Wavefront::Exception::InvalidGranularity
    abort "'#{options[:granularity]}' is not a valid granularity."
  end
end

#granularity(t_start, t_end) ⇒ Object



91
92
93
# File 'lib/wavefront-cli/query.rb', line 91

def granularity(t_start, t_end)
  options[:granularity] || default_granularity(t_start - t_end)
end

#handle_errcode404(_status) ⇒ Object



129
130
131
# File 'lib/wavefront-cli/query.rb', line 129

def handle_errcode404(_status)
  'Perhaps metric does not exist for given host.'
end

#no_api_responseObject



15
16
17
# File 'lib/wavefront-cli/query.rb', line 15

def no_api_response
  %w[do_aliases]
end

#q_optsHash

Returns options for the SDK query method.

Returns:

  • (Hash)

    options for the SDK query method



54
55
56
57
58
59
60
61
# File 'lib/wavefront-cli/query.rb', line 54

def q_opts
  basic_q_opts.tap do |o|
    o[:n] = options[:name]
    o[:p] = options[:points]
    o[:view] = 'HISTOGRAM' if options[:histogramview]
    o[:cached] = false if options[:nocache]
  end.compact
end

#window_endInteger

Returns end of query window. If one has been given, that; if not, now.

Returns:

  • (Integer)

    end of query window. If one has been given, that; if not, now



86
87
88
89
# File 'lib/wavefront-cli/query.rb', line 86

def window_end
  t = options[:end] ? options[:end].dup : Time.now
  parse_time(t, true)
end

#window_startInteger

Returns start of query window. If one has been given, that; if not, ten minutes ago.

Returns:

  • (Integer)

    start of query window. If one has been given, that; if not, ten minutes ago



78
79
80
81
# File 'lib/wavefront-cli/query.rb', line 78

def window_start
  t = options[:start] ? options[:start].dup : Time.now - 600
  parse_time(t, true)
end