Commit abefad8b authored by Ross Kaffenberger's avatar Ross Kaffenberger
Browse files

Fnordmetric.options method to merge default, server_configuration and given opts

parent 23970a7c
Loading
Loading
Loading
Loading
+21 −25
Original line number Diff line number Diff line
@@ -21,36 +21,32 @@ module FnordMetric
    @@server_configuration = configuration
  end

  def self.default_options(opts)

    opts[:redis_url] ||= "redis://localhost:6379"
    opts[:redis_prefix] ||= "fnordmetric"

    opts[:inbound_stream] ||= ["0.0.0.0", "1337"]
    opts[:web_interface] ||= ["0.0.0.0", "4242"]
    opts[:web_interface_server] ||= "thin"

    opts[:start_worker] ||= true
    opts[:print_stats] ||= 3

    # events that aren't processed after 2 min get dropped
    opts[:event_queue_ttl] ||= 120

    # event data is kept for one month
    opts[:event_data_ttl] ||= 3600*24*30

    # session data is kept for one month
    opts[:session_data_ttl] ||= 3600*24*30

    opts
  end

  def self.start_em(opts)
  def self.default_options(opts = {})
    {
      :redis_url => "redis://localhost:6379",
      :redis_prefix => "fnordmetric",
      :inbound_stream => ["0.0.0.0", "1337"],
      :web_interface => ["0.0.0.0", "4242"],
      :web_interface_server => "thin",
      :start_worker => true,
      :print_stats => 3,
      :event_queue_ttl => 120,
      :event_data_ttl => 3600*24*30,
      :session_data_ttl => 3600*24*30
    }.merge(opts)
  end

  def self.options(opts = {})
    default_options(@@server_configuration || {}).merge(opts)
  end

  def self.start_em(opts = {})
    EM.run do

      trap("TERM", &method(:shutdown))
      trap("INT",  &method(:shutdown))

      opts = options(opts)
      app = embedded(opts)

      if opts[:web_interface]
@@ -113,7 +109,7 @@ module FnordMetric
  # `:inbound_stream` starts the TCP interface
  # `:print_stats`    periodicaly prints worker stats
  def self.embedded(opts={})
    opts = default_options(opts)
    opts = options(opts)
    app  = nil

    if opts[:rack_app] or opts[:web_interface]
+11 −10
Original line number Diff line number Diff line
require 'securerandom'
class FnordMetric::API
  @@opts = nil

  def initialize opts
    @@opts = FnordMetric.default_options(opts)
    connect
+56 −0
Original line number Diff line number Diff line
require 'spec_helper'

describe FnordMetric do
  after { FnordMetric.server_configuration = nil }

  it "has default_options" do
    opts = FnordMetric.default_options
    opts[:redis_url].should eq("redis://localhost:6379")
    opts[:redis_prefix].should eq("fnordmetric")
    opts[:inbound_stream].should eq(["0.0.0.0", "1337"])
    opts[:web_interface].should eq(["0.0.0.0", "4242"])
    opts[:web_interface_server].should eq("thin")
    opts[:start_worker].should be_true
    opts[:print_stats].should == 3
    opts[:event_queue_ttl].should == 120
    opts[:event_data_ttl].should == 3600*24*30
    opts[:session_data_ttl].should == 3600*24*30
  end

  it "default_options can be overriden" do
    opts = FnordMetric.default_options({
      :redis_url => "redis://example.com:6379",
      :redis_prefix => "foobar",
      :inbound_stream => ["0.0.0.0", "7331"],
      :web_interface => ["0.0.0.0", "2424"],
      :web_interface_server => "hatetepe",
      :start_worker => false,
      :print_stats => false,
      :event_queue_ttl => 10,
      :event_data_ttl => 20,
      :session_data_ttl => 30,
    })
    opts[:redis_url].should eq("redis://example.com:6379")
    opts[:redis_prefix].should eq("foobar")
    opts[:inbound_stream].should eq(["0.0.0.0", "7331"])
    opts[:web_interface].should eq(["0.0.0.0", "2424"])
    opts[:web_interface_server].should eq("hatetepe")
    opts[:start_worker].should be_false
    opts[:print_stats].should be_false
    opts[:event_queue_ttl].should == 10
    opts[:event_data_ttl].should == 20
    opts[:session_data_ttl].should == 30
  end

  it "merges default, server configuration, and given opts as options" do
    FnordMetric.server_configuration = { :web_interface_server => "hatetepe", :print_stats => true }
    opts = FnordMetric.options(:redis_url => "redis://example.com:6379", :print_stats => false)

    opts[:redis_url].should eq("redis://example.com:6379")
    opts[:redis_prefix].should eq("fnordmetric")
    opts[:web_interface_server].should eq("hatetepe")
    opts[:print_stats].should be_false
  end


end
 No newline at end of file