edgee_components_runtime/
payload.rs

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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;

#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct Payload {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data_collection: Option<DataCollection>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct Context {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page: Option<Page>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<User>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub client: Option<Client>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub campaign: Option<Campaign>,

    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub session: Option<Session>,
}

impl Context {
    pub fn fill_in(&mut self, other: &Context) {
        if let Some(page) = &mut self.page {
            page.fill_in(other.page.as_ref().unwrap());
        }
        if let Some(user) = &mut self.user {
            user.fill_in(other.user.as_ref().unwrap());
        }
    }
}

#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct DataCollection {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub components: Option<HashMap<String, bool>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<Context>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub events: Option<Vec<Event>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub consent: Option<Consent>,
}

impl DataCollection {
    pub fn populate_event_contexts(&mut self, from: &str) {
        let components = self.components.clone();

        // if events are set, we use the data collection context to fill in the missing fields
        if let Some(events) = &mut self.events {
            for event in events.iter_mut() {
                event.uuid = uuid::Uuid::new_v4().to_string();
                event.timestamp = Utc::now();
                event.from = Some(from.to_string());

                // fill in the missing context fields
                if let Some(context) = &mut event.context {
                    context.fill_in(&self.context.clone().unwrap());
                } else {
                    event.context = self.context.clone();
                }

                if event.consent.is_none() {
                    event.consent = self.consent.clone();
                }

                if let Some(data) = &mut event.data {
                    if event.event_type == EventType::Page {
                        // data is a Page
                        if let EventData::Page(event_data) = data {
                            event_data
                                .fill_in(&self.context.clone().unwrap().page.clone().unwrap());
                        }
                    }

                    if event.event_type == EventType::User {
                        // data is an User
                        if let EventData::User(user_data) = data {
                            user_data.fill_in(&self.context.clone().unwrap().user.clone().unwrap());
                        }
                    }
                } else {
                    if event.event_type == EventType::Page {
                        event.data =
                            Some(EventData::Page(self.context.clone().unwrap().page.unwrap()));
                    }

                    if event.event_type == EventType::User {
                        event.data =
                            Some(EventData::User(self.context.clone().unwrap().user.unwrap()));
                    }
                }

                if event.components.is_none() {
                    event.components = components.clone();
                }
            }
        }
    }
}

#[derive(Serialize, Debug, Default, Clone)]
pub struct Event {
    #[serde(skip_deserializing)]
    pub uuid: String,

    #[serde(skip_deserializing)]
    pub timestamp: DateTime<Utc>,

    #[serde(rename = "type")]
    pub event_type: EventType,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<EventData>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub context: Option<Context>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub components: Option<HashMap<String, bool>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub consent: Option<Consent>,
}

impl<'de> Deserialize<'de> for Event {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct EventHelper {
            #[serde(rename = "type")]
            event_type: EventType,
            data: Option<serde_json::Value>,
            context: Option<Context>,
            components: Option<HashMap<String, bool>>,
            from: Option<String>,
            consent: Option<Consent>,
        }

        let helper = EventHelper::deserialize(deserializer)?;
        let data = match helper.event_type {
            EventType::Page => helper
                .data
                .map(|d| serde_json::from_value(d).map(EventData::Page))
                .transpose()
                .unwrap_or_default(),
            EventType::User => helper
                .data
                .map(|d| serde_json::from_value(d).map(EventData::User))
                .transpose()
                .unwrap_or_default(),
            EventType::Track => helper
                .data
                .map(|d| serde_json::from_value(d).map(EventData::Track))
                .transpose()
                .unwrap_or_default(),
        };

        Ok(Event {
            uuid: uuid::Uuid::new_v4().to_string(),
            timestamp: Utc::now(),
            event_type: helper.event_type,
            data,
            context: helper.context,
            components: helper.components,
            from: helper.from,
            consent: helper.consent,
        })
    }
}

impl Event {
    pub fn is_component_enabled(&self, name: &str) -> &bool {
        // if destinations is not set, return true
        if self.components.is_none() {
            return &true;
        }

        // get destinations.get("all")
        let all = self
            .components
            .as_ref()
            .unwrap()
            .get("all")
            .unwrap_or(&true);

        // check if the destination is enabled
        if self.components.as_ref().unwrap().contains_key(name) {
            return self.components.as_ref().unwrap().get(name).unwrap();
        }
        all
    }

