Class: Scout::MetricProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_api/metric_proxy.rb

Overview

This class works similar to Ruby on Rails’ AssociationProxy, providing a nice interface to metrics from owner objects in Scout (ex: Server, Group, Plugin). See stackoverflow.com/questions/1529606/how-do-rails-association-methods-work for background

Example usage:

# all metrics associated with the group
group.metrics
# average value of all metrics w/name 'idle' associated with the server
server.metrics.average(:name => 'idle')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ MetricProxy

:nodoc:



15
16
17
18
19
20
# File 'lib/scout_api/metric_proxy.rb', line 15

def initialize(owner) #:nodoc:
  @owner = owner
  @avg_calc = Scout::MetricCalculation.new(self,:AVG)
  @min_calc = Scout::MetricCalculation.new(self,:MIN)
  @max_calc = Scout::MetricCalculation.new(self,:MAX)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/scout_api/metric_proxy.rb', line 101

def method_missing(method, *args)
  if load_target
    unless @target.respond_to?(method)
      message = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}"
      raise NoMethodError, message
    end

    if block_given?
      @target.send(method, *args)  { |*block_args| yield(*block_args) }
    else
      @target.send(method, *args)
    end
  end
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



13
14
15
# File 'lib/scout_api/metric_proxy.rb', line 13

def owner
  @owner
end

Instance Method Details

#all(options = nil) ⇒ Array

Find all metrics w/ :name => metric_name associated with the proxy owner (Group, Server, or Plugin).

Example:

server.metrics.all(:name => 'request_rate') => returns all metrics on server w/the name ‘request_rate’.

Returns:

  • (Array)

    An array of [Scout::Metric] objects



66
67
68
69
70
71
# File 'lib/scout_api/metric_proxy.rb', line 66

def all(options = nil)
  metric_name = options[:name] if options
  Scout::Metric.all(
    owner_key_value.merge!(:name => metric_name)
  )
end

#average(options) ⇒ Hash Also known as: avg

Calculate the average value of the metric w/:name => metric_name associated with the proxy owner (Group, Server, or Plugin).

See Scout::Metric#average for options.

Returns:

  • (Hash)


28
29
30
31
32
# File 'lib/scout_api/metric_proxy.rb', line 28

def average(options)
  @avg_calc.metric_name = metric_from_options(options)
  @avg_calc.options = options
  @avg_calc
end

#find_targetObject

:nodoc:



77
78
79
80
81
82
83
# File 'lib/scout_api/metric_proxy.rb', line 77

def find_target #:nodoc:
  if @owner.is_a?(Scout::Plugin) # plugin already has metric info
    @owner.descriptor_hash.map { |d| Scout::Metric.new(d) }
  else
    Scout::Metric.all(owner_key_value)
  end
end

#load_targetObject

:nodoc:



73
74
75
# File 'lib/scout_api/metric_proxy.rb', line 73

def load_target #:nodoc:
  @target = find_target
end

#maximum(options) ⇒ Hash Also known as: max

Calculate the maximum value of the metric w/:name => metric_name associated with the proxy owner (Group, Server, or Plugin).

See Metric#average for options.

Returns:

  • (Hash)


52
53
54
55
56
# File 'lib/scout_api/metric_proxy.rb', line 52

def maximum(options)
  @max_calc.metric_name = metric_from_options(options)
  @max_calc.options = options    
  @max_calc
end

#minimum(options) ⇒ Hash Also known as: min

Calculate the minimum value of the metric w/:name => metric_name associated with the proxy owner (Group, Server, or Plugin).

See Metric#average for options.

Returns:

  • (Hash)


40
41
42
43
44
# File 'lib/scout_api/metric_proxy.rb', line 40

def minimum(options)
  @min_calc.metric_name = metric_from_options(options)
  @min_calc.options = options    
  @min_calc
end