Skip to content

Commit 4ee0b6c

Browse files
committed
feat: EvalDetail support variation-index
1 parent a1f27a9 commit 4ee0b6c

File tree

5 files changed

+56
-33
lines changed

5 files changed

+56
-33
lines changed

Cargo.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
edition = "2021"
33
name = "feature-probe-server-sdk"
4-
version = "1.0.4"
4+
version = "1.0.5"
55
license = "Apache-2.0"
66
authors = ["maintain@featureprobe.com"]
77
description = "FeatureProbe Server Side SDK for Rust"
@@ -43,8 +43,8 @@ thiserror = "1.0"
4343
tracing = "0.1"
4444
url = "2"
4545

46-
feature-probe-event-std = { optional = true, version = "1.0.3", package="feature-probe-event" }
47-
feature-probe-event-tokio = { optional = true, version = "1.0.3", features=["use_tokio"], default-features=false, package="feature-probe-event" }
46+
feature-probe-event-std = { optional = true, version = "1.0.4", package="feature-probe-event" }
47+
feature-probe-event-tokio = { optional = true, version = "1.0.4", features=["use_tokio"], default-features=false, package="feature-probe-event" }
4848

4949
reqwest = { optional = true, version = "0.11", default-features = false, features = ["rustls-tls", "json"] }
5050
tokio = {optional = true, version = "1", features = ["full"]}
@@ -59,5 +59,5 @@ criterion = "0.3"
5959
rusty-hook = "^0.11.2"
6060
tokio = { version = "1", features = ["full"] }
6161
tracing-subscriber = "0.3"
62-
feature-probe-server = "1.0.3"
62+
feature-probe-server = "1.0.4"
6363

src/evalutate.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ pub enum Serve {
1818
Split(Distribution),
1919
}
2020

21+
#[derive(Serialize, Deserialize, Debug)]
22+
pub struct Variation {
23+
pub value: Value,
24+
pub index: usize,
25+
}
26+
2127
impl Serve {
22-
pub fn select_variation(&self, eval_param: &EvalParams) -> Result<Value, FPError> {
28+
pub fn select_variation(&self, eval_param: &EvalParams) -> Result<Variation, FPError> {
2329
let variations = eval_param.variations;
2430
let index = match self {
2531
Serve::Select(i) => *i,
@@ -33,9 +39,17 @@ impl Serve {
3339
variations.len()
3440
))),
3541
None => Err(FPError::EvalError),
36-
Some(v) => Ok(v.clone()),
42+
Some(v) => Ok(Variation {
43+
value: v.clone(),
44+
index,
45+
}),
3746
}
3847
}
48+
49+
pub fn select_variation_value(&self, eval_param: &EvalParams) -> Result<Value, FPError> {
50+
let v = self.select_variation(eval_param)?;
51+
Ok(v.value)
52+
}
3953
}
4054

4155
#[derive(Serialize, Deserialize, Debug, PartialEq)]
@@ -119,6 +133,7 @@ pub struct EvalParams<'a> {
119133
pub struct EvalDetail<T> {
120134
pub value: Option<T>,
121135
pub rule_index: Option<usize>,
136+
pub variation_index: Option<usize>,
122137
pub version: Option<u64>,
123138
pub reason: String,
124139
}
@@ -151,16 +166,16 @@ impl Toggle {
151166
};
152167

153168
if !self.enabled {
154-
return self.disabled_serve.select_variation(&eval_param);
169+
return self.disabled_serve.select_variation_value(&eval_param);
155170
}
156171

157172
for rule in &self.rules {
158-
if let Some(value) = rule.serve_variation(&eval_param)? {
159-
return Ok(value);
173+
if let Some(v) = rule.serve_variation(&eval_param)? {
174+
return Ok(v.value);
160175
}
161176
}
162177

163-
self.default_serve.select_variation(&eval_param)
178+
self.default_serve.select_variation_value(&eval_param)
164179
}
165180

166181
pub fn eval_detail(
@@ -176,8 +191,10 @@ impl Toggle {
176191
variations: &self.variations,
177192
};
178193
if !self.enabled {
194+
let v = self.disabled_serve.select_variation(&eval_param).ok();
179195
return EvalDetail {
180-
value: self.disabled_serve.select_variation(&eval_param).ok(),
196+
variation_index: v.as_ref().map(|v| v.index),
197+
value: v.map(|v| v.value),
181198
version: Some(self.version),
182199
reason: "disabled".to_owned(),
183200
..Default::default()
@@ -188,7 +205,8 @@ impl Toggle {
188205
Ok(opt_value) => {
189206
if let Some(v) = opt_value {
190207
return EvalDetail {
191-
value: Some(v),
208+
value: Some(v.value),
209+
variation_index: Some(v.index),
192210
rule_index: Some(i),
193211
version: Some(self.version),
194212
reason: format!("rule {}", i),
@@ -209,7 +227,8 @@ impl Toggle {
209227

210228
match self.default_serve.select_variation(&eval_param) {
211229
Ok(v) => EvalDetail {
212-
value: Some(v),
230+
value: Some(v.value),
231+
variation_index: Some(v.index),
213232
version: Some(self.version),
214233
reason: "default.".to_owned(),
215234
..Default::default()
@@ -269,7 +288,7 @@ struct Rule {
269288
}
270289

271290
impl Rule {
272-
pub fn serve_variation(&self, eval_param: &EvalParams) -> Result<Option<Value>, FPError> {
291+
pub fn serve_variation(&self, eval_param: &EvalParams) -> Result<Option<Variation>, FPError> {
273292
let user = eval_param.user;
274293
let segment_repo = eval_param.segment_repo;
275294
match self

src/feature_probe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl FeatureProbe {
154154
time: unix_timestamp(),
155155
key: toggle.to_owned(),
156156
value: value.clone(),
157-
index: detail.rule_index,
157+
index: detail.variation_index,
158158
version: detail.version,
159159
reason: detail.reason.clone(),
160160
});

src/sync.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ mod tests {
160160
use super::*;
161161
use crate::SdkAuthorization;
162162
use axum::{routing::get, Json, Router, TypedHeader};
163+
use headers::UserAgent;
163164
use std::{fs, net::SocketAddr, path::PathBuf};
164165

165166
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
@@ -206,8 +207,11 @@ mod tests {
206207

207208
async fn server_sdk_toggles(
208209
TypedHeader(SdkAuthorization(sdk_key)): TypedHeader<SdkAuthorization>,
210+
TypedHeader(user_agent): TypedHeader<UserAgent>,
209211
) -> Json<Repository> {
210212
assert_eq!(sdk_key, "sdk-key");
213+
assert!(user_agent.to_string().len() > 0);
214+
println!(">>> {}", user_agent);
211215
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
212216
path.push("resources/fixtures/repo.json");
213217
let json_str = fs::read_to_string(path).unwrap();

0 commit comments

Comments
 (0)