Skip to content

Commit

Permalink
feat: add variation_key and variation_value to EvalFlagDetails
Browse files Browse the repository at this point in the history
  • Loading branch information
rasendubi committed Jul 18, 2024
1 parent 5c66545 commit c546daf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions eppo_core/src/ufc/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ impl Flag {
FlagEvaluationError::ConfigurationError
})?;

visitor.on_variation(variation);

let assignment_value = variation
.value
.to_assignment_value(self.variation_type)
Expand Down
26 changes: 24 additions & 2 deletions eppo_core/src/ufc/eval_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
use crate::{Attributes, Configuration};

use super::{
eval::AllocationNonMatchReason, eval_visitor::*, Assignment, FlagEvaluationError, Split,
eval::AllocationNonMatchReason, eval_visitor::*, Assignment, FlagEvaluationError, Split, Value,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -19,6 +19,11 @@ pub struct EvalFlagDetails {
/// Details of configuration used for evaluation. None if configuration hasn't been fetched yet.
pub configuration_details: Option<ConfigurationDetails>,
pub result: Result<Assignment, FlagEvaluationError>,
/// Key of the selected variation.
pub variation_key: Option<String>,
/// Value of the selected variation. Could be `None` if no variation is selected, or selected
/// value is absent in configuration (configuration error).
pub variation_value: Option<Value>,
pub allocations: Vec<EvalAllocationDetails>,
}

Expand Down Expand Up @@ -60,13 +65,17 @@ pub(crate) struct EvalFlagDetailsBuilder {

result: Option<Result<Assignment, FlagEvaluationError>>,

variation_key: Option<String>,
variation_value: Option<Value>,

/// List of allocation keys. Used to sort `allocation_eval_results`.
allocation_keys_order: Vec<String>,
allocation_eval_results: HashMap<String, EvalAllocationDetails>,
}

pub(crate) struct EvalAllocationDetailsBuilder<'a> {
result: &'a mut EvalAllocationDetails,
variation_key: &'a mut Option<String>,
}

impl EvalFlagDetailsBuilder {
Expand All @@ -83,6 +92,8 @@ impl EvalFlagDetailsBuilder {
now,
configuration_details: None,
result: None,
variation_key: None,
variation_value: None,
allocation_keys_order: Vec::new(),
allocation_eval_results: HashMap::new(),
}
Expand All @@ -98,6 +109,8 @@ impl EvalFlagDetailsBuilder {
result: self.result.expect(
"EvalFlagDetailsBuilder.build() should only be called after evaluation is complete",
),
variation_key: self.variation_key,
variation_value: self.variation_value,
allocations: self
.allocation_keys_order
.into_iter()
Expand Down Expand Up @@ -127,7 +140,10 @@ impl EvalVisitor for EvalFlagDetailsBuilder {
key: allocation.key.clone(),
result: EvalAllocationResult::Unevaluated,
});
EvalAllocationDetailsBuilder { result }
EvalAllocationDetailsBuilder {
result,
variation_key: &mut self.variation_key,
}
}

fn on_configuration(&mut self, configuration: &Configuration) {
Expand All @@ -144,13 +160,19 @@ impl EvalVisitor for EvalFlagDetailsBuilder {
.extend(flag.allocations.iter().map(|it| &it.key).cloned());
}

fn on_variation(&mut self, variation: &super::Variation) {
self.variation_value = Some(variation.value.clone());
}

fn on_result(&mut self, result: &Result<super::Assignment, super::FlagEvaluationError>) {
self.result = Some(result.clone());
}
}

impl<'a> EvalAllocationVisitor for EvalAllocationDetailsBuilder<'a> {
fn on_result(&mut self, result: Result<&Split, AllocationNonMatchReason>) {
*self.variation_key = result.ok().map(|split| split.variation_key.clone());

self.result.result = match result {
Ok(_) => EvalAllocationResult::Matched,
Err(AllocationNonMatchReason::BeforeStartDate) => EvalAllocationResult::BeforeStartDate,
Expand Down
5 changes: 5 additions & 0 deletions eppo_core/src/ufc/eval_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::Configuration;

use super::{
eval::AllocationNonMatchReason, Allocation, Assignment, Flag, FlagEvaluationError, Split,
Variation,
};

pub(super) trait EvalVisitor {
Expand All @@ -20,6 +21,10 @@ pub(super) trait EvalVisitor {
#[inline]
fn on_flag_configuration(&mut self, flag: &Flag) {}

#[allow(unused_variables)]
#[inline]
fn on_variation(&mut self, variation: &Variation) {}

fn visit_allocation<'a>(&'a mut self, allocation: &Allocation) -> Self::AllocationVisitor<'a>;

/// Called with evaluation result.
Expand Down

0 comments on commit c546daf

Please sign in to comment.