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



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

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



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

def experiments
  @experiments
end

#nameObject

Returns the value of attribute name



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

def name
  @name
end

Class Method Details

.allObject



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#saveObject



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

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