Class: FnordMetric::Event

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_id, opts) ⇒ Event

Returns a new instance of Event.



57
58
59
60
# File 'lib/fnordmetric/web/event.rb', line 57

def initialize(event_id, opts)
  @opts = opts
  @event_id = event_id
end

Instance Attribute Details

#event_idObject

Returns the value of attribute event_id.



4
5
6
# File 'lib/fnordmetric/web/event.rb', line 4

def event_id
  @event_id
end

#timeObject

Returns the value of attribute time.



4
5
6
# File 'lib/fnordmetric/web/event.rb', line 4

def time
  @time
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/fnordmetric/web/event.rb', line 4

def type
  @type
end

Class Method Details

.all(opts) ⇒ Object

def self.track!(event_type, event_data) end



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/fnordmetric/web/event.rb', line 9

def self.all(opts)
  opts[:limit] ||= 100

  range_opts = { :withscores => true }
  range_opts.merge!(:limit => [0,opts[:limit]]) if opts[:limit]

  events = opts[:redis].zrevrangebyscore(
    "#{opts[:namespace_prefix]}-timeline", 
    '+inf', opts[:since]||'0',
    range_opts
  )

  events = events[0..opts[:limit] - 1]

  unless events.first.is_a?(Array)
    events = events.in_groups_of(2).map do |event_id, ts|
      [event_id, Float(ts)]
    end
  end

  events.map do |event_id, ts|
    next if event_id.blank?
    find(event_id, opts).tap{ |e| e.time = "%.f" % ts }
  end
end

.by_session_key(_session_key, opts) ⇒ Object



44
45
46
47
48
49
# File 'lib/fnordmetric/web/event.rb', line 44

def self.by_session_key(_session_key, opts)
  session = Session.find(_session_key, opts)
  session.fetch_event_ids!(200).reject(&:blank?).map do |event_id|
    find(event_id, opts)
  end
end

.by_type(_type, opts) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/fnordmetric/web/event.rb', line 35

def self.by_type(_type, opts)
  opts[:redis].lrange(
    "#{opts[:namespace_prefix]}-type-#{_type}", 
    0, 200).map do |event_id|
    next if event_id.blank?
    find(event_id, opts).tap{ |e| }
  end
end

.find(event_id, opts) ⇒ Object



51
52
53
54
55
# File 'lib/fnordmetric/web/event.rb', line 51

def self.find(event_id, opts)
  self.new(event_id, opts).tap do |event|
    event.fetch!
  end
end

Instance Method Details

#data(key = nil) ⇒ Object Also known as: []



84
85
86
# File 'lib/fnordmetric/web/event.rb', line 84

def data(key=nil)
  key ? @data[key.to_s] : @data
end

#fetch!Object



62
63
64
65
66
# File 'lib/fnordmetric/web/event.rb', line 62

def fetch!
  @data = JSON.parse(fetch_json).tap do |event|
    @type = event.delete("_type")
  end
end

#fetch_jsonObject



68
69
70
# File 'lib/fnordmetric/web/event.rb', line 68

def fetch_json
  @opts[:redis].get(redis_key) || "{}"
end

#idObject



80
81
82
# File 'lib/fnordmetric/web/event.rb', line 80

def id
  @event_id
end

#redis_keyObject



72
73
74
# File 'lib/fnordmetric/web/event.rb', line 72

def redis_key
  [@opts[:redis_prefix], :event, @event_id].join("-")
end

#session_keyObject



76
77
78
# File 'lib/fnordmetric/web/event.rb', line 76

def session_key
  @data["_session"] ? Digest::MD5.hexdigest(@data["_session"]) : nil
end

#to_jsonObject



90
91
92
93
94
95
96
97
# File 'lib/fnordmetric/web/event.rb', line 90

def to_json
  @data.merge!(
    :_type => @type, 
    :_session_key => session_key,
    :_eid => @event_id,
    :_time => @time
  )
end