All Downloads are FREE. Search and download functionalities are using the official Maven repository.

util.EvaluationFlag.kt Maven / Gradle / Ivy

There is a newer version: 0.5.1
Show newest version
package com.amplitude.util

import com.amplitude.cohort.USER_GROUP_TYPE
import com.amplitude.experiment.evaluation.EvaluationCondition
import com.amplitude.experiment.evaluation.EvaluationFlag
import com.amplitude.experiment.evaluation.EvaluationOperator
import com.amplitude.experiment.evaluation.EvaluationSegment

internal fun Collection.getAllCohortIds(): Set {
    return getGroupedCohortIds().flatMap { it.value }.toSet()
}

internal fun Collection.getGroupedCohortIds(): Map> {
    val cohortIds = mutableMapOf>()
    for (flag in this) {
        for (entry in flag.getGroupedCohortIds()) {
            cohortIds.getOrPut(entry.key) { mutableSetOf() } += entry.value
        }
    }
    return cohortIds
}

internal fun EvaluationFlag.getAllCohortIds(): Set {
    return getGroupedCohortIds().flatMap { it.value }.toSet()
}

internal fun EvaluationFlag.getGroupedCohortIds(): Map> {
    val cohortIds = mutableMapOf>()
    for (segment in this.segments) {
        for (entry in segment.getGroupedCohortConditionIds()) {
            cohortIds.getOrPut(entry.key) { mutableSetOf() } += entry.value
        }
    }
    return cohortIds
}

private fun EvaluationSegment.getGroupedCohortConditionIds(): Map> {
    val cohortIds = mutableMapOf>()
    if (conditions == null) {
        return cohortIds
    }
    for (outer in conditions!!) {
        for (condition in outer) {
            if (condition.isCohortFilter()) {
                // User cohort selector is [context, user, cohort_ids]
                // Groups cohort selector is [context, groups, {group_type}, cohort_ids]
                if (condition.selector.size > 2) {
                    val contextSubtype = condition.selector[1]
                    val groupType = if (contextSubtype == "user") {
                        USER_GROUP_TYPE
                    } else if (condition.selector.contains("groups")) {
                        condition.selector[2]
                    } else {
                        continue
                    }
                    cohortIds.getOrPut(groupType) { mutableSetOf() } += condition.values
                }
            }
        }
    }
    return cohortIds
}

// Only cohort filters use these operators.
private fun EvaluationCondition.isCohortFilter(): Boolean =
    (this.op == EvaluationOperator.SET_CONTAINS_ANY || this.op == EvaluationOperator.SET_DOES_NOT_CONTAIN_ANY) &&
        this.selector.isNotEmpty() && this.selector.last() == "cohort_ids"




© 2015 - 2024 Weber Informatics LLC | Privacy Policy