Class: Bluecap::Cohort
- Inherits:
-
Object
- Object
- Bluecap::Cohort
- Defined in:
- lib/bluecap/cohort.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#initial_event ⇒ Object
readonly
Returns the value of attribute initial_event.
-
#report_id ⇒ Object
readonly
Returns the value of attribute report_id.
Instance Method Summary collapse
-
#date_str ⇒ Object
Convert the date of the cohort to a string.
-
#id ⇒ Object
An identifier for the cohort.
-
#initialize(options) ⇒ Cohort
constructor
Initialize a Cohort.
-
#key ⇒ Object
A Redis key containing a bitmask of all the properties for this cohort.
-
#keys ⇒ Object
Generate an array of Redis keys for use in creating a bitmask of the cohort.
-
#keys_for_attributes ⇒ Object
Generate an array of Redis keys where the attributes of users in the cohort can be found.
-
#total ⇒ Object
Calculate the total number of users in this cohort by doing a bitcount on the cohort bitmask.
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() @initial_event = .fetch(:initial_event) @attributes = .fetch(:attributes, Hash.new) @date = .fetch(:date) @report_id = .fetch(:report_id) end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
4 5 6 |
# File 'lib/bluecap/cohort.rb', line 4 def attributes @attributes end |
#date ⇒ Object (readonly)
Returns the value of attribute date.
4 5 6 |
# File 'lib/bluecap/cohort.rb', line 4 def date @date end |
#initial_event ⇒ Object (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_id ⇒ Object (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_str ⇒ Object
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 |
#id ⇒ Object
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 |
#key ⇒ Object
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 |
#keys ⇒ Object
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_attributes ⇒ Object
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 |
#total ⇒ Object
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 |