Skip to main content

systemprompt_analytics/models/
mod.rs

1pub mod cli;
2mod engagement;
3mod events;
4mod fingerprint;
5mod funnel;
6
7#[allow(unused_imports)]
8pub use cli::*;
9pub use engagement::{CreateEngagementEventInput, EngagementEvent, EngagementOptionalMetrics};
10pub use events::{
11    AnalyticsEventBatchResponse, AnalyticsEventCreated, AnalyticsEventType, ConversionEventData,
12    CreateAnalyticsEventBatchInput, CreateAnalyticsEventInput, EngagementEventData,
13    LinkClickEventData, ScrollEventData,
14};
15pub use fingerprint::{FingerprintAnalysisResult, FingerprintReputation, FlagReason};
16pub use funnel::{
17    CreateFunnelInput, CreateFunnelStepInput, Funnel, FunnelMatchType, FunnelProgress, FunnelStats,
18    FunnelStep, FunnelStepStats, FunnelWithSteps,
19};
20
21use chrono::{DateTime, Utc};
22use serde::{Deserialize, Serialize};
23use sqlx::FromRow;
24use systemprompt_identifiers::{ContextId, SessionId, UserId};
25
26#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
27pub struct UserMetricsWithTrends {
28    #[serde(rename = "users_24h")]
29    pub count_24h: i64,
30    #[serde(rename = "users_7d")]
31    pub count_7d: i64,
32    #[serde(rename = "users_30d")]
33    pub count_30d: i64,
34    #[serde(rename = "users_prev_24h")]
35    pub prev_24h: i64,
36    #[serde(rename = "users_prev_7d")]
37    pub prev_7d: i64,
38    #[serde(rename = "users_prev_30d")]
39    pub prev_30d: i64,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
43pub struct RecentConversation {
44    pub context_id: ContextId,
45    pub agent_name: String,
46    pub user_name: String,
47    pub status: String,
48    pub message_count: i64,
49    pub started_at: DateTime<Utc>,
50}
51
52#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
53pub struct ContentStat {
54    pub title: String,
55    pub slug: String,
56    pub views_5m: i64,
57    pub views_1h: i64,
58    pub views_1d: i64,
59    pub views_7d: i64,
60    pub views_30d: i64,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
64pub struct AnalyticsSession {
65    pub session_id: SessionId,
66    pub user_id: Option<UserId>,
67    pub fingerprint_hash: Option<String>,
68    pub ip_address: Option<String>,
69    pub user_agent: Option<String>,
70    pub device_type: Option<String>,
71    pub browser: Option<String>,
72    pub os: Option<String>,
73    pub country: Option<String>,
74    pub city: Option<String>,
75    pub referrer_url: Option<String>,
76    pub utm_source: Option<String>,
77    pub utm_medium: Option<String>,
78    pub utm_campaign: Option<String>,
79    pub is_bot: bool,
80    pub is_scanner: Option<bool>,
81    pub is_behavioral_bot: Option<bool>,
82    pub behavioral_bot_reason: Option<String>,
83    pub started_at: Option<DateTime<Utc>>,
84    pub last_activity_at: Option<DateTime<Utc>>,
85    pub ended_at: Option<DateTime<Utc>>,
86    pub request_count: Option<i32>,
87    pub task_count: Option<i32>,
88    pub ai_request_count: Option<i32>,
89    pub message_count: Option<i32>,
90}
91
92#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
93pub struct AnalyticsEvent {
94    // SQLx: analytics event primary key (text column, no typed ID defined)
95    pub id: String,
96    pub event_type: String,
97    pub event_category: String,
98    pub severity: String,
99    pub user_id: UserId,
100    pub session_id: Option<SessionId>,
101    pub message: Option<String>,
102    pub metadata: Option<String>,
103    pub timestamp: DateTime<Utc>,
104}
105
106#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
107pub struct ErrorSummary {
108    pub error_type: String,
109    pub count: i64,
110    pub last_occurred: DateTime<Utc>,
111    pub sample_message: Option<String>,
112}
113
114#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
115pub struct PlatformOverview {
116    pub total_users: i64,
117    pub active_users_24h: i64,
118    pub active_users_7d: i64,
119    pub total_sessions: i64,
120    pub active_sessions: i64,
121    pub total_contexts: i64,
122    pub total_tasks: i64,
123    pub total_ai_requests: i64,
124}
125
126#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
127pub struct CostOverview {
128    pub total_cost: f64,
129    pub cost_24h: f64,
130    pub cost_7d: f64,
131    pub cost_30d: f64,
132    pub avg_cost_per_request: f64,
133}
134
135#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
136pub struct ActivityTrend {
137    pub date: DateTime<Utc>,
138    pub sessions: i64,
139    pub contexts: i64,
140    pub tasks: i64,
141    pub ai_requests: i64,
142    pub tool_executions: i64,
143}
144
145#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
146pub struct TopUser {
147    pub user_id: UserId,
148    pub user_name: String,
149    pub session_count: i64,
150    pub task_count: i64,
151    pub ai_request_count: i64,
152    pub total_cost: f64,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
156pub struct TopAgent {
157    pub agent_name: String,
158    pub task_count: i64,
159    pub success_rate: f64,
160    pub avg_duration_ms: i64,
161}
162
163#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
164pub struct TopTool {
165    pub tool_name: String,
166    pub execution_count: i64,
167    pub success_rate: f64,
168    pub avg_duration_ms: i64,
169}
170
171#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
172pub struct TrafficSummary {
173    pub total_sessions: i64,
174    pub unique_visitors: i64,
175    pub page_views: i64,
176    pub avg_session_duration_seconds: f64,
177    pub bounce_rate: f64,
178}
179
180#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
181pub struct TrafficSource {
182    pub source: String,
183    pub sessions: i64,
184    pub percentage: f64,
185}
186
187#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
188pub struct DeviceBreakdown {
189    pub device_type: String,
190    pub count: i64,
191    pub percentage: f64,
192}
193
194#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
195pub struct BrowserBreakdown {
196    pub browser: String,
197    pub count: i64,
198    pub percentage: f64,
199}
200
201#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
202pub struct GeographicBreakdown {
203    pub country: String,
204    pub count: i64,
205    pub percentage: f64,
206}
207
208#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize, FromRow)]
209pub struct BotTrafficStats {
210    pub total_requests: i64,
211    pub bot_requests: i64,
212    pub human_requests: i64,
213    pub bot_percentage: f64,
214}
215
216#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
217pub struct ConversationSummary {
218    pub total_conversations: i64,
219    pub active_conversations: i64,
220    pub completed_conversations: i64,
221    pub avg_messages_per_conversation: f64,
222    pub avg_duration_minutes: f64,
223}
224
225#[derive(Debug, Clone, Copy, Serialize, Deserialize, FromRow)]
226pub struct ConversationTrend {
227    pub date: DateTime<Utc>,
228    pub new_conversations: i64,
229    pub completed_conversations: i64,
230    pub total_messages: i64,
231}
232
233#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
234pub struct ConversationByAgent {
235    pub agent_name: String,
236    pub conversation_count: i64,
237    pub avg_messages: f64,
238    pub success_rate: f64,
239}