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,
:hide_gauge_explorer, :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.



10
11
12
13
14
15
16
17
18
19
20
21
22
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



115
116
117
118
119
# 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

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

#announce(event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



67
68
69
70
71
72
73
74
# 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) ⇒ Object



76
77
78
79
# 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) ⇒ Object



81
82
83
84
# 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) ⇒ Object



167
168
169
170
171
172
173
# 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 = {}) ⇒ Object



109
110
111
112
113
# 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) ⇒ Object



193
194
195
196
197
198
199
# 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) ⇒ Object



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

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

#load_gaugesObject



180
181
182
183
184
185
186
187
# 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

#opt_dashboard(dashboard, opts) ⇒ Object



163
164
165
# File 'lib/fnordmetric/namespace.rb', line 163

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

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



137
138
139
140
141
142
# 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 = {}) ⇒ Object



144
145
146
147
148
149
150
# 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_usersObject



121
122
123
# File 'lib/fnordmetric/namespace.rb', line 121

def opt_hide_active_users
  @flags[:hide_active_users] = true
end

#opt_hide_gauge_explorerObject



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

def opt_hide_gauge_explorer
  @flags[:hide_gauge_explorer] = true
end

#opt_hide_overviewObject



129
130
131
# File 'lib/fnordmetric/namespace.rb', line 129

def opt_hide_overview
  @flags[:hide_overview] = true
end

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



152
153
154
155
156
# 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) ⇒ Object



133
134
135
# File 'lib/fnordmetric/namespace.rb', line 133

def opt_set_title(title)
  @title = title
end

#opt_widget(dashboard, widget) ⇒ Object



158
159
160
161
# 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) ⇒ Object



24
25
26
27
28
29
# 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 = {}) ⇒ Object



105
106
107
# 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) ⇒ Object



175
176
177
178
# 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_redisObject



189
190
191
# File 'lib/fnordmetric/namespace.rb', line 189

def sync_redis
  @sync_redis || @redis
end

#titleObject



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

def title
  @title
end

#to_jsonObject



201
202
203
204
205
206
# File 'lib/fnordmetric/namespace.rb', line 201

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

#tokenObject



91
92
93
# File 'lib/fnordmetric/namespace.rb', line 91

def token
  @key
end