Skip to content

Commit

Permalink
feat: Statsig provider evaluate boolean updates (#691)
Browse files Browse the repository at this point in the history
Signed-off-by: liran2000 <liran2000@gmail.com>
  • Loading branch information
liran2000 authored Feb 26, 2024
1 parent 66d19e2 commit 04df666
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
8 changes: 7 additions & 1 deletion providers/statsig/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
<!-- x-release-please-end-version -->

## Concepts
* Boolean evaluation gets [gate](https://docs.statsig.com/server/javaSdk#checking-a-gate) status.
* String/Integer/Double evaluations evaluation gets [Dynamic config](https://docs.statsig.com/server/javaSdk#reading-a-dynamic-config) or [Layer](https://docs.statsig.com/server/javaSdk#getting-an-layerexperiment) evaluation.
As the key represents an inner attribute, feature config is required as a parameter with data needed for evaluation.
For an example of dynamic config of product alias, need to differentiate between dynamic config or layer, and the dynamic config name.
* Boolean evaluation gets [gate](https://docs.statsig.com/server/javaSdk#checking-a-gate) status when feature config is not passed.
When feature config exists, it evaluates to the config/layer attribute, similar to String/Integer/Float evaluations.
* Object evaluation gets a structure representing the dynamic config or layer.
* [Private Attributes](https://docs.statsig.com/server/javaSdk#private-attributes) are supported as 'privateAttributes' context key.

Expand Down Expand Up @@ -71,3 +72,8 @@ Unit test based on Statsig [Local Overrides](https://docs.statsig.com/server/jav
As it is limited, evaluation context based tests are limited.
See [statsigProviderTest](./src/test/java/dev/openfeature/contrib/providers/statsig/StatsigProviderTest.java)
for more information.

## Known issues
- Gate BooleanEvaluation with default value true cannot fallback to true.
https://github.com/statsig-io/java-server-sdk/issues/22

Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,27 @@ public Metadata getMetadata() {
public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defaultValue, EvaluationContext ctx) {
verifyEvaluation();
StatsigUser user = ContextTransformer.transform(ctx);
Future<Boolean> featureOn = Statsig.checkGateAsync(user, key);
Boolean evaluatedValue = featureOn.get();
Boolean evaluatedValue = defaultValue;
try {
FeatureConfig featureConfig = parseFeatureConfig(ctx);
switch (featureConfig.getType()) {
case CONFIG:
DynamicConfig dynamicConfig = fetchDynamicConfig(user, featureConfig);
evaluatedValue = dynamicConfig.getBoolean(key, defaultValue);
break;
case LAYER:
Layer layer = fetchLayer(user, featureConfig);
evaluatedValue = layer.getBoolean(key, defaultValue);
break;
default:
break;
}
} catch (Exception e) {
log.debug("could not fetch feature config. checking gate {}.", key);
Future<Boolean> featureOn = Statsig.checkGateAsync(user, key);
evaluatedValue = featureOn.get();
}

return ProviderEvaluation.<Boolean>builder()
.value(evaluatedValue)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static void setUp() {
private static void buildFlags() {
Statsig.overrideGate(FLAG_NAME, true);
Map<String, Object> configMap = new HashMap<>();
configMap.put("boolean", true);
configMap.put("alias", "test");
configMap.put("revision", INT_FLAG_VALUE);
configMap.put("price", DOUBLE_FLAG_VALUE);
Expand Down Expand Up @@ -118,6 +119,17 @@ void getBooleanEvaluation() {
assertEquals(true, client.getBooleanValue(FLAG_NAME, false));
assertEquals(false, statsigProvider.getBooleanEvaluation("non-existing", false, new ImmutableContext()).getValue());
assertEquals(false, client.getBooleanValue("non-existing", false));

// expected to succeed when https://github.com/statsig-io/java-server-sdk/issues/22 is resolved and adopted
// assertEquals(true, client.getBooleanValue("non-existing", true));

MutableContext evaluationContext = new MutableContext();
MutableContext featureConfig = new MutableContext();
featureConfig.add("type", "CONFIG");
featureConfig.add("name", "product");
evaluationContext.add("feature_config", featureConfig);
assertEquals(true, statsigProvider.getBooleanEvaluation("boolean", false,
evaluationContext).getValue());
}

@Test
Expand Down

0 comments on commit 04df666

Please sign in to comment.