class FnordMetric::Namespace

Attributes

dashboards[R]
flags[R]
gauges[R]
handlers[R]
key[R]
opts[R]

Public Class Methods

new(key, opts) click to toggle source
# File lib/fnordmetric/namespace.rb, line 10
def initialize(key, opts)
  @gauges = Hash.new
  @dashboards = Hash.new
  @handlers = Hash.new.with_indifferent_access
  @title = key
  @opts = opts
  @key = key

  @flags = {
    :hide_active_users => (FnordMetric.options[:enable_active_users] == false),
    :hide_gauge_explorer => (FnordMetric.options[:enable_gauge_explorer] == false)
  }
end

Public Instance Methods

announce(event) click to toggle source
# File lib/fnordmetric/namespace.rb, line 31
def announce(event)
  if !@flags[:hide_active_users]
    announce_to_timeline(event)
    announce_to_typelist(event)
  end

  if event[:_session]
    event[:_session_key] = announce_to_session(event).session_key 
  end

  if event[:_type].to_sym == :_enterprise
    ctx = FnordMetric::Context.new(opts, FnordMetric::Enterprise::CompatibilityHandler)
    ctx.call(event, @redis, self)
    return self
  end

  if FnordMetric::ZeroConfigGauge::TYPES.include?(event[:_type].to_sym)
    ctx = FnordMetric::Context.new(opts, FnordMetric::ZeroConfigGauge::Handler)
    ctx.call(event, @redis, self)
    return self
  end

  res = [
    @handlers[event[:_type].to_s],
    @handlers["*"]
  ].flatten.compact.each do |context|
    context.call(event, @redis, self)
  end.size

  if res == 0
    FnordMetric.error("no handler for event-type: #{event[:_type]}")
  end

  self
end
announce_to_session(event) click to toggle source
# File lib/fnordmetric/namespace.rb, line 67
def announce_to_session(event)
  FnordMetric::Session.create(@opts.clone.merge(
    :namespace_key => @key,
    :namespace_prefix => key_prefix,
    :redis => @redis,
    :event => event
  ))
end
announce_to_timeline(event) click to toggle source
# File lib/fnordmetric/namespace.rb, line 76
def announce_to_timeline(event)
  timeline_key = key_prefix(:timeline)
  @redis.zadd(timeline_key, event[:_time], event[:_eid])
end
announce_to_typelist(event) click to toggle source
# File lib/fnordmetric/namespace.rb, line 81
def announce_to_typelist(event)
  typelist_key = key_prefix("type-#{event[:_type]}")
  @redis.lpush(typelist_key, event[:_eid])
end
build_widget(opts) click to toggle source
# File lib/fnordmetric/namespace.rb, line 167
def build_widget(opts)
  _gauges = [opts[:gauges]].flatten.map do |g|
    @gauges[g] || FnordMetric::ZeroConfigGauge.new(g, self)
  end
  widget_klass = "FnordMetric::#{opts.fetch(:type).to_s.capitalize}Widget"
  widget_klass.constantize.new(opts.merge(:gauges => _gauges))
end
events(_ids, opts={}) click to toggle source
# File lib/fnordmetric/namespace.rb, line 109
def events(_ids, opts={})
  return FnordMetric::Event.all(extend_opts(opts)) if _ids == :all
  return FnordMetric::Event.by_type(opts.delete(:type), extend_opts(opts)) if _ids == :by_type
  return FnordMetric::Event.by_session_key(opts.delete(:session_key), extend_opts(opts)) if _ids == :by_session_key
end
extend_opts(opts) click to toggle source
# File lib/fnordmetric/namespace.rb, line 193
def extend_opts(opts)
  opts.merge(
    :namespace_prefix => key_prefix,
    :redis_prefix => @opts[:redis_prefix],
    :redis => @redis
  )
end
key_prefix(append=nil) click to toggle source
# File lib/fnordmetric/namespace.rb, line 87
def key_prefix(append=nil)
  [@opts[:redis_prefix], @key, append].compact.join("-")
end
load_gauges() click to toggle source
# File lib/fnordmetric/namespace.rb, line 180
def load_gauges
  gaugelist_key = key_prefix("zero-config-gauges")
  sync_redis.hgetall(gaugelist_key).each do |gauge_key, gauge_opts|
    gopts = JSON.parse(gauge_opts).symbolize_keys
    gopts.delete(:zero_config)
    opt_gauge(gauge_key.to_sym, gopts)
  end
