Coverage Summary for Class: InAppABTestLogic (cloud.mindbox.mobile_sdk.abtests)
| Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
| InAppABTestLogic |
100%
(5/5)
|
93.8%
(15/16)
|
95.1%
(39/41)
|
97%
(258/266)
|
| InAppABTestLogic$getInAppsPool$1 |
|
| InAppABTestLogic$PoolKey |
100%
(1/1)
|
|
100%
(4/4)
|
100%
(17/17)
|
| InAppABTestLogic$WhenMappings |
|
| Total |
100%
(6/6)
|
93.8%
(15/16)
|
95.6%
(43/45)
|
97.2%
(275/283)
|
package cloud.mindbox.mobile_sdk.abtests
import cloud.mindbox.mobile_sdk.inapp.domain.interfaces.repositories.MobileConfigRepository
import cloud.mindbox.mobile_sdk.inapp.domain.models.ABTest
import cloud.mindbox.mobile_sdk.logger.MindboxLog
import cloud.mindbox.mobile_sdk.repository.MindboxPreferences
internal class InAppABTestLogic(
private val mixer: CustomerAbMixer,
private val repository: MobileConfigRepository,
) : MindboxLog {
private data class PoolKey(
val abtests: List<ABTest>,
val uuid: String,
val allInApps: List<String>,
)
@Volatile
private var cachedPool: Pair<PoolKey, Set<String>>? = null
suspend fun getInAppsPool(allInApps: List<String>): Set<String> = getInAppsPool(allInApps, repository.getABTests())
fun getInAppsPool(allInApps: List<String>, abtests: List<ABTest>): Set<String> {
val uuid = MindboxPreferences.deviceUuid
val key = PoolKey(abtests, uuid, allInApps)
cachedPool?.let { (cachedKey, pool) ->
if (cachedKey == key) return pool
}
return calculatePool(abtests, uuid, allInApps).also { pool -> cachedPool = key to pool }
}
private fun calculatePool(abtests: List<ABTest>, uuid: String, allInApps: List<String>): Set<String> {
if (abtests.isEmpty()) {
logI("Abtests is empty. Use all inApps.")
return allInApps.toSet()
}
if (allInApps.isEmpty()) {
return emptySet()
}
val inAppsForAbtest: List<Set<String>> = abtests.map { abtest ->
val hash = mixer.stringModulusHash(
identifier = uuid,
salt = abtest.salt.uppercase(),
)
logI("Mixer calculate hash $hash for abtest ${abtest.id} with salt ${abtest.salt} and deviceUuid $uuid")
val targetVariantInApps: MutableSet<String> = mutableSetOf()
val otherVariantInApps: MutableSet<String> = mutableSetOf()
abtest.variants.onEach { variant ->
val inApps: List<String> = when (variant.kind) {
ABTest.Variant.VariantKind.ALL -> allInApps
ABTest.Variant.VariantKind.CONCRETE -> variant.inapps
}
if ((variant.lower until variant.upper).contains(hash)) {
targetVariantInApps.addAll(inApps)
logI("Selected variant $variant for ${abtest.id}")
} else {
otherVariantInApps.addAll(inApps)
}
}
(targetVariantInApps + (allInApps - otherVariantInApps)).also { inappsSet ->
logI("For abtest ${abtest.id} determined $inappsSet")
}
}
if (inAppsForAbtest.isEmpty()) {
logW("No inApps after calculation abtests logic. InApp will not be shown.")
return setOf()
}
return if (inAppsForAbtest.size == 1) {
inAppsForAbtest.first()
} else {
getIntersectionForAllABTests(inAppsForAbtest)
}
}
private fun getIntersectionForAllABTests(inAppsForAbtest: List<Set<String>>): Set<String> =
inAppsForAbtest.reduce { acc, list -> acc.intersect(list) }
}