Class: Boffin::Tracker
- Inherits:
-
Object
- Object
- Boffin::Tracker
- Defined in:
- lib/boffin/tracker.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#hit_types ⇒ Object
Returns the value of attribute hit_types.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
- #hit(hit_type, instance, uniquenesses = []) ⇒ Hit
- #hit_count(hit_type, instance) ⇒ Fixnum
- #hit_count_for_session_id(hit_type, instance, sess_obj) ⇒ Fixnum
-
#initialize(class_or_ns, hit_types = [], config = Boffin.config.dup) ⇒ Tracker
constructor
A new instance of Tracker.
-
#keyspace(uniq = false) ⇒ Keyspace
Keyspace associated with this tracker.
-
#redis ⇒ Redis
The Redis connection for this Tracker's config.
-
#top(type_or_weights, opts = {}) ⇒ Object
Performs set union across the specified number of hours, days, or months to calculate the members with the highest hit counts.
- #uhit_count(hit_type, instance) ⇒ Fixnum
Constructor Details
#initialize(class_or_ns, hit_types = [], config = Boffin.config.dup) ⇒ Tracker
Returns a new instance of Tracker.
18 19 20 21 22 23 24 |
# File 'lib/boffin/tracker.rb', line 18 def initialize(class_or_ns, hit_types = [], config = Boffin.config.dup) @namespace = Utils.object_as_namespace(class_or_ns) @hit_types = hit_types @config = config @keyspace = Keyspace.new(self) @ukeyspace = Keyspace.new(self, true) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
4 5 6 |
# File 'lib/boffin/tracker.rb', line 4 def config @config end |
#hit_types ⇒ Object
Returns the value of attribute hit_types.
5 6 7 |
# File 'lib/boffin/tracker.rb', line 5 def hit_types @hit_types end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
4 5 6 |
# File 'lib/boffin/tracker.rb', line 4 def namespace @namespace end |
Instance Method Details
#hit(hit_type, instance, uniquenesses = []) ⇒ Hit
33 34 35 36 |
# File 'lib/boffin/tracker.rb', line 33 def hit(hit_type, instance, uniquenesses = []) validate_hit_type(hit_type) Hit.new(self, hit_type, instance, uniquenesses) end |
#hit_count(hit_type, instance) ⇒ Fixnum
44 45 46 47 |
# File 'lib/boffin/tracker.rb', line 44 def hit_count(hit_type, instance) validate_hit_type(hit_type) redis.get(keyspace.hit_count(hit_type, instance)).to_i end |
#hit_count_for_session_id(hit_type, instance, sess_obj) ⇒ Fixnum
67 68 69 70 71 |
# File 'lib/boffin/tracker.rb', line 67 def hit_count_for_session_id(hit_type, instance, sess_obj) validate_hit_type(hit_type) sessid = Utils.object_as_session_identifier(sess_obj) redis.zscore(keyspace.hits(hit_type, instance), sessid).to_i end |
#keyspace(uniq = false) ⇒ Keyspace
Returns Keyspace associated with this tracker.
120 121 122 |
# File 'lib/boffin/tracker.rb', line 120 def keyspace(uniq = false) uniq ? @ukeyspace : @keyspace end |
#redis ⇒ Redis
Returns The Redis connection for this Tracker's config.
125 126 127 |
# File 'lib/boffin/tracker.rb', line 125 def redis @config.redis end |
#top(type_or_weights, opts = {}) ⇒ Object
The result set returned is cached in Redis for the duration of Config#cache_expire_secs
Only one of :hours
, :days
, or :months
should be specified in the
options hash as they can not be combined.
Performs set union across the specified number of hours, days, or months to calculate the members with the highest hit counts. The operation can be performed on one hit type, or multiple hit types with weights.
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/boffin/tracker.rb', line 105 def top(type_or_weights, opts = {}) validate_hit_type(type_or_weights) unit, size = *Utils.extract_time_unit(opts) keyspace = keyspace(opts[:unique]) if type_or_weights.is_a?(Hash) multiunion(keyspace, type_or_weights, unit, size, opts) else union(keyspace, type_or_weights, unit, size, opts) end end |
#uhit_count(hit_type, instance) ⇒ Fixnum
55 56 57 58 |
# File 'lib/boffin/tracker.rb', line 55 def uhit_count(hit_type, instance) validate_hit_type(hit_type) redis.zcard(keyspace.hits(hit_type, instance)).to_i end |