Ejemplo n.º 1
0
        private object GetTypeCastedVariableValue(string value, FeatureVariable.VariableType type)
        {
            object result = null;

            switch (type)
            {
            case FeatureVariable.VariableType.BOOLEAN:
                bool.TryParse(value, out bool booleanValue);
                result = booleanValue;
                break;

            case FeatureVariable.VariableType.DOUBLE:
                double.TryParse(value, out double doubleValue);
                result = doubleValue;
                break;

            case FeatureVariable.VariableType.INTEGER:
                int.TryParse(value, out int intValue);
                result = intValue;
                break;

            case FeatureVariable.VariableType.STRING:
                result = value;
                break;
            }

            if (result == null)
            {
                Logger.Log(LogLevel.ERROR, $@"Unable to cast variable value ""{value}"" to type ""{type}"".");
            }

            return(result);
        }
Ejemplo n.º 2
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 string GetFeatureVariableValueForType(string featureKey, string variableKey, string userId,
                                                             UserAttributes userAttributes, FeatureVariable.VariableType variableType)
        {
            if (string.IsNullOrEmpty(featureKey))
            {
                Logger.Log(LogLevel.ERROR, "Feature flag key must not be empty.");
                return(null);
            }

            if (string.IsNullOrEmpty(variableKey))
            {
                Logger.Log(LogLevel.ERROR, "Variable key must not be empty.");
                return(null);
            }

            if (string.IsNullOrEmpty(userId))
            {
                Logger.Log(LogLevel.ERROR, "User ID must not be empty.");
                return(null);
            }

            var featureFlag = Config.GetFeatureFlagFromKey(featureKey);

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

            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(null);
            }
            else if (featureVariable.Type != variableType)
            {
                Logger.Log(LogLevel.ERROR,
                           $@"Variable is of type ""{featureVariable.Type}"", but you requested it as type ""{variableType}"".");
                return(null);
            }

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

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

                if (featureVariableUsageInstance != null)
                {
                    variableValue = featureVariableUsageInstance.Value;
                    Logger.Log(LogLevel.INFO,
                               $@"Returning variable value ""{variableValue}"" for variation ""{variation.Key}"" of feature flag ""{featureFlag.Key}"".");
                }
                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 ""{featureFlag.Key}"", returning default value ""{variableValue}"".");
            }

            return(variableValue);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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 string GetFeatureVariableValueForType(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(null);
            }

            var featureFlag = Config.GetFeatureFlagFromKey(featureKey);

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

            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(null);
            }
            else if (featureVariable.Type != variableType)
            {
                Logger.Log(LogLevel.ERROR,
                           $@"Variable is of type ""{featureVariable.Type}"", but you requested it as type ""{variableType}"".");
                return(null);
            }

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

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

                if (featureVariableUsageInstance != null)
                {
                    variableValue = featureVariableUsageInstance.Value;
                    Logger.Log(LogLevel.INFO,
                               $@"Returning variable value ""{variableValue}"" for variation ""{variation.Key}"" of feature flag ""{featureFlag.Key}"".");
                }
                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 ""{featureFlag.Key}"", returning default value ""{variableValue}"".");
            }

            return(variableValue);
        }