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.
-
#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_errcode_404(_status) ⇒ Object
- #no_api_response ⇒ Object
-
#q_opts ⇒ Hash
rubocop:disable Metrics/AbcSize.
-
#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, #dispatch, #display, #display_api_error, #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_message, #import_object, #import_to_create, #import_update, #initialize, #item_dump_call, #load_display_class, #load_file, #load_from_stdin, #load_json, #load_yaml, #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, #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.
117 118 119 120 121 |
# File 'lib/wavefront-cli/query.rb', line 117 def all_aliases WavefrontCli::OptHandler.new().opts.select do |line| line.to_s.start_with?('q_') end end |
#default_granularity(window) ⇒ Object
Work out a sensible granularity based on the time window
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/wavefront-cli/query.rb', line 91 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
105 106 107 108 109 110 111 112 113 |
# File 'lib/wavefront-cli/query.rb', line 105 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
85 86 87 |
# File 'lib/wavefront-cli/query.rb', line 85 def granularity(t_start, t_end) [:granularity] || default_granularity(t_start - t_end) end |
#handle_errcode_404(_status) ⇒ Object
123 124 125 |
# File 'lib/wavefront-cli/query.rb', line 123 def handle_errcode_404(_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
rubocop:disable Metrics/AbcSize
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/wavefront-cli/query.rb', line 55 def q_opts { autoEvents: [:events], i: [:inclusive], summarization: [:summarize] || 'mean', listMode: true, strict: true, includeObsoleteMetrics: [:obsolete], sorted: true }.tap do |o| o[:n] = [:name] if [:name] o[:p] = [:points] if [:points] end end |
#window_end ⇒ Integer
Returns end of query window. If one has been given, that; if not, now.
80 81 82 83 |
# File 'lib/wavefront-cli/query.rb', line 80 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.
72 73 74 75 |
# File 'lib/wavefront-cli/query.rb', line 72 def window_start t = [:start] ? [:start].dup : Time.now - 600 parse_time(t, true) end |