    pub fn is_all_components_disabled(&self) -> bool {
        if self.components.is_none() {
            return false;
        }

        // iterate over all components and check if there is at least one enabled
        for enabled in self.components.as_ref().unwrap().values() {
            if *enabled {
                return false;
            }
        }

        true
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub enum EventType {
    #[serde(rename = "page")]
    #[default]
    Page,
    #[serde(rename = "user")]
    User,
    #[serde(rename = "track")]
    Track,
}

impl fmt::Display for EventType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            EventType::Page => write!(f, "page"),
            EventType::User => write!(f, "user"),
            EventType::Track => write!(f, "track"),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum EventData {
    Page(Page),
    User(User),
    Track(Track),
}

impl Default for EventData {
    fn default() -> Self {
        EventData::Page(Page::default())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Consent {
    #[serde(rename = "pending")]
    Pending,
    #[serde(rename = "granted")]
    Granted,
    #[serde(rename = "denied")]
    Denied,
}

impl fmt::Display for Consent {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Consent::Pending => write!(f, "pending"),
            Consent::Granted => write!(f, "granted"),
            Consent::Denied => write!(f, "denied"),
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Page {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub category: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub keywords: Option<Vec<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub url: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub path: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub search: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub referrer: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, serde_json::Value>>, // Properties field is free-form
}

impl Page {
    fn fill_in(&mut self, other: &Page) {
        if self.name.is_none() {
            self.name = other.name.clone();
        }
        if self.category.is_none() {
            self.category = other.category.clone();
        }
        if self.keywords.is_none() {
            self.keywords = other.keywords.clone();
        }
        if self.title.is_none() {
            self.title = other.title.clone();
        }
        if self.url.is_none() {
            self.url = other.url.clone();
        }
        if self.path.is_none() {
            self.path = other.path.clone();
        }
        if self.search.is_none() {
            self.search = other.search.clone();
        }
        if self.referrer.is_none() {
            self.referrer = other.referrer.clone();
        }
        if self.properties.is_none() {
            self.properties = other.properties.clone();
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct User {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_id: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub anonymous_id: Option<String>,

    #[serde(skip_deserializing, default)]
    pub edgee_id: String,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, serde_json::Value>>, // Properties field is free-form
}

impl User {
    fn fill_in(&mut self, other: &User) {
        if self.user_id.is_none() {
            self.user_id = other.user_id.clone();
        }
        if self.anonymous_id.is_none() {
            self.anonymous_id = other.anonymous_id.clone();
        }
        self.edgee_id = other.edgee_id.clone();
        if self.properties.is_none() {
            self.properties = other.properties.clone();
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Track {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, serde_json::Value>>, // Properties field is free-form
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Campaign {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub medium: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub term: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub creative_format: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub marketing_tactic: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Client {
    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub ip: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub locale: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub accept_language: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub timezone: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub user_agent: Option<String>,

    // Low Entropy Client Hint Data - from sec-ch-ua header
    // The brand and version information for each brand associated with the browser, in a comma-separated list. ex: "Chromium;130|Google Chrome;130|Not?A_Brand;99"
    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub user_agent_version_list: Option<String>,

    // Low Entropy Client Hint Data - from Sec-Ch-Ua-Mobile header
    // Indicates whether the browser is on a mobile device. ex: 0
    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub user_agent_mobile: Option<String>,

    // Low Entropy Client Hint Data - from Sec-Ch-Ua-Platform header
    // The platform or operating system on which the user agent is running. Ex: macOS
    #[serde(skip_serializing_if = "Option::is_none", skip_deserializing)]
    pub os_name: Option<String>,

    // High Entropy Client Hint Data - from Sec-Ch-Ua-Arch header
    // User Agent Architecture. ex: arm
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_agent_architecture: Option<String>,

    // High Entropy Client Hint Data - from Sec-Ch-Ua-Bitness header
    // The "bitness" of the user-agent's underlying CPU architecture. This is the size in bits of an integer or memory address—typically 64 or 32 bits. ex: 64
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_agent_bitness: Option<String>,

    // High Entropy Client Hint Data - from Sec-Ch-Ua-Full-Version-List header
    // The brand and full version information for each brand associated with the browser, in a comma-separated list. ex: Chromium;112.0.5615.49|Google Chrome;112.0.5615.49|Not?A-Brand;99.0.0.0
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_agent_full_version_list: Option<String>,

    // High Entropy Client Hint Data - from Sec-Ch-Ua-Model header
    // The device model on which the browser is running. Will likely be empty for desktop browsers. ex: Nexus 6
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_agent_model: Option<String>,

    // High Entropy Client Hint Data - from Sec-Ch-Ua-Platform-Version header
    // The version of the operating system on which the user agent is running. Ex: 12.2.1
    #[serde(skip_serializing_if = "Option::is_none")]
    pub os_version: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub screen_width: Option<i32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub screen_height: Option<i32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub screen_density: Option<f32>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub continent: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub country_code: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub country_name: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub city: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct Session {
    pub session_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub previous_session_id: Option<String>,
    pub session_count: u32,
    pub session_start: bool,
    pub first_seen: DateTime<Utc>,
    pub last_seen: DateTime<Utc>,
}