11package dev .openfeature .contrib .providers .statsig ;
22
3- import com .statsig .sdk .APIFeatureGate ;
4- import com .statsig .sdk .DynamicConfig ;
5- import com .statsig .sdk .EvaluationReason ;
6- import com .statsig .sdk .Layer ;
7- import com .statsig .sdk .Statsig ;
8- import com .statsig .sdk .StatsigUser ;
3+ import com .statsig .DynamicConfig ;
4+ import com .statsig .FeatureGate ;
5+ import com .statsig .Layer ;
6+ import com .statsig .Statsig ;
7+ import com .statsig .StatsigUser ;
98import dev .openfeature .sdk .EvaluationContext ;
109import dev .openfeature .sdk .EventProvider ;
1110import dev .openfeature .sdk .Metadata ;
1413import dev .openfeature .sdk .Structure ;
1514import dev .openfeature .sdk .Value ;
1615import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
17- import java .util .ArrayList ;
18- import java .util .List ;
19- import java .util .concurrent .Future ;
16+ import java .util .concurrent .CompletableFuture ;
2017import lombok .AllArgsConstructor ;
2118import lombok .Getter ;
2219import lombok .SneakyThrows ;
2320import lombok .extern .slf4j .Slf4j ;
24- import org .jetbrains .annotations .NotNull ;
2521
2622/** Provider implementation for Statsig. */
2723@ Slf4j
@@ -33,6 +29,9 @@ public class StatsigProvider extends EventProvider {
3329 private static final String FEATURE_CONFIG_KEY = "feature_config" ;
3430 private final StatsigProviderConfig statsigProviderConfig ;
3531
32+ @ Getter
33+ private Statsig statsig ;
34+
3635 /**
3736 * Constructor.
3837 *
@@ -50,8 +49,8 @@ public StatsigProvider(StatsigProviderConfig statsigProviderConfig) {
5049 */
5150 @ Override
5251 public void initialize (EvaluationContext evaluationContext ) throws Exception {
53- Future < Void > initFuture =
54- Statsig . initializeAsync ( statsigProviderConfig . getSdkKey (), statsigProviderConfig . getOptions () );
52+ statsig = new Statsig ( statsigProviderConfig . getSdkKey (), statsigProviderConfig . getOptions ());
53+ CompletableFuture < Void > initFuture = statsig . initialize ( );
5554 initFuture .get ();
5655
5756 statsigProviderConfig .postInit ();
@@ -74,13 +73,14 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
7473 Value featureConfigValue = ctx .getValue (FEATURE_CONFIG_KEY );
7574 String reason = null ;
7675 if (featureConfigValue == null ) {
77- APIFeatureGate featureGate = Statsig .getFeatureGate (user , key );
78- reason = featureGate .getReason ().getReason ();
76+ FeatureGate featureGate = statsig .getFeatureGate (user , key );
77+ reason = featureGate .getEvaluationDetails ().getReason ();
78+ evaluatedValue = featureGate .getValue ();
7979
8080 // in case of evaluation failure, remain with default value.
81- if (!assumeFailure (featureGate )) {
82- evaluatedValue = featureGate .getValue ();
83- }
81+ // if (!assumeFailure(featureGate)) {
82+ // evaluatedValue = featureGate.getValue();
83+ // }
8484 } else {
8585 FeatureConfig featureConfig = parseFeatureConfig (ctx );
8686 switch (featureConfig .getType ()) {
@@ -107,13 +107,13 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
107107 https://github.com/statsig-io/java-server-sdk/issues/22#issuecomment-2002346349
108108 failure is assumed by reason, since success status is not returned.
109109 */
110- private boolean assumeFailure (APIFeatureGate featureGate ) {
111- EvaluationReason reason = featureGate .getReason ();
112- return EvaluationReason .DEFAULT .equals (reason )
113- || EvaluationReason .UNINITIALIZED .equals (reason )
114- || EvaluationReason .UNRECOGNIZED .equals (reason )
115- || EvaluationReason .UNSUPPORTED .equals (reason );
116- }
110+ // private boolean assumeFailure(FeatureGate featureGate) {
111+ // EvaluationReason reason = featureGate.getEvaluationDetails() .getReason();
112+ // return EvaluationReason.DEFAULT.equals(reason)
113+ // || EvaluationReason.UNINITIALIZED.equals(reason)
114+ // || EvaluationReason.UNRECOGNIZED.equals(reason)
115+ // || EvaluationReason.UNSUPPORTED.equals(reason);
116+ // }
117117
118118 @ Override
119119 public ProviderEvaluation <String > getStringEvaluation (String key , String defaultValue , EvaluationContext ctx ) {
@@ -198,26 +198,19 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
198198
199199 @ SneakyThrows
200200 protected DynamicConfig fetchDynamicConfig (StatsigUser user , FeatureConfig featureConfig ) {
201- return Statsig . getConfigAsync (user , featureConfig .getName ()). get ( );
201+ return statsig . getDynamicConfig (user , featureConfig .getName ());
202202 }
203203
204204 @ SneakyThrows
205205 protected Layer fetchLayer (StatsigUser user , FeatureConfig featureConfig ) {
206- return Statsig . getLayerAsync (user , featureConfig .getName ()). get ( );
206+ return statsig . getLayer (user , featureConfig .getName ());
207207 }
208208
209209 private Value toValue (DynamicConfig dynamicConfig ) {
210210 MutableContext mutableContext = new MutableContext ();
211211 mutableContext .add ("name" , dynamicConfig .getName ());
212212 mutableContext .add ("value" , Structure .mapToStructure (dynamicConfig .getValue ()));
213213 mutableContext .add ("ruleID" , dynamicConfig .getRuleID ());
214- mutableContext .add ("groupName" , dynamicConfig .getGroupName ());
215- List <Value > secondaryExposures = new ArrayList <>();
216- dynamicConfig .getSecondaryExposures ().forEach (secondaryExposure -> {
217- Value value = Value .objectToValue (secondaryExposure );
218- secondaryExposures .add (value );
219- });
220- mutableContext .add ("secondaryExposures" , secondaryExposures );
221214 return new Value (mutableContext );
222215 }
223216
@@ -227,17 +220,11 @@ private Value toValue(Layer layer) {
227220 mutableContext .add ("value" , Structure .mapToStructure (layer .getValue ()));
228221 mutableContext .add ("ruleID" , layer .getRuleID ());
229222 mutableContext .add ("groupName" , layer .getGroupName ());
230- List <Value > secondaryExposures = new ArrayList <>();
231- layer .getSecondaryExposures ().forEach (secondaryExposure -> {
232- Value value = Value .objectToValue (secondaryExposure );
233- secondaryExposures .add (value );
234- });
235- mutableContext .add ("secondaryExposures" , secondaryExposures );
236- mutableContext .add ("allocatedExperiment" , layer .getAllocatedExperiment ());
223+ mutableContext .add ("allocatedExperiment" , layer .getAllocatedExperimentName ());
237224 return new Value (mutableContext );
238225 }
239226
240- @ NotNull private static FeatureConfig parseFeatureConfig (EvaluationContext ctx ) {
227+ private static FeatureConfig parseFeatureConfig (EvaluationContext ctx ) {
241228 Value featureConfigValue = ctx .getValue (FEATURE_CONFIG_KEY );
242229 if (featureConfigValue == null ) {
243230 throw new IllegalArgumentException ("feature config not found at evaluation context." );
@@ -262,8 +249,10 @@ private Value toValue(Layer layer) {
262249 @ SneakyThrows
263250 @ Override
264251 public void shutdown () {
265- log .info ("shutdown" );
266- Statsig .shutdown ();
252+ log .info ("shutdown begin" );
253+ CompletableFuture <Void > shutdownFuture = statsig .shutdown ();
254+ shutdownFuture .get ();
255+ log .info ("shutdown end" );
267256 }
268257
269258 /** Feature config, as required for evaluation. */
0 commit comments