Exemplo n.º 1
0
        /// <inheritdoc/>
        public FeatureFlagsState AllFlagsState(User user, params FlagsStateOption[] options)
        {
            if (IsOffline())
            {
                Log.Warn("AllFlagsState() was called when client is in offline mode. Returning empty state.");
                return(new FeatureFlagsState(false));
            }
            if (!Initialized())
            {
                if (_featureStore.Initialized())
                {
                    Log.Warn("AllFlagsState() called before client initialized; using last known values from feature store");
                }
                else
                {
                    Log.Warn("AllFlagsState() called before client initialized; feature store unavailable, returning empty state");
                    return(new FeatureFlagsState(false));
                }
            }
            if (user == null || user.Key == null)
            {
                Log.Warn("AllFlagsState() called with null user or null user key. Returning empty state");
                return(new FeatureFlagsState(false));
            }

            var state                = new FeatureFlagsState(true);
            var clientSideOnly       = FlagsStateOption.HasOption(options, FlagsStateOption.ClientSideOnly);
            var withReasons          = FlagsStateOption.HasOption(options, FlagsStateOption.WithReasons);
            var detailsOnlyIfTracked = FlagsStateOption.HasOption(options, FlagsStateOption.DetailsOnlyForTrackedFlags);
            IDictionary <string, FeatureFlag> flags = _featureStore.All(VersionedDataKind.Features);

            foreach (KeyValuePair <string, FeatureFlag> pair in flags)
            {
                var flag = pair.Value;
                if (clientSideOnly && !flag.ClientSide)
                {
                    continue;
                }
                try
                {
                    FeatureFlag.EvalResult result = flag.Evaluate(user, _featureStore, EventFactory.Default);
                    state.AddFlag(flag, result.Result.Value.InnerValue, result.Result.VariationIndex,
                                  withReasons ? result.Result.Reason : null, detailsOnlyIfTracked);
                }
                catch (Exception e)
                {
                    Log.ErrorFormat("Exception caught for feature flag \"{0}\" when evaluating all flags: {1}", flag.Key, Util.ExceptionMessage(e));
                    Log.Debug(e.ToString(), e);
                    EvaluationReason reason = new EvaluationReason.Error(EvaluationErrorKind.EXCEPTION);
                    state.AddFlag(flag, null, null, withReasons ? reason : null, detailsOnlyIfTracked);
                }
            }
            return(state);
        }
 internal FeatureFlagsStateBuilder(FlagsStateOption[] options)
 {
     _detailsOnlyIfTracked = FlagsStateOption.HasOption(options, FlagsStateOption.DetailsOnlyForTrackedFlags);
     _withReasons          = FlagsStateOption.HasOption(options, FlagsStateOption.WithReasons);
 }