Skip to content

Commit

Permalink
Merge pull request #49 from statsig-io/expmeta
Browse files Browse the repository at this point in the history
Add a method to fetch experiment group names and values
  • Loading branch information
tore-statsig authored Nov 19, 2021
2 parents 6e72abf + 737bc34 commit 4c643fb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
kotlin.code.style=official
version=0.8.0
version=0.8.1
1 change: 1 addition & 0 deletions src/main/kotlin/com/statsig/sdk/APIDownloadedConfigs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal data class APIRule(
@SerializedName("salt") val salt: String?,
@SerializedName("conditions") val conditions: Array<APICondition>,
@SerializedName("idType") val idType: String,
@SerializedName("groupName") val groupName: String,
)

internal data class APICondition(
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/com/statsig/sdk/Evaluator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ internal class Evaluator {
CountryLookup.initialize()
}

fun getVariants(configName: String): Map<String, String> {
var variants : MutableMap<String, String> = HashMap()
if (!dynamicConfigs.containsKey(configName)) {
return variants
}
val config = dynamicConfigs[configName]
for (r : APIRule in config!!.rules) {
variants[r.groupName] = r.returnValue.toString()
}
return variants
}

fun setDownloadedConfigs(downloadedConfig: APIDownloadedConfigs) {
for (config in downloadedConfig.featureGates) {
featureGates[config.name] = config
Expand Down
9 changes: 9 additions & 0 deletions src/main/kotlin/com/statsig/sdk/Statsig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ class Statsig {
return statsigServer.getExperimentAsync(user, experimentName)
}

/**
* @deprecated - we make no promises of support for this API
*/
@JvmStatic
fun _getExperimentGroups(experimentName: String): Map<String, String> {
enforceInitialized()
return statsigServer._getExperimentGroups(experimentName)
}

@JvmStatic
fun shutdown() {
runBlocking { statsigServer.shutdown() }
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/com/statsig/sdk/StatsigServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ sealed class StatsigServer {

abstract fun getExperimentAsync(user: StatsigUser, experimentName: String): CompletableFuture<DynamicConfig>

/**
* @deprecated - we make no promises of support for this API
*/
abstract fun _getExperimentGroups(experimentName: String): Map<String, String>

abstract fun shutdown()

internal abstract suspend fun flush()
Expand All @@ -67,7 +72,7 @@ sealed class StatsigServer {
}
}

private const val VERSION = "0.8.0"
private const val VERSION = "0.8.1"

private class StatsigServerImpl(
serverSecret: String,
Expand Down Expand Up @@ -227,6 +232,13 @@ private class StatsigServerImpl(
}
}

/**
* @deprecated - we make no promises of support for this API
*/
override fun _getExperimentGroups(experimentName: String): Map<String, String> {
return configEvaluator.getVariants(experimentName)
}

override fun shutdown() {
runBlocking {
shutdownSuspend()
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/statsig/sdk/StatsigE2ETest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class StatsigE2ETest {
assert(config.getInt("number", 0) == 4)
assert(config.getString("string", "") == "default")
assert(config.getBoolean("boolean", true))

var groups = driver._getExperimentGroups("test_config")
assert(groups["statsig email"].equals("{number=7.0, string=statsig, boolean=false}"))

driver.shutdown()

val eventLogInput = withTimeout(TEST_TIMEOUT) {
Expand Down

0 comments on commit 4c643fb

Please sign in to comment.