Permalink
Browse files

Adding a generator object for fakelia.

Prefer an object to a module include.
  • Loading branch information...
1 parent 38e40d4 commit 8eb6c6fcfa03eddc335040647048390e634cd109 @seryl seryl committed Dec 6, 2011
Showing with 95 additions and 48 deletions.
  1. +7 −5 examples/rand_example.rb
  2. +27 −27 examples/slope_example.rb
  3. +16 −11 lib/fakelia/application.rb
  4. +5 −5 lib/fakelia/client.rb
  5. +40 −0 lib/fakelia/generator.rb
View
@@ -1,14 +1,16 @@
module Fakelia::Scripts
- module RandExample
+ class RandExample
include Fakelia::Client
- graph "rand_graph" do
- units 'req/min'
- type 'uint8'
+ def initialize
+ graph "rand_graph" do
+ units 'req/min'
+ type 'uint8'
+ end
end
# Send random data.
- def self.update
+ def update
rand(150)
end
end
View
@@ -1,39 +1,39 @@
module Fakelia::Scripts
- module SlopeExample
+ class SlopeExample
include Fakelia::Client
- graph "slope_example" do
- units 'req/min'
- type 'uint8'
+ def initialize
+ graph "slope_example" do
+ units 'req/min'
+ type 'uint8'
+ end
end
# Generates a nicely sloped curve.
- class << self
- def update
- if @growth.nil?
- @growth = true
- end
- @last_value ||= 5
- check_slope
- next_value
+ def update
+ if @growth.nil?
+ @growth = true
end
-
- def check_slope
- if @last_value < 4
- @growth = true
- elsif @last_value > 40
- @growth = false
- end
+ @last_value ||= 5
+ check_slope
+ next_value
+ end
+
+ def check_slope
+ if @last_value < 4
+ @growth = true
+ elsif @last_value > 40
+ @growth = false
end
-
- def next_value
- if @growth
- @last_value += 4
- else
- @last_value -= 4
- end
- @last_value
+ end
+
+ def next_value
+ if @growth
+ @last_value += 4
+ else
+ @last_value -= 4
end
+ @last_value
end
end
end
View
@@ -1,5 +1,5 @@
module Fakelia
- # The
+ # The generic fakelia app to run every module in your scripts folder.
class Application
# The default configuration parameters
DEFAULT_CONFIGURATION = {
@@ -49,13 +49,18 @@ def start_event_loop
setup_script_modules
EM.run do
+ @config.merge ({:spoof => @spoof})
+ @class_objects ||= {}
+
EM.add_periodic_timer(@config[:interval]) do
- @script_modules.each do |m|
- @config.merge ({:spoof => @spoof})
- Fakelia::Scripts.const_get(m).send(:update_ganglia,
- @config.select { |k, v|
- [:hostname, :group, :gmond_host, :gmond_port, :spoof].include? k
- })
+ @script_classes.each do |cls|
+ unless @class_objects.has_key? cls.name
+ @class_objects[cls] = cls.new(@config)
+ end
+
+ @class_objects[cls].update_ganglia(@config.select { |k, v|
+ [:hostname, :group, :gmond_host, :gmond_port, :spoof].include? k
+ })
end
end
end
@@ -65,7 +70,7 @@ def start_event_loop
#
# @example
# module Fakelia::Scripts
- # module Example
+ # class Example
# include Fakelia::Client
#
# graph "example graph" do
@@ -96,9 +101,9 @@ def setup_script_modules
require scr
end
- @script_modules = Fakelia::Scripts.constants.select { |c|
- Fakelia::Scripts.const_get(c).class == Module }
- @script_modules
+ @script_classes = Fakelia::Scripts.constants.select { |c|
+ Fakelia::Scripts.const_get(c).class == Class }
+ @script_classes
end
end
end
View
@@ -3,8 +3,8 @@ module Fakelia
module Client
extend ActiveSupport::Concern
- # Class methods for the default Fakelia::Client.
- module ClassMethods
+ # Instance methods for the default Fakelia::Client.
+ module InstanceMethods
# The units string on the graph.
#
# @param [ String ] str The units displayed on the graph.
@@ -70,7 +70,7 @@ def slope(str='both')
# @return The graph options.
def graph(name, &block)
raise "Graph requires a name as the first parameter" if name.nil?
- self.class_eval &block
+ instance_eval &block
@graph_options = {}
@graph_options.merge!({
@@ -90,8 +90,8 @@ def graph(name, &block)
#
# @params [ Hash ] options The options passed from the Fakelia server.
def update_ganglia(options = {})
- if self.methods.grep /update/
- self.send_to_ganglia(self.update, options)
+ if methods.grep /update/
+ send_to_ganglia(update, options)
else
raise Exception, "The update method for #{self} requires the :update method to be defined."
end
View
@@ -0,0 +1,40 @@
+$:.unshift File.join(File.dirname(__FILE__), '..')
+require 'fakelia'
+
+module Fakelia
+ #
+ class Generator
+ include Fakelia::Client
+
+ # Creates a new Fakelia Generator
+ #
+ # @example
+ # {
+ # :name => "rand_graph",
+ # :units => "req/min",
+ # :type => "uint8"
+ # }
+ #
+ # @param [ Hash ] config The graph configuration
+ def initialize(host, port, config)
+ graph config[:name] do
+ generate_graph_from config
+ units 'req/min'
+ type 'uint8'
+ end
+
+ require 'pp'
+ pp @graph_options
+ end
+
+ def generate_graph_from(config)
+ graph_opts = config.dup
+ graph_opts.delete(:name)
+ graph_opts.each_pair do |key, value|
+ send(key, value)
+ end
+ end
+ end
+end
+
+Fakelia::Generator.new({:name => "awesome", :units => "cool"})

0 comments on commit 8eb6c6f

Please sign in to comment.