sigstat 0.0.3-beta.2

Statsig Rust SDK for usage in multi-user server environments.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::evaluation::comparisons::{
    compare_arrays, compare_numbers, compare_str_with_regex, compare_strings_in_array,
    compare_time, compare_versions,
};
use crate::evaluation::dynamic_value::DynamicValue;
use crate::evaluation::evaluation_details::EvaluationDetails;
use crate::evaluation::evaluation_types::SecondaryExposure;
use crate::evaluation::evaluator_context::EvaluatorContext;
use crate::spec_types::{Condition, Rule};
use crate::{dyn_value, log_e, unwrap_or_return, StatsigErr};
use chrono::Utc;
use lazy_static::lazy_static;

const TAG: &str = stringify!(Evaluator);

pub struct Evaluator;

lazy_static! {
    static ref EMPTY_STR: String = "".to_string();
    static ref DEFAULT_RULE: String = "default".to_string();
    static ref DISABLED_RULE: String = "disabled".to_string();
    static ref EMPTY_DYNAMIC_VALUE: DynamicValue = DynamicValue::new();
}

#[derive(Clone)]
pub enum SpecType {
    Gate,
    DynamicConfig,
    Experiment,
    Layer,
}

impl Evaluator {
    pub fn evaluate_with_details(
        ctx: &mut EvaluatorContext,
        spec_name: &str,
        spec_type: &SpecType,
    ) -> Result<EvaluationDetails, StatsigErr> {
        let recognized = Self::evaluate(ctx, spec_name, spec_type)?;

        if !recognized {
            return Ok(EvaluationDetails::unrecognized(ctx.spec_store_data));
        }

        if let Some(reason) = ctx.result.override_reason {
            return Ok(EvaluationDetails::recognized_but_overridden(
                ctx.spec_store_data,
                reason,
            ));
        }

        Ok(EvaluationDetails::recognized(
            ctx.spec_store_data,
            &ctx.result,
        ))
    }

    pub fn evaluate(
        ctx: &mut EvaluatorContext,
        spec_name: &str,
        spec_type: &SpecType,
    ) -> Result<bool, StatsigErr> {
        if try_apply_override(ctx, spec_name, spec_type) {
            return Ok(true);
        }

        let spec = unwrap_or_return!(
            match spec_type {
                SpecType::Gate => ctx.spec_store_data.values.feature_gates.get(spec_name),
                SpecType::DynamicConfig =>
                    ctx.spec_store_data.values.dynamic_configs.get(spec_name),
                SpecType::Experiment => ctx.spec_store_data.values.dynamic_configs.get(spec_name),
                SpecType::Layer => ctx.spec_store_data.values.layer_configs.get(spec_name),
            },
            Ok(false)
        );

        if ctx.result.id_type.is_none() {
            ctx.result.id_type = Some(&spec.id_type);
        }

        if ctx.result.version.is_none() {
            if let Some(version) = spec.version {
                ctx.result.version = Some(version);
            }
        }

        if let Some(is_active) = spec.is_active {
            ctx.result.is_experiment_active = is_active;
        }

        if let Some(has_shared_params) = spec.has_shared_params {
            ctx.result.is_in_layer = has_shared_params;
        }

        if let Some(explicit_params) = &spec.explicit_parameters {
            ctx.result.explicit_parameters = Some(explicit_params);
        }

        for rule in spec.rules.iter() {
            evaluate_rule(ctx, rule)?;

            if ctx.result.unsupported {
                return Ok(true);
            }

            if !ctx.result.bool_value {
                continue;
            }

            if evaluate_config_delegate(ctx, rule)? {
                ctx.finalize_evaluation();
                return Ok(true);
            }

            let did_pass = evaluate_pass_percentage(ctx, rule, &spec.salt);

            if did_pass {
                ctx.result.bool_value = rule.return_value.string_value != "false";
                ctx.result.json_value = rule.return_value.json_value.clone();
            } else {
                ctx.result.bool_value = spec.default_value.string_value == "true";
                ctx.result.json_value = spec.default_value.json_value.clone();
            }

            ctx.result.rule_id = Some(&rule.id);
            ctx.result.group_name = rule.group_name.as_ref();
            ctx.result.is_experiment_group = rule.is_experiment_group.unwrap_or(false);
            ctx.result.is_experiment_active = spec.is_active.unwrap_or(false);
            ctx.finalize_evaluation();
            return Ok(true);
        }

        ctx.result.bool_value = spec.default_value.string_value == "true";
        ctx.result.json_value = spec.default_value.json_value.clone();
        ctx.result.rule_id = match spec.enabled {
            true => Some(&DEFAULT_RULE),
            false => Some(&DISABLED_RULE),
        };
        ctx.finalize_evaluation();

        Ok(true)
    }
}

