|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Copyright 2019, Optimizely and contributors |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +module Optimizely |
| 19 | + class OptimizelyConfig |
| 20 | + def initialize(project_config) |
| 21 | + @project_config = project_config |
| 22 | + end |
| 23 | + |
| 24 | + def config |
| 25 | + experiments_map_object = experiments_map |
| 26 | + features_map = get_features_map(experiments_map_object) |
| 27 | + { |
| 28 | + 'experimentsMap' => experiments_map_object, |
| 29 | + 'featuresMap' => features_map, |
| 30 | + 'revision' => @project_config.revision |
| 31 | + } |
| 32 | + end |
| 33 | + |
| 34 | + private |
| 35 | + |
| 36 | + def experiments_map |
| 37 | + feature_variables_map = @project_config.feature_flags.reduce({}) do |result_map, feature| |
| 38 | + result_map.update(feature['id'] => feature['variables']) |
| 39 | + end |
| 40 | + @project_config.experiments.reduce({}) do |experiments_map, experiment| |
| 41 | + experiments_map.update( |
| 42 | + experiment['key'] => { |
| 43 | + 'id' => experiment['id'], |
| 44 | + 'key' => experiment['key'], |
| 45 | + 'variationsMap' => experiment['variations'].reduce({}) do |variations_map, variation| |
| 46 | + variation_object = { |
| 47 | + 'id' => variation['id'], |
| 48 | + 'key' => variation['key'], |
| 49 | + 'variablesMap' => get_merged_variables_map(variation, experiment['id'], feature_variables_map) |
| 50 | + } |
| 51 | + variation_object['featureEnabled'] = variation['featureEnabled'] if @project_config.feature_experiment?(experiment['id']) |
| 52 | + variations_map.update(variation['key'] => variation_object) |
| 53 | + end |
| 54 | + } |
| 55 | + ) |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + # Merges feature key and type from feature variables to variation variables. |
| 60 | + def get_merged_variables_map(variation, experiment_id, feature_variables_map) |
| 61 | + feature_ids = @project_config.experiment_feature_map[experiment_id] |
| 62 | + return {} unless feature_ids |
| 63 | + |
| 64 | + experiment_feature_variables = feature_variables_map[feature_ids[0]] |
| 65 | + # temporary variation variables map to get values to merge. |
| 66 | + temp_variables_id_map = {} |
| 67 | + if variation['variables'] |
| 68 | + temp_variables_id_map = variation['variables'].reduce({}) do |variables_map, variable| |
| 69 | + variables_map.update( |
| 70 | + variable['id'] => { |
| 71 | + 'id' => variable['id'], |
| 72 | + 'value' => variable['value'] |
| 73 | + } |
| 74 | + ) |
| 75 | + end |
| 76 | + end |
| 77 | + experiment_feature_variables.reduce({}) do |variables_map, feature_variable| |
| 78 | + variation_variable = temp_variables_id_map[feature_variable['id']] |
| 79 | + variable_value = variation['featureEnabled'] && variation_variable ? variation_variable['value'] : feature_variable['defaultValue'] |
| 80 | + variables_map.update( |
| 81 | + feature_variable['key'] => { |
| 82 | + 'id' => feature_variable['id'], |
| 83 | + 'key' => feature_variable['key'], |
| 84 | + 'type' => feature_variable['type'], |
| 85 | + 'value' => variable_value |
| 86 | + } |
| 87 | + ) |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + def get_features_map(all_experiments_map) |
| 92 | + @project_config.feature_flags.reduce({}) do |features_map, feature| |
| 93 | + features_map.update( |
| 94 | + feature['key'] => { |
| 95 | + 'id' => feature['id'], |
| 96 | + 'key' => feature['key'], |
| 97 | + 'experimentsMap' => feature['experimentIds'].reduce({}) do |experiments_map, experiment_id| |
| 98 | + experiment_key = @project_config.experiment_id_map[experiment_id]['key'] |
| 99 | + experiments_map.update(experiment_key => all_experiments_map[experiment_key]) |
| 100 | + end, |
| 101 | + 'variablesMap' => feature['variables'].reduce({}) do |variables, variable| |
| 102 | + variables.update( |
| 103 | + variable['key'] => { |
| 104 | + 'id' => variable['id'], |
| 105 | + 'key' => variable['key'], |
| 106 | + 'type' => variable['type'], |
| 107 | + 'value' => variable['defaultValue'] |
| 108 | + } |
| 109 | + ) |
| 110 | + end |
| 111 | + } |
| 112 | + ) |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments