Skip to content

Commit 83b0a0f

Browse files
authored
[AI-FSSDK] [FSSDK-12760] Add localHoldouts datafile section (#400)
* [AI-FSSDK] [FSSDK-12760] Add localHoldouts datafile section * [FSSDK-12760] Empty commit to re-trigger CI
1 parent 1652249 commit 83b0a0f

4 files changed

Lines changed: 225 additions & 110 deletions

File tree

‎lib/optimizely/config/datafile_project_config.rb‎

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class DatafileProjectConfig < ProjectConfig
3333
:group_id_map, :rollout_id_map, :rollout_experiment_id_map, :variation_id_map,
3434
:variation_id_to_variable_usage_map, :variation_key_map, :variation_id_map_by_experiment_id,
3535
:variation_key_map_by_experiment_id, :flag_variation_map, :integration_key_map, :integrations,
36-
:public_key_for_odp, :host_for_odp, :all_segments, :region, :holdouts, :holdout_id_map,
37-
:global_holdouts, :rule_holdouts_map
36+
:public_key_for_odp, :host_for_odp, :all_segments, :region, :holdouts, :local_holdouts,
37+
:holdout_id_map, :global_holdouts, :rule_holdouts_map
3838
# Boolean - denotes if Optimizely should remove the last block of visitors' IP address before storing event data
3939
attr_reader :anonymize_ip
4040

@@ -71,7 +71,10 @@ def initialize(datafile, logger, error_handler)
7171
@send_flag_decisions = config.fetch('sendFlagDecisions', false)
7272
@integrations = config.fetch('integrations', [])
7373
@region = config.fetch('region', 'US')
74+
# `holdouts` carries only global holdouts; `localHoldouts` (new top-level section) carries
75+
# only rule-scoped local holdouts. Section membership is the sole signal for scope.
7476
@holdouts = config.fetch('holdouts', [])
77+
@local_holdouts = config.fetch('localHoldouts', [])
7578

7679
# Default to US region if not specified
7780
@region = 'US' if @region.nil? || @region.empty?
@@ -118,25 +121,38 @@ def initialize(datafile, logger, error_handler)
118121
@global_holdouts = []
119122
@rule_holdouts_map = {}
120123

124+
# Section membership determines scope: entries in `holdouts` are global, entries in
125+
# `localHoldouts` are local. Any `includedRules` on a `holdouts` entry is stripped/ignored.
121126
@holdouts.each do |holdout|
122127
next unless holdout['status'] == 'Running'
123128

124129
# Ensure holdout has layerId field (holdouts don't have campaigns)
125130
holdout['layerId'] ||= ''
131+
# Strip includedRules from global-section entries — section membership is the sole signal.
132+
holdout.delete('includedRules')
126133

127134
@holdout_id_map[holdout['id']] = holdout
135+
@global_holdouts << holdout
136+
end
128137

129-
# Build global vs local holdout mappings
130-
# A holdout is global when includedRules is nil/absent (applies to all rules)
131-
# A holdout is local when includedRules is a non-nil array (applies only to specified rules)
132-
if holdout_global?(holdout)
133-
@global_holdouts << holdout
134-
else
135-
included_rules = holdout['includedRules'] || []
136-
included_rules.each do |rule_id|
137-
@rule_holdouts_map[rule_id] ||= []
138-
@rule_holdouts_map[rule_id] << holdout
139-
end
138+
@local_holdouts.each do |holdout|
139+
next unless holdout['status'] == 'Running'
140+
141+
# Local holdouts without includedRules are invalid — log and skip (do not fall back to global).
142+
included_rules = holdout['includedRules']
143+
if included_rules.nil? || !included_rules.is_a?(Array) || included_rules.empty?
144+
@logger.log(
145+
Logger::ERROR,
146+
"Local holdout '#{holdout['key'] || holdout['id']}' is missing or has empty 'includedRules'; skipping."
147+
)
148+
next
149+
end
150+
151+
holdout['layerId'] ||= ''
152+
@holdout_id_map[holdout['id']] = holdout
153+
included_rules.each do |rule_id|
154+
@rule_holdouts_map[rule_id] ||= []
155+
@rule_holdouts_map[rule_id] << holdout
140156
end
141157
end
142158

@@ -217,10 +233,11 @@ def initialize(datafile, logger, error_handler)
217233
# Generate flag_variation_map after injection so it includes everyone-else variations
218234
@flag_variation_map = generate_feature_variation_map(@feature_flags)
219235

220-
# Adding Holdout variations in variation id and key maps
221-
return unless @holdouts && !@holdouts.empty?
236+
# Adding Holdout variations in variation id and key maps (both global and local sections).
237+
all_holdouts = (@holdouts || []) + (@local_holdouts || [])
238+
return if all_holdouts.empty?
222239

223-
@holdouts.each do |holdout|
240+
all_holdouts.each do |holdout|
224241
next unless holdout['status'] == 'Running'
225242

226243
holdout_key = holdout['key']
@@ -659,7 +676,7 @@ def get_holdout(holdout_id)
659676
end
660677

661678
def get_holdouts_for_rule(rule_id)
662-
# Returns running local holdouts that target a specific rule ID.
679+
# Returns running local holdouts (from the `localHoldouts` section) that target the rule.
663680
# Local holdouts apply only to the rules listed in their includedRules array.
664681
#
665682
# rule_id - String ID of the experiment/delivery rule
@@ -669,9 +686,9 @@ def get_holdouts_for_rule(rule_id)
669686
end
670687

671688
def holdout_global?(holdout)
672-
# Determines whether a holdout is global (applies to all rules) or local (applies to specific rules).
673-
# A holdout is global when includedRules is nil or absent from the datafile.
674-
# A holdout with an empty array [] is a local holdout with no matching rules (NOT global).
689+
# Returns true when the holdout came from the global `holdouts` section.
690+
# Section membership is the sole signal for scope; `ProjectConfig` strips `includedRules`
691+
# from `holdouts`-section entries at parse time, so absence of the key is equivalent.
675692
#
676693
# holdout - Holdout hash from the datafile
677694
#

‎lib/optimizely/helpers/constants.rb‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,26 @@ module Constants
352352
}
353353
}
354354
}
355+
},
356+
'localHoldouts' => {
357+
'type' => 'array',
358+
'items' => {
359+
'type' => 'object',
360+
'properties' => {
361+
'id' => {
362+
'type' => 'string'
363+
},
364+
'key' => {
365+
'type' => 'string'
366+
},
367+
'status' => {
368+
'type' => 'string'
369+
},
370+
'includedRules' => {
371+
'type' => %w[array null]
372+
}
373+
}
374+
}
355375
}
356376
},
357377
'required' => %w[

0 commit comments

Comments
 (0)