Skip to content

Commit

Permalink
specs for metric methods, ability to retrieve metrics from a descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
jnewland committed Feb 5, 2010
1 parent aa55912 commit d2b48f1
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/scout_scout/alert.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ScoutScout::Alert < Hashie::Mash
attr_writer :client

# The Scout client that generated this alert
#
# @return [ScoutScout::Client]
Expand Down
17 changes: 12 additions & 5 deletions lib/scout_scout/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,38 +32,45 @@ def self.all(hostname)
#
# @return [Array] An array of ScoutScout::Alert objects
def active_alerts
@active_alerts ||= @alert_hash.map { |a| ScoutScout::Alert.new(a) }
@active_alerts ||= @alert_hash.map { |a| decorate_with_client(ScoutScout::Alert.new(a)) }
end

# Recent alerts for this client
#
# @return [Array] An array of ScoutScout::Alert objects
def alerts
response = ScoutScout.get("/#{ScoutScout.account}/clients/#{self.id}/activities.xml")
response['alerts'].map { |alert| ScoutScout::Alert.new(alert) }
response['alerts'].map { |alert| decorate_with_client(ScoutScout::Alert.new(alert)) }
end

# Details about all plugins for this client
#
# @return [Array] An array of ScoutScout::Plugin objects
def plugins
response = ScoutScout.get("/#{ScoutScout.account}/clients/#{self.id}/plugins.xml")
response['plugins'].map { |plugin| ScoutScout::Plugin.new(plugin) }
response['plugins'].map { |plugin| decorate_with_client(ScoutScout::Plugin.new(plugin)) }
end

# Details about a specific plugin
#
# @return [ScoutScout::Plugin]
def plugin(id)
response = ScoutScout.get("/#{ScoutScout.account}/clients/#{self.id}/plugins/#{id}.xml")
ScoutScout::Plugin.new(response['plugin'])
decorate_with_client(ScoutScout::Plugin.new(response['plugin']))
end

# All descriptors for this client
#
# @return [Array] An array of ScoutScout::Descriptor objects
def descriptors
ScoutScout::Descriptor.all(:host => hostname)
ScoutScout::Descriptor.all(:host => hostname).map { |d| decorate_with_client(d) }
end

protected

def decorate_with_client(hashie)
hashie.client = self
hashie
end

end
26 changes: 26 additions & 0 deletions lib/scout_scout/descriptor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ScoutScout::Descriptor < Hashie::Mash
attr_accessor :client, :plugin

# Search for descriptors by matching name and hostname.
#
# Options:
Expand All @@ -11,4 +13,28 @@ def self.all(options = {})
response = ScoutScout.get("/#{ScoutScout.account}/descriptors.xml?descriptor=#{CGI.escape(options[:descriptor] || String.new)}&host=#{options[:host]}")
response['ar_descriptors'].map { |descriptor| ScoutScout::Descriptor.new(descriptor) }
end

# @return [ScoutScout::Metric]
def average(opts = {})
ScoutScout::Cluster.average(name, options_for_relationship(opts))
end

# @return [ScoutScout::Metric]
def maximum(opts = {})
ScoutScout::Cluster.maximum(name, options_for_relationship(opts))
end

# @return [ScoutScout::Metric]
def maximum(opts = {})
ScoutScout::Cluster.maximum(name, options_for_relationship(opts))
end

protected

def options_for_relationship(opts = {})
relationship_options = {}
relationship_options[:host] = client.hostname if client
opts.merge(relationship_options)
end

end
13 changes: 12 additions & 1 deletion lib/scout_scout/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class ScoutScout::Plugin < Hashie::Mash
attr_accessor :client

def initialize(hash)
if hash['descriptors'] && hash['descriptors']['descriptor']
@descriptor_hash = hash['descriptors']['descriptor']
Expand All @@ -11,6 +13,15 @@ def initialize(hash)
#
# @return [Array] An array of ScoutScout::Descriptor objects
def descriptors
@descriptors ||= @descriptor_hash.map { |d| ScoutScout::Descriptor.new(d) }
@descriptors ||= @descriptor_hash.map { |d| decorate_with_client_and_plugin(ScoutScout::Descriptor.new(d)) }
end

protected

def decorate_with_client_and_plugin(hashie)
hashie.client = self.client
hashie.plugin = self
hashie
end

end
5 changes: 5 additions & 0 deletions spec/fixtures/data.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<data>
<value>31.10</value>
<label>Cpu last minute</label>
<units></units>
</data>
40 changes: 40 additions & 0 deletions spec/scout_scout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@
end
end
end
describe 'descriptors' do
before(:each) do
@scout_scout.stub_get('descriptors.xml?descriptor=&host=&','descriptors.xml')
@descriptors = ScoutScout::Descriptor.all
end
it "should be an array ScoutScout::Descriptor objects" do
@descriptors.first.class.should == ScoutScout::Descriptor
end
it "should be accessable" do
@descriptors.size.should == 30
end
end
describe 'descriptor metrics' do
before(:each) do
@scout_scout.stub_get('data/value?descriptor=cpu_last_minute&function=AVG&consolidate=SUM&host=&start=&end=&','data.xml')
@metric = ScoutScout::Cluster.average('cpu_last_minute')
end
it "should be a ScoutScout::Metric object" do
@metric.class.should == ScoutScout::Metric
end
it "should contain the value" do
@metric.value.should == '31.10'
end
end
end
describe 'individual clients' do
describe 'should be accessable' do
Expand Down Expand Up @@ -145,5 +169,21 @@
@descriptors.first.class.should == ScoutScout::Descriptor
end
end
describe 'descriptor metrics' do
before(:each) do
@scout_scout.stub_get('clients/13431.xml', 'client.xml')
@client = ScoutScout::Client.first(13431)
@scout_scout.stub_get('clients/13431/plugins.xml', 'plugins.xml')
@plugins = @client.plugins
@scout_scout.stub_get('data/value?descriptor=passenger_process_active&function=AVG&consolidate=SUM&host=foobar.com&start=&end=&','data.xml')
@metric = @plugins.first.descriptors.first.average
end
it "should be a ScoutScout::Metric object" do
@metric.class.should == ScoutScout::Metric
end
it "should contain the value" do
@metric.value.should == '31.10'
end
end
end
end

0 comments on commit d2b48f1

Please sign in to comment.