Class: Scout::MetricProxy
- Inherits:
-
Object
- Object
- Scout::MetricProxy
- 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
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
Instance Method Summary collapse
- #all(options = nil) ⇒ Array
- #average(options) ⇒ Hash (also: #avg)
-
#find_target ⇒ Object
:nodoc:.
-
#initialize(owner) ⇒ MetricProxy
constructor
:nodoc:.
-
#load_target ⇒ Object
:nodoc:.
- #maximum(options) ⇒ Hash (also: #max)
- #minimum(options) ⇒ Hash (also: #min)
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) = "undefined method `#{method.to_s}' for \"#{@target}\":#{@target.class.to_s}" raise NoMethodError, end if block_given? @target.send(method, *args) { |*block_args| yield(*block_args) } else @target.send(method, *args) end end end |
Instance Attribute Details
#owner ⇒ Object (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
66 67 68 69 70 71 |
# File 'lib/scout_api/metric_proxy.rb', line 66 def all( = nil) metric_name = [:name] if 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.
28 29 30 31 32 |
# File 'lib/scout_api/metric_proxy.rb', line 28 def average() @avg_calc.metric_name = () @avg_calc. = @avg_calc end |
#find_target ⇒ Object
: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_target ⇒ Object
: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
52 53 54 55 56 |
# File 'lib/scout_api/metric_proxy.rb', line 52 def maximum() @max_calc.metric_name = () @max_calc. = @max_calc end |
#minimum(options) ⇒ Hash Also known as: min
40 41 42 43 44 |
# File 'lib/scout_api/metric_proxy.rb', line 40 def minimum() @min_calc.metric_name = () @min_calc. = @min_calc end |