Class: AmplitudeExperiment::LocalEvaluationClient
- Inherits:
-
Object
- Object
- AmplitudeExperiment::LocalEvaluationClient
- Defined in:
- lib/experiment/local/client.rb
Overview
Main client for fetching variant data.
Instance Method Summary collapse
-
#evaluate(user, flag_keys = []) ⇒ Hash[String, Variant]
deprecated
Deprecated.
Please use #evaluate_v2 instead
-
#evaluate_v2(user, flag_keys = [], options = nil) ⇒ Hash[String, Variant]
TODO: ruby backwards compatibility for evaluate_v2 to be looked at again Locally evaluates flag variants for a user.
-
#initialize(api_key, config = nil) ⇒ LocalEvaluationClient
constructor
Creates a new Experiment Client instance.
-
#start ⇒ Object
Fetch initial flag configurations and start polling for updates.
-
#stop ⇒ Object
Stop polling for flag configurations.
Constructor Details
#initialize(api_key, config = nil) ⇒ LocalEvaluationClient
Creates a new Experiment Client instance.
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 45 46 |
# File 'lib/experiment/local/client.rb', line 16 def initialize(api_key, config = nil) @api_key = api_key @config = config || LocalEvaluationConfig.new @logger = @config.logger @flags = nil @flags_mutex = Mutex.new raise ArgumentError, 'Experiment API key is empty' if @api_key.nil? || @api_key.empty? @engine = Evaluation::Engine.new @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 # Exposure service is always instantiated, using deployment key if no api key provided @exposure_service = nil @exposure_service = ExposureService.new(AmplitudeAnalytics::Amplitude.new(config.exposure_config.api_key, configuration: config.exposure_config), ExposureFilter.new(config.exposure_config.cache_capacity)) if config&.exposure_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]
Please use #evaluate_v2 instead
Locally evaluates flag variants for a user.
55 56 57 58 |
# File 'lib/experiment/local/client.rb', line 55 def evaluate(user, flag_keys = []) variants = evaluate_v2(user, flag_keys) AmplitudeExperiment.filter_default_variants(variants) end |
#evaluate_v2(user, flag_keys = [], options = nil) ⇒ Hash[String, Variant]
TODO: ruby backwards compatibility for evaluate_v2 to be looked at again 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).
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/experiment/local/client.rb', line 70 def evaluate_v2(user, flag_keys = [], = nil) flags = @flag_config_storage.flag_configs return {} if flags.nil? sorted_flags = Evaluation::TopologicalSort.sort(flags, flag_keys) required_cohorts_in_storage(sorted_flags) user = enrich_user_with_cohorts(user, flags) if @config.cohort_sync_config context = AmplitudeExperiment.user_to_evaluation_context(user) @logger.debug("[Experiment] Evaluate: User: #{context} - Rules: #{flags}") if @config.debug result = @engine.evaluate(context, sorted_flags) @logger.debug("[Experiment] evaluate - result: #{result}") if @config.debug variants = AmplitudeExperiment.evaluation_variants_json_to_variants(result) @exposure_service&.track(Exposure.new(user, variants)) if &.tracks_exposure == true # @deprecated Assignment tracking is deprecated. Use ExposureService with Exposure tracking instead. @assignment_service&.track(Assignment.new(user, variants)) variants end |
#start ⇒ Object
Fetch initial flag configurations and start polling for updates. You must call this function to begin polling for flag config updates.
91 92 93 94 95 96 |
# File 'lib/experiment/local/client.rb', line 91 def start return if @is_running @logger.debug('[Experiment] poller - start') if @debug @deployment_runner.start end |
#stop ⇒ Object
Stop polling for flag configurations. Close resource like connection pool with client
99 100 101 102 |
# File 'lib/experiment/local/client.rb', line 99 def stop @is_running = false @deployment_runner.stop end |