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

#hit(hit_type, instance, uniquenesses = []) ⇒ Hit

Parameters:

  • hit_type (Symbol)
  • instance (#as_member, #id, #to_s)
  • uniquenesses (Array) (defaults to: [])

Returns:

Raises:

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



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

Parameters:

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

Returns:

  • (Fixnum)

Raises:

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



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

Parameters:

  • hit_type (Symbol)
  • instance (#as_member, #id, #to_s)
  • sess_obj (#as_member, #id, #to_s)

Returns:

  • (Fixnum)

Raises:

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



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.

Parameters:

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

    If true the unique-scoped keyspace is returned

Returns:

  • (Keyspace)

    Keyspace associated with this tracker



120
121
122
# File 'lib/boffin/tracker.rb', line 120

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



125
126
127
# File 'lib/boffin/tracker.rb', line 125

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 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.



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

Parameters:

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

Returns:

  • (Fixnum)

Raises:

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



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