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/// Events for user/session changes
58#[derive(Debug, Clone)]
59#[non_exhaustive]
60pub enum UserEvent {
61    UserCreated { user_id: String },
62    UserUpdated { user_id: String },
63    SessionCreated { user_id: String, session_id: String },
64    SessionEnded { user_id: String, session_id: String },
65}
66
67/// Publisher for user-related events
68pub trait UserEventPublisher: Send + Sync {
69    fn publish_user_event(&self, event: UserEvent);
70}
71
72/// Events for analytics updates
73#[derive(Debug, Clone)]
74#[non_exhaustive]
75pub enum AnalyticsEvent {
76    /// Analytics data has been updated
77    Updated,
78    /// A new AI request was completed
79    AiRequestCompleted { tokens_used: i64 },
80    /// User activity was recorded
81    UserActivityRecorded { user_id: String },
82}
83
84/// Publisher for analytics events
85pub trait AnalyticsEventPublisher: Send + Sync {
86    fn publish_analytics_event(&self, event: AnalyticsEvent);
87}