<commit_before>module Moona
  class Base
    attr_reader :user_key

    # we pass in a user hash to identify the user
    # so that we can keep data 
    def initialize(user_key)
      @user_key = user_key
      @interface = Interface.new
    end

    def env
      Moona.env
    end

    def get_token_for_experiment(experiment_name)
      # val = UserVariant.new(Moona.cache.read(self.cache_key(expiriment_name)))
      if val = Moona.cache.read(self.cache_key(experiment_name))
        return UserVariant.new(val).token
      end
      return nil
    end

    def get_variant(experiment_name)
      Celluloid::Future.new do
        self.get_variant_value(experiment_name)
      end
    end

    def reward_all_experiments(amount)
      Moona.get_all_experiments.each do |experiment_name|
        self.reward_experiment(experiment_name, amount)
      end
    end

    def reward_experiment(experiment_name, amount)
      if token = self.get_token_for_experiment(experiment_name)
        # UUID for this experiment
        self.make_remote_request(:default => false) do
          experiment_uuid = Moona.experiment_uuid(experiment_name)
          @interface.reward_experiment(experiment_uuid, token, amount)
          return true
        end
      end
      return false
    end



    protected
    # cache key for a given experiment and our user
    def cache_key(experiment_name)
      "#{Moona.cache_namespace}#{self.user_key}_#{experiment_name}"
    end

    # protected method to make the actual calls to get values
    # from the cache or from myna
    def get_variant_value(experiment_name)
      
      # UUID for this experiment
      experiment_uuid = Moona.experiment_uuid(experiment_name)
      default_value = Moona.get_default_value_for_experiment(experiment_name)
      
      self.make_remote_request(:default => default_value) do
        resp = Moona.cache.fetch(self.cache_key(experiment_name)) do
          @interface.get_suggestion(experiment_uuid).to_hash
        end
        UserVariant.new(resp).choice || default_value
      end
    end

    def make_remote_request(opts = {}, &block)
      begin
        Timeout::timeout(1.0) do
          yield
        end
      rescue Exception => e
        raise e
        return opts[:default]
      end
    end<commit_msg>making re-raise only happen in test<commit_after>module Moona
  class Base
    attr_reader :user_key

    # we pass in a user hash to identify the user
    # so that we can keep data 
    def initialize(user_key)
      @user_key = user_key
      @interface = Interface.new
    end

    def env
      Moona.env
    end

    def get_token_for_experiment(experiment_name)
      # val = UserVariant.new(Moona.cache.read(self.cache_key(expiriment_name)))
      if val = Moona.cache.read(self.cache_key(experiment_name))
        return UserVariant.new(val).token
      end
      return nil
    end

    def get_variant(experiment_name)
      Celluloid::Future.new do
        self.get_variant_value(experiment_name)
      end
    end

    def reward_all_experiments(amount)
      Moona.get_all_experiments.each do |experiment_name|
        self.reward_experiment(experiment_name, amount)
      end
    end

    def reward_experiment(experiment_name, amount)
      if token = self.get_token_for_experiment(experiment_name)
        # UUID for this experiment
        self.make_remote_request(:default => false) do
          experiment_uuid = Moona.experiment_uuid(experiment_name)
          @interface.reward_experiment(experiment_uuid, token, amount)
          return true
        end
      end
      return false
    end



    protected
    # cache key for a given experiment and our user
    def cache_key(experiment_name)
      "#{Moona.cache_namespace}#{self.user_key}_#{experiment_name}"
    end

    # protected method to make the actual calls to get values
    # from the cache or from myna
    def get_variant_value(experiment_name)
      
      # UUID for this experiment
      experiment_uuid = Moona.experiment_uuid(experiment_name)
      default_value = Moona.get_default_value_for_experiment(experiment_name)
      
      self.make_remote_request(:default => default_value) do
        resp = Moona.cache.fetch(self.cache_key(experiment_name)) do
          @interface.get_suggestion(experiment_uuid).to_hash
        end
        UserVariant.new(resp).choice || default_value
      end
    end

    def make_remote_request(opts = {}, &block)
      begin
        Timeout::timeout(1.0) do
          yield
        end
      rescue Exception => e
        raise e if Moona.env == "test"
        return opts[:default]
      end
    end
{'fraction_non_alphanumeric': 0.06754708811431046, 'fraction_numerical': 0.0008659883091578263, 'mean_word_length': 2.6263736263736264, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1500304', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1392, 'n_words': 319}