コード例 #1
0
        protected UnlaunchFeature GetFeature(string flagKey, string identity, IEnumerable <UnlaunchAttribute> attributes)
        {
            if (!_flagMap.ContainsKey(flagKey))
            {
                throw new Exception("Flag doesn't exist");
            }
            var user = attributes == null?UnlaunchUser.Create(identity) : UnlaunchUser.CreateWithAttributes(identity, attributes);

            return(Evaluator.Evaluate(_flagMap[flagKey], user));
        }
コード例 #2
0
        private UnlaunchFeature Evaluate(string flagKey, string identity, IEnumerable <UnlaunchAttribute> attributes)
        {
            if (string.IsNullOrEmpty(flagKey))
            {
                throw new ArgumentException("flagKey must not be null or empty");
            }

            if (string.IsNullOrWhiteSpace(identity))
            {
                throw new ArgumentException($"userId must be a string and cannot contain any whitespace characters: {identity}");
            }

            if (_shutdownInitiated.Get())
            {
                Logger.Debug($"Asked to evaluate flag {flagKey} but shutdown already initiated on the client");
                return(UnlaunchConstants.GetControlFeatureByName(flagKey));
            }

            if (!IsReady())
            {
                Logger.Warn("the SDK is not ready. Returning the SDK default 'control' as variation which may not give the right result");
                return(UnlaunchConstants.GetControlFeatureByName(flagKey));
            }

            var user = attributes == null?UnlaunchUser.Create(identity) : UnlaunchUser.CreateWithAttributes(identity, attributes);

            FeatureFlag flag;

            try
            {
                flag = _dataStore.GetFlag(flagKey);
            }
            catch (Exception e)
            {
                return(new UnlaunchFeature(flagKey, UnlaunchConstants.FlagDefaultReturnType,
                                           null, $"there was an error fetching flag: {e.Message}"));
            }

            if (flag == null)
            {
                Logger.Warn($"UnlaunchFeature '{flagKey}' not found in the data store. Returning 'control' variation");
                return(new UnlaunchFeature(flagKey, UnlaunchConstants.FlagDefaultReturnType,
                                           null, "flag was not found in the in-memory cache"));
            }

            var result     = _evaluator.Evaluate(flag, user);
            var impression = new Impression
            {
                type             = UnlaunchConstants.EventTypeForImpressionEvents,
                key              = flag.Key,
                secondaryKey     = result.GetVariation(),
                flagKey          = flag.Key,
                flagStatus       = flag.Enabled.ToString(),
                evaluationReason = result.GetEvaluationReason(),
                userId           = user.GetId(),
                variationKey     = result.GetVariation()
            };

            Track(impression);

            return(result);
        }