Class: Blackbeard::CohortMetric

Inherits:
Object
  • Object
show all
Includes:
Chartable, ConfigurationMethods
Defined in:
lib/blackbeard/cohort_metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Chartable

#recent_days, #recent_days_chart, #recent_hours, #recent_hours_chart

Methods included from ConfigurationMethods

#config, #db, #guest_method, included, #tz

Constructor Details

#initialize(cohort, metric) ⇒ CohortMetric

Returns a new instance of CohortMetric.



8
9
10
11
# File 'lib/blackbeard/cohort_metric.rb', line 8

def initialize(cohort, metric)
  @cohort = cohort
  @metric = metric
end

Instance Attribute Details

#cohortObject (readonly)

Returns the value of attribute cohort.



6
7
8
# File 'lib/blackbeard/cohort_metric.rb', line 6

def cohort
  @cohort
end

#metricObject (readonly)

Returns the value of attribute metric.



6
7
8
# File 'lib/blackbeard/cohort_metric.rb', line 6

def metric
  @metric
end

Instance Method Details

#add(context, amount) ⇒ Object



17
18
19
20
21
# File 'lib/blackbeard/cohort_metric.rb', line 17

def add(context, amount)
  uid = context.unique_identifier
  hour_id = cohort.hour_id_for_participant(uid)
  metric_data.add_at(hour_id, uid, amount) unless hour_id.nil?
end

#chartable_result_for_day(date) ⇒ Object



36
37
38
39
# File 'lib/blackbeard/cohort_metric.rb', line 36

def chartable_result_for_day(date)
  participants = cohort.data.participants_for_day(date)
  result_per_participant( metric_data.result_for_day(date), participants)
end

#chartable_result_for_hour(hour) ⇒ Object



31
32
33
34
# File 'lib/blackbeard/cohort_metric.rb', line 31

def chartable_result_for_hour(hour)
  participants = cohort.data.participants_for_hour(hour)
  result_per_participant( metric_data.result_for_hour(hour), participants)
end

#chartable_segmentsObject



27
28
29
# File 'lib/blackbeard/cohort_metric.rb', line 27

def chartable_segments
  metric_data.segments.map{|s| "avg #{s}" }
end

#metric_dataObject



23
24
25
# File 'lib/blackbeard/cohort_metric.rb', line 23

def metric_data
  @metric_data ||= MetricData.const_get(type.capitalize).new(metric, nil, cohort)
end

#result_per_participant(result, participants) ⇒ Object



41
42
43
44
45
# File 'lib/blackbeard/cohort_metric.rb', line 41

def result_per_participant(result, participants)
  participants = participants.to_f
  result.keys.each{|k| result["avg #{k}"] = result[k].to_f / participants }
  result
end

#typeObject



13
14
15
# File 'lib/blackbeard/cohort_metric.rb', line 13

def type
  metric.type
end