Skip to content

Commit 9c8cb72

Browse files
committed
Removed getVariationFromExperiment as it was extra loop fixed some bugs and other minor issue due to which tests were failing
1 parent 1522796 commit 9c8cb72

File tree

8 files changed

+31
-102
lines changed

8 files changed

+31
-102
lines changed

OptimizelySDK.Net35/OptimizelySDK.Net35.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,9 @@
322322
</Compile>
323323
<Compile Include="..\OptimizelySDK\OptimizelyDecisionContext.cs">
324324
<Link>OptimizelyDecisionContext.cs</Link>
325+
</Compile>
326+
<Compile Include="..\OptimizelySDK\ForcedDecisionsStore.cs">
327+
<Link>ForcedDecisionsStore.cs</Link>
325328
</Compile>
326329
<Compile Include="..\OptimizelySDK\OptimizelyForcedDecision.cs">
327330
<Link>OptimizelyForcedDecision.cs</Link>

OptimizelySDK.Net40/OptimizelySDK.Net40.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@
345345
</Compile>
346346
<Compile Include="..\OptimizelySDK\OptimizelyDecisionContext.cs">
347347
<Link>OptimizelyDecisionContext.cs</Link>
348+
</Compile>
349+
<Compile Include="..\OptimizelySDK\ForcedDecisionsStore.cs">
350+
<Link>ForcedDecisionsStore.cs</Link>
348351
</Compile>
349352
<Compile Include="..\OptimizelySDK\OptimizelyForcedDecision.cs">
350353
<Link>OptimizelyForcedDecision.cs</Link>

OptimizelySDK.NetStandard16/OptimizelySDK.NetStandard16.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<Compile Include="..\OptimizelySDK\OptimizelyUserContext.cs" />
9797
<Compile Include="..\OptimizelySDK\Config\FallbackProjectConfigManager.cs" />
9898
<Compile Include="..\OptimizelySDK\OptimizelyDecisionContext.cs" />
99+
<Compile Include="..\OptimizelySDK\ForcedDecisionsStore.cs" />
99100
<Compile Include="..\OptimizelySDK\OptimizelyForcedDecision.cs">
100101
<Link>OptimizelyForcedDecision.cs</Link>
101102
</Compile>

