Class: SplitIoClient::SplitClient
- Inherits:
-
Object
- Object
- SplitIoClient::SplitClient
- Defined in:
- lib/splitclient-rb/clients/split_client.rb
Instance Method Summary collapse
- #destroy ⇒ Object
-
#get_treatment(key, split_name, attributes = nil, split_data = nil, store_impressions = true, multiple = false, evaluator = nil) ⇒ String/Hash
obtains the treatment for a given feature.
- #get_treatments(key, split_names, attributes = nil) ⇒ Object
- #impression_router ⇒ Object
-
#initialize(api_key, config = {}, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository) ⇒ SplitIoClient
constructor
Creates a new split client instance that connects to split.io API.
- #keys_from_key(key) ⇒ Object
- #parsed_treatment(multiple, treatment_data) ⇒ Object
- #route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes) ⇒ Object
- #route_impressions(split_names, matching_key, bucketing_key, time, treatments_labels_change_numbers, attributes) ⇒ Object
- #store_impression(split_name, matching_key, bucketing_key, treatment, store_impressions, attributes) ⇒ Object
Constructor Details
#initialize(api_key, config = {}, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository) ⇒ SplitIoClient
Creates a new split client instance that connects to split.io API.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 9 def initialize(api_key, config = {}, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository) @config = config @splits_repository = splits_repository @segments_repository = segments_repository @impressions_repository = impressions_repository @metrics_repository = metrics_repository @adapter = adapter end |
Instance Method Details
#destroy ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 128 def destroy @config.logger.info('Split client shutdown started...') if @config.debug_enabled @config.threads[:impressions_sender].raise(SplitIoClient::ImpressionShutdownException) @config.threads.reject { |k, _| k == :impressions_sender }.each do |name, thread| Thread.kill(thread) end @metrics_repository.clear @splits_repository.clear @segments_repository.clear @config.logger.info('Split client shutdown complete') if @config.debug_enabled end |
#get_treatment(key, split_name, attributes = nil, split_data = nil, store_impressions = true, multiple = false, evaluator = nil) ⇒ String/Hash
obtains the treatment for a given feature
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 56 def get_treatment( key, split_name, attributes = nil, split_data = nil, store_impressions = true, multiple = false, evaluator = nil ) bucketing_key, matching_key = keys_from_key(key) treatment_data = { label: Engine::Models::Label::DEFINITION_NOT_FOUND, treatment: SplitIoClient::Engine::Models::Treatment::CONTROL } evaluator ||= Engine::Parser::Evaluator.new(@segments_repository, @splits_repository) if matching_key.nil? @config.logger.warn('matching_key was null for split_name: ' + split_name.to_s) return parsed_treatment(multiple, treatment_data) end if split_name.nil? @config.logger.warn('split_name was null for key: ' + key) return parsed_treatment(multiple, treatment_data) end start = Time.now begin split = multiple ? split_data : @splits_repository.get_split(split_name) if split.nil? @config.logger.debug("split_name: #{split_name} does not exist. Returning CONTROL") return parsed_treatment(multiple, treatment_data) else treatment_data = evaluator.call( { bucketing_key: bucketing_key, matching_key: matching_key }, split, attributes ) end rescue StandardError => error @config.log_found_exception(__method__.to_s, error) store_impression( split_name, matching_key, bucketing_key, { treatment: SplitIoClient::Engine::Models::Treatment::CONTROL, label: SplitIoClient::Engine::Models::Label::EXCEPTION }, store_impressions, attributes ) return parsed_treatment(multiple, treatment_data) end begin latency = (Time.now - start) * 1000.0 # Disable impressions if @config.impressions_queue_size == -1 split && store_impression(split_name, matching_key, bucketing_key, treatment_data, store_impressions, attributes) # Measure @adapter.metrics.time('sdk.get_treatment', latency) rescue StandardError => error @config.log_found_exception(__method__.to_s, error) store_impression( split_name, matching_key, bucketing_key, { treatment: SplitIoClient::Engine::Models::Treatment::CONTROL, label: SplitIoClient::Engine::Models::Label::EXCEPTION }, store_impressions, attributes ) return parsed_treatment(multiple, treatment_data) end parsed_treatment(multiple, treatment_data) end |
#get_treatments(key, split_names, attributes = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 20 def get_treatments(key, split_names, attributes = nil) bucketing_key, matching_key = keys_from_key(key) evaluator = Engine::Parser::Evaluator.new(@segments_repository, @splits_repository, true) treatments_labels_change_numbers = @splits_repository.get_splits(split_names).each_with_object({}) do |(name, data), memo| memo.merge!(name => get_treatment(key, name, attributes, data, false, true, evaluator)) end if @config.impressions_queue_size > 0 time = (Time.now.to_f * 1000.0).to_i @impressions_repository.add_bulk( matching_key, bucketing_key, treatments_labels_change_numbers, time ) route_impressions(split_names, matching_key, bucketing_key, time, treatments_labels_change_numbers, attributes) end split_names = treatments_labels_change_numbers.keys treatments = treatments_labels_change_numbers.values.map { |v| v[:treatment] } Hash[split_names.zip(treatments)] end |
#impression_router ⇒ Object
183 184 185 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 183 def impression_router @impression_router ||= SplitIoClient::ImpressionRouter.new(@config) end |
#keys_from_key(key) ⇒ Object
187 188 189 190 191 192 193 194 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 187 def keys_from_key(key) case key.class.to_s when 'Hash' key.values_at(:bucketing_key, :matching_key).map { |k| k.nil? ? nil : k.to_s } else [nil, key].map { |k| k.nil? ? nil : k.to_s } end end |
#parsed_treatment(multiple, treatment_data) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 196 def parsed_treatment(multiple, treatment_data) if multiple { treatment: treatment_data[:treatment], label: treatment_data[:label], change_number: treatment_data[:change_number] } else treatment_data[:treatment] end end |
#route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes) ⇒ Object
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 161 def route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes) impression_router.add( split_name: split_name, matching_key: matching_key, bucketing_key: bucketing_key, time: time, treatment: treatment, attributes: attributes ) end |
#route_impressions(split_names, matching_key, bucketing_key, time, treatments_labels_change_numbers, attributes) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 172 def route_impressions(split_names, matching_key, bucketing_key, time, treatments_labels_change_numbers, attributes) impression_router.add_bulk( split_names: split_names, matching_key: matching_key, bucketing_key: bucketing_key, time: time, treatments_labels_change_numbers: treatments_labels_change_numbers, attributes: attributes ) end |
#store_impression(split_name, matching_key, bucketing_key, treatment, store_impressions, attributes) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/splitclient-rb/clients/split_client.rb', line 143 def store_impression(split_name, matching_key, bucketing_key, treatment, store_impressions, attributes) time = (Time.now.to_f * 1000.0).to_i route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes) if @config.impression_listener && store_impressions return if @config.impressions_queue_size <= 0 || !store_impressions @impressions_repository.add(split_name, 'keyName' => matching_key, 'bucketingKey' => bucketing_key, 'treatment' => treatment[:treatment], 'label' => @config.labels_enabled ? treatment[:label] : nil, 'time' => time, 'changeNumber' => treatment[:change_number] ) rescue StandardError => error @config.log_found_exception(__method__.to_s, error) end |