Class: Boffin::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/boffin/tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_or_ns, hit_types = [], config = Boffin.config.dup) ⇒ Tracker

Returns a new instance of Tracker.

Examples:

Tracker.new(MyModel, [:views, likes])
Tracker.new(:urls,   [:shares, :clicks])

Parameters:

  • class_or_ns (String, Symbol, #to_s)

    A string, symbol or any object that responds to #to_s that will be used to namespace this keys of this Tracker.

  • hit_types (Array<Symbol>) (defaults to: [])

    A list of hit types that this Tracker will allow, if empty then any hit type will be allowed.

  • config (Config) (defaults to: Boffin.config.dup)

    A Config instance to use instead of Boffin.config



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

#configObject (readonly)

Returns the value of attribute config.



4
5
6
# File 'lib/boffin/tracker.rb', line 4

def config
  @config
end

#hit_typesObject

Returns the value of attribute hit_types.



5
6
7
# File 'lib/boffin/tracker.rb', line 5

def hit_types
  @hit_types
end

#namespaceObject (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

Parameters:

  • hit_type (Symbol)

    Type of hit.

  • instance (#as_member, #id, #to_s)

    Object to track.

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :unique (true, #as_member, #id, #to_s) — default: false

    If true will return a count of unique hits. If passed an object, will use that object as a unique identifier and return the score associated with it.

Returns:

  • (Float)

Raises:

  • Boffin::UndefinedHitTypeError Raised if a list of hit types is available and the provided hit type is not in the list.



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

Parameters:

  • hit_type (Symbol)
  • instance (#as_member, #id, #to_s)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :unique (Array) — default: []

    uniquenesses

  • :increment (Fixnum) — default: 1

    hit increment

Returns:

Raises:

  • Boffin::UndefinedHitTypeError Raised if a list of hit types is available and the provided hit type is not in the list.



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.

Parameters:

  • uniq (true, false) (defaults to: false)

    If true the unique-scoped keyspace is returned

Returns:

  • (Keyspace)

    Keyspace associated with this tracker



116
117
118
# File 'lib/boffin/tracker.rb', line 116

def keyspace(uniq = false)
  uniq ? @ukeyspace : @keyspace
end

#redisRedis

Returns The Redis connection for this Tracker's config.

Returns:

  • (Redis)

    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

Note:

The result set returned is cached in Redis for the duration of Config#cache_expire_secs

Note:

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.

Examples:

Return IDs of most viewed and liked listings in the past 6 days with scores

@tracker.top({ views: 1, likes: 1 }, counts: true, days: 6)

Return IDS of most viewed and liked listings in the past 6 days with scores (Alternate syntax)

@tracker.top([[:views, 1], [:likes, 1]], counts: true, days: 6)

Return IDs of most viewed listings in the past 12 hours

@tracker.top(:views, hours: 12)

Parameters:

  • type_or_weights (Symbol, Hash)

    When Hash the set union is calculated

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :unique (true, false) — default: false

    If true then only unique hits are considered in the calculation

  • :counts (true, false) — default: false

    If true then scores are returned along with the top members

  • :order (:desc, :asc) — default: :desc

    The order of the results, in decending (most hits to least hits) or ascending (least hits to most hits) order.

  • :hours (Fixnum)

    Perform union for hit counts over the last n hours.

  • :days (Fixnum)

    Perform union for hit counts over the last n days.

  • :months (Fixnum)

    Perform union for hit counts over the last n months.

Raises:

  • Boffin::UndefinedHitTypeError If a list of hit types is available and any of the provided hit types is not in the list.



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