Class: Blackbeard::Metric

Inherits:
Storable show all
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 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.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/blackbeard/metric.rb', line 24

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.



7
8
9
# File 'lib/blackbeard/metric.rb', line 7

def type
  @type
end

#type_idObject (readonly)

Returns the value of attribute type_id.



7
8
9
# File 'lib/blackbeard/metric.rb', line 7

def type_id
  @type_id
end

Class Method Details

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



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

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

.find(type, type_id) ⇒ Object



16
17
18
# File 'lib/blackbeard/metric.rb', line 16

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

.find_or_create(type, type_id) ⇒ Object



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

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

.new_from_key(key) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/blackbeard/metric.rb', line 36

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

Instance Method Details

#add(context, amount) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/blackbeard/metric.rb', line 52

def add(context, amount)
  uid = context.unique_identifier
  metric_data.add(uid, amount)
  groups.each do |group|
    segment = group.segment_for(context)
    metric_data(group).add(uid, amount, segment) unless segment.nil?
  end
end

#addable_groupsObject



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

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

#metric_data(group = nil) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/blackbeard/metric.rb', line 61

def metric_data(group = nil)
  @metric_data ||= {}
  @metric_data[group] ||= begin
    raise GroupNotInMetric unless group.nil? || has_group?(group)
    MetricData.const_get(type.capitalize).new(self, group)
  end
end

#nameObject



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

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

#recent_daysObject



48
49
50
# File 'lib/blackbeard/metric.rb', line 48

def recent_days
  metric_data.recent_days
end

#recent_hoursObject



44
45
46
# File 'lib/blackbeard/metric.rb', line 44

def recent_hours
  metric_data.recent_hours
end