private bool MatchRule(LionUser user, RuleObj rule) { foreach (var condition in rule.Conditions) { var hasAttribute = false; var attributeName = ""; foreach (var key in user.Custom.Keys) { if (key.Equals(condition.UserAttribute.Name)) { hasAttribute = true; attributeName = key; break; } } if (!hasAttribute) { return(false); } else { var attributeValue = user.Custom[attributeName].ToString(); if (!LionMatchOperator.Match(condition.Operation, attributeValue, condition.ExpectValue)) { return(false); } } } return(true); }
/// <summary> /// variate the bool type feature flag /// </summary> /// <param name="key">feature flag key</param> /// <param name="user">the business user which will sent to the lion service</param> /// <param name="defaultValue">the default return value when there are any kind of exception occurred</param> /// <returns></returns> public bool BoolVariation(string key, LionUser user, bool defaultValue = false) { try { var feedbackUser = new LionUser(user.Key); //send user request event and save user if (!string.IsNullOrEmpty(user.Key)) { feedbackUser = SendFlagRequestEvent(key, user); } var requestUrl = string.Format("{0}/Flags/{1}", DefaultAPIUri, key); var response = _httpClient.GetAsync(requestUrl); if (response.Result.StatusCode != HttpStatusCode.OK) { return(defaultValue); } var result = response.Result.Content.ReadAsStringAsync().Result; var flag = JsonConvert.DeserializeObject <Models.FeatureFlagTargeting>(result); var feature = new Feature(flag); return(feature.Evaluate(feedbackUser, defaultValue)); } catch { return(defaultValue); } }
public static LionUser AndCustomAttribute(this LionUser user, string attribute, float value) { if (attribute == string.Empty) { throw new ArgumentException("Attribute Name can not be empty"); } user.Custom.Add(attribute, new JValue(value)); return(user); }
private LionUser SendFlagRequestEvent(string key, LionUser user) { var userAPI = string.Format("{0}/User", DefaultAPIUri); var httpContent = new StringContent(JsonConvert.SerializeObject(user), Encoding.UTF8, "application/json"); var response = _httpClient.PostAsync(userAPI, httpContent); if (response.Result.StatusCode != HttpStatusCode.OK) { return(new LionUser(user.Key) { Name = user.Name, Email = user.Email, Custom = user.Custom, }); } var result = response.Result.Content.ReadAsStringAsync().Result; return(JsonConvert.DeserializeObject <LionUser>(result)); }
private bool CheckUserBelongSegment(LionUser user, SegmentTargetingObj segment) { var includeUserKeys = (from item in segment.IncludeUsers select item.Key).ToList(); if (includeUserKeys.Contains(user.Key)) { return(true); } var excludeUserKeys = (from item in segment.ExcludeUsers select item.Key).ToList(); if (excludeUserKeys.Contains(user.Key)) { return(false); } foreach (var rule in segment.Rules) { if (MatchRule(user, rule)) { return(true); } } return(false); }
public static LionUser AndEmail(this LionUser user, string email) { user.Email = email; return(user); }
public static LionUser AndName(this LionUser user, string name) { user.Name = name; return(user); }
private bool MatchesUserTarget(LionUser user, bool defaultValue) { #region Targeting Off if (!_flag.IsTargeting) { var targetingOffVairation = (from item in _flag.FeatureFlagVariations where item.FeatureFlagVariationGuid.Equals(_flag.TargetingOffVariation) select item).FirstOrDefault(); if (targetingOffVairation != null) { return(Boolean.Parse(targetingOffVairation.Value)); } else { return(defaultValue); } } #endregion #region Targeting User var feature_on_targeting_users = _flag.FeatureOnTargetingUsers; if (feature_on_targeting_users.Any(i => i.Key.Equals(user.Key))) { return(true); } var feature_off_targeting_users = _flag.FeatureOffTargetingUsers; if (feature_off_targeting_users.Any(i => i.Key.Equals(user.Key))) { return(false); } #endregion #region Targeting Rule foreach (var rule in _flag.Rules) { var isMatchRule = true; foreach (var condition in rule.Conditions) { if (!isMatchRule) { break; } if (condition.UserAttribute.DataType.Equals("group")) { var segment = (from item in _flag.FeatureFlagSegments where item.SegmentGuid.ToString().Equals(condition.ExpectValue) select item).FirstOrDefault(); if (segment == null) { isMatchRule = false; break; } else { var userBelongSegment = CheckUserBelongSegment(user, segment); if ((!userBelongSegment && condition.UserAttribute.Name.Equals("用户在组中")) || (userBelongSegment && condition.UserAttribute.Name.Equals("用户不在组中"))) { isMatchRule = false; break; } } } else { var hasAttribute = false; var attributeName = ""; foreach (var key in user.Custom.Keys) { if (key.Equals(condition.UserAttribute.Name)) { hasAttribute = true; attributeName = key; break; } } if (!hasAttribute) { isMatchRule = false; break; } else { #region Get User Attibute Value var attributeValue = user.Custom[attributeName].ToString(); isMatchRule = LionMatchOperator.Match(condition.Operation, attributeValue, condition.ExpectValue); #endregion } } } //All conditions are passed if (isMatchRule) { var matchedVariation = (from item in _flag.FeatureFlagVariations where item.FeatureFlagVariationGuid.Equals(rule.VariationGuid) select item).FirstOrDefault(); if (matchedVariation != null) { return(Boolean.Parse(matchedVariation.Value)); } } } #endregion #region Default var defaultVariation = (from item in _flag.FeatureFlagVariations where item.FeatureFlagVariationGuid.Equals(_flag.DefaultVariation) select item).FirstOrDefault(); if (defaultVariation != null) { return(Boolean.Parse(defaultVariation.Value)); } else { return(defaultValue); } #endregion }
public bool Evaluate(LionUser user, bool defaultValue) { return(MatchesUserTarget(user, defaultValue)); }