Skip to content

Commit 2df38ac

Browse files
author
Bilal Al
committed
updated engine classes and repo helper
1 parent 49d9abe commit 2df38ac

File tree

6 files changed

+182
-79
lines changed

6 files changed

+182
-79
lines changed

lib/splitclient-rb/engine/common/impressions_manager.rb

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ def initialize(config,
1818
@unique_keys_tracker = unique_keys_tracker
1919
end
2020

21-
def build_impression(matching_key, bucketing_key, split_name, treatment, params = {})
21+
def build_impression(matching_key, bucketing_key, split_name, treatment, impressions_disabled, params = {})
2222
impression_data = impression_data(matching_key, bucketing_key, split_name, treatment, params[:time])
23-
2423
begin
25-
case @config.impressions_mode
26-
when :debug # In DEBUG mode we should calculate the pt only.
27-
impression_data[:pt] = @impression_observer.test_and_set(impression_data)
28-
when :none # In NONE mode we should track the total amount of evaluations and the unique keys.
24+
if @config.impressions_mode == :none || impressions_disabled # In NONE mode we should track the total amount of evaluations and the unique keys.
2925
@impression_counter.inc(split_name, impression_data[:m])
3026
@unique_keys_tracker.track(split_name, matching_key)
27+
elsif @config.impressions_mode == :debug # In DEBUG mode we should calculate the pt only.
28+
impression_data[:pt] = @impression_observer.test_and_set(impression_data)
3129
else # In OPTIMIZED mode we should track the total amount of evaluations and deduplicate the impressions.
3230
impression_data[:pt] = @impression_observer.test_and_set(impression_data)
33-
3431
@impression_counter.inc(split_name, impression_data[:m]) unless impression_data[:pt].nil?
3532
end
3633
rescue StandardError => e
@@ -40,24 +37,25 @@ def build_impression(matching_key, bucketing_key, split_name, treatment, params
4037
impression(impression_data, params[:attributes])
4138
end
4239

43-
def track(impressions)
44-
return if impressions.empty?
45-
46-
stats = { dropped: 0, queued: 0, dedupe: 0 }
47-
begin
48-
case @config.impressions_mode
49-
when :none
50-
return
51-
when :debug
52-
track_debug_mode(impressions, stats)
53-
when :optimized
54-
track_optimized_mode(impressions, stats)
40+
def track(impressions_decorator)
41+
return if impressions_decorator.empty?
42+
43+
impressions_decorator.each do |impression_decorator|
44+
stats = { dropped: 0, queued: 0, dedupe: 0 }
45+
begin
46+
if @config.impressions_mode == :none || impression_decorator[:disabled]
47+
return
48+
elsif @config.impressions_mode == :debug
49+
track_debug_mode([impression_decorator[:impression]], stats)
50+
elsif @config.impressions_mode == :optimized
51+
track_optimized_mode([impression_decorator[:impression]], stats)
52+
end
53+
rescue StandardError => e
54+
@config.log_found_exception(__method__.to_s, e)
55+
ensure
56+
record_stats(stats)
57+
impression_router.add_bulk([impression_decorator[:impression]])
5558
end
56-
rescue StandardError => e
57-
@config.log_found_exception(__method__.to_s, e)
58-
ensure
59-
record_stats(stats)
60-
impression_router.add_bulk(impressions)
6159
end
6260
end
6361

@@ -126,11 +124,10 @@ def track_debug_mode(impressions, stats)
126124

127125
def track_optimized_mode(impressions, stats)
128126
optimized_impressions = impressions.select { |imp| should_queue_impression?(imp[:i]) }
129-
127+
stats[:dedupe] = impressions.length - optimized_impressions.length
130128
return if optimized_impressions.empty?
131129

132130
stats[:dropped] = @impressions_repository.add_bulk(optimized_impressions)
133-
stats[:dedupe] = impressions.length - optimized_impressions.length
134131
stats[:queued] = optimized_impressions.length - stats[:dropped]
135132
end
136133
end

lib/splitclient-rb/engine/synchronizer.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def start_periodic_data_recording
4848
events_sender
4949
start_telemetry_sync_task
5050
end
51-
51+
5252
impressions_count_sender
5353
start_unique_keys_tracker_task
5454
end
@@ -175,7 +175,7 @@ def attempt_splits_sync(target_cn, fetch_options, max_retries, retry_delay_secon
175175

176176
# Starts thread which loops constantly and sends impressions to the Split API
177177
def impressions_sender
178-
ImpressionsSender.new(@impressions_repository, @config, @impressions_api).call unless @config.impressions_mode == :none
178+
ImpressionsSender.new(@impressions_repository, @config, @impressions_api).call
179179
end
180180

181181
# Starts thread which loops constantly and sends events to the Split API
@@ -185,7 +185,7 @@ def events_sender
185185

186186
# Starts thread which loops constantly and sends impressions count to the Split API
187187
def impressions_count_sender
188-
ImpressionsCountSender.new(@config, @impression_counter, @impressions_sender_adapter).call unless @config.impressions_mode == :debug
188+
ImpressionsCountSender.new(@config, @impression_counter, @impressions_sender_adapter).call
189189
end
190190

191191
def start_telemetry_sync_task
@@ -203,7 +203,7 @@ def sync_result(success, remaining_attempts, segment_names = nil)
203203
def sync_splits_and_segments
204204
@config.logger.debug('Synchronizing feature flags and segments ...') if @config.debug_enabled
205205
splits_result = @split_fetcher.fetch_splits
206-
206+
207207
splits_result[:success] && @segment_fetcher.fetch_segments
208208
end
209209
end

lib/splitclient-rb/helpers/repository_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ def self.update_feature_flag_repository(feature_flag_repository, feature_flags,
1313
next
1414
end
1515

16+
unless feature_flag.key?(:impressionsDisabled)
17+
feature_flag[:impressionsDisabled] = false
18+
config.logger.debug("feature flag (#{feature_flag[:name]}) does not have `impressionsDisabled` field, setting it to `false`") if config.debug_enabled
19+
end
20+
1621
config.logger.debug("storing feature flag (#{feature_flag[:name]})") if config.debug_enabled
1722
to_add.push(feature_flag)
1823
end

0 commit comments

Comments
 (0)