Class: FnordMetric::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/fnordmetric/namespace.rb

Constant Summary collapse

@@opts =
[:event, :gauge, :widget]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts) ⇒ Namespace

Returns a new instance of Namespace.



7
8
9
10
11
12
13
# File 'lib/fnordmetric/namespace.rb', line 7

def initialize(key, opts)    
  @gauges = Hash.new
  @dashboards = Hash.new
  @handlers = Hash.new                 
  @opts = opts
  @key = key      
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



83
84
85
86
# File 'lib/fnordmetric/namespace.rb', line 83

def method_missing(m, *args, &block)
  raise "unknown option: #{m}" unless @@opts.include?(m)
  send(:"opt_#{m}", *args, &block)
end

Instance Attribute Details

#dashboards(name = nil) ⇒ Object (readonly)

Returns the value of attribute dashboards.



3
4
5
# File 'lib/fnordmetric/namespace.rb', line 3

def dashboards
  @dashboards
end

#gaugesObject (readonly)

Returns the value of attribute gauges.



3
4
5
# File 'lib/fnordmetric/namespace.rb', line 3

def gauges
  @gauges
end

#handlersObject (readonly)

Returns the value of attribute handlers.



3
4
5
# File 'lib/fnordmetric/namespace.rb', line 3

def handlers
  @handlers
end

#keyObject (readonly)

Returns the value of attribute key.



3
4
5
# File 'lib/fnordmetric/namespace.rb', line 3

def key
  @key
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/fnordmetric/namespace.rb', line 3

def opts
  @opts
end

Instance Method Details

#announce(event) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fnordmetric/namespace.rb', line 21

def announce(event)                  
  announce_to_timeline(event)
  announce_to_typelist(event)
  
  if event[:_session]
    event[:_session_key] = announce_to_session(event).session_key 
  end

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

  self
end

#announce_to_session(event) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/fnordmetric/namespace.rb', line 39

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) ⇒ Object



48
49
50
51
# File 'lib/fnordmetric/namespace.rb', line 48

def announce_to_timeline(event)
  timeline_key = key_prefix(:timeline)
  @redis.zadd(timeline_key, event[:_time], event[:_eid])
end

#announce_to_typelist(event) ⇒ Object



53
54
55
56
# File 'lib/fnordmetric/namespace.rb', line 53

def announce_to_typelist(event)
  typelist_key = key_prefix("type-#{event[:_type]}")
  @redis.lpush(typelist_key, event[:_eid])
end

#build_widget(opts) ⇒ Object



106
107
108
109
110
# File 'lib/fnordmetric/namespace.rb', line 106

def build_widget(opts)
  _gauges = [opts[:gauges]].flatten.map{ |g| @gauges.fetch(g) }
  widget_klass = "FnordMetric::#{opts.fetch(:type).to_s.capitalize}Widget"
  widget_klass.constantize.new(opts.merge(:gauges => _gauges))
end

#events(_ids, opts = {}) ⇒ Object



78
79
80
81
# File 'lib/fnordmetric/namespace.rb', line 78

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
end

#extend_opts(opts) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/fnordmetric/namespace.rb', line 112

def extend_opts(opts)
  opts.merge(
    :namespace_prefix => key_prefix,
    :redis_prefix => @opts[:redis_prefix],
    :redis => @redis
  )
end

#key_prefix(append = nil) ⇒ Object



59
60
61
# File 'lib/fnordmetric/namespace.rb', line 59

def key_prefix(append=nil)
  [@opts[:redis_prefix], @key, append].compact.join("-")
end

#opt_event(event_type, opts = {}, &block) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/fnordmetric/namespace.rb', line 88

def opt_event(event_type, opts={}, &block)    
  opts.merge!(:redis => @redis, :gauges => @gauges)   
  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 = {}) ⇒ Object



96
97
98
99
# File 'lib/fnordmetric/namespace.rb', line 96

def opt_gauge(gauge_key, opts={})
  opts.merge!(:key => gauge_key, :key_prefix => key_prefix)
  @gauges[gauge_key] ||= FnordMetric::Gauge.new(opts)   
end

#opt_widget(dashboard, widget) ⇒ Object



101
102
103
104
# File 'lib/fnordmetric/namespace.rb', line 101

def opt_widget(dashboard, widget)
  widget = build_widget(widget) if widget.is_a?(Hash)
  dashboards(dashboard).add_widget(widget)
end

#ready!(redis) ⇒ Object



15
16
17
18
19
# File 'lib/fnordmetric/namespace.rb', line 15

def ready!(redis)
  @redis = redis
  @gauges.map{ |k,g| g.add_redis(@redis) }
  self
end

#sessions(_ids, opts = {}) ⇒ Object



74
75
76
# File 'lib/fnordmetric/namespace.rb', line 74

def sessions(_ids, opts={})
  return FnordMetric::Session.all(extend_opts(opts)) if _ids == :all
end

#tokenObject



63
64
65
# File 'lib/fnordmetric/namespace.rb', line 63

def token
  @key
end