Skip to main content

systemprompt_traits/
events.rs

1use chrono::{DateTime, Utc};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4#[non_exhaustive]
5pub enum LogEventLevel {
6    Error,
7    Warn,
8    Info,
9    Debug,
10    Trace,
11}
12
13impl std::str::FromStr for LogEventLevel {
14    type Err = String;
15
16    fn from_str(s: &str) -> Result<Self, Self::Err> {
17        match s.to_lowercase().as_str() {
18            "error" => Ok(Self::Error),
19            "warn" | "warning" => Ok(Self::Warn),
20            "info" => Ok(Self::Info),
21            "debug" => Ok(Self::Debug),
22            "trace" => Ok(Self::Trace),
23            _ => Err(format!("unknown log level: {s}")),
24        }
25    }
26}
27
28#[derive(Debug, Clone)]
29pub struct LogEventData {
30    pub timestamp: DateTime<Utc>,
31    pub level: LogEventLevel,
32    pub module: String,
33    pub message: String,
34}
35
36impl LogEventData {
37    #[must_use]
38    pub fn new(
39        timestamp: DateTime<Utc>,
40        level: LogEventLevel,
41        module: impl Into<String>,
42        message: impl Into<String>,
43    ) -> Self {
44        Self {
45            timestamp,
46            level,
47            module: module.into(),
48            message: message.into(),
49        }
50    }
51}
52
53pub trait LogEventPublisher: Send + Sync {
54    fn publish_log(&self, event: LogEventData);
55}
56
57#[derive(Debug, Clone)]
58#[non_exhaustive]
59pub enum UserEvent {
60    UserCreated { user_id: String },
61    UserUpdated { user_id: String },
62    SessionCreated { user_id: String, session_id: String },
63    SessionEnded { user_id: String, session_id: String },
64}
65
66pub trait UserEventPublisher: Send + Sync {
67    fn publish_user_event(&self, event: UserEvent);
68}
69
70#[derive(Debug, Clone)]
71#[non_exhaustive]
72pub enum AnalyticsEvent {
73    Updated,
74    AiRequestCompleted { tokens_used: i64 },
75    UserActivityRecorded { user_id: String },
76}
77
78pub trait AnalyticsEventPublisher: Send + Sync {
79    fn publish_analytics_event(&self, event: AnalyticsEvent);
80}