Class: Bluecap::Cohort

Inherits:
Object
  • Object
show all
Defined in:
lib/bluecap/cohort.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Cohort

Initialize a Cohort.

options - A Hash containing options to define the cohort:

:initial_event - The String event that members of the cohort
                 shared.
:attributes    - The Hash attributes of cohort members.
:date          - The Date the initial event occurred on.
:report_id     - The Integer identifier of the report this
                 cohort is being used with.

Examples

Bluecap::Cohort.new :initial_event => 'Created Account',
  :attributes => {:country => 'Australia', :gender => 'Female'},
  :date => Date.parse('20120701'),
  :report_id => 1


22
23
24
25
26
27
# File 'lib/bluecap/cohort.rb', line 22

def initialize(options)
  @initial_event = options.fetch(:initial_event)
  @attributes = options.fetch(:attributes, Hash.new)
  @date = options.fetch(:date)
  @report_id = options.fetch(:report_id)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



4
5
6
# File 'lib/bluecap/cohort.rb', line 4

def attributes
  @attributes
end

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/bluecap/cohort.rb', line 4

def date
  @date
end

#initial_eventObject (readonly)

Returns the value of attribute initial_event.



4
5
6
# File 'lib/bluecap/cohort.rb', line 4

def initial_event
  @initial_event
end

#report_idObject (readonly)

Returns the value of attribute report_id.



4
5
6
# File 'lib/bluecap/cohort.rb', line 4

def report_id
  @report_id
end

Instance Method Details

#date_strObject

Convert the date of the cohort to a string.

Returns the String date.



32
33
34
# File 'lib/bluecap/cohort.rb', line 32

def date_str
  @date.strftime("%Y%m%d")
end

#idObject

An identifier for the cohort. The date of the initial event is currently unique for each cohort in a report, but that might change in the future if cohorts are not limited to the initial event occurring on a single day.

Returns the String id.



41
42
43
# File 'lib/bluecap/cohort.rb', line 41

def id
  date_str
end

#keyObject

A Redis key containing a bitmask of all the properties for this cohort. The method is memoized since the bitmask is cached in Redis for an hour.

Returns the String key name in Redis.



49
50
51
52
53
54
55
56
57
58
# File 'lib/bluecap/cohort.rb', line 49

def key
  return @key if @key

  key_name = Bluecap::Keys.cohort(@report_id, id)
  Bluecap.redis.multi do
    Bluecap.redis.bitop('and', key_name, keys)
    Bluecap.redis.expire(key_name, 3600)
  end
  @key = key_name
end

#keysObject

Generate an array of Redis keys for use in creating a bitmask of the cohort. This consists of the bitset of the users with the initial event along with all the attributes of the cohort.

Returns the Array of String keys.



74
75
76
77
78
# File 'lib/bluecap/cohort.rb', line 74

def keys
  keys_for_initial_event = Array.new
  keys_for_initial_event.push(Bluecap::Keys.event(@initial_event, date_str))
  keys_for_initial_event += keys_for_attributes
end

#keys_for_attributesObject

Generate an array of Redis keys where the attributes of users in the cohort can be found.

Returns the Array of String keys.



64
65
66
67
# File 'lib/bluecap/cohort.rb', line 64

def keys_for_attributes
  attributes = Bluecap::Attributes.new :id => 0, :attributes => @attributes
  attributes.keys
end

#totalObject

Calculate the total number of users in this cohort by doing a bitcount on the cohort bitmask. The method is memoized as the calculation is expensive.

Returns the Integer total.



85
86
87
88
89
# File 'lib/bluecap/cohort.rb', line 85

def total
  return @total if @total

  @total = Bluecap.redis.bitcount(key) || 0
end