Class: AmplitudeExperiment::LocalEvaluationClient

Inherits:
Object
  • Object
show all
Defined in:
lib/experiment/local/client.rb

Overview

Main client for fetching variant data.

Instance Method Summary collapse

Constructor Details

#initialize(api_key, config = nil) ⇒ LocalEvaluationClient

Creates a new Experiment Client instance.

Parameters:

  • api_key (String)

    The environment API Key

  • config (LocalEvaluationConfig) (defaults to: nil)

    The config object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/experiment/local/client.rb', line 15

def initialize(api_key, config = nil)
  require 'experiment/local/evaluation/evaluation'
  @api_key = api_key
  @config = config || LocalEvaluationConfig.new
  @flags = nil
  @flags_mutex = Mutex.new
  @logger = Logger.new($stdout)
  @logger.level = if @config.debug
                    Logger::DEBUG
                  else
                    Logger::INFO
                  end
  @fetcher = LocalEvaluationFetcher.new(api_key, @logger, @config.server_url)
  raise ArgumentError, 'Experiment API key is empty' if @api_key.nil? || @api_key.empty?

  @assignment_service = nil
  @assignment_service = AssignmentService.new(AmplitudeAnalytics::Amplitude.new(config.assignment_config.api_key, configuration: config.assignment_config), AssignmentFilter.new(config.assignment_config.cache_capacity)) if config&.assignment_config
end

Instance Method Details

#evaluate(user, flag_keys = []) ⇒ Hash[String, Variant]

Locally evaluates flag variants for a user.

Parameters:

  • user (User)

    The user to evaluate

  • flag_keys (String[]) (defaults to: [])

    The flags to evaluate with the user. If empty, all flags from the flag cache are evaluated

Returns:

  • (Hash[String, Variant])

    The evaluated variants



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/experiment/local/client.rb', line 40

def evaluate(user, flag_keys = [])
  flags = @flags_mutex.synchronize do
    @flags
  end
  return {} if flags.nil?

  user_str = user.to_json

  @logger.debug("[Experiment] Evaluate: User: #{user_str} - Rules: #{flags}") if @config.debug
  result = evaluation(flags, user_str)
  @logger.debug("[Experiment] evaluate - result: #{result}") if @config.debug
  parse_results(result, flag_keys, user)
end

#startObject

Fetch initial flag configurations and start polling for updates. You must call this function to begin polling for flag config updates.



56
57
58
59
60
61
# File 'lib/experiment/local/client.rb', line 56

def start
  return if @is_running

  @logger.debug('[Experiment] poller - start') if @debug
  run
end

#stopObject

Stop polling for flag configurations. Close resource like connection pool with client



64
65
66
67
68
# File 'lib/experiment/local/client.rb', line 64

def stop
  @poller_thread&.exit
  @is_running = false
  @poller_thread = nil
end