use crate::{defaults::Defaults, enrollment::ExperimentMetadata, NimbusError, Result};
use serde_derive::*;
use serde_json::{Map, Value};
use std::collections::HashSet;
use uuid::Uuid;
const DEFAULT_TOTAL_BUCKETS: u32 = 10000;
#[derive(Debug, Clone)]
pub struct EnrolledExperiment {
pub feature_ids: Vec<String>,
pub slug: String,
pub user_facing_name: String,
pub user_facing_description: String,
pub branch_slug: String,
}
#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct Experiment {
pub schema_version: String,
pub slug: String,
pub app_name: Option<String>,
pub app_id: Option<String>,
pub channel: Option<String>,
pub user_facing_name: String,
pub user_facing_description: String,
pub is_enrollment_paused: bool,
pub bucket_config: BucketConfig,
pub branches: Vec<Branch>,
#[serde(default)]
pub feature_ids: Vec<String>,
pub targeting: Option<String>,
pub start_date: Option<String>, pub end_date: Option<String>, pub proposed_duration: Option<u32>,
pub proposed_enrollment: u32,
pub reference_branch: Option<String>,
#[serde(default)]
pub is_rollout: bool,
pub published_date: Option<chrono::DateTime<chrono::Utc>>,
}
#[cfg_attr(not(feature = "stateful"), allow(unused))]
impl Experiment {
pub(crate) fn has_branch(&self, branch_slug: &str) -> bool {
self.branches
.iter()
.any(|branch| branch.slug == branch_slug)
}
pub(crate) fn get_branch(&self, branch_slug: &str) -> Option<&Branch> {
self.branches.iter().find(|b| b.slug == branch_slug)
}
pub(crate) fn get_feature_ids(&self) -> Vec<String> {
let branches = &self.branches;
let feature_ids = branches
.iter()
.flat_map(|b| {
b.get_feature_configs()
.iter()
.map(|f| f.to_owned().feature_id)
.collect::<Vec<_>>()
})
.collect::<HashSet<_>>();
feature_ids.into_iter().collect()
}
#[cfg(test)]
pub(crate) fn patch(&self, patch: Value) -> Self {
let mut experiment = serde_json::to_value(self).unwrap();
if let (Some(e), Some(w)) = (experiment.as_object(), patch.as_object()) {
let mut e = e.clone();
for (key, value) in w {
e.insert(key.clone(), value.clone());
}
experiment = serde_json::to_value(e).unwrap();
}
serde_json::from_value(experiment).unwrap()
}
}
impl ExperimentMetadata for Experiment {
fn get_slug(&self) -> String {
self.slug.clone()
}
fn is_rollout(&self) -> bool {
self.is_rollout
}
}
pub fn parse_experiments(payload: &str) -> Result<Vec<Experiment>> {
let value: Value = match serde_json::from_str(payload) {
Ok(v) => v,
Err(e) => {
return Err(NimbusError::JSONError(
"value = nimbus::schema::parse_experiments::serde_json::from_str".into(),
e.to_string(),
))
}
};
let data = value
.get("data")
.ok_or(NimbusError::InvalidExperimentFormat)?;
let mut res = Vec::new();
for exp in data
.as_array()
.ok_or(NimbusError::InvalidExperimentFormat)?
{
match serde_json::from_value::<Experiment>(exp.clone()) {
Ok(exp) => res.push(exp),
Err(e) => {
log::trace!("Malformed experiment data: {:#?}", exp);
log::warn!(
"Malformed experiment found! Experiment {}, Error: {}",
exp.get("id").unwrap_or(&serde_json::json!("ID_NOT_FOUND")),
e
);
}
}
}
Ok(res)
}
#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct FeatureConfig {
pub feature_id: String,
#[serde(default)]
pub value: Map<String, Value>,
}
impl Defaults for FeatureConfig {
fn defaults(&self, fallback: &Self) -> Result<Self> {
if self.feature_id != fallback.feature_id {
Err(NimbusError::InternalError(
"Cannot merge feature configs from different features",
))
} else {
Ok(FeatureConfig {
feature_id: self.feature_id.clone(),
value: self.value.defaults(&fallback.value)?,
})
}
}
}
#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq)]
pub struct Branch {
pub slug: String,
pub ratio: i32,
#[serde(skip_serializing_if = "Option::is_none")]
pub feature: Option<FeatureConfig>,
#[serde(skip_serializing_if = "Option::is_none")]
pub features: Option<Vec<FeatureConfig>>,
}
impl Branch {
pub(crate) fn get_feature_configs(&self) -> Vec<FeatureConfig> {
match (&self.features, &self.feature) {
(Some(features), _) => features.clone(),
(None, Some(feature)) => vec![feature.clone()],
_ => Default::default(),
}
}
}
fn default_buckets() -> u32 {
DEFAULT_TOTAL_BUCKETS
}
#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct BucketConfig {
pub randomization_unit: RandomizationUnit,
pub namespace: String,
pub start: u32,
pub count: u32,
#[serde(default = "default_buckets")]
pub total: u32,
}
#[allow(unused)]
#[cfg(test)]
impl BucketConfig {
pub(crate) fn always() -> Self {
Self {
start: 0,
count: default_buckets(),
total: default_buckets(),
..Default::default()
}
}
}
pub struct AvailableExperiment {
pub slug: String,
pub user_facing_name: String,
pub user_facing_description: String,
pub branches: Vec<ExperimentBranch>,
pub reference_branch: Option<String>,
}
pub struct ExperimentBranch {
pub slug: String,
pub ratio: i32,
}
impl From<Experiment> for AvailableExperiment {
fn from(exp: Experiment) -> Self {
Self {
slug: exp.slug,
user_facing_name: exp.user_facing_name,
user_facing_description: exp.user_facing_description,
branches: exp.branches.into_iter().map(|b| b.into()).collect(),
reference_branch: exp.reference_branch,
}
}
}
impl From<Branch> for ExperimentBranch {
fn from(branch: Branch) -> Self {
Self {
slug: branch.slug,
ratio: branch.ratio,
}
}
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum RandomizationUnit {
NimbusId,
UserId,
}
impl Default for RandomizationUnit {
fn default() -> Self {
Self::NimbusId
}
}
#[derive(Default)]
pub struct AvailableRandomizationUnits {
pub user_id: Option<String>,
pub nimbus_id: Option<String>,
}
impl AvailableRandomizationUnits {
pub fn with_user_id(user_id: &str) -> Self {
Self {
user_id: Some(user_id.to_string()),
nimbus_id: None,
}
}
pub fn with_nimbus_id(nimbus_id: &Uuid) -> Self {
Self {
user_id: None,
nimbus_id: Some(nimbus_id.to_string()),
}
}
pub fn apply_nimbus_id(&self, nimbus_id: &Uuid) -> Self {
Self {
user_id: self.user_id.clone(),
nimbus_id: Some(nimbus_id.to_string()),
}
}
pub fn get_value<'a>(&'a self, wanted: &'a RandomizationUnit) -> Option<&'a str> {
match wanted {
RandomizationUnit::NimbusId => self.nimbus_id.as_deref(),
RandomizationUnit::UserId => self.user_id.as_deref(),
}
}
}