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.



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

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.



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

def experiments
  @experiments
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.allObject



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

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



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

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



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

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



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

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



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

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



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

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



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

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

#saveObject



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

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