Class: FnordMetric::Session

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

Constant Summary collapse

@@meta_attributes =
%w(name picture)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_key, redis_opts = nil) ⇒ Session

Returns a new instance of Session.



42
43
44
45
# File 'lib/fnordmetric/session.rb', line 42

def initialize(session_key, redis_opts=nil)
  @session_key = session_key
  add_redis(*redis_opts) if redis_opts
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#pictureObject

Returns the value of attribute picture.



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

def picture
  @picture
end

#updated_atObject

Returns the value of attribute updated_at.



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

def updated_at
  @updated_at
end

Class Method Details

.all(opts) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fnordmetric/session.rb', line 26

def self.all(opts)    
  set_key = "#{opts[:namespace_prefix]}-session"
  limit = (opts[:limit].try(:to_i)||0)-1
  session_ids = opts[:redis].zrevrange(set_key, 0, limit, :withscores => true)

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

  session_ids.map do |session_key, ts|
    find(session_key, opts).tap{ |s| s.updated_at = "%.f" % ts }
  end
end

.create(opts) ⇒ Object



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

def self.create(opts)
  redis = opts.fetch(:redis)
  event = opts[:event]

  hash = Digest::MD5.hexdigest(event[:_session])
  set_key = "#{opts[:namespace_prefix]}-session"

  self.new(hash).tap do |session|
    session.add_redis(redis, set_key)
    session.add_event(event)
    session.expire(opts[:session_data_ttl])
  end
end

.find(session_key, opts) ⇒ Object



21
22
23
24
# File 'lib/fnordmetric/session.rb', line 21

def self.find(session_key, opts)
  set_key = "#{opts[:namespace_prefix]}-session"
  self.new(session_key, [opts[:redis], set_key])
end

Instance Method Details

#add_data(key, value) ⇒ Object



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

def add_data(key, value)    
  @redis.hset(redis_key(:data), key, value)
end

#add_event(event) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/fnordmetric/session.rb', line 97

def add_event(event)    
  @redis.zadd(redis_key(:events), event[:_time], event[:_eid])    

  add_data(:_picture, event[:url]) if event[:_type] == "_set_picture"    
  add_data(:_name, event[:name]) if event[:_type] == "_set_name"    
  add_event_data(event) if event[:_type] == "_set_data" 
  touch(event[:_time])
end

#add_event_data(event) ⇒ Object



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

def add_event_data(event)
  event.each do |key, value|
    add_data(key, value) unless key.to_s.starts_with?("_")
  end
end

#add_redis(redis, prefix) ⇒ Object



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

def add_redis(redis, prefix)
  @redis_prefix = prefix
  @redis = redis
end

#data(key = nil) ⇒ Object



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

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

#event_idsObject



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

def event_ids
  @event_ids || []
end

#eventsObject



67
68
69
# File 'lib/fnordmetric/session.rb', line 67

def events
  [] 
end

#expire(time) ⇒ Object



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

def expire(time)
  @redis.expire(redis_key(:events), time)
  @redis.expire(redis_key(:data), time)
end

#fetch_data!Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/fnordmetric/session.rb', line 116

def fetch_data!
  @data = Hash.new
  @redis.hgetall(redis_key(:data)).each do |key, value|    
    if key.to_s.starts_with?("_")
      fetch_meta_key(key, value)
    else      
      @data[key.intern] = value 
    end
  end
end

#fetch_event_ids!(since = -1)) ⇒ Object



134
135
136
137
# File 'lib/fnordmetric/session.rb', line 134

def fetch_event_ids!(since=-1)
  # FIXME: use WITHSCORE to get the timestamps and return event objects
  @event_ids = @redis.zrevrange(redis_key(:events), 0, since)
end

#fetch_meta_key(key, value) ⇒ Object



127
128
129
130
131
132
# File 'lib/fnordmetric/session.rb', line 127

def fetch_meta_key(key, value)
  meta_key = key[1..-1]
  if @@meta_attributes.include?(meta_key)
    instance_variable_set(:"@#{meta_key}", value)
  end
end

#redis_key(append = nil) ⇒ Object



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

def redis_key(append=nil)
  [@redis_prefix, @session_key, append].compact.join("-")
end

#session_keyObject



47
48
49
# File 'lib/fnordmetric/session.rb', line 47

def session_key
  @session_key
end

#to_jsonObject



71
72
73
74
75
76
77
# File 'lib/fnordmetric/session.rb', line 71

def to_json
  { :session_key => session_key }.tap do |hash| 
    hash.merge!(:_picture => @picture) if @picture
    hash.merge!(:_name => @name) if @name
    hash.merge!(:_updated_at => @updated_at) if @updated_at
  end
end

#touch(time = Time.now.to_i) ⇒ Object



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

def touch(time=Time.now.to_i)      
  @redis.zadd(@redis_prefix, time, @session_key)
end