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
- #count(hit_type, instance, opts = {}) ⇒ Float
- #hit(hit_type, instance, opts = {}) ⇒ Hit
-
#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.
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
#count(hit_type, instance, opts = {}) ⇒ Float
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/boffin/tracker.rb', line 53 def count(hit_type, instance, opts = {}) validate_hit_type(hit_type) count = case when opts[:unique] == true redis.zcard(keyspace.hits(hit_type, instance)) when opts[:unique] uid = Utils.object_as_uid(opts[:unique]) redis.zscore(keyspace.hits(hit_type, instance), uid) else redis.get(keyspace.hit_count(hit_type, instance)) end (count && count.to_f) || 0.0 end |
#hit(hit_type, instance, opts = {}) ⇒ Hit
35 36 37 38 |
# File 'lib/boffin/tracker.rb', line 35 def hit(hit_type, instance, opts = {}) validate_hit_type(hit_type) Hit.new(self, hit_type, instance, opts) end |
#keyspace(uniq = false) ⇒ Keyspace
Returns Keyspace associated with this tracker.
116 117 118 |
# File 'lib/boffin/tracker.rb', line 116 def keyspace(uniq = false) uniq ? @ukeyspace : @keyspace end |
#redis ⇒ Redis
Returns The Redis connection for this Tracker's config.
121 122 123 |
# File 'lib/boffin/tracker.rb', line 121 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.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/boffin/tracker.rb', line 101 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 |