end
method_missing(m, *args, &block) click to toggle source
# File lib/fnordmetric/namespace.rb, line 115
def method_missing(m, *args, &block)
  return send(:opt_multigauge, *args.unshift(m), &block) if @@multi_gauges.include?(m)
  raise "unknown option: #{m}" unless @@opts.include?(m)
  send(:"opt_#{m}", *args, &block)
end
opt_dashboard(dashboard, opts) click to toggle source
# File lib/fnordmetric/namespace.rb, line 163
def opt_dashboard(dashboard, opts)
  dashboards(dashboard, opts)
end
opt_event(event_type, opts={}, &block) click to toggle source
# File lib/fnordmetric/namespace.rb, line 137
def opt_event(event_type, opts={}, &block)
  FnordMetric::Context.new(opts, block).tap do |context|
    @handlers[event_type.to_s] ||= []
    @handlers[event_type.to_s] << context
  end
end
opt_gauge(gauge_key, opts={}) click to toggle source
# File lib/fnordmetric/namespace.rb, line 144
def opt_gauge(gauge_key, opts={})
  opts.merge!(:key => gauge_key)
  store_gauge(gauge_key, opts) if opts[:zero_config]
  opts.merge!(:key_prefix => key_prefix)
  klass = "FnordMetric::#{(opts[:type] || "").to_s.camelize}Gauge".constantize
  @gauges[gauge_key] ||= klass.new(opts)
end
opt_hide_active_users() click to toggle source
# File lib/fnordmetric/namespace.rb, line 121
def opt_hide_active_users
  @flags[:hide_active_users] = true
end
opt_hide_gauge_explorer() click to toggle source
# File lib/fnordmetric/namespace.rb, line 125
def opt_hide_gauge_explorer
  @flags[:hide_gauge_explorer] = true
end
opt_hide_overview() click to toggle source
# File lib/fnordmetric/namespace.rb, line 129
def opt_hide_overview
  @flags[:hide_overview] = true
end
opt_multigauge(gauge_type, gauge_key, opts={}) click to toggle source
# File lib/fnordmetric/namespace.rb, line 152
def opt_multigauge(gauge_type, gauge_key, opts={})
  opts.merge!(:key => gauge_key, :key_prefix => key_prefix)
  klass = "FnordMetric::#{gauge_type.to_s.camelize}"
  @gauges[gauge_key] ||= klass.constantize.new(opts)   
end
opt_set_title(title) click to toggle source
# File lib/fnordmetric/namespace.rb, line 133
def opt_set_title(title)
  @title = title
end
opt_widget(dashboard, widget) click to toggle source
# File lib/fnordmetric/namespace.rb, line 158
def opt_widget(dashboard, widget)
  widget = build_widget(widget) if widget.is_a?(Hash)
  dashboards(dashboard).add_widget(widget)
end
ready!(redis, sync_redis = nil) click to toggle source
# File lib/fnordmetric/namespace.rb, line 24
def ready!(redis, sync_redis = nil)
  @redis = redis
  @sync_redis = sync_redis
  load_gauges
  self
end
sessions(_ids, opts={}) click to toggle source
# File lib/fnordmetric/namespace.rb, line 105
def sessions(_ids, opts={})
  return FnordMetric::Session.all(extend_opts(opts)) if _ids == :all
end
store_gauge(gauge_key, opts) click to toggle source
# File lib/fnordmetric/namespace.rb, line 175
def store_gauge(gauge_key, opts)
  gaugelist_key = key_prefix("zero-config-gauges")
  sync_redis.hset(gaugelist_key, gauge_key, opts.to_json)
end
sync_redis() click to toggle source
# File lib/fnordmetric/namespace.rb, line 189
def sync_redis
  @sync_redis || @redis
end
title() click to toggle source
# File lib/fnordmetric/namespace.rb, line 95
def title
  @title
end
to_json() click to toggle source
# File lib/fnordmetric/namespace.rb, line 201
def to_json
  flags.merge(
    :token => token,
    :title => title
  ).to_json
end
token() click to toggle source
# File lib/fnordmetric/namespace.rb, line 91
def token
  @key
end