Class: SplitIoClient::SplitClient

Inherits:
Object
  • Object
show all
Defined in:
lib/splitclient-rb/clients/split_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository, events_repository) ⇒ SplitIoClient

Creates a new split client instance that connects to split.io API.

Parameters:

  • api_key (String) —

    the API key for your split account



10
11
12
13
14
15
16
17
18
19
# File 'lib/splitclient-rb/clients/split_client.rb', line 10

def initialize(api_key, adapter = nil, splits_repository, segments_repository, impressions_repository, metrics_repository, events_repository)
  @splits_repository = splits_repository
  @segments_repository = segments_repository
  @impressions_repository = impressions_repository
  @metrics_repository = metrics_repository
  @events_repository = events_repository
  @destroyed = false

  @adapter = adapter
end

Instance Method Details

#destroy ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/splitclient-rb/clients/split_client.rb', line 126

def destroy
  SplitIoClient.configuration.logger.info('Split client shutdown started...') if SplitIoClient.configuration.debug_enabled
  SplitIoClient.configuration.threads[:impressions_sender].raise(SplitIoClient::ImpressionShutdownException)
  SplitIoClient.configuration.threads.reject { |k, _| k == :impressions_sender }.each do |name, thread|
    Thread.kill(thread)
  end
  @metrics_repository.clear
  @splits_repository.clear
  @segments_repository.clear
  @events_repository.clear

  SplitIoClient.configuration.logger.info('Split client shutdown complete') if SplitIoClient.configuration.debug_enabled
  SplitIoClient.configuration.valid_mode = false
  @destroyed = true
end

#get_treatment(key, split_name, attributes = {}, split_data = nil, store_impressions = true, multiple = false, evaluator = nil) ⇒ String/Hash

obtains the treatment for a given feature

Parameters:

  • key (String/Hash) —

    user id or hash with matching_key/bucketing_key

  • split_name (String/Array) —

    name of the feature that is being validated or array of them

  • attributes (Hash) (defaults to: {}) —

    attributes to pass to the treatment class

  • split_data (Hash) (defaults to: nil) —

    split data, when provided this method doesn't fetch splits_repository for the data

  • store_impressions (Boolean) (defaults to: true) —

    impressions aren't stored if this flag is false

  • multiple (Hash) (defaults to: false) —

    internal flag to signal if method is called by get_treatments

  • evaluator (Evaluator) (defaults to: nil) —

    Evaluator class instance, used to cache treatments

Returns:

  • (String/Hash) —

    Treatment as String or Hash of treatments in case of array of features



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
# File 'lib/splitclient-rb/clients/split_client.rb', line 72

def get_treatment(
    key, split_name, attributes = {}, split_data = nil, store_impressions = true,
    multiple = false, evaluator = nil
  )

  control_treatment = { label: Engine::Models::Label::EXCEPTION, treatment: SplitIoClient::Engine::Models::Treatment::CONTROL }
  parsed_control_treatment = parsed_treatment(multiple, control_treatment)

  bucketing_key, matching_key = keys_from_key(key)

  return parsed_control_treatment unless valid_client && SplitIoClient::Validators.valid_get_treatment_parameters(key, split_name, matching_key, bucketing_key, attributes)

  bucketing_key = bucketing_key ? bucketing_key.to_s : nil
  matching_key = matching_key.to_s
  sanitized_split_name = split_name.to_s.strip

  if split_name.to_s != sanitized_split_name
    SplitIoClient.configuration.logger.warn("get_treatment: split_name #{split_name} has extra whitespace, trimming")
    split_name = sanitized_split_name
  end

  evaluator ||= Engine::Parser::Evaluator.new(@segments_repository, @splits_repository)

  begin
    start = Time.now

    split = multiple ? split_data : @splits_repository.get_split(split_name)

    if split.nil?
      SplitIoClient.configuration.logger.warn("split_name: #{split_name} does not exist. Returning CONTROL")
      return parsed_control_treatment
    end

    treatment_data =
      evaluator.call(
      { bucketing_key: bucketing_key, matching_key: matching_key }, split, attributes
    )

    latency = (Time.now - start) * 1000.0
    store_impression(split_name, matching_key, bucketing_key, treatment_data, store_impressions, attributes)

    # Measure
    @adapter.metrics.time('sdk.get_treatment', latency) unless multiple
  rescue StandardError => error
    SplitIoClient.configuration.log_found_exception(__method__.to_s, error)

    store_impression(split_name, matching_key, bucketing_key, control_treatment, store_impressions, attributes)

    return parsed_control_treatment
  end

  parsed_treatment(multiple, treatment_data)
