Class: FnordMetric::Namespace

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

Constant Summary collapse

@@opts =
[:event, :gauge, :widget, :set_title, :hide_active_users, :hide_overview, :dashboard]
@@multi_gauges =
[:timeseries_gauge, :toplist_gauge, :distribution_gauge]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, opts) ⇒ Namespace

Returns a new instance of Namespace.



8
9
10
11
12
13
14
15
16
17
# File 'lib/fnordmetric/namespace.rb', line 8

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



98
99
100
101
102
# File 'lib/fnordmetric/namespace.rb', line 98

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

Instance Attribute Details

#dashboards(name = nil, opts = {}) ⇒ Object (readonly)

Returns the value of attribute dashboards.



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

def dashboards
  @dashboards
end

#flagsObject (readonly)

Returns the value of attribute flags.



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

def flags
  @flags
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

#active_users_availableObject



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

def active_users_available
  @active_users_available
end

#announce(event) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/fnordmetric/namespace.rb', line 24

def announce(event)
  announce_to_timeline(event)
  announce_to_typelist(event)

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

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

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

  self
end

#announce_to_session(event) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/fnordmetric/namespace.rb', line 46

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



55
56
57
58
# File 'lib/fnordmetric/namespace.rb', line 55

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

#announce_to_typelist(event) ⇒ Object



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

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

#build_widget(opts) ⇒ Object



145
146
147
148
149
# File 'lib/fnordmetric/namespace.rb', line 145

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



92
93
94
95
96
# File 'lib/fnordmetric/namespace.rb', line 92

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



151
152
153
154
155
156
157
# File 'lib/fnordmetric/namespace.rb', line 151

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

#key_prefix(append = nil) ⇒ Object



66
67
68
# File 'lib/fnordmetric/namespace.rb', line 66

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

#opt_dashboard(dashboard, opts) ⇒ Object



141
142
143
# File 'lib/fnordmetric/namespace.rb', line 141

def opt_dashboard(dashboard, opts)
  dashboards(dashboard, opts)
end

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



116
117
118
119
120
121
122
# File 'lib/fnordmetric/namespace.rb', line 116

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



124
125
126
127
128
# File 'lib/fnordmetric/namespace.rb', line 124

def opt_gauge(gauge_key, opts={})
  opts.merge!(:key => gauge_key, :key_prefix => key_prefix)
  klass = "FnordMetric::#{(opts[:type] || "").to_s.camelize}Gauge".constantize
  @gauges[gauge_key] ||= klass.new(opts)
end

#opt_hide_active_usersObject



104
105
106
# File 'lib/fnordmetric/namespace.rb', line 104

def opt_hide_active_users
  @flags[:hide_active_users] = true
end

#opt_hide_overviewObject



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

def opt_hide_overview
  @flags[:hide_overview] = true
end

#opt_multigauge(gauge_type, gauge_key, opts = {}) ⇒ Object



130
131
132
133
134
# File 'lib/fnordmetric/namespace.rb', line 130

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



112
113
114
# File 'lib/fnordmetric/namespace.rb', line 112

def opt_set_title(title)
  @title = title
end

#opt_widget(dashboard, widget) ⇒ Object



136
137
138
139
# File 'lib/fnordmetric/namespace.rb', line 136

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

#ready!(redis) ⇒ Object



19
20
21
22
# File 'lib/fnordmetric/namespace.rb', line 19

def ready!(redis)
  @redis = redis
  self
end

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



88
89
90
# File 'lib/fnordmetric/namespace.rb', line 88

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

#titleObject



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

def title
  @title
end

#to_jsonObject



159
160
161
162
163
164
# File 'lib/fnordmetric/namespace.rb', line 159

def to_json
  flags.merge(
    :token => token,
    :title => title
  ).to_json
end

#tokenObject



70
71
72
# File 'lib/fnordmetric/namespace.rb', line 70

def token
  @key
end