-
Notifications
You must be signed in to change notification settings - Fork 604
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add experiment manager to control whether we enable a feature (#…
- Loading branch information
1 parent
7355afe
commit 5d4f824
Showing
3 changed files
with
121 additions
and
5 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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use super::base::Config; | ||
use anyhow::Result; | ||
use std::collections::HashMap; | ||
|
||
/// It is the ground truth for init experiments. The experiment names in users' experiment list but not | ||
/// in the list will be remove from user list; The experiment names in the ground-truth list but not | ||
/// in users' experiment list will be added to user list with default value false; | ||
const ALL_EXPERIMENTS: &[(&str, bool)] = &[("EXPERIMENT_CONFIG", false)]; | ||
|
||
/// Experiment configuration management | ||
pub struct ExperimentManager; | ||
|
||
impl ExperimentManager { | ||
/// Get all experiments and their configurations | ||
/// | ||
/// - Ensures the user's experiment list is synchronized with `ALL_EXPERIMENTS`. | ||
/// - Adds missing experiments from `ALL_EXPERIMENTS` with the default value. | ||
/// - Removes experiments not in `ALL_EXPERIMENTS`. | ||
pub fn get_all() -> Result<Vec<(String, bool)>> { | ||
let config = Config::global(); | ||
let mut experiments: HashMap<String, bool> = config.get("experiments").unwrap_or_default(); | ||
|
||
// Synchronize the user's experiments with the ground truth (`ALL_EXPERIMENTS`) | ||
for &(key, default_value) in ALL_EXPERIMENTS { | ||
experiments.entry(key.to_string()).or_insert(default_value); | ||
} | ||
|
||
// Remove experiments not in `ALL_EXPERIMENTS` | ||
experiments.retain(|key, _| ALL_EXPERIMENTS.iter().any(|(k, _)| k == key)); | ||
|
||
Ok(experiments.into_iter().collect()) | ||
} | ||
|
||
/// Enable or disable an experiment | ||
pub fn set_enabled(name: &str, enabled: bool) -> Result<()> { | ||
let config = Config::global(); | ||
|
||
// Load existing experiments or initialize a new map | ||
let mut experiments: HashMap<String, bool> = | ||
config.get("experiments").unwrap_or_else(|_| HashMap::new()); | ||
|
||
// Update the status of the experiment | ||
experiments.insert(name.to_string(), enabled); | ||
|
||
// Save the updated experiments map | ||
config.set("experiments", serde_json::to_value(experiments)?)?; | ||
Ok(()) | ||
} | ||
|
||
/// Check if an experiment is enabled | ||
pub fn is_enabled(name: &str) -> Result<bool> { | ||
let config = Config::global(); | ||
|
||
// Load existing experiments or initialize a new map | ||
let experiments: HashMap<String, bool> = | ||
config.get("experiments").unwrap_or_else(|_| HashMap::new()); | ||
|
||
// Return whether the experiment is enabled, defaulting to false | ||
Ok(*experiments.get(name).unwrap_or(&false)) | ||
} | ||
} |
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,6 +1,8 @@ | ||
mod base; | ||
mod experiments; | ||
mod extensions; | ||
|
||
pub use crate::agents::ExtensionConfig; | ||
pub use base::{Config, ConfigError, APP_STRATEGY}; | ||
pub use experiments::ExperimentManager; | ||
pub use extensions::{ExtensionEntry, ExtensionManager}; |