Class: Wavefront::Search

Inherits:
CoreApi show all
Defined in:
lib/wavefront-sdk/search.rb

Overview

Manage and query Wavefront searches. The /search API path has a lot of paths, with a lot of duplication. The current state of this class covers the whole API with two methods, but leaves a lot up to the user. It may grow, with convenience methods.

Instance Attribute Summary

Attributes inherited from CoreApi

#api, #creds, #logger, #opts, #update_keys

Instance Method Summary collapse

Methods inherited from CoreApi

#api_base, #api_path, #hash_for_update, #initialize, #setup_api, #time_to_ms

Methods included from Mixins

#log, #parse_relative_time, #parse_time, #relative_time, #time_multiplier, #valid_relative_time?

Methods included from Validators

#uuid?, #wf_account_id?, #wf_alert_id?, #wf_alert_severity?, #wf_apitoken_id?, #wf_aws_external_id?, #wf_cloudintegration_id?, #wf_dashboard_id?, #wf_derivedmetric_id?, #wf_distribution?, #wf_distribution_count?, #wf_distribution_interval?, #wf_distribution_values?, #wf_epoch?, #wf_event_id?, #wf_granularity?, #wf_ingestionpolicy_id?, #wf_integration_id?, #wf_link_id?, #wf_link_template?, #wf_maintenance_window_id?, #wf_message_id?, #wf_metric_name?, #wf_metricspolicy_id?, #wf_monitoredapplication_id?, #wf_monitoredcluster_id?, #wf_ms_ts?, #wf_name?, #wf_notificant_id?, #wf_permission?, #wf_point?, #wf_point_tag?, #wf_point_tags?, #wf_proxy_id?, #wf_role_id?, #wf_sampling_value?, #wf_savedsearch_entity?, #wf_savedsearch_id?, #wf_serviceaccount_id?, #wf_source_id?, #wf_spansamplingpolicy_id?, #wf_string?, #wf_tag?, #wf_trace?, #wf_ts?, #wf_user_id?, #wf_usergroup_id?, #wf_value?, #wf_version?, #wf_webhook_id?

Constructor Details

This class inherits a constructor from Wavefront::CoreApi

Instance Method Details

#body(query, options) ⇒ Object

Build a query body



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/wavefront-sdk/search.rb', line 50

def body(query, options)
  ret = query_limits(options)

  if query && !query.empty?
    ret[:query] = [query].flatten.map do |q|
      q.tap { |iq| iq[:matchingMethod] ||= 'CONTAINS' }
    end

    ret[:sort] = sort_field(options, query)
  end

  ret
end

#query_limits(options) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/wavefront-sdk/search.rb', line 64

def query_limits(options)
  { limit: options[:limit] || 10 }.tap do |ret|
    if options[:cursor]
      ret[:cursor] = options[:cursor]
    else
      ret[:offset] = options[:offset] || 0
    end
  end
end

#raw_facet_search(entity = nil, body = nil, deleted = false, facet = false) ⇒ Object

POST /api/v2/search/entity/facets POST /api/v2/search/entity/facet rubocop:disable Metrics/ParameterLists

Parameters:

  • entity (String) (defaults to: nil)

    the type of Wavefront object you wish to search

  • body (Hash) (defaults to: nil)

    the query to use for searching. Refer to the Wavefront Swagger docs for the correct format.

  • deleted (Boolean) (defaults to: false)

    whether to search deleted (true) or active (false) entities

  • facet (String) (defaults to: false)

    the facet on which to search. If this is false, the assumption is that multiple facets will be specified in the body. See the Swagger docs for more information.

Raises:

  • (ArgumentError)


119
120
121
122
123
124
125
126
127
# File 'lib/wavefront-sdk/search.rb', line 119

def raw_facet_search(entity = nil, body = nil, deleted = false,
                     facet = false)
  raise ArgumentError unless entity.is_a?(String) && body.is_a?(Hash)

  path = [entity]
  path << 'deleted' if deleted
  path << (facet || 'facets')
  api.post(path, body, 'application/json')
end

#raw_search(entity = nil, body = nil, deleted = false) ⇒ Object

POST /api/v2/search/entity POST /api/v2/search/entity/deleted Run a search query. This single method maps to many API paths.

Parameters:

  • entity (String) (defaults to: nil)

    the type of Wavefront object you wish to search

  • body (Hash) (defaults to: nil)

    the query to use for searching. Refer to the Wavefront Swagger docs for the correct format. Specifying multiple key - value pairs performs a logical AND on the constraints.

  • deleted (Boolean) (defaults to: false)

    whether to search deleted (true) or active (false) entities



94
95
96
97
98
99
100
101
102
103
# File 'lib/wavefront-sdk/search.rb', line 94

def raw_search(entity = nil, body = nil, deleted = false)
  unless (entity.is_a?(String) || entity.is_a?(Symbol)) &&
         body.is_a?(Hash)
    raise ArgumentError
  end

  path = [entity]
  path << 'deleted' if deleted
  api.post(path, body, 'application/json')
end

#search(entity, query, options = {}) ⇒ Object

POST /api/v2/search/entity POST /api/v2/search/entity/deleted Run a search query. This single method maps to many API paths. It is a wrapper around #raw_search() for common, single key-value searches. If you need to do more complicated things, use #raw_search().

containing the following keys:

key [String] the field on which to search
value [String] what to search for
matchingMethod [String] the method to match values. Defaults
  to 'CONTAINS'. Must be one of CONTAINS, STARTSWITH, EXACT,
  TAGPATH
If an array of hashes is supplied, Wavefront will apply a
logical AND to the given key-value pairs.

Parameters:

  • entity (String, Symbol)

    the type of Wavefront object you wish to search. e.g. :alert, :dashboard

  • query (Array, Hash)

    A single hash, or array of hashes,

  • value (String)

    the value to search for

  • options (Hash) (defaults to: {})

    tune the query: keys are: deleted [Boolean] whether to search deleted (true) or active

    (false) entities
    

    limit [Integer] how many results to return. Defaults to 0

    (all of them)
    

    offset [Integer] return results after this offset desc: [Boolean] return results in descending order. Defaults

    to false. Sorting is done on the 'key' of the first query
    hash.
    

Raises:

  • (ArgumentError)


42
43
44
45
46
# File 'lib/wavefront-sdk/search.rb', line 42

def search(entity, query, options = {})
  raise ArgumentError unless options.is_a?(Hash)

  raw_search(entity, body(query, options), options[:deleted] || false)
end

#sort_field(options, query) ⇒ Object



74
75
76
77
78
79
# File 'lib/wavefront-sdk/search.rb', line 74

def sort_field(options, query)
  field = options[:sort_field] || [query].flatten.first[:key]

  { field: field,
    ascending: !options[:desc] || true }
end