Class: Unleash::Metrics

Inherits:
Object
  • Object
show all
Defined in:
lib/unleash/metrics.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMetrics

Returns a new instance of Metrics.



5
6
7
8
# File 'lib/unleash/metrics.rb', line 5

def initialize
  self.features = {}
  self.features_lock = Mutex.new
end

Instance Attribute Details

#featuresObject

Returns the value of attribute features.



3
4
5
# File 'lib/unleash/metrics.rb', line 3

def features
  @features
end

#features_lockObject

Returns the value of attribute features_lock.



3
4
5
# File 'lib/unleash/metrics.rb', line 3

def features_lock
  @features_lock
end

Instance Method Details

#increment(feature, choice) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/unleash/metrics.rb', line 16

def increment(feature, choice)
  raise "InvalidArgument choice must be :yes or :no" unless [:yes, :no].include? choice

  self.features_lock.synchronize do
    self.features[feature] = { yes: 0, no: 0 } unless self.features.include? feature
    self.features[feature][choice] += 1
  end
end

#increment_variant(feature, choice, variant) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/unleash/metrics.rb', line 25

def increment_variant(feature, choice, variant)
  self.features_lock.synchronize do
    self.features[feature] = { yes: 0, no: 0 } unless self.features.include? feature
    self.features[feature][choice] += 1
    self.features[feature]['variant'] = {}     unless self.features[feature].include? 'variant'
    self.features[feature]['variant'][variant] = 0 unless self.features[feature]['variant'].include? variant
    self.features[feature]['variant'][variant] += 1
  end
end

#resetObject



35
36
37
38
39
# File 'lib/unleash/metrics.rb', line 35

def reset
  self.features_lock.synchronize do
    self.features = {}
  end
end

#to_sObject



10
11
12
13
14
# File 'lib/unleash/metrics.rb', line 10

def to_s
  self.features_lock.synchronize do
    return self.features.to_json
  end
end