Exemple #1
0
        /// <summary>
        /// Gets the feature variable value for given type.
        /// </summary>
        /// <param name="featureKey">The feature flag key</param>
        /// <param name="variableKey">The variable key</param>
        /// <param name="userId">The user ID</param>
        /// <param name="userAttributes">The user's attributes</param>
        /// <param name="variableType">Variable type</param>
        /// <returns>string | null Feature variable value</returns>
        public virtual T GetFeatureVariableValueForType <T>(string featureKey, string variableKey, string userId,
                                                            UserAttributes userAttributes, FeatureVariable.VariableType variableType)
        {
            var inputValues = new Dictionary <string, string>
            {
                { USER_ID, userId },
                { FEATURE_KEY, featureKey },
                { VARIABLE_KEY, variableKey }
            };

            if (!ValidateStringInputs(inputValues))
            {
                return(default(T));
            }

            var featureFlag = Config.GetFeatureFlagFromKey(featureKey);

            if (string.IsNullOrEmpty(featureFlag.Key))
            {
                return(default(T));
            }

            var featureVariable = featureFlag.GetFeatureVariableFromKey(variableKey);

            if (featureVariable == null)
            {
                Logger.Log(LogLevel.ERROR,
                           $@"No feature variable was found for key ""{variableKey}"" in feature flag ""{featureKey}"".");
                return(default(T));
            }
            else if (featureVariable.Type != variableType)
            {
                Logger.Log(LogLevel.ERROR,
                           $@"Variable is of type ""{featureVariable.Type}"", but you requested it as type ""{variableType}"".");
                return(default(T));
            }

            var featureEnabled = false;
            var variableValue  = featureVariable.DefaultValue;
            var decision       = DecisionService.GetVariationForFeature(featureFlag, userId, userAttributes);

            if (decision.Variation != null)
            {
                var variation = decision.Variation;
                featureEnabled = variation.FeatureEnabled.GetValueOrDefault();
                var featureVariableUsageInstance = variation.GetFeatureVariableUsageFromId(featureVariable.Id);

                if (featureVariableUsageInstance != null)
                {
                    if (variation.FeatureEnabled == true)
                    {
                        variableValue = featureVariableUsageInstance.Value;
                        Logger.Log(LogLevel.INFO, $@"Returning variable value ""{variableValue}"" for variation ""{variation.Key}"" of feature flag ""{featureKey}"".");
                    }
                    else
                    {
                        Logger.Log(LogLevel.INFO, $@"Feature ""{featureKey}"" is not enabled for user {userId}. Returning default value for variable ""{variableKey}"".");
                    }
                }
                else
                {
                    Logger.Log(LogLevel.INFO, $@"Variable ""{variableKey}"" is not used in variation ""{variation.Key}"", returning default value ""{variableValue}"".");
                }
            }
            else
            {
                Logger.Log(LogLevel.INFO,
                           $@"User ""{userId}"" is not in any variation for feature flag ""{featureKey}"", returning default value ""{variableValue}"".");
            }

            var sourceInfo = new Dictionary <string, string>();

            if (decision?.Source == FeatureDecision.DECISION_SOURCE_FEATURE_TEST)
            {
                sourceInfo["experimentKey"] = decision.Experiment.Key;
                sourceInfo["variationKey"]  = decision.Variation.Key;
            }

            var typeCastedValue = GetTypeCastedVariableValue(variableValue, variableType);
            var decisionInfo    = new Dictionary <string, object>
            {
                { "featureKey", featureKey },
                { "featureEnabled", featureEnabled },
                { "variableKey", variableKey },
                { "variableValue", typeCastedValue },
                { "variableType", variableType.ToString().ToLower() },
                { "source", decision?.Source },
                { "sourceInfo", sourceInfo },
            };

            NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Decision, DecisionNotificationTypes.FEATURE_VARIABLE, userId,
                                                 userAttributes ?? new UserAttributes(), decisionInfo);
            return((T)typeCastedValue);
        }