Class: Split::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/split/metric.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Metric

Returns a new instance of Metric.



7
8
9
10
11
12
13
# File 'lib/split/metric.rb', line 7

def initialize(attrs = {})
  attrs.each do |key,value|
    if self.respond_to?("#{key}=")
      self.send("#{key}=", value)
    end
  end
end

Instance Attribute Details

#experimentsObject

Returns the value of attribute experiments.



5
6
7
# File 'lib/split/metric.rb', line 5

def experiments
  @experiments
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/split/metric.rb', line 4

def name
  @name
end

Class Method Details

.allObject



55
56
57
58
59
60
61
62
63
# File 'lib/split/metric.rb', line 55

def self.all
  redis_metrics = Split.redis.hgetall(:metrics).collect do |key, value|
    find(key)
  end
  configuration_metrics = Split.configuration.metrics.collect do |key, value|
    new(name: key, experiments: value)
  end
  redis_metrics | configuration_metrics
end

.find(name) ⇒ Object



39
40
41
42
43
44
# File 'lib/split/metric.rb', line 39

def self.find(name)
  name = name.intern if name.is_a?(String)
  metric = load_from_configuration(name)
  metric = load_from_redis(name) if metric.nil?
  metric
end

.find_or_create(attrs) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/split/metric.rb', line 46

def self.find_or_create(attrs)
  metric = find(attrs[:name])
  unless metric
    metric = new(attrs)
    metric.save
  end
  metric
end

.load_from_configuration(name) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/split/metric.rb', line 30

def self.load_from_configuration(name)
  metrics = Split.configuration.metrics
  if metrics && metrics[name]
    Split::Metric.new(:experiments => metrics[name], :name => name)
  else
    nil
  end
end

.load_from_redis(name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/split/metric.rb', line 15

def self.load_from_redis(name)
  metric = Split.redis.hget(:metrics, name)
  if metric
    experiment_names = metric.split(',')

    experiments = experiment_names.collect do |experiment_name|
      Split::ExperimentCatalog.find(experiment_name)
    end

    Split::Metric.new(:name => name, :experiments => experiments)
  else
    nil
  end
end

.possible_experiments(metric_name) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/split/metric.rb', line 65

def self.possible_experiments(metric_name)
  experiments = []
  metric  = Split::Metric.find(metric_name)
  if metric
    experiments << metric.experiments
  end
  experiment = Split::ExperimentCatalog.find(metric_name)
  if experiment
    experiments << experiment
  end
  experiments.flatten
end

Instance Method Details

#complete!Object



82
83
84
85
86
# File 'lib/split/metric.rb', line 82

def complete!
  experiments.each do |experiment|
    experiment.complete!
  end
end

#saveObject



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

def save
  Split.redis.hset(:metrics, name, experiments.map(&:name).join(','))
end