Skip to main content

systemprompt_analytics/models/
mod.rs

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