StratoSDK is a framework with a declarative approach similar to Flutter/React, written and designed entirely for Rust.
| 1 | mod event_store; |
| 2 | |
| 3 | use chrono::{DateTime, Utc}; |
| 4 | use lazy_static::lazy_static; |
| 5 | use parking_lot::Mutex; |
| 6 | |
| 7 | use serde_json::Value; |
| 8 | use std::borrow::Cow; |
| 9 | |
| 10 | use event_store::*; |
| 11 | pub use event_store::{Event, EventPayload}; |
| 12 | |
| 13 | lazy_static! { |
| 14 | static ref TELEMETRY: Mutex<EventStore> = Mutex::new(EventStore::new()); |
| 15 | } |
| 16 | |
| 17 | #[macro_export] |
| 18 | macro_rules! record_telemetry_from_ctx { |
| 19 | ($user_id: expr, $anonymous_id: expr, $name:expr, $payload: expr, $contains_ugc: expr, $ctx: expr) => {{ |
| 20 | let timestamp = $crate::time::get_current_time(); |
| 21 | $ctx.background_executor() |
| 22 | .spawn(async move { |
| 23 | $crate::telemetry::record_event( |
| 24 | $user_id, |
| 25 | $anonymous_id, |
| 26 | $name, |
| 27 | $payload, |
| 28 | $contains_ugc, |
| 29 | timestamp, |
| 30 | ) |
| 31 | }) |
| 32 | .detach(); |
| 33 | }}; |
| 34 | } |
| 35 | |
| 36 | #[macro_export] |
| 37 | macro_rules! record_telemetry_on_executor { |
| 38 | ($user_id: expr, $anonymous_id: expr, $name:expr, $payload: expr, $contains_ugc: expr, $executor: expr) => {{ |
| 39 | let timestamp = $crate::time::get_current_time(); |
| 40 | let _ = $executor |
| 41 | .spawn(async move { |
| 42 | $crate::telemetry::record_event( |
| 43 | $user_id, |
| 44 | $anonymous_id, |
| 45 | $name, |
| 46 | $payload, |
| 47 | $contains_ugc, |
| 48 | timestamp, |
| 49 | ) |
| 50 | }) |
| 51 | .detach(); |
| 52 | }}; |
| 53 | } |
| 54 | |
| 55 | /// Creates a new `Event`, but does not record it. It is up to the caller to determine when, and |
| 56 | /// how, the event should be recorded. |
| 57 | pub fn create_event( |
| 58 | user_id: Option<String>, |
| 59 | anonymous_id: String, |
| 60 | name: Cow<'static, str>, |
| 61 | payload: Option<Value>, |
| 62 | contains_ugc: bool, |
| 63 | timestamp: DateTime<Utc>, |
| 64 | ) -> Event { |
| 65 | let mut telemetry = TELEMETRY.lock(); |
| 66 | telemetry.create_event( |
| 67 | user_id, |
| 68 | anonymous_id, |
| 69 | name, |
| 70 | payload, |
| 71 | contains_ugc, |
| 72 | timestamp, |
| 73 | ) |
| 74 | } |
| 75 | |
| 76 | pub fn record_event( |
| 77 | user_id: Option<String>, |
| 78 | anonymous_id: String, |
| 79 | name: Cow<'static, str>, |
| 80 | payload: Option<Value>, |
| 81 | contains_ugc: bool, |
| 82 | timestamp: DateTime<Utc>, |
| 83 | ) { |
| 84 | let mut telemetry = TELEMETRY.lock(); |
| 85 | telemetry.record_event( |
| 86 | user_id, |
| 87 | anonymous_id, |
| 88 | name, |
| 89 | payload, |
| 90 | contains_ugc, |
| 91 | timestamp, |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | pub fn record_identify_user_event(user_id: String, anonymous_id: String, timestamp: DateTime<Utc>) { |
| 96 | let mut telemetry = TELEMETRY.lock(); |
| 97 | telemetry.record_identify_user_event(user_id, anonymous_id, timestamp); |
| 98 | } |
| 99 | |
| 100 | /// Adds a 'App Active' event to the global event queue. This should only be called in an async |
| 101 | /// context. |
| 102 | pub fn record_app_active_event( |
| 103 | user_id: Option<String>, |
| 104 | anonymous_id: String, |
| 105 | timestamp: DateTime<Utc>, |
| 106 | ) { |
| 107 | let mut telemetry = TELEMETRY.lock(); |
| 108 | telemetry.record_app_active(user_id, anonymous_id, timestamp); |
| 109 | } |
| 110 | |
| 111 | pub fn flush_events() -> Vec<Event> { |
| 112 | TELEMETRY.lock().events.drain(..).collect() |
| 113 | } |
| 114 |