end

#get_treatments(key, split_names, attributes = {}) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/splitclient-rb/clients/split_client.rb', line 21

def get_treatments(key, split_names, attributes = {})
  return nil unless SplitIoClient::Validators.valid_get_treatments_parameters(split_names)

  sanitized_split_names = sanitize_split_names(split_names)

  if sanitized_split_names.empty?
    SplitIoClient.configuration.logger.error('get_treatments: split_names must be a non-empty Array')
    return {}
  end

  bucketing_key, matching_key = keys_from_key(key)
  bucketing_key = bucketing_key ? bucketing_key.to_s : nil
  matching_key = matching_key ? matching_key.to_s : nil

  evaluator = Engine::Parser::Evaluator.new(@segments_repository, @splits_repository, true)
  start = Time.now
  treatments_labels_change_numbers =
    @splits_repository.get_splits(sanitized_split_names).each_with_object({}) do |(name, data), memo|
      memo.merge!(name => get_treatment(key, name, attributes, data, false, true, evaluator))
    end
  latency = (Time.now - start) * 1000.0
  # Measure
  @adapter.metrics.time('sdk.get_treatments', latency)

  unless SplitIoClient.configuration.disable_impressions
    time = (Time.now.to_f * 1000.0).to_i
    @impressions_repository.add_bulk(
      matching_key, bucketing_key, treatments_labels_change_numbers, time
    )

    route_impressions(sanitized_split_names, matching_key, bucketing_key, time, treatments_labels_change_numbers, attributes)
  end

  split_names_keys = treatments_labels_change_numbers.keys
  treatments = treatments_labels_change_numbers.values.map { |v| v[:treatment] }

  Hash[split_names_keys.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
end

#keys_from_key(key) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/splitclient-rb/clients/split_client.rb', line 198

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 }
  else
    [nil, key].map { |k| k.nil? ? nil : k }
  end
end

#parsed_treatment(multiple, treatment_data) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
# File 'lib/splitclient-rb/clients/split_client.rb', line 207

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

#sanitize_split_names(split_names) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/splitclient-rb/clients/split_client.rb', line 219

def sanitize_split_names(split_names)
  split_names.compact.uniq.select do |split_name|
    if (split_name.is_a?(String) || split_name.is_a?(Symbol)) && !split_name.empty?
      true
    elsif split_name.is_a?(String) && split_name.empty?
      SplitIoClient.configuration.logger.warn('get_treatments: you passed an empty split_name, split_name must be a non-empty String or a Symbol')
      false
    else
      SplitIoClient.configuration.logger.warn('get_treatments: you passed an invalid split_name, split_name must be a non-empty String or a Symbol')
      false
    end
  end
end

#store_impression(split_name, matching_key, bucketing_key, treatment, store_impressions, attributes) ⇒ Object



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

def store_impression(split_name, matching_key, bucketing_key, treatment, store_impressions, attributes)
  time = (Time.now.to_f * 1000.0).to_i

  return if SplitIoClient.configuration.disable_impressions || !store_impressions

  @impressions_repository.add(
    matching_key,
    bucketing_key,
    split_name,
    treatment,
    time
  )

  route_impression(split_name, matching_key, bucketing_key, time, treatment, attributes)

rescue StandardError => error
  SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
end

#track(key, traffic_type_name, event_type, value = nil) ⇒ Object



187
188
189
190
191
192
193
194
195
196
# File 'lib/splitclient-rb/clients/split_client.rb', line 187

def track(key, traffic_type_name, event_type, value = nil)
  return false unless valid_client && SplitIoClient::Validators.valid_track_parameters(key, traffic_type_name, event_type, value)
  begin
    @events_repository.add(key.to_s, traffic_type_name.downcase, event_type.to_s, (Time.now.to_f * 1000).to_i, value)
    true
  rescue StandardError => error
    SplitIoClient.configuration.log_found_exception(__method__.to_s, error)
    false
  end
end