Sha256: 56b613ee156b3f14a4586d7bb4ec3ecbeff5cdb70beebed3875ede1f312e2769

Contents?: true

Size: 1.83 KB

Versions: 9

Compression:

Stored size: 1.83 KB

Contents

module Split
  class Metric
    attr_accessor :name
    attr_accessor :experiments

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

    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::Experiment.find(experiment_name)
        end

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

    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

    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

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

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

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

    private

    def self.normalize_metric(label)
      if Hash === label
        metric_name = label.keys.first
        goals = label.values.first
      else
        metric_name = label
        goals = []
      end
      return metric_name, goals
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
split-0.7.1 lib/split/metric.rb
split-0.7.0 lib/split/metric.rb
split-0.6.6 lib/split/metric.rb
split-0.6.5 lib/split/metric.rb
split-0.6.4 lib/split/metric.rb
split-0.6.3 lib/split/metric.rb
split-0.6.2 lib/split/metric.rb
split-0.6.1 lib/split/metric.rb
split-0.6.0 lib/split/metric.rb