-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,635 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
package com.featurevisor.sdk | ||
|
||
import com.goncalossilva.murmurhash.MurmurHash3 | ||
import kotlin.math.floor | ||
|
||
object Bucket { | ||
|
||
private const val HASH_SEED = 1u | ||
private const val MAX_HASH_VALUE = 4294967296 // 2^32 | ||
|
||
// 100% * 1000 to include three decimal places in the same integer value | ||
private const val MAX_BUCKETED_NUMBER = 100000 | ||
|
||
fun getBucketedNumber(bucketKey: String): Int { | ||
val hashValue = MurmurHash3(HASH_SEED).hash32x86(bucketKey.toByteArray()) | ||
val ratio = hashValue.toDouble() / MAX_HASH_VALUE | ||
|
||
return kotlin.math.floor(ratio * MAX_BUCKETED_NUMBER).toInt() | ||
return floor(ratio * MAX_BUCKETED_NUMBER).toInt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.featurevisor.sdk | ||
|
||
sealed class FeaturevisorError(message: String) : Throwable(message = message) { | ||
|
||
/// Thrown when attempting to init Featurevisor instance without passing datafile and datafileUrl. | ||
/// At least one of them is required to init the SDK correctly | ||
object MissingDatafileOptions : FeaturevisorError("Missing data file options") | ||
|
||
class FetchingDataFileFailed(val result: String) : FeaturevisorError("Fetching data file failed") | ||
|
||
/// Thrown when receiving unparseable Datafile JSON responses. | ||
/// - Parameters: | ||
/// - data: The data being parsed. | ||
/// - errorMessage: The message from the error which occured during parsing. | ||
class UnparsableJson(val data: String?, errorMessage: String) : FeaturevisorError(errorMessage) | ||
|
||
/// Thrown when attempting to construct an invalid URL. | ||
/// - Parameter string: The invalid URL string. | ||
class InvalidUrl(val url: String?) : FeaturevisorError("Invalid URL") | ||
|
||
object MissingDatafileUrlWhileRefreshing : FeaturevisorError("Missing datafile url need to refresh") | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/featurevisor/sdk/Instance+Activation.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.featurevisor.sdk | ||
|
||
import com.featurevisor.types.AttributeValue | ||
import com.featurevisor.types.Context | ||
import com.featurevisor.types.EventName.ACTIVATION | ||
import com.featurevisor.types.FeatureKey | ||
import com.featurevisor.types.VariationValue | ||
|
||
fun FeaturevisorInstance.activate(featureKey: FeatureKey, context: Context = emptyMap()): VariationValue? { | ||
val evaluation = evaluateVariation(featureKey, context) | ||
val variationValue = evaluation.variation?.value ?: evaluation.variationValue ?: return null | ||
val finalContext = interceptContext?.invoke(context) ?: context | ||
val captureContext = mutableMapOf<String, AttributeValue>() | ||
val attributesForCapturing = datafileReader.getAllAttributes() | ||
.filter { it.capture == true } | ||
|
||
attributesForCapturing.forEach { attribute -> | ||
finalContext[attribute.key]?.let { | ||
captureContext[attribute.key] = it | ||
} | ||
} | ||
|
||
emitter.emit( | ||
ACTIVATION, | ||
featureKey, | ||
variationValue, | ||
finalContext, | ||
captureContext, | ||
evaluation | ||
) | ||
|
||
return variationValue | ||
} |
Oops, something went wrong.