Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 40 additions & 12 deletions crates/superposition_provider/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::sync::Arc;

use log::{debug, error, info, warn};
use serde_json::Value;
use serde_json::{Map, Value};
use superposition_core::experiment::ExperimentGroups;
use superposition_core::{
eval_config, get_applicable_variants, Experiments, MergeStrategy,
Expand All @@ -22,17 +22,17 @@ pub use open_feature::{
};

#[derive(Debug)]
pub struct CacConfig {
pub struct CacClient {
superposition_options: SuperpositionOptions,
options: ConfigurationOptions,
fallback_config: Option<serde_json::Map<String, Value>>,
cached_config: Arc<RwLock<Option<Config>>>,
last_updated: Arc<RwLock<Option<chrono::DateTime<chrono::Utc>>>>,
pub last_updated: Arc<RwLock<Option<chrono::DateTime<chrono::Utc>>>>,
evaluation_cache: RwLock<HashMap<String, HashMap<String, Value>>>,
polling_task: RwLock<Option<JoinHandle<()>>>,
}

impl CacConfig {
impl CacClient {
pub fn new(
superposition_options: SuperpositionOptions,
options: ConfigurationOptions,
Expand Down Expand Up @@ -217,7 +217,7 @@ impl CacConfig {
pub async fn evaluate_config(
&self,
query_data: &serde_json::Map<String, Value>,
prefix_filter: Option<&[String]>,
prefix_filter: Option<Vec<String>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not reference, if we dont need reference here then in line 233 of this file, we dont need the to_vec conversion anymore

) -> Result<serde_json::Map<String, Value>> {
let cached_config = self.cached_config.read().await;
match cached_config.as_ref() {
Expand Down Expand Up @@ -264,17 +264,17 @@ impl CacConfig {

/// Experimentation Configuration client
#[derive(Debug)]
pub struct ExperimentationConfig {
pub struct ExperimentationClient {
superposition_options: SuperpositionOptions,
options: ExperimentationOptions,
cached_experiments: Arc<RwLock<Option<Experiments>>>,
cached_experiment_groups: Arc<RwLock<Option<ExperimentGroups>>>,
last_updated: Arc<RwLock<Option<chrono::DateTime<chrono::Utc>>>>,
pub last_updated: Arc<RwLock<Option<chrono::DateTime<chrono::Utc>>>>,
evaluation_cache: RwLock<HashMap<String, HashMap<String, Value>>>,
polling_task: RwLock<Option<JoinHandle<()>>>,
}

impl ExperimentationConfig {
impl ExperimentationClient {
pub fn new(
superposition_options: SuperpositionOptions,
options: ExperimentationOptions,
Expand Down Expand Up @@ -483,8 +483,8 @@ impl ExperimentationConfig {
.await
.map_err(|e| {
SuperpositionError::NetworkError(format!(
"Failed to list experiments: {}",
e
"Failed to list experiments: {:?}",
e.raw_response()
))
})?;

Expand Down Expand Up @@ -523,8 +523,8 @@ impl ExperimentationConfig {
.await
.map_err(|e| {
SuperpositionError::NetworkError(format!(
"Failed to list experiment groups: {}",
e
"Failed to list experiment groups: {:?}",
e.raw_response()
))
})?;

Expand All @@ -543,6 +543,34 @@ impl ExperimentationConfig {
cached_experiments.clone()
}

pub async fn get_satisfied_experiments(
&self,
context: &Map<String, Value>,
filter_prefixes: Option<Vec<String>>,
) -> Result<Experiments> {
let cached_experiments = self.cached_experiments.read().await;
let experiments = match cached_experiments.as_ref() {
Some(experiments) => experiments,
None => {
return Err(SuperpositionError::ConfigError(
"No cached experiments available, please check if the experimentation settings are configured correctly".into(),
))
}
};

superposition_core::experiment::get_satisfied_experiments(
experiments,
context,
filter_prefixes,
)
.map_err(|e| {
SuperpositionError::ConfigError(format!(
"Failed to get satisfied experiments: {}",
e
))
})
}

pub async fn get_cached_experiment_groups(&self) -> Option<ExperimentGroups> {
let cached_experiment_groups = self.cached_experiment_groups.read().await;
cached_experiment_groups.clone()
Expand Down
4 changes: 2 additions & 2 deletions crates/superposition_provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod tests {
None,
);

let cac_config = CacConfig::new(superposition_options, config_options);
let cac_config = CacClient::new(superposition_options, config_options);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let cac_config = CacClient::new(superposition_options, config_options);
let cac_client = CacClient::new(superposition_options, config_options);


assert!(cac_config.get_cached_config().await.is_none());
}
Expand All @@ -49,7 +49,7 @@ mod tests {
OnDemandStrategy::default(),
));

let exp_config = ExperimentationConfig::new(superposition_options, exp_options);
let exp_config = ExperimentationClient::new(superposition_options, exp_options);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let exp_config = ExperimentationClient::new(superposition_options, exp_options);
let exp_client = ExperimentationClient::new(superposition_options, exp_options);


// Test that we can get None for cached experiments initially
assert!(exp_config.get_cached_experiments().await.is_none());
Expand Down
Loading
Loading