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
use super::utils;
use super::utils::hashing;
use serde::{Deserialize, Serialize};

use super::types::FlagsmithValue;

#[derive(Eq, PartialEq, Hash, Serialize, Deserialize, Clone, Debug)]
pub struct Feature {
    pub id: u32,
    pub name: String,
    #[serde(rename = "type")]
    feature_type: Option<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MultivariateFeatureOption {
    pub value: FlagsmithValue,
    pub id: Option<u32>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct MultivariateFeatureStateValue {
    pub multivariate_feature_option: MultivariateFeatureOption,
    pub percentage_allocation: f32,
    pub id: Option<u32>,

    #[serde(default = "utils::get_uuid")]
    pub mv_fs_value_uuid: String,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FeatureState {
    pub feature: Feature,
    pub enabled: bool,
    pub django_id: Option<u32>,

    #[serde(default = "utils::get_uuid")]
    pub featurestate_uuid: String,
    pub multivariate_feature_state_values: Vec<MultivariateFeatureStateValue>,
    #[serde(rename = "feature_state_value")]
    value: FlagsmithValue,
}

impl FeatureState {
    pub fn get_value(&self, identity_id: Option<&str>) -> FlagsmithValue {
        let value = match identity_id {
            Some(id) if self.multivariate_feature_state_values.len() > 0 => {
                self.get_multivariate_value(id)
            }
            _ => self.value.clone(),
        };
        return value;
    }
    fn get_multivariate_value(&self, identity_id: &str) -> FlagsmithValue {
        let object_id = match self.django_id {
            Some(django_id) => django_id.to_string(),
            None => self.featurestate_uuid.clone(),
        };
        let percentage_value =
            hashing::get_hashed_percentage_for_object_ids(vec![&object_id, identity_id], 1);
        let mut start_percentage = 0.0;
        // Iterate over the mv options in order of id (so we get the same value each
        // time) to determine the correct value to return to the identity based on
        // the percentage allocations of the multivariate options. This gives us a
        // way to ensure that the same value is returned every time we use the same
        // percentage value.
        let mut mv_fs_values = self.multivariate_feature_state_values.clone();
        mv_fs_values.sort_by_key(|mv_fs_value| match mv_fs_value.id {
            Some(id) => id.to_string(),
            _ => mv_fs_value.mv_fs_value_uuid.clone(),
        });
        for mv_value in mv_fs_values {
            let limit = mv_value.percentage_allocation + start_percentage;
            if start_percentage <= percentage_value && percentage_value < limit {
                return mv_value.multivariate_feature_option.value;
            }

            start_percentage = limit
        }
        // default to return the control value if no MV values found, although this
        // should never happen
        return self.value.clone();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::*;

    #[test]
    fn deserializing_fs_creates_default_uuid_if_not_present() {
        let feature_state_json = r#"{
            "multivariate_feature_state_values": [
        {
            "id": 3404,
            "multivariate_feature_option": {
              "value": "baz"
            },
            "percentage_allocation": 30
          }
            ],
            "feature_state_value": 1,
            "django_id": 1,
            "feature": {
                "name": "feature1",
                "type": null,
                "id": 1
            },
            "segment_id": null,
            "enabled": false
        }"#;

        let feature_state: FeatureState = serde_json::from_str(feature_state_json).unwrap();
        assert_eq!(feature_state.featurestate_uuid.is_empty(), false);
        assert_eq!(
            feature_state.multivariate_feature_state_values[0]
                .mv_fs_value_uuid
                .is_empty(),
            false
        );
    }

    #[test]
    fn serialize_and_deserialize_feature_state() {
        let feature_state_json = serde_json::json!(
            {
                "multivariate_feature_state_values": [],
                "feature_state_value": 1,
                "featurestate_uuid":"a6ff815f-63ed-4e72-99dc-9124c442ce4d",
                "django_id": 1,
                "feature": {
                    "name": "feature1",
                    "type": null,
                    "id": 1
                },
                "enabled": false
            }
        );
        let feature_state: FeatureState =
            serde_json::from_value(feature_state_json.clone()).unwrap();

        let given_json = serde_json::to_value(&feature_state).unwrap();
        assert_eq!(given_json, feature_state_json)
    }

    #[rstest]
    #[case("2", "foo".to_string())] // Generated hash percentage 26
    #[case("8", "bar".to_string())] // Generated hash percentage 38
    #[case("1", "control".to_string())] // Generated hash percentage 84
    fn feature_state_get_value_mv_values(
        #[case] identity_id: &str,
        #[case] expected_value: String,
    ) {
        let mv_feature_value_1 = "foo";
        let mv_feature_value_2 = "bar";
        let feature_state_json = serde_json::json!(
            {
                "multivariate_feature_state_values": [
                    {
                        "id": 1,
                        "multivariate_feature_option": {
                            "id":1,
                            "value": mv_feature_value_1
                        },
                        "percentage_allocation": 30
                    },
                    {
                        "id": 2,
                        "multivariate_feature_option": {
                            "id":2,
                            "value": mv_feature_value_2
                        },
                        "percentage_allocation": 30
                    }
                ],
                "feature_state_value": "control",
                "featurestate_uuid":"a6ff815f-63ed-4e72-99dc-9124c442ce4d",
                "django_id": 1,
                "feature": {
                    "name": "feature1",
                    "type": null,
                    "id": 1
                },
                "enabled": true
            }
        );
        let feature_state: FeatureState =
            serde_json::from_value(feature_state_json.clone()).unwrap();
        let value = feature_state.get_value(Some(identity_id));
        assert_eq!(value.value, expected_value);
    }

    #[rstest]
    fn feature_state_get_value_uses_django_id_for_multivariate_value_calculation_if_not_none() {
        let mv_feature_value_1 = "foo";
        let mv_feature_value_2 = "bar";
        let fs_uuid = "a6ff815f-63ed-4e72-99dc-9124c442ce4d";
        let feature_state_json = serde_json::json!(
            {
                "multivariate_feature_state_values": [
                    {
                        "id": 1,
                        "multivariate_feature_option": {
                            "id":1,
                            "value": mv_feature_value_1
                        },
                        "percentage_allocation": 30
                    },
                    {
                        "id": 2,
                        "multivariate_feature_option": {
                            "id":2,
                            "value": mv_feature_value_2
                        },
                        "percentage_allocation": 30
                    }
                ],
                "feature_state_value": "control",
                "featurestate_uuid":fs_uuid,
                "django_id": 1,
                "feature": {
                    "name": "feature1",
                    "type": null,
                    "id": 1
                },
                "enabled": true
            }
        );
        // When
        let feature_state: FeatureState =
            serde_json::from_value(feature_state_json.clone()).unwrap();

        // Then
        let value = feature_state.get_value(Some("1"));
        // Since hash percentage generated using fs_uuid and identity_id `13` is 9.7
        // and hash percentage generated using mv_fs django id `1` and identity_id `1` is 84
        // the value returned should be the control value if django id was used for the calculation
        assert_eq!(value.value, "control".to_string());
    }
}