Skip to content

Commit

Permalink
Merge pull request #30 from alippo-com/refactor/sdk_initialization
Browse files Browse the repository at this point in the history
refactor: SDK initialization
  • Loading branch information
DK070202 authored Feb 1, 2023
2 parents ad690e5 + 6c4eac9 commit 44f6d0e
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 152 deletions.
13 changes: 8 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@

# 1.2.0+0
- Breaking change: Made initialization of sdk asynchronous.
- Added run method to Evaluate experiment.
- Removed SDKBuilder.

# 1.1.2
- Fixed `feature` evaluation on string comparison.

# 1.1.1+2

# 1.1.1
- Fixed `condition` evaluation while attribute/s is/are null.

# 1.1.0+2

# 1.1.0
- Migrated to dart 2.17.2.
- fix: Data parsing problem.
- Moved to json-serializable from manual one.
Expand All @@ -16,7 +20,6 @@
- Changed `attr` type for `evaluateCondition`.

# 1.0.0+1

- First Release Version.
- AB Testing.
- Feature Flag.
Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,32 @@ Integration is super easy:
Now you can start/stop tests, adjust coverage and variation weights, and apply a winning variation to 100% of traffic, all within the Growth Book App without deploying code changes to your site.
```dart
final GrowthBookSDK sdkInstance = GBSDKBuilderApp(
final GrowthBookSDK sdkInstance = await GBSDKBuilderApp(
apiKey: "<API_KEY>",
attributes: {
/// Specify attributes.
},
growthBookTrackingCallBack: (gbExperiment, gbExperimentResult) {},
hostURL: '<GrowthBook_URL>',
apiKey: '<YOUR API KEY>'
).initialize();

```

There are additional properties which can be setup at the time of initialization

```dart
final GrowthBookSDK newSdkInstance = GBSDKBuilderApp(
final GrowthBookSDK newSdkInstance =await GBSDKBuilderApp(
apiKey: "<API_KEY>",
attributes: {
/// Specify user attributes.
},
client: NetworkClient(), // Provide network dispatcher.
growthBookTrackingCallBack: (gbExperiment, gbExperimentResult) {},
hostURL: '<GrowthBook_URL>',
).setNetworkDispatcher(
// set network dispatcher.
)
.setForcedVariations({})
. // Set forced variations
setQAMode(true)// Set qamode
.initialize();
forcedVariations: {} // Optional provide force variation.
qaMode: true, // Set qamode
).initialize();
```

Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
20 changes: 8 additions & 12 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,14 @@ class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
initializeSDK();
}

void initializeSDK() {
gb = GBSDKBuilderApp(
apiKey: kReleaseMode ? '<PROD_KEY>' : '<DEV_KEY>',
hostURL: '<HOST_URL>',
attributes: userAttr,
growthBookTrackingCallBack: (experiment, experimentResult) {
/// Track feature.
})
.initialize()
..afterFetch = () {
setState(() {});
};
void initializeSDK() async {
gb = await GBSDKBuilderApp(
apiKey: kReleaseMode ? '<PROD_KEY>' : '<DEV_KEY>',
hostURL: '<HOST_URL>',
attributes: userAttr,
growthBookTrackingCallBack: (exp, rst) {},
).initialize();
setState(() {});
}

Widget _getRightWidget() {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.2"
version: "1.2.0+0"
http_parser:
dependency: transitive
description:
Expand Down
32 changes: 14 additions & 18 deletions lib/src/Evaluator/experiment_evaluator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class GBExperimentEvaluator {
/// (not in experiment, variationId 0)
///
/// If context.enabled is false, return immediately (not in experiment, variationId 0)
if (experiment.variations!.length < 2 || !context.enabled!) {
if (experiment.variations.length < 2 || !context.enabled!) {
return _getExperimentResult(experiment: experiment, gbContext: context);
}

Expand Down Expand Up @@ -44,9 +44,9 @@ class GBExperimentEvaluator {
/// If experiment.namespace is set, check if hash value is included in the
/// range and if not, return immediately (not in experiment, variationId 0)
if (experiment.namespace != null) {
var namespace = GBUtils().getGBNameSpace(experiment.namespace!);
var namespace = const GBUtils().getGBNameSpace(experiment.namespace!);
if (experiment.namespace != null &&
!GBUtils().inNamespace(attributeValue, namespace!)) {
!const GBUtils().inNamespace(attributeValue, namespace!)) {
return _getExperimentResult(experiment: experiment, gbContext: context);
}
}
Expand All @@ -66,27 +66,27 @@ class GBExperimentEvaluator {
if (weights == null) {
// Default weights to an even split between all variations
experiment.weights =
GBUtils().getEqualWeights(experiment.variations?.length ?? 1);
const GBUtils().getEqualWeights(experiment.variations.length);
}

final coverage = experiment.coverage ?? 1.0;
experiment.coverage = coverage;

/// Calculate bucket ranges for the variations
/// Convert weights/coverage to ranges
final List<GBBucketRange> bucketRange = GBUtils().getBucketRanges(
experiment.variations!.length,
final List<GBBucketRange> bucketRange = const GBUtils().getBucketRanges(
experiment.variations.length,
coverage,
experiment.weights != null
? experiment.weights!
.map((e) => double.parse(e.toString()))
.toList()
: []);

final hash = GBUtils().hash(attributeValue + experiment.key);
final hash = const GBUtils().hash(attributeValue + experiment.key);
late final int assigned;
if (hash != null) {
assigned = GBUtils().chooseVariation(hash, bucketRange);
assigned = const GBUtils().chooseVariation(hash, bucketRange);
}
// If not assigned a variation (assigned === -1), return immediately (not in experiment, variationId 0)
if (assigned == -1) {
Expand Down Expand Up @@ -130,21 +130,17 @@ class GBExperimentEvaluator {
var targetVariationIndex = variationIndex;

// Check whether variationIndex lies within bounds of variations size
if (experiment.variations != null) {
if (targetVariationIndex < 0 ||
targetVariationIndex >= experiment.variations!.length) {
// Set to 0
targetVariationIndex = 0;
}
if (targetVariationIndex < 0 ||
targetVariationIndex >= experiment.variations.length) {
// Set to 0
targetVariationIndex = 0;
}

dynamic targetValue = 0;

// check whether variations are non empty - then only query array against index
if (experiment.variations != null) {
if (experiment.variations!.isNotEmpty) {
targetValue = experiment.variations![targetVariationIndex];
}
if (experiment.variations.isNotEmpty) {
targetValue = experiment.variations[targetVariationIndex];
}

// Hash Attribute - used for Experiment Calculations
Expand Down
4 changes: 2 additions & 2 deletions lib/src/Model/experiment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ part 'experiment.g.dart';
class GBExperiment {
GBExperiment({
this.key,
this.variations,
this.variations = const [],
this.namespace,
this.condition,
this.hashAttribute,
Expand All @@ -22,7 +22,7 @@ class GBExperiment {
String? key;

/// The different variations to choose between
List? variations = [];
List variations = [];

/// A tuple that contains the namespace identifier, plus a range of coverage for the experiment
List? namespace;
Expand Down
2 changes: 1 addition & 1 deletion lib/src/Model/experiment.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/src/Utils/gb_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class FNV {
/// - chooseVariation
/// - getGBNameSpace
class GBUtils {
const GBUtils();

/// Hashes a string to a float between 0 and 1
/// fnv32a returns an integer, so we convert that to a float using a modulus:
Expand Down
Loading

0 comments on commit 44f6d0e

Please sign in to comment.