OptimizelySDK.NetStandard20/OptimizelySDK.NetStandard20.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,9 @@
307307
<Compile Include="..\OptimizelySDK\OptimizelyDecisionContext.cs">
308308
<Link>OptimizelyDecisionContext.cs</Link>
309309
</Compile>
310+
<Compile Include="..\OptimizelySDK\ForcedDecisionsStore.cs">
311+
<Link>ForcedDecisionsStore.cs</Link>
312+
</Compile>
310313
<Compile Include="..\OptimizelySDK\OptimizelyForcedDecision.cs">
311314
<Link>OptimizelyForcedDecision.cs</Link>
312315
</Compile>

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -501,47 +501,23 @@ public virtual Result<FeatureDecision> GetVariationForFeatureExperiment(FeatureF
501501

502502
if (string.IsNullOrEmpty(experiment.Key))
503503
continue;
504+
505+
var decisionVariation = GetVariationFromExperimentRule(config, featureFlag.Key, experiment, user, options);
506+
reasons += decisionVariation?.DecisionReasons;
504507

505-
var variationResult = GetVariationFromExperiment(config, featureFlag, experiment, user, options);
506-
reasons += variationResult.DecisionReasons;
508+
var variation = decisionVariation?.ResultObject;
507509

508-
if (variationResult?.ResultObject?.Experiment != null && variationResult?.ResultObject?.Variation?.Id != null)
510+
if (variation?.Id != null)
509511
{
510512
Logger.Log(LogLevel.INFO, reasons.AddInfo($"The user \"{userId}\" is bucketed into experiment \"{experiment.Key}\" of feature \"{featureFlag.Key}\"."));
511-
return Result<FeatureDecision>.NewResult(new FeatureDecision(experiment, variationResult.ResultObject.Variation, FeatureDecision.DECISION_SOURCE_FEATURE_TEST), reasons);
512-
}
513-
}
514-
515-
Logger.Log(LogLevel.INFO, reasons.AddInfo($"The user \"{userId}\" is not bucketed into any of the experiments on the feature \"{featureFlag.Key}\"."));
516-
return Result<FeatureDecision>.NullResult(reasons);
517-
}
518-
519-
520-
521-
private Result<FeatureDecision> GetVariationFromExperiment(ProjectConfig config, FeatureFlag flag, Experiment experiment, OptimizelyUserContext user, OptimizelyDecideOption[] options)
522-
{
523-
var reasons = new DecisionReasons();
524513

525-
if (flag.ExperimentIds.Any())
526-
{
527-
foreach (var expId in flag.ExperimentIds)
528-
{
529-
config.ExperimentIdMap.TryGetValue(expId, out var exp);
530-
531-
var decisionVariation = GetVariationFromExperimentRule(config, flag.Key, experiment, user, options);
532-
reasons += decisionVariation?.DecisionReasons;
533-
534-
var variation = decisionVariation?.ResultObject;
535-
536-
if (variation != null)
537-
{
538-
var featureDecision = new FeatureDecision(exp, variation, FeatureDecision.DECISION_SOURCE_FEATURE_TEST);
539-
540-
return Result<FeatureDecision>.NewResult(featureDecision, reasons);
541-
}
514+
var featureDecision = new FeatureDecision(experiment, variation, FeatureDecision.DECISION_SOURCE_FEATURE_TEST);
515+
return Result<FeatureDecision>.NewResult(featureDecision, reasons);
542516
}
517+
543518
}
544519

520+
Logger.Log(LogLevel.INFO, reasons.AddInfo($"The user \"{userId}\" is not bucketed into any of the experiments on the feature \"{featureFlag.Key}\"."));
545521
return Result<FeatureDecision>.NullResult(reasons);
546522
}
547523

OptimizelySDK/OptimizelyDecisionContext.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
namespace OptimizelySDK
1818
{
1919
/// <summary>
20-
/// ///TODO: Add documentation.
20+
/// OptimizelyDecisionContext contains flag key and rule key to be used for setting
21+
/// and getting forced decision.
2122
/// </summary>
2223
public class OptimizelyDecisionContext
2324
{

OptimizelySDK/OptimizelySDK.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
<Compile Include="OptimizelyDecisions\OptimizelyDecideOption.cs" />
9393
<Compile Include="OptimizelyDecisions\OptimizelyDecision.cs" />
9494
<Compile Include="OptimizelyForcedDecision.cs" />
95+
<Compile Include="ForcedDecisionsStore.cs" />
9596
<Compile Include="OptimizelyUserContext.cs" />
9697
<Compile Include="OptimizelyJSON.cs" />
9798
<Compile Include="Entity\Rollout.cs" />

OptimizelySDK/OptimizelyUserContext.cs

Lines changed: 9 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -23,63 +23,6 @@
2323

2424
namespace OptimizelySDK
2525
{
26-
27-
public class ForcedDecisionsStore
28-
{
29-
public const string OPTI_NULL_RULE_KEY = "$opt-null-rule-key";
30-
31-
private Dictionary<string, Dictionary<string, OptimizelyForcedDecision>> ForcedDecisionsMap { get; set; }
32-
33-
public ForcedDecisionsStore()
34-
{
35-
ForcedDecisionsMap = new Dictionary<string, Dictionary<string, OptimizelyForcedDecision>>();
36-
}
37-
38-
public int Count {
39-
get {
40-
return ForcedDecisionsMap.Count;
41-
}
42-
}
43-
public bool Remove(OptimizelyDecisionContext context)
44-
{
45-
if (string.IsNullOrEmpty(context.RuleKey)) {
46-
return ForcedDecisionsMap.Remove(context.FlagKey);
47-
} else if (ForcedDecisionsMap.TryGetValue(context.FlagKey, out var flagDecisionMap)) {
48-
return flagDecisionMap.Remove(context.RuleKey);
49-
}
50-
51-
return false;
52-
}
53-
54-
public void RemoveAll()
55-
{
56-
ForcedDecisionsMap.Clear();
57-
}
58-
59-
public OptimizelyForcedDecision this[OptimizelyDecisionContext context]
60-
{
61-
get {
62-
if (context != null && !string.IsNullOrEmpty(context.FlagKey)
63-
&& ForcedDecisionsMap.TryGetValue(context.FlagKey, out Dictionary<string, OptimizelyForcedDecision> flagForcedDecision)) {
64-
var ruleKey = context.RuleKey ?? OPTI_NULL_RULE_KEY;
65-
flagForcedDecision.TryGetValue(ruleKey, out var forcedDecision);
66-
return forcedDecision;
67-
68-
}
69-
return null;
70-
}
71-
set {
72-
if (context != null && !string.IsNullOrEmpty(context.FlagKey)) {
73-
var ruleKey = context.RuleKey ?? OPTI_NULL_RULE_KEY;
74-
ForcedDecisionsMap[context.FlagKey] = new Dictionary<string, OptimizelyForcedDecision> {
75-
{ ruleKey, value }
76-
};
77-
}
78-
}
79-
80-
}
81-
}
82-
8326
/// <summary>
8427
/// OptimizelyUserContext defines user contexts that the SDK will use to make decisions for
8528
/// </summary>
@@ -103,13 +46,6 @@ public class OptimizelyUserContext
10346
public OptimizelyUserContext(Optimizely optimizely, string userId, UserAttributes userAttributes, IErrorHandler errorHandler, ILogger logger) :
10447
this(optimizely, userId, userAttributes, null, errorHandler, logger)
10548
{
106-
107-
//ErrorHandler = errorHandler;
108-
//Logger = logger;
109-
//Optimizely = optimizely;
110-
//Attributes = userAttributes ?? new UserAttributes();
111-
//ForcedDecisionsStore = new ForcedDecisionsStore();
112-
//UserId = userId;
11349
}
11450

11551
public OptimizelyUserContext(Optimizely optimizely, string userId, UserAttributes userAttributes, ForcedDecisionsStore forcedDecisionsStore, IErrorHandler errorHandler, ILogger logger)
@@ -327,8 +263,7 @@ public OptimizelyForcedDecision GetForcedDecision(OptimizelyDecisionContext cont
327263
/// <summary>
328264
/// Removes a forced decision.
329265
/// </summary>
330-
/// <param name="flagKey">The flag key.</param>
331-
/// <param name="ruleKey"></param>
266+
/// <param name="context">The context object containing flag and rule key.</param>
332267
/// <returns>Whether the item was removed.</returns>
333268
public bool RemoveForcedDecision(OptimizelyDecisionContext context)
334269
{
@@ -338,6 +273,12 @@ public bool RemoveForcedDecision(OptimizelyDecisionContext context)
338273
return false;
339274
}
340275

276+
if (!Optimizely.IsValid)
277+
{
278+
Logger.Log(LogLevel.ERROR, DecisionMessage.SDK_NOT_READY);
279+
return false;
280+
}
281+
341282
return ForcedDecisionsStore.Remove(context);
342283
}
343284

@@ -363,8 +304,8 @@ public bool RemoveAllForcedDecisions()
363304
/// <summary>
364305
/// Finds a validated forced decision.
365306
/// </summary>
366-
/// <param name="context">The flag key.</param> ///TODO: Need to correct it.
367-
/// <param name="ruleKey">The rule key.</param>
307+
/// <param name="context">Object containing flag and rule key of which forced decision is set.</param>
308+
/// <param name="config">The Project config.</param>
368309
/// <returns>A result with the variation</returns>
369310
public Result<Variation> FindValidatedForcedDecision(OptimizelyDecisionContext context, ProjectConfig config)
370311
{

0 commit comments

Comments
 (0)