Skip to content

[FSSDK-11140] Ruby: Update project config to track CMAB properties #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion lib/optimizely/config/datafile_project_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DatafileProjectConfig < ProjectConfig
attr_reader :datafile, :account_id, :attributes, :audiences, :typed_audiences, :events,
:experiments, :feature_flags, :groups, :project_id, :bot_filtering, :revision,
:sdk_key, :environment_key, :rollouts, :version, :send_flag_decisions,
:attribute_key_map, :audience_id_map, :event_key_map, :experiment_feature_map,
:attribute_key_map, :attribute_id_to_key_map, :audience_id_map, :event_key_map, :experiment_feature_map,
:experiment_id_map, :experiment_key_map, :feature_flag_key_map, :feature_variable_key_map,
:group_id_map, :rollout_id_map, :rollout_experiment_id_map, :variation_id_map,
:variation_id_to_variable_usage_map, :variation_key_map, :variation_id_map_by_experiment_id,
Expand Down Expand Up @@ -82,6 +82,10 @@ def initialize(datafile, logger, error_handler)

# Utility maps for quick lookup
@attribute_key_map = generate_key_map(@attributes, 'key')
@attribute_id_to_key_map = {}
@attributes.each do |attribute|
@attribute_id_to_key_map[attribute['id']] = attribute['key']
end
@event_key_map = generate_key_map(@events, 'key')
@group_id_map = generate_key_map(@groups, 'id')
@group_id_map.each do |key, group|
Expand Down Expand Up @@ -440,6 +444,40 @@ def get_attribute_id(attribute_key)
nil
end

def get_attribute_by_key(attribute_key)
# Get attribute for the provided attribute key.
#
# Args:
# Attribute key for which attribute is to be fetched.
#
# Returns:
# Attribute corresponding to the provided attribute key.
attribute = @attribute_key_map[attribute_key]
return attribute if attribute

invalid_attribute_error = InvalidAttributeError.new(attribute_key)
@logger.log Logger::ERROR, invalid_attribute_error.message
@error_handler.handle_error invalid_attribute_error
nil
end

def get_attribute_key_by_id(attribute_id)
# Get attribute key for the provided attribute ID.
#
# Args:
# Attribute ID for which attribute is to be fetched.
#
# Returns:
# Attribute key corresponding to the provided attribute ID.
attribute = @attribute_id_to_key_map[attribute_id]
return attribute if attribute

invalid_attribute_error = InvalidAttributeError.new(attribute_id)
@logger.log Logger::ERROR, invalid_attribute_error.message
@error_handler.handle_error invalid_attribute_error
nil
end

def variation_id_exists?(experiment_id, variation_id)
# Determines if a given experiment ID / variation ID pair exists in the datafile
#
Expand Down
15 changes: 15 additions & 0 deletions lib/optimizely/helpers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ module Constants
},
'forcedVariations' => {
'type' => 'object'
},
'cmab' => {
'type' => 'object'
}
},
'required' => %w[
Expand Down Expand Up @@ -303,6 +306,18 @@ module Constants
},
'required' => %w[key]
}
},
'cmab' => {
'type' => 'object',
'properties' => {
'attributeIds' => {
'type' => 'array',
'items' => {'type' => 'string'}
},
'trafficAllocation' => {
'type' => 'integer'
}
}
}
},
'required' => %w[
Expand Down
4 changes: 4 additions & 0 deletions lib/optimizely/project_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def get_whitelisted_variations(experiment_id); end

def get_attribute_id(attribute_key); end

def get_attribute_by_key(attribute_key); end

def get_attribute_key_by_id(attribute_id); end

def variation_id_exists?(experiment_id, variation_id); end

def get_feature_flag_from_key(feature_flag_key); end
Expand Down
61 changes: 61 additions & 0 deletions spec/config/datafile_project_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,67 @@
end
end

describe '#test_cmab_field_population' do
it 'Should return CMAB details' do
config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY))
config_dict['experiments'][0]['cmab'] = {'attributeIds' => %w[808797688 808797689], 'trafficAllocation' => 4000}
config_dict['experiments'][0]['trafficAllocation'] = []

config_json = JSON.dump(config_dict)
project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler)

experiment = project_config.get_experiment_from_key('test_experiment')
expect(experiment['cmab']).to eq({'attributeIds' => %w[808797688 808797689], 'trafficAllocation' => 4000})

experiment2 = project_config.get_experiment_from_key('test_experiment_with_audience')
expect(experiment2['cmab']).to eq(nil)
end
it 'should return nil if cmab field is missing' do
config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY))
config_dict['experiments'][0].delete('cmab')
config_json = JSON.dump(config_dict)
project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler)
experiment = project_config.get_experiment_from_key('test_experiment')
expect(experiment['cmab']).to eq(nil)
end

it 'should handle empty cmab object' do
config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY))
config_dict['experiments'][0]['cmab'] = {}
config_json = JSON.dump(config_dict)
project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler)
experiment = project_config.get_experiment_from_key('test_experiment')
expect(experiment['cmab']).to eq({})
end

it 'should handle cmab with only attributeIds' do
config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY))
config_dict['experiments'][0]['cmab'] = {'attributeIds' => %w[808797688]}
config_json = JSON.dump(config_dict)
project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler)
experiment = project_config.get_experiment_from_key('test_experiment')
expect(experiment['cmab']).to eq({'attributeIds' => %w[808797688]})
end

it 'should handle cmab with only trafficAllocation' do
config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY))
config_dict['experiments'][0]['cmab'] = {'trafficAllocation' => 1234}
config_json = JSON.dump(config_dict)
project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler)
experiment = project_config.get_experiment_from_key('test_experiment')
expect(experiment['cmab']).to eq({'trafficAllocation' => 1234})
end

it 'should not affect other experiments when cmab is set' do
config_dict = Marshal.load(Marshal.dump(OptimizelySpec::VALID_CONFIG_BODY))
config_dict['experiments'][0]['cmab'] = {'attributeIds' => %w[808797688 808797689], 'trafficAllocation' => 4000}
config_json = JSON.dump(config_dict)
project_config = Optimizely::DatafileProjectConfig.new(config_json, logger, error_handler)
experiment2 = project_config.get_experiment_from_key('test_experiment_with_audience')
expect(experiment2['cmab']).to eq(nil)
end
end

describe '#feature_experiment' do
let(:config) { Optimizely::DatafileProjectConfig.new(config_body_JSON, logger, error_handler) }

Expand Down