Skip to content

Raju/ups fsc #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 85 additions & 40 deletions core-api/src/main/java/com/optimizely/ab/Optimizely.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/****************************************************************************
* Copyright 2016-2023, Optimizely, Inc. and contributors *
* Copyright 2016-2024, Optimizely, Inc. and contributors *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
Expand Down Expand Up @@ -42,6 +42,7 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.ThreadSafe;

import java.io.Closeable;
import java.util.*;

Expand Down Expand Up @@ -1191,58 +1192,53 @@ private OptimizelyUserContext createUserContextCopy(@Nonnull String userId, @Non
return new OptimizelyUserContext(this, userId, attributes, Collections.EMPTY_MAP, null, false);
}

Optional<FeatureDecision> getForcedDecision(@Nonnull String flagKey,
@Nonnull DecisionReasons decisionReasons,
@Nonnull ProjectConfig projectConfig,
@Nonnull OptimizelyUserContext user) {

OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flagKey, null);
DecisionResponse<Variation> forcedDecisionVariation = decisionService.validatedForcedDecision(optimizelyDecisionContext, projectConfig, user);
decisionReasons.merge(forcedDecisionVariation.getReasons());
if (forcedDecisionVariation.getResult() != null) {
return Optional.of(new FeatureDecision(null, forcedDecisionVariation.getResult(), FeatureDecision.DecisionSource.FEATURE_TEST));
}

return Optional.empty();
}

OptimizelyDecision decide(@Nonnull OptimizelyUserContext user,
@Nonnull String key,
@Nonnull List<OptimizelyDecideOption> options) {

ProjectConfig projectConfig = getProjectConfig();
if (projectConfig == null) {
return OptimizelyDecision.newErrorDecision(key, user, DecisionMessage.SDK_NOT_READY.reason());
}
return decideForKeys(user, Arrays.asList(key), options).get(key);
}

FeatureFlag flag = projectConfig.getFeatureKeyMapping().get(key);
if (flag == null) {
return OptimizelyDecision.newErrorDecision(key, user, DecisionMessage.FLAG_KEY_INVALID.reason(key));
}

private OptimizelyDecision createOptimizelyDecision(
OptimizelyUserContext user,
String flagKey,
FeatureDecision flagDecision,
DecisionReasons decisionReasons,
List<OptimizelyDecideOption> allOptions,
ProjectConfig projectConfig
) {
String userId = user.getUserId();
Map<String, Object> attributes = user.getAttributes();
Boolean decisionEventDispatched = false;
List<OptimizelyDecideOption> allOptions = getAllOptions(options);
DecisionReasons decisionReasons = DefaultDecisionReasons.newInstance(allOptions);

Map<String, ?> copiedAttributes = new HashMap<>(attributes);
FeatureDecision flagDecision;

// Check Forced Decision
OptimizelyDecisionContext optimizelyDecisionContext = new OptimizelyDecisionContext(flag.getKey(), null);
DecisionResponse<Variation> forcedDecisionVariation = decisionService.validatedForcedDecision(optimizelyDecisionContext, projectConfig, user);
decisionReasons.merge(forcedDecisionVariation.getReasons());
if (forcedDecisionVariation.getResult() != null) {
flagDecision = new FeatureDecision(null, forcedDecisionVariation.getResult(), FeatureDecision.DecisionSource.FEATURE_TEST);
} else {
// Regular decision
DecisionResponse<FeatureDecision> decisionVariation = decisionService.getVariationForFeature(
flag,
user,
projectConfig,
allOptions);
flagDecision = decisionVariation.getResult();
decisionReasons.merge(decisionVariation.getReasons());
}

Boolean flagEnabled = false;
if (flagDecision.variation != null) {
if (flagDecision.variation.getFeatureEnabled()) {
flagEnabled = true;
}
}
logger.info("Feature \"{}\" is enabled for user \"{}\"? {}", key, userId, flagEnabled);
logger.info("Feature \"{}\" is enabled for user \"{}\"? {}", flagKey, userId, flagEnabled);

Map<String, Object> variableMap = new HashMap<>();
if (!allOptions.contains(OptimizelyDecideOption.EXCLUDE_VARIABLES)) {
DecisionResponse<Map<String, Object>> decisionVariables = getDecisionVariableMap(
flag,
projectConfig.getFeatureKeyMapping().get(flagKey),
flagDecision.variation,
flagEnabled);
variableMap = decisionVariables.getResult();
Expand All @@ -1261,22 +1257,28 @@ OptimizelyDecision decide(@Nonnull OptimizelyUserContext user,
// add to event metadata as well (currently set to experimentKey)
String ruleKey = flagDecision.experiment != null ? flagDecision.experiment.getKey() : null;


Boolean decisionEventDispatched = false;

Map<String, Object> attributes = user.getAttributes();
Map<String, ?> copiedAttributes = new HashMap<>(attributes);

if (!allOptions.contains(OptimizelyDecideOption.DISABLE_DECISION_EVENT)) {
decisionEventDispatched = sendImpression(
projectConfig,
flagDecision.experiment,
userId,
copiedAttributes,
flagDecision.variation,
key,
flagKey,
decisionSource.toString(),
flagEnabled);
}

DecisionNotification decisionNotification = DecisionNotification.newFlagDecisionNotificationBuilder()
.withUserId(userId)
.withAttributes(copiedAttributes)
.withFlagKey(key)
.withFlagKey(flagKey)
.withEnabled(flagEnabled)
.withVariables(variableMap)
.withVariationKey(variationKey)
Expand All @@ -1291,7 +1293,7 @@ OptimizelyDecision decide(@Nonnull OptimizelyUserContext user,
flagEnabled,
optimizelyJSON,
ruleKey,
key,
flagKey,
user,
reasonsToReport);
}
Expand All @@ -1303,18 +1305,61 @@ Map<String, OptimizelyDecision> decideForKeys(@Nonnull OptimizelyUserContext use

ProjectConfig projectConfig = getProjectConfig();
if (projectConfig == null) {
logger.error("Optimizely instance is not valid, failing isFeatureEnabled call.");
logger.error("Optimizely instance is not valid, failing decideForKeys call.");
return decisionMap;
}

if (keys.isEmpty()) return decisionMap;

List<OptimizelyDecideOption> allOptions = getAllOptions(options);

Map<String, FeatureDecision> flagDecisions = new HashMap<>();
Map<String, DecisionReasons> decisionReasonsMap = new HashMap<>();

List<FeatureFlag> flagsWithoutForcedDecision = new ArrayList<>();

List<String> validKeys = new ArrayList<>();

for (String key : keys) {
OptimizelyDecision decision = decide(user, key, options);
if (!allOptions.contains(OptimizelyDecideOption.ENABLED_FLAGS_ONLY) || decision.getEnabled()) {
decisionMap.put(key, decision);
FeatureFlag flag = projectConfig.getFeatureKeyMapping().get(key);
if (flag == null) {
decisionMap.put(key, OptimizelyDecision.newErrorDecision(key, user, DecisionMessage.FLAG_KEY_INVALID.reason(key)));
continue;
}

validKeys.add(key);

DecisionReasons decisionReasons = DefaultDecisionReasons.newInstance(allOptions);
Optional<FeatureDecision> forcedDecision = getForcedDecision(key, decisionReasons, projectConfig, user);
decisionReasonsMap.put(key, decisionReasons);

if (forcedDecision.isPresent()) {
flagDecisions.put(key, forcedDecision.get());
} else {
flagsWithoutForcedDecision.add(flag);
}
}

List<DecisionResponse<FeatureDecision>> decisionList =
decisionService.getVariationsForFeatureList(flagsWithoutForcedDecision, user, projectConfig, allOptions);

for (int i = 0; i < flagsWithoutForcedDecision.size(); i++) {
DecisionResponse<FeatureDecision> decision = decisionList.get(i);
String flagKey = flagsWithoutForcedDecision.get(i).getKey();
flagDecisions.put(flagKey, decision.getResult());
decisionReasonsMap.get(flagKey).merge(decision.getReasons());
}

for (String key: validKeys) {
FeatureDecision flagDecision = flagDecisions.get(key);
DecisionReasons decisionReasons = decisionReasonsMap.get((key));

OptimizelyDecision optimizelyDecision = createOptimizelyDecision(
user, key, flagDecision, decisionReasons, allOptions, projectConfig
);

if (!allOptions.contains(OptimizelyDecideOption.ENABLED_FLAGS_ONLY) || optimizelyDecision.getEnabled()) {
decisionMap.put(key, optimizelyDecision);
}
}

Expand Down
Loading
Loading