|
| 1 | +use crate::ufc::{Assignment, AssignmentFormat, Environment, VariationType}; |
| 2 | +use crate::{Attributes, Configuration, Str}; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use std::collections::HashMap; |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +// Request |
| 8 | +#[derive(Debug, Deserialize)] |
| 9 | +pub struct PrecomputedAssignmentsServiceRequestBody { |
| 10 | + pub subject_key: Str, |
| 11 | + pub subject_attributes: Arc<Attributes>, |
| 12 | + // TODO: Add bandit actions |
| 13 | + // #[serde(rename = "banditActions")] |
| 14 | + // #[serde(skip_serializing_if = "Option::is_none")] |
| 15 | + // bandit_actions: Option<HashMap<String, serde_json::Value>>, |
| 16 | +} |
| 17 | + |
| 18 | +// Response |
| 19 | +#[derive(Debug, Serialize)] |
| 20 | +#[serde(rename_all = "camelCase")] |
| 21 | +pub struct FlagAssignment { |
| 22 | + pub allocation_key: Str, |
| 23 | + pub variation_key: Str, |
| 24 | + pub variation_type: VariationType, |
| 25 | + pub variation_value: serde_json::Value, |
| 26 | + /// Additional user-defined logging fields for capturing extra information related to the |
| 27 | + /// assignment. |
| 28 | + #[serde(flatten)] |
| 29 | + pub extra_logging: HashMap<String, String>, |
| 30 | + pub do_log: bool, |
| 31 | +} |
| 32 | + |
| 33 | +impl FlagAssignment { |
| 34 | + pub fn try_from_assignment(assignment: Assignment) -> Option<Self> { |
| 35 | + // WARNING! There is a problem here. The event is only populated for splits |
| 36 | + // that have `do_log` set to true in the wire format. This means that |
| 37 | + // all the ones present here are logged, but any splits that are not |
| 38 | + // logged are not present here. |
| 39 | + // |
| 40 | + // This is a problem for us because we want to be able to return |
| 41 | + // precomputed assignments for any split, logged or not, since we |
| 42 | + // want to be able to return them for all flags. |
| 43 | + // |
| 44 | + // We need to fix this. |
| 45 | + assignment.event.as_ref().map(|event| Self { |
| 46 | + allocation_key: event.base.allocation.clone(), |
| 47 | + variation_key: event.base.variation.clone(), |
| 48 | + variation_type: assignment.value.variation_type(), |
| 49 | + variation_value: assignment.value.variation_value(), |
| 50 | + extra_logging: event |
| 51 | + .base |
| 52 | + .extra_logging |
| 53 | + .iter() |
| 54 | + .map(|(k, v)| (k.clone(), v.clone())) |
| 55 | + .collect(), |
| 56 | + do_log: true, |
| 57 | + }) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Debug, Serialize)] |
| 62 | +#[serde(rename_all = "camelCase")] |
| 63 | +pub struct PrecomputedAssignmentsServiceResponse { |
| 64 | + created_at: chrono::DateTime<chrono::Utc>, |
| 65 | + format: AssignmentFormat, |
| 66 | + environment: Environment, |
| 67 | + flags: HashMap<String, FlagAssignment>, |
| 68 | +} |
| 69 | + |
| 70 | +impl PrecomputedAssignmentsServiceResponse { |
| 71 | + pub fn from_configuration( |
| 72 | + configuration: Arc<Configuration>, |
| 73 | + flags: HashMap<String, FlagAssignment>, |
| 74 | + ) -> Self { |
| 75 | + Self { |
| 76 | + created_at: chrono::Utc::now(), |
| 77 | + format: AssignmentFormat::Precomputed, |
| 78 | + environment: { |
| 79 | + Environment { |
| 80 | + name: configuration.flags.compiled.environment.name.clone(), |
| 81 | + } |
| 82 | + }, |
| 83 | + flags, |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments