-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
1159eee
commit 760ff6f
Showing
9 changed files
with
194 additions
and
125 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
119 changes: 93 additions & 26 deletions
119
Provider/src/main/java/com/spotify/confidence/ConfidenceExtensions.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 |
---|---|---|
@@ -1,48 +1,115 @@ | ||
package com.spotify.confidence | ||
|
||
import android.content.Context | ||
import dev.openfeature.sdk.OpenFeatureAPI | ||
import com.spotify.confidence.client.ResolveResponse | ||
import dev.openfeature.sdk.EvaluationContext | ||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.serialization.encodeToString | ||
import kotlinx.serialization.json.Json | ||
|
||
val confidenceFlushPolicy = object : FlushPolicy { | ||
private var size = 0 | ||
override fun reset() { | ||
size = 0 | ||
interface ConfidenceContextProvider { | ||
fun confidenceContext(): ConfidenceValue.Struct | ||
} | ||
|
||
typealias ConfidenceFieldsType = Map<String, ConfidenceValue> | ||
|
||
interface ConfidenceAPI : ConfidenceContextProvider { | ||
fun fork(context: ConfidenceContext): ConfidenceAPI | ||
fun putContext(context: ConfidenceContext) | ||
} | ||
|
||
interface ConfidenceContext { | ||
val name: String | ||
val value: ConfidenceValue | ||
} | ||
|
||
class PageContext(private val page: String) : ConfidenceContext { | ||
override val value: ConfidenceValue | ||
get() = ConfidenceValue.String(page) | ||
override val name: String | ||
get() = "page" | ||
} | ||
|
||
class CommonContext : ConfidenceContextProvider { | ||
override fun confidenceContext(): ConfidenceValue.Struct = ConfidenceValue.Struct(mapOf()) | ||
} | ||
|
||
fun EvaluationContext.toConfidenceContext() = object : ConfidenceContext { | ||
override val name: String = "open_feature" | ||
override val value: ConfidenceValue | ||
get() = ConfidenceValue.Struct( | ||
asMap() | ||
.map { it.key to ConfidenceValue.String(it.value.toString()) } | ||
.toMap() + ("targeting_key" to ConfidenceValue.String(getTargetingKey())) | ||
) | ||
} | ||
|
||
class Confidence( | ||
val clientSecret: String, | ||
private val root: ConfidenceContextProvider = CommonContext() | ||
) : ConfidenceAPI { | ||
private var contextMap: ConfidenceValue.Struct = ConfidenceValue.Struct(mapOf()) | ||
override fun putContext(context: ConfidenceContext) { | ||
val map = contextMap.value.toMutableMap() | ||
map[context.name] = context.value | ||
contextMap = ConfidenceValue.Struct(map) | ||
} | ||
|
||
override fun hit(event: Event) { | ||
size++ | ||
override fun confidenceContext(): ConfidenceValue.Struct { | ||
return ConfidenceValue.Struct(root.confidenceContext().value + contextMap.value) | ||
} | ||
|
||
override fun shouldFlush(): Boolean { | ||
return size > 4 | ||
override fun fork(context: ConfidenceContext) = Confidence(clientSecret, this).also { | ||
it.putContext(context) | ||
} | ||
} | ||
|
||
fun ConfidenceFeatureProvider.eventSender( | ||
internal fun ConfidenceAPI.resolve(flags: List<String>): ResolveResponse { | ||
TODO() | ||
} | ||
|
||
fun Confidence.openFeatureProvider( | ||
context: Context, | ||
initialisationStrategy: InitialisationStrategy | ||
): ConfidenceFeatureProvider = ConfidenceFeatureProvider.create( | ||
context, | ||
confidenceAPI = this, | ||
clientSecret = clientSecret, | ||
initialisationStrategy = initialisationStrategy | ||
) | ||
|
||
fun Confidence.eventSender( | ||
context: Context, | ||
dispatcher: CoroutineDispatcher = Dispatchers.IO | ||
): EventSender = EventSenderImpl.create( | ||
clientSecret = this.clientSecret(), | ||
clientSecret = clientSecret, | ||
dispatcher = dispatcher, | ||
flushPolicies = listOf(confidenceFlushPolicy), | ||
scope = EventsScope( | ||
fields = { | ||
OpenFeatureAPI.getEvaluationContext()?.let { evalContext -> | ||
val map = mutableMapOf<String, EventValue>() | ||
map["targeting_key"] = EventValue.String(evalContext.getTargetingKey()) | ||
evalContext.asMap().forEach { | ||
map[it.key] = EventValue.String(Json.encodeToString(it.value)) | ||
} | ||
map | ||
} ?: mapOf() | ||
} | ||
), | ||
context = context | ||
context = context, | ||
confidenceContext = this | ||
).onLowMemory { files -> | ||
val sortedFiles = files.sortedBy { it.lastModified() } | ||
sortedFiles.take(10).forEach { it.delete() } | ||
} | ||
|
||
val confidenceFlushPolicy = object : FlushPolicy { | ||
private var size = 0 | ||
override fun reset() { | ||
size = 0 | ||
} | ||
|
||
override fun hit(event: Event) { | ||
size++ | ||
} | ||
|
||
override fun shouldFlush(): Boolean { | ||
return size > 4 | ||
} | ||
} | ||
|
||
sealed class ConfidenceValue { | ||
data class String(val value: kotlin.String) : ConfidenceValue() | ||
data class Double(val value: kotlin.Double) : ConfidenceValue() | ||
data class Boolean(val value: kotlin.Boolean) : ConfidenceValue() | ||
data class Int(val value: kotlin.Int) : ConfidenceValue() | ||
data class Struct(val value: Map<kotlin.String, ConfidenceValue>) : ConfidenceValue() | ||
} |
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
Oops, something went wrong.