18
18
import com .optimizely .ab .annotations .VisibleForTesting ;
19
19
import com .optimizely .ab .bucketing .Bucketer ;
20
20
import com .optimizely .ab .bucketing .DecisionService ;
21
+ import com .optimizely .ab .bucketing .FeatureDecision ;
21
22
import com .optimizely .ab .bucketing .UserProfileService ;
22
23
import com .optimizely .ab .config .Attribute ;
23
24
import com .optimizely .ab .config .EventType ;
41
42
import com .optimizely .ab .internal .EventTagUtils ;
42
43
import com .optimizely .ab .notification .NotificationBroadcaster ;
43
44
import com .optimizely .ab .notification .NotificationListener ;
45
+
44
46
import org .slf4j .Logger ;
45
47
import org .slf4j .LoggerFactory ;
46
48
47
- import javax .annotation .CheckForNull ;
48
- import javax .annotation .Nonnull ;
49
- import javax .annotation .Nullable ;
50
- import javax .annotation .concurrent .ThreadSafe ;
51
49
import java .util .ArrayList ;
52
50
import java .util .Collections ;
53
51
import java .util .HashMap ;
54
52
import java .util .List ;
55
53
import java .util .Map ;
56
54
55
+ import javax .annotation .CheckForNull ;
56
+ import javax .annotation .Nonnull ;
57
+ import javax .annotation .Nullable ;
58
+ import javax .annotation .concurrent .ThreadSafe ;
59
+
57
60
/**
58
61
* Top-level container class for Optimizely functionality.
59
62
* Thread-safe, so can be created as a singleton and safely passed around.
@@ -324,34 +327,29 @@ public void track(@Nonnull String eventName,
324
327
@ Nonnull Map <String , String > attributes ) {
325
328
FeatureFlag featureFlag = projectConfig .getFeatureKeyMapping ().get (featureKey );
326
329
if (featureFlag == null ) {
327
- logger .info ("No feature flag was found for key \" " + featureKey + " \" ." );
330
+ logger .info ("No feature flag was found for key \" {} \" ." , featureKey );
328
331
return false ;
329
332
}
330
333
331
334
Map <String , String > filteredAttributes = filterAttributes (projectConfig , attributes );
332
335
333
- Variation variation = decisionService .getVariationForFeature (featureFlag , userId , filteredAttributes );
334
-
335
- if (variation != null ) {
336
- Experiment experiment = projectConfig .getExperimentForVariationId (variation .getId ());
337
- if (experiment != null ) {
338
- // the user is in an experiment for the feature
336
+ FeatureDecision featureDecision = decisionService .getVariationForFeature (featureFlag , userId , filteredAttributes );
337
+ if (featureDecision .variation != null ) {
338
+ if (featureDecision .decisionSource .equals (FeatureDecision .DecisionSource .EXPERIMENT )) {
339
339
sendImpression (
340
340
projectConfig ,
341
- experiment ,
341
+ featureDecision . experiment ,
342
342
userId ,
343
343
filteredAttributes ,
344
- variation );
345
- }
346
- else {
347
- logger .info ("The user \" " + userId +
348
- "\" is not being experimented on in feature \" " + featureKey + "\" ." );
344
+ featureDecision .variation );
345
+ } else {
346
+ logger .info ("The user \" {}\" is not included in an experiment for feature \" {}\" ." ,
347
+ userId , featureKey );
349
348
}
350
- logger .info ("Feature \" " + featureKey + " \" is enabled for user \" " + userId + " \" ." );
349
+ logger .info ("Feature \" {} \" is enabled for user \" {} \" ." , featureKey , userId );
351
350
return true ;
352
- }
353
- else {
354
- logger .info ("Feature \" " + featureKey + "\" is not enabled for user \" " + userId + "\" ." );
351
+ } else {
352
+ logger .info ("Feature \" {}\" is not enabled for user \" {}\" ." , featureKey , userId );
355
353
return false ;
356
354
}
357
355
}
@@ -433,10 +431,9 @@ public void track(@Nonnull String eventName,
433
431
if (variableValue != null ) {
434
432
try {
435
433
return Double .parseDouble (variableValue );
436
- }
437
- catch (NumberFormatException exception ) {
434
+ } catch (NumberFormatException exception ) {
438
435
logger .error ("NumberFormatException while trying to parse \" " + variableValue +
439
- "\" as Double. " + exception );
436
+ "\" as Double. " + exception );
440
437
}
441
438
}
442
439
return null ;
@@ -479,10 +476,9 @@ public void track(@Nonnull String eventName,
479
476
if (variableValue != null ) {
480
477
try {
481
478
return Integer .parseInt (variableValue );
482
- }
483
- catch (NumberFormatException exception ) {
479
+ } catch (NumberFormatException exception ) {
484
480
logger .error ("NumberFormatException while trying to parse \" " + variableValue +
485
- "\" as Integer. " + exception .toString ());
481
+ "\" as Integer. " + exception .toString ());
486
482
}
487
483
}
488
484
return null ;
@@ -531,17 +527,16 @@ String getFeatureVariableValueForType(@Nonnull String featureKey,
531
527
@ Nonnull LiveVariable .VariableType variableType ) {
532
528
FeatureFlag featureFlag = projectConfig .getFeatureKeyMapping ().get (featureKey );
533
529
if (featureFlag == null ) {
534
- logger .info ("No feature flag was found for key \" " + featureKey + " \" ." );
530
+ logger .info ("No feature flag was found for key \" {} \" ." , featureKey );
535
531
return null ;
536
532
}
537
533
538
534
LiveVariable variable = featureFlag .getVariableKeyToLiveVariableMap ().get (variableKey );
539
- if (variable == null ) {
540
- logger .info ("No feature variable was found for key \" " + variableKey + " \" in feature flag \" " +
541
- featureKey + " \" ." );
535
+ if (variable == null ) {
536
+ logger .info ("No feature variable was found for key \" {} \" in feature flag \" {} \" ." ,
537
+ variableKey , featureKey );
542
538
return null ;
543
- }
544
- else if (!variable .getType ().equals (variableType )) {
539
+ } else if (!variable .getType ().equals (variableType )) {
545
540
logger .info ("The feature variable \" " + variableKey +
546
541
"\" is actually of type \" " + variable .getType ().toString () +
547
542
"\" type. You tried to access it as type \" " + variableType .toString () +
@@ -551,23 +546,19 @@ else if (!variable.getType().equals(variableType)) {
551
546
552
547
String variableValue = variable .getDefaultValue ();
553
548
554
- Variation variation = decisionService .getVariationForFeature (featureFlag , userId , attributes );
555
-
556
- if (variation != null ) {
549
+ FeatureDecision featureDecision = decisionService .getVariationForFeature (featureFlag , userId , attributes );
550
+ if (featureDecision .variation != null ) {
557
551
LiveVariableUsageInstance liveVariableUsageInstance =
558
- variation .getVariableIdToLiveVariableUsageInstanceMap ().get (variable .getId ());
552
+ featureDecision . variation .getVariableIdToLiveVariableUsageInstanceMap ().get (variable .getId ());
559
553
if (liveVariableUsageInstance != null ) {
560
554
variableValue = liveVariableUsageInstance .getValue ();
561
- }
562
- else {
555
+ } else {
563
556
variableValue = variable .getDefaultValue ();
564
557
}
565
- }
566
- else {
567
- logger .info ("User \" " + userId +
568
- "\" was not bucketed into any variation for feature flag \" " + featureKey +
569
- "\" . The default value \" " + variableValue +
570
- "\" for \" " + variableKey + "\" is being returned."
558
+ } else {
559
+ logger .info ("User \" {}\" was not bucketed into any variation for feature flag \" {}\" . " +
560
+ "The default value \" {}\" for \" {}\" is being returned." ,
561
+ userId , featureKey , variableValue , variableKey
571
562
);
572
563
}
573
564
0 commit comments