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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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_key_map.key?(attribute_key)

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_id_to_key_map.key?(attribute_id)

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
17 changes: 17 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,20 @@ module Constants
},
'required' => %w[key]
}
},
'cmab' => {
'type' => 'object',
'items' => {
'type' => 'object',
'properties' => {
'attributeIds' => {
'type' => 'array'
},
'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
17 changes: 17 additions & 0 deletions spec/config/datafile_project_config_spec.rb
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new test is good, but you might add further tests for the new attribute lookup methods to verify error cases and edge cases.

Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,23 @@
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
end

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

Expand Down