Skip to content
Permalink
Browse files

Issue #541: Fretboard: ExperimentDescriptor should use the experiment…

… name instead of the id

Closes #541: Fretboard: ExperimentDescriptor should use the experiment name instead of the id
  • Loading branch information...
fercarcedo authored and pocmo committed Aug 2, 2018
1 parent 6764c4d commit a9d6ba5863c12411f6b5b9a3cf9bc7dc5900e806
@@ -98,10 +98,10 @@ And then you have to register it on the manifest, just like any other `JobServic
### Checking if a user is part of an experiment
In order to check if a user is part of a specific experiment, Fretboard provides two APIs: a Kotlin-friendly
`withExperiment` API and a more Java-like `isInExperiment`. In both cases you pass an instance of `ExperimentDescriptor`
with the id of the experiment you want to check:
with the name of the experiment you want to check:

```Kotlin
val descriptor = ExperimentDescriptor("first-experiment-id")
val descriptor = ExperimentDescriptor("first-experiment-name")
fretboard.withExperiment(descriptor) {
someButton.setBackgroundColor(Color.RED)
}
@@ -110,11 +110,11 @@ otherButton.isEnabled = fretboard.isInExperiment(descriptor)
```

### Getting experiment metadata
Fretboard allows experiments to carry associated metadata, which can be retrieved using the kotlin-friendly
Fretboard allows experiments to carry associated metadata, which can be retrieved using the Kotlin-friendly
`withExperiment` API or the more Java-like `getExperiment` API, like this:

```Kotlin
val descriptor = ExperimentDescriptor("first-experiment-id")
val descriptor = ExperimentDescriptor("first-experiment-name")
fretboard.withExperiment(descriptor) {
toolbar.setColor(Color.parseColor(it.payload?.get("color") as String))
}
@@ -126,14 +126,14 @@ Fretboard allows you to force activate / deactivate a specific experiment via `s
simply have to pass true to activate it, false to deactivate:

```Kotlin
val descriptor = ExperimentDescriptor("first-experiment-id")
val descriptor = ExperimentDescriptor("first-experiment-name")
fretboard.setOverride(context, descriptor, true)
```

You can also clear an override for an experiment or all overrides:

```Kotlin
val descriptor = ExperimentDescriptor("first-experiment-id")
val descriptor = ExperimentDescriptor("first-experiment-name")
fretboard.clearOverride(context, descriptor)
fretboard.clearAllOverrides(context)
```
@@ -17,7 +17,7 @@ data class Experiment(
/**
* Human-readable name of the experiment
*/
val name: String? = null,
val name: String,
/**
* Detailed description of the experiment
*/
@@ -4,4 +4,4 @@

package mozilla.components.service.fretboard

data class ExperimentDescriptor(val id: String)
data class ExperimentDescriptor(val name: String)
@@ -18,12 +18,12 @@ internal class ExperimentEvaluator(private val valuesProvider: ValuesProvider =
val experiment = getExperiment(experimentDescriptor, experiments) ?: return null
val isEnabled = isInBucket(userBucket, experiment) && matches(context, experiment)
context.getSharedPreferences(OVERRIDES_PREF_NAME, Context.MODE_PRIVATE).let {
return if (it.getBoolean(experimentDescriptor.id, isEnabled)) experiment else null
return if (it.getBoolean(experimentDescriptor.name, isEnabled)) experiment else null
}
}

fun getExperiment(descriptor: ExperimentDescriptor, experiments: List<Experiment>): Experiment? {
return experiments.firstOrNull { it.id == descriptor.id }
return experiments.firstOrNull { it.name == descriptor.name }
}

private fun matches(context: Context, experiment: Experiment): Boolean {
@@ -73,14 +73,14 @@ internal class ExperimentEvaluator(private val valuesProvider: ValuesProvider =
fun setOverride(context: Context, descriptor: ExperimentDescriptor, active: Boolean) {
context.getSharedPreferences(OVERRIDES_PREF_NAME, Context.MODE_PRIVATE)
.edit()
.putBoolean(descriptor.id, active)
.putBoolean(descriptor.name, active)
.apply()
}

fun clearOverride(context: Context, descriptor: ExperimentDescriptor) {
context.getSharedPreferences(OVERRIDES_PREF_NAME, Context.MODE_PRIVATE)
.edit()
.remove(descriptor.id)
.remove(descriptor.name)
.apply()
}

@@ -97,7 +97,7 @@ class Fretboard(
* @return active experiments
*/
fun getActiveExperiments(context: Context): List<Experiment> {
return experiments.filter { isInExperiment(context, ExperimentDescriptor(it.id)) }.toList()
return experiments.filter { isInExperiment(context, ExperimentDescriptor(it.name)) }.toList()
}

/**
@@ -43,7 +43,7 @@ class JSONExperimentParser {
val payloadJson: JSONObject? = jsonObject.optJSONObject(PAYLOAD_KEY)
val payload = if (payloadJson != null) jsonToPayload(payloadJson) else null
return Experiment(jsonObject.getString(ID_KEY),
jsonObject.tryGetString(NAME_KEY),
jsonObject.getString(NAME_KEY),
jsonObject.tryGetString(DESCRIPTION_KEY),
matcher,
bucket,
Oops, something went wrong.

0 comments on commit a9d6ba5

Please sign in to comment.
You can’t perform that action at this time.