Skip to main content

systemprompt_analytics/models/
mod.rs

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