예제 #1
0
        /// <summary>
        /// Determine whether a feature is enabled.
        /// Send an impression event if the user is bucketed into an experiment using the feature.
        /// </summary>
        /// <param name="experimentKey">The experiment key</param>
        /// <param name="userId">The user ID</param>
        /// <param name="userAttributes">The user's attributes.</param>
        /// <returns>True if feature is enabled, false or null otherwise</returns>
        public virtual bool IsFeatureEnabled(string featureKey, string userId, UserAttributes userAttributes = null)
        {
            if (string.IsNullOrEmpty(userId))
            {
                Logger.Log(LogLevel.ERROR, "User ID must not be empty.");
                return(false);
            }

            if (string.IsNullOrEmpty(featureKey))
            {
                Logger.Log(LogLevel.ERROR, "Feature flag key must not be empty.");
                return(false);
            }

            var featureFlag = Config.GetFeatureFlagFromKey(featureKey);

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

            if (!Validator.IsFeatureFlagValid(Config, featureFlag))
            {
                return(false);
            }

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

            if (decision != null)
            {
                if (decision.Source == FeatureDecision.DECISION_SOURCE_EXPERIMENT)
                {
                    SendImpressionEvent(decision.Experiment, decision.Variation, userId, userAttributes);
                }
                else
                {
                    Logger.Log(LogLevel.INFO, $@"The user ""{userId}"" is not being experimented on feature ""{featureKey}"".");
                }
                if (decision.Variation.IsFeatureEnabled)
                {
                    Logger.Log(LogLevel.INFO, $@"Feature flag ""{featureKey}"" is enabled for user ""{userId}"".");
                    return(true);
                }
            }


            Logger.Log(LogLevel.INFO, $@"Feature flag ""{featureKey}"" is not enabled for user ""{userId}"".");
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Determine whether a feature is enabled.
        /// Send an impression event if the user is bucketed into an experiment using the feature.
        /// </summary>
        /// <param name="featureKey">The feature key</param>
        /// <param name="userId">The user ID</param>
        /// <param name="userAttributes">The user's attributes.</param>
        /// <returns>True if feature is enabled, false or null otherwise</returns>
        public virtual bool IsFeatureEnabled(string featureKey, string userId, UserAttributes userAttributes = null)
        {
            var inputValues = new Dictionary <string, string>
            {
                { USER_ID, userId },
                { FEATURE_KEY, featureKey }
            };

            if (!ValidateStringInputs(inputValues))
            {
                return(false);
            }

            var featureFlag = Config.GetFeatureFlagFromKey(featureKey);

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

            if (!Validator.IsFeatureFlagValid(Config, featureFlag))
            {
                return(false);
            }

            bool featureEnabled = false;
            var  sourceInfo     = new Dictionary <string, string>();
            var  decision       = DecisionService.GetVariationForFeature(featureFlag, userId, userAttributes);

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

                if (decision.Source == FeatureDecision.DECISION_SOURCE_FEATURE_TEST)
                {
                    sourceInfo["experimentKey"] = decision.Experiment.Key;
                    sourceInfo["variationKey"]  = variation.Key;
                    SendImpressionEvent(decision.Experiment, variation, userId, userAttributes);
                }
                else
                {
                    Logger.Log(LogLevel.INFO, $@"The user ""{userId}"" is not being experimented on feature ""{featureKey}"".");
                }
            }

            if (featureEnabled == true)
            {
                Logger.Log(LogLevel.INFO, $@"Feature flag ""{featureKey}"" is enabled for user ""{userId}"".");
            }
            else
            {
                Logger.Log(LogLevel.INFO, $@"Feature flag ""{featureKey}"" is not enabled for user ""{userId}"".");
            }

            var decisionInfo = new Dictionary <string, object>
            {
                { "featureKey", featureKey },
                { "featureEnabled", featureEnabled },
                { "source", decision.Source },
                { "sourceInfo", sourceInfo },
            };

            NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Decision, DecisionNotificationTypes.FEATURE, userId,
                                                 userAttributes ?? new UserAttributes(), decisionInfo);
            return(featureEnabled);
        }
예제 #3
0
        /// <summary>
        /// Determine whether a feature is enabled.
        /// Send an impression event if the user is bucketed into an experiment using the feature.
        /// </summary>
        /// <param name="featureKey">The feature key</param>
        /// <param name="userId">The user ID</param>
        /// <param name="userAttributes">The user's attributes.</param>
        /// <returns>True if feature is enabled, false or null otherwise</returns>
        public virtual bool IsFeatureEnabled(string featureKey, string userId, UserAttributes userAttributes = null)
        {
            var config = ProjectConfigManager?.GetConfig();

            if (config == null)
            {
                Logger.Log(LogLevel.ERROR, "Datafile has invalid format. Failing 'IsFeatureEnabled'.");

                return(false);
            }

            var inputValues = new Dictionary <string, string>
            {
                { USER_ID, userId },
                { FEATURE_KEY, featureKey }
            };

            if (!ValidateStringInputs(inputValues))
            {
                return(false);
            }

            var featureFlag = config.GetFeatureFlagFromKey(featureKey);

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

            if (!Validator.IsFeatureFlagValid(config, featureFlag))
            {
                return(false);
            }

            bool featureEnabled = false;
            var  sourceInfo     = new Dictionary <string, string>();
            var  decision       = DecisionService.GetVariationForFeature(featureFlag, userId, config, userAttributes);
            var  variation      = decision.Variation;
            var  decisionSource = decision?.Source ?? FeatureDecision.DECISION_SOURCE_ROLLOUT;


            if (variation != null)
            {
                featureEnabled = variation.FeatureEnabled.GetValueOrDefault();

                // This information is only necessary for feature tests.
                // For rollouts experiments and variations are an implementation detail only.
                if (decision.Source == FeatureDecision.DECISION_SOURCE_FEATURE_TEST)
                {
                    sourceInfo["experimentKey"] = decision.Experiment.Key;
                    sourceInfo["variationKey"]  = variation.Key;
                }
                else
                {
                    Logger.Log(LogLevel.INFO, $@"The user ""{userId}"" is not being experimented on feature ""{featureKey}"".");
                }
            }

            if (featureEnabled == true)
            {
                Logger.Log(LogLevel.INFO, $@"Feature flag ""{featureKey}"" is enabled for user ""{userId}"".");
            }
            else
            {
                Logger.Log(LogLevel.INFO, $@"Feature flag ""{featureKey}"" is not enabled for user ""{userId}"".");
            }

            var decisionInfo = new Dictionary <string, object>
            {
                { "featureKey", featureKey },
                { "featureEnabled", featureEnabled },
                { "source", decision.Source },
                { "sourceInfo", sourceInfo },
            };

            SendImpressionEvent(decision.Experiment, variation, userId, userAttributes, config, featureKey, decisionSource, featureEnabled);

            NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Decision, DecisionNotificationTypes.FEATURE, userId,
                                                 userAttributes ?? new UserAttributes(), decisionInfo);
            return(featureEnabled);
        }