Class: WavefrontCli::Query
- 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
-
#query_string ⇒ Object
readonly
Returns the value of attribute query_string.
Attributes inherited from Base
#klass, #klass_word, #options, #wf
Instance Method Summary collapse
-
#all_aliases ⇒ Hash
All query aliases for the active profile.
-
#basic_q_opts ⇒ Object
Every query gets these options.
-
#default_granularity(window) ⇒ Object
Work out a sensible granularity based on the time window.
- #do_aliases ⇒ Object
- #do_default ⇒ Object
- #do_raw ⇒ Object
- #do_run ⇒ Object
- #extra_validation ⇒ Object
- #granularity(t_start, t_end) ⇒ Object
- #handle_errcode404(_status) ⇒ Object
- #no_api_response ⇒ Object
-
#q_opts ⇒ Hash
Options for the SDK query method.
-
#window_end ⇒ Integer
End of query window.
-
#window_start ⇒ Integer
Start of query window.
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_string ⇒ Object (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_aliases ⇒ Hash
Returns 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().opts.select do |line| line.to_s.start_with?('q_') end end |
#basic_q_opts ⇒ Object
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: [:events], i: [:inclusive], summarization: [:summarize] || 'mean', listMode: true, strict: ![:nostrict], includeObsoleteMetrics: [: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_aliases ⇒ Object
48 49 50 |
# File 'lib/wavefront-cli/query.rb', line 48 def do_aliases all_aliases end |
#do_default ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/wavefront-cli/query.rb', line 19 def do_default qs = query_string || [:'<query>'] t_start = window_start t_end = window_end granularity = granularity(t_start, t_end) t_end = nil unless [:end] wf.query(qs, granularity, t_start, t_end, q_opts) end |
#do_raw ⇒ Object
30 31 32 33 |
# File 'lib/wavefront-cli/query.rb', line 30 def do_raw wf.raw([:'<metric>'], [:host], [:start], [:end]) end |
#do_run ⇒ Object
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: [:'<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_validation ⇒ Object
111 112 113 114 115 116 117 118 119 |
# File 'lib/wavefront-cli/query.rb', line 111 def extra_validation return unless [:granularity] begin wf_granularity?([:granularity]) rescue Wavefront::Exception::InvalidGranularity abort "'#{[: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) [: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_response ⇒ Object
15 16 17 |
# File 'lib/wavefront-cli/query.rb', line 15 def no_api_response %w[do_aliases] end |
#q_opts ⇒ Hash
Returns 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] = [:name] o[:p] = [:points] o[:view] = 'HISTOGRAM' if [:histogramview] o[:cached] = false if [:nocache] end.compact end |
#window_end ⇒ Integer
Returns 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 = [:end] ? [:end].dup : Time.now parse_time(t, true) end |
#window_start ⇒ Integer
Returns 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 = [:start] ? [:start].dup : Time.now - 600 parse_time(t, true) end |