Class: Blackbeard::Metric

Inherits:
Storable show all
Includes:
Chartable
Defined in:
lib/blackbeard/metric.rb

Instance Attribute Summary collapse

Attributes inherited from Storable

#id, #new_record

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Chartable

#recent_days, #recent_days_chart, #recent_hours, #recent_hours_chart

Methods inherited from Storable

#==, all, all_keys, count, #key, key_for, master_key, #master_key, new_from_keys, #new_record?, on_reload, on_reload_methods, on_save, on_save_methods, #reload, #save, set_master_key

Methods included from StorableAttributes

included

Methods included from StorableHasSet

included

Methods included from StorableHasMany

included

Methods included from ConfigurationMethods

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

Constructor Details

#initialize(*args) ⇒ Metric

Returns a new instance of Metric.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blackbeard/metric.rb', line 31

def initialize(*args)
  if args.size == 1 && args[0] =~ /::/
    @type, @type_id = args[0].split(/::/)
  elsif args.size == 2
    @type = args[0]
    @type_id = args[1]
  else
    raise ArgumentError
  end
  super("#{@type}::#{@type_id}")
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

#type_idObject (readonly)

Returns the value of attribute type_id.



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

def type_id
  @type_id
end

Class Method Details

.create(type, type_id, options = {}) ⇒ Object



19
20
21
# File 'lib/blackbeard/metric.rb', line 19

def self.create(type, type_id, options = {})
  super("#{type}::#{type_id}", options)
end

.find(type, type_id) ⇒ Object



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

def self.find(type, type_id)
  super("#{type}::#{type_id}")
end

.find_or_create(type, type_id) ⇒ Object



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

def self.find_or_create(type, type_id)
  super("#{type}::#{type_id}")
end

.new_from_key(key) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/blackbeard/metric.rb', line 43

def self.new_from_key(key)
  if key =~ /^#{master_key}::(.+)::(.+)$/
    new($1,$2)
  else
    nil
  end
end

Instance Method Details

#add(context, amount) ⇒ Object



59
60
61
62
63
64
# File 'lib/blackbeard/metric.rb', line 59

def add(context, amount)
  uid = context.unique_identifier
  metric_data.add(uid, amount)
  group_metrics.each { |gm| gm.add(context, amount) }
  cohort_metrics.each { |cm| cm.add(context, amount) }
end

#addable_cohortsObject



78
79
80
# File 'lib/blackbeard/metric.rb', line 78

def addable_cohorts
  Cohort.all.reject{ |c| cohort_ids.include?(c.id) }
end

#addable_groupsObject



74
75
76
# File 'lib/blackbeard/metric.rb', line 74

def addable_groups
  Group.all.reject{ |g| group_ids.include?(g.id) }
end

#chartable_result_for_day(date) ⇒ Object



90
91
92
# File 'lib/blackbeard/metric.rb', line 90

def chartable_result_for_day(date)
  metric_data.result_for_day(date)
end

#chartable_result_for_hour(hour) ⇒ Object



86
87
88
# File 'lib/blackbeard/metric.rb', line 86

def chartable_result_for_hour(hour)
  metric_data.result_for_hour(hour)
end

#chartable_segmentsObject



82
83
84
# File 'lib/blackbeard/metric.rb', line 82

def chartable_segments
  metric_data.segments
end

#cohort_metricsObject



55
56
57
# File 'lib/blackbeard/metric.rb', line 55

def cohort_metrics
  cohorts.map{ |c| CohortMetric.new(c, self) }
end

#group_metricsObject



51
52
53
# File 'lib/blackbeard/metric.rb', line 51

def group_metrics
  groups.map{ |g| GroupMetric.new(g, self) }
end

#metric_dataObject



66
67
68
# File 'lib/blackbeard/metric.rb', line 66

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

#nameObject



70
71
72
# File 'lib/blackbeard/metric.rb', line 70

def name
  storable_attributes_hash['name'] || type_id
end