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)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/experiment/local/client.rb', line 14

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
  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

  @cohort_storage = InMemoryCohortStorage.new
  @flag_config_storage = InMemoryFlagConfigStorage.new
  @flag_config_fetcher = LocalEvaluationFetcher.new(@api_key, @logger, @config.server_url)
  @cohort_loader = nil
  unless @config.cohort_sync_config.nil?
    @cohort_download_api = DirectCohortDownloadApi.new(@config.cohort_sync_config.api_key,
                                                       @config.cohort_sync_config.secret_key,
                                                       @config.cohort_sync_config.max_cohort_size,
                                                       @config.cohort_sync_config.cohort_server_url,
                                                       @logger)
    @cohort_loader = CohortLoader.new(@cohort_download_api, @cohort_storage)
  end
  @deployment_runner = DeploymentRunner.new(@config, @flag_config_fetcher, @flag_config_storage, @cohort_storage, @logger, @cohort_loader)
end

Instance Method Details

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

Deprecated.

Please use #evaluate_v2 instead

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



53
54
55
56
# File 'lib/experiment/local/client.rb', line 53

def evaluate(user, flag_keys = [])
  variants = evaluate_v2(user, flag_keys)
  AmplitudeExperiment.filter_default_variants(variants)
end

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

Locally evaluates flag variants for a user.

This function will only evaluate flags for the keys specified in the flag_keys argument. If flag_keys is
missing or None, all flags are evaluated. This function differs from evaluate as it will return a default
variant object if the flag was evaluated but the user was not assigned (i.e. off).

Parameters:

  • user (User)

    The user to evaluate

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

    The flags to evaluate with the user, if empty all flags are evaluated

Returns:

  • (Hash[String, Variant])

    The evaluated variants



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/experiment/local/client.rb', line 66

def evaluate_v2(user, flag_keys = [])
  flags = @flag_config_storage.flag_configs
  return {} if flags.nil?

  sorted_flags = AmplitudeExperiment.topological_sort(flags, flag_keys.to_set)
  required_cohorts_in_storage(sorted_flags)
  flags_json = sorted_flags.to_json
  user = enrich_user_with_cohorts(user, flags) if @config.cohort_sync_config
  context = AmplitudeExperiment.user_to_evaluation_context(user)
  context_json = context.to_json

  @logger.debug("[Experiment] Evaluate: User: #{context_json} - Rules: #{flags}") if @config.debug
  result = evaluation(flags_json, context_json)
  @logger.debug("[Experiment] evaluate - result: #{result}") if @config.debug
  variants = AmplitudeExperiment.evaluation_variants_json_to_variants(result)
  @assignment_service&.track(Assignment.new(user, variants))
  variants
end

#startObject

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



87
88
89
90
91
92
# File 'lib/experiment/local/client.rb', line 87

def start
  return if @is_running

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

#stopObject

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



95
96
97
98
# File 'lib/experiment/local/client.rb', line 95

def stop
  @is_running = false
  @deployment_runner.stop
end