fn try_apply_override(ctx: &mut EvaluatorContext, spec_name: &str, spec_type: &SpecType) -> bool {
    let adapter = match ctx.override_adapter {
        Some(adapter) => adapter,
        None => return false,
    };

    match spec_type {
        SpecType::Gate => {
            adapter.get_gate_override(&ctx.user.user_data, spec_name, &mut ctx.result)
        }

        SpecType::DynamicConfig => {
            adapter.get_dynamic_config_override(&ctx.user.user_data, spec_name, &mut ctx.result)
        }

        SpecType::Experiment => {
            adapter.get_experiment_override(&ctx.user.user_data, spec_name, &mut ctx.result)
        }

        SpecType::Layer => {
            adapter.get_layer_override(&ctx.user.user_data, spec_name, &mut ctx.result)
        }
    }
}

fn evaluate_rule<'a>(ctx: &mut EvaluatorContext<'a>, rule: &'a Rule) -> Result<(), StatsigErr> {
    let mut all_conditions_pass = true;
    // println!("--- Eval Rule {} ---", rule.id);
    for condition_hash in rule.conditions.iter() {
        // println!("Condition Hash {}", condition_hash);
        let opt_condition = ctx.spec_store_data.values.condition_map.get(condition_hash);
        let condition = match opt_condition {
            Some(c) => c,
            None => {
                // todo: log condition not found error
                ctx.result.unsupported = true;
                return Ok(());
            }
        };

        evaluate_condition(ctx, condition)?;

        if !ctx.result.bool_value {
            all_conditions_pass = false
        }
    }

    ctx.result.bool_value = all_conditions_pass;

    Ok(())
}

fn evaluate_condition<'a>(
    ctx: &mut EvaluatorContext<'a>,
    condition: &'a Condition,
) -> Result<(), StatsigErr> {
    let temp_value;
    let target_value = condition
        .target_value
        .as_ref()
        .unwrap_or(&EMPTY_DYNAMIC_VALUE);
    let condition_type = &condition.condition_type;

    let value = match condition_type as &str {
        "public" => {
            ctx.result.bool_value = true;
            return Ok(());
        }
        "fail_gate" | "pass_gate" => {
            evaluate_nested_gate(ctx, target_value, condition_type)?;
            return Ok(());
        }
        "user_field" | "ua_based" | "ip_based" => ctx.user.get_user_value(&condition.field),
        "environment_field" => ctx.user.get_value_from_environment(&condition.field),
        "current_time" => {
            temp_value = DynamicValue::for_timestamp_evaluation(Utc::now().timestamp_millis());
            Some(&temp_value)
        }
        "user_bucket" => {
            temp_value = get_hash_for_user_bucket(ctx, condition);
            Some(&temp_value)
        }
        "target_app" => *ctx.app_id,
        "unit_id" => ctx.user.get_unit_id(&condition.id_type),
        _ => {
            ctx.result.unsupported = true;
            return Ok(());
        }
    }
    .unwrap_or(&EMPTY_DYNAMIC_VALUE);

    // println!("Eval Condition {}, {:?}", condition_type, value);

    let operator = match &condition.operator {
        Some(operator) => operator,
        None => {
            ctx.result.unsupported = true;
            return Ok(());
        }
    };

    ctx.result.bool_value = match operator as &str {
        // numerical comparisons
        "gt" | "gte" | "lt" | "lte" => compare_numbers(value, target_value, operator),

        // version comparisons
        "version_gt" | "version_gte" | "version_lt" | "version_lte" | "version_eq"
        | "version_neq" => compare_versions(value, target_value, operator),

        // string/array comparisons
        "any"
        | "none"
        | "str_starts_with_any"
        | "str_ends_with_any"
        | "str_contains_any"
        | "str_contains_none" => compare_strings_in_array(value, target_value, operator, true),
        "any_case_sensitive" | "none_case_sensitive" => {
            compare_strings_in_array(value, target_value, operator, false)
        }
        "str_matches" => compare_str_with_regex(value, target_value),

        // time comparisons
        "before" | "after" | "on" => compare_time(value, target_value, operator),

        // strict equals
        "eq" => value == target_value,
        "neq" => value != target_value,

        // id_lists
        "in_segment_list" | "not_in_segment_list" => {
            evaluate_id_list(ctx, operator, target_value, value)
        }

        "array_contains_any"
        | "array_contains_none"
        | "array_contains_all"
        | "not_array_contains_all" => compare_arrays(value, target_value, operator),

        _ => {
            ctx.result.unsupported = true;
            return Ok(());
        }
    };

    Ok(())
}

fn evaluate_id_list(
    ctx: &mut EvaluatorContext<'_>,
    op: &str,
    target_value: &DynamicValue,
    value: &DynamicValue,
) -> bool {
    let list_name = unwrap_or_return!(&target_value.string_value, false);
    let id_lists = &ctx.spec_store_data.id_lists;

    let list = unwrap_or_return!(id_lists.get(list_name), false);

    let value = unwrap_or_return!(&value.string_value, false);
    let hashed = ctx.hashing.sha256(value);
    let lookup_id: String = hashed.chars().take(8).collect();

    let is_in_list = list.ids.contains(&lookup_id);

    if op == "not_in_segment_list" {
        return !is_in_list;
    }

    is_in_list
}

fn evaluate_nested_gate<'a>(
    ctx: &mut EvaluatorContext<'a>,
    target_value: &'a DynamicValue,
    condition_type: &'a String,
) -> Result<(), StatsigErr> {
    let gate_name = match target_value.string_value.as_ref() {
        Some(name) => name,
        None => {
            log_e!(
                TAG,
                "Invalid target_value for condition {}, {:?}",
                condition_type,
                target_value
            );
            ctx.result.unsupported = true;
            return Ok(());
        }
    };

    ctx.increment_nesting()?;
    let res = Evaluator::evaluate(ctx, gate_name, &SpecType::Gate)?;

    if ctx.result.unsupported || !res {
        return Ok(());
    }

    if !&gate_name.starts_with("segment:") {
        let res = &ctx.result;
        let expo = SecondaryExposure {
            gate: gate_name.clone(),
            gate_value: res.bool_value.to_string(),
            rule_id: res.rule_id.unwrap_or(&EMPTY_STR).clone(),
        };

        ctx.result.secondary_exposures.push(expo);
    }

    if condition_type == "fail_gate" {
        ctx.result.bool_value = !ctx.result.bool_value
    }
    Ok(())
}

fn evaluate_config_delegate<'a>(
    ctx: &mut EvaluatorContext<'a>,
    rule: &'a Rule,
) -> Result<bool, StatsigErr> {
    let delegate = unwrap_or_return!(&rule.config_delegate, Ok(false));
    let delegate_spec = unwrap_or_return!(
        ctx.spec_store_data.values.dynamic_configs.get(delegate),
        Ok(false)
    );

    ctx.result.undelegated_secondary_exposures = Some(ctx.result.secondary_exposures.clone());

    ctx.increment_nesting()?;
    let res = Evaluator::evaluate(ctx, delegate, &SpecType::Experiment)?;
    if !res {
        ctx.result.undelegated_secondary_exposures = None;
        return Ok(false);
    }

    ctx.result.explicit_parameters = delegate_spec.explicit_parameters.as_ref();
    ctx.result.config_delegate = rule.config_delegate.as_ref();

    Ok(true)
}

fn evaluate_pass_percentage(ctx: &mut EvaluatorContext, rule: &Rule, spec_salt: &String) -> bool {
    if rule.pass_percentage == 100f64 {
        return true;
    }

    if rule.pass_percentage == 0f64 {
        return false;
    }

    let rule_salt = rule.salt.as_ref().unwrap_or(&rule.id);
    let unit_id = ctx
        .user
        .get_unit_id(&rule.id_type)
        .unwrap_or(&EMPTY_DYNAMIC_VALUE)
        .string_value
        .as_ref()
        .unwrap_or(&EMPTY_STR);
    let input = format!("{}.{}.{}", spec_salt, rule_salt, unit_id);
    match ctx.hashing.evaluation_hash(&input) {
        Some(hash) => ((hash % 10000) as f64) < rule.pass_percentage * 100.0,
        None => false,
    }
}

fn get_hash_for_user_bucket(ctx: &mut EvaluatorContext, condition: &Condition) -> DynamicValue {
    let unit_id = ctx
        .user
        .get_unit_id(&condition.id_type)
        .unwrap_or(&EMPTY_DYNAMIC_VALUE)
        .string_value
        .as_ref()
        .unwrap_or(&EMPTY_STR);

    let mut salt: &String = &EMPTY_STR;

    if let Some(add_values) = &condition.additional_values {
        if let Some(v) = &add_values["salt"].string_value {
            salt = v;
        }
    }

    let input = format!("{}.{}", salt, unit_id);
    let hash = ctx.hashing.evaluation_hash(&input).unwrap_or(1);
    dyn_value!(hash % 